Skip to content

Commit 096f46f

Browse files
authored
use multiple params (#121)
* use mupltiple params * 2.6.72
1 parent ed30ba5 commit 096f46f

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@themost/query",
3-
"version": "2.6.71",
3+
"version": "2.6.72",
44
"description": "@themost/query is a query builder for SQL. It includes a wide variety of helper functions for building complex SQL queries under node.js.",
55
"main": "index.js",
66
"scripts": {

query.d.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ export declare class QueryExpression {
2626
hasPaging(): boolean;
2727
distinct(value: any): this;
2828
where<T>(expr: (value: T, ...param: any[]) => any, ...params: any[]): this;
29+
where<T>(expr: (value: T, arg1: K) => any, value1: K): this;
30+
where<T>(expr: (value: T, arg1: K, arg2: L) => any, value1: K, value2: L): this;
31+
where<T>(expr: (value: T, arg1: K, arg2: L, arg3: M) => any, value1: K, value2: L, value3: M): this;
32+
where<T>(expr: (value: T, arg1: K, arg2: L, arg3: M,
33+
arg4: N) => any, value1: K, value2: L, value3: M, value4: N): this;
34+
where<T>(expr: (value: T, arg1: K, arg2: L, arg3: M,
35+
arg4: N, arg5: O) => any, value1: K, value2: L, value3: M, value4: N, value5: O): this;
36+
where<T>(expr: (value: T, arg1: K, arg2: L, arg3: M,
37+
arg4: N, arg5: O, arg6: P) => any, value1: K, value2: L, value3: M, value4: N, value5: O, value6: P): this;
38+
where<T>(expr: (value: T, arg1: K, arg2: L, arg3: M,
39+
arg4: N, arg5: O, arg6: P, arg7: Q) => any, value1: K, value2: L, value3: M, value4: N, value5: O, value6: P, value7: Q): this;
2940
where(expr: string | any): this;
3041
injectWhere(where: any);
3142
delete(entity: string): this;
@@ -35,8 +46,19 @@ export declare class QueryExpression {
3546
update(entity: string): this;
3647
set(obj: any): this;
3748
select(...field: Array<any>): this;
38-
select<T>(expr: (value: T, ...param: any) => any, params?: any): this;
39-
select<T,J>(expr: (value1: T, value2: J, ...param: any) => any, params?: any): this;
49+
select<T>(expr: (value: T, ...param: any) => any, ...params: any[]): this;
50+
select<T>(expr: (value: T, arg1: K) => any, value1: K): this;
51+
select<T>(expr: (value: T, arg1: K, arg2: L) => any, value1: K, value2: L): this;
52+
select<T>(expr: (value: T, arg1: K, arg2: L, arg3: M) => any, value1: K, value2: L, value3: M): this;
53+
select<T>(expr: (value: T, arg1: K, arg2: L, arg3: M, arg4: N) => any,
54+
value1: K, value2: L, value3: M, value4: N): this;
55+
select<T>(expr: (value: T, arg1: K, arg2: L, arg3: M, arg4: N, arg5: O) => any,
56+
value1: K, value2: L, value3: M, value4: N, value5: O): this;
57+
select<T>(expr: (value: T, arg1: K, arg2: L, arg3: M, arg4: N, arg5: O, arg6: P) => any,
58+
value1: K, value2: L, value3: M, value4: N, value5: O, value6: P): this;
59+
select<T>(expr: (value: T, arg1: K, arg2: L, arg3: M, arg4: N, arg5: O, arg6: P, arg7: Q) => any,
60+
value1: K, value2: L, value3: M, value4: N, value5: O, value6: P, value7: Q): this;
61+
select<T,J>(expr: (value1: T, value2: J, ...param: any) => any, ...params: any[]): this;
4062
count(alias: string): this;
4163
from(entity: string): this;
4264
from(entity: QueryEntity): this;
@@ -49,7 +71,7 @@ export declare class QueryExpression {
4971
rightJoin(entity: any, props?: any, alias?: any): this;
5072
rightJoin(entity: QueryEntity): this;
5173
with(obj: any): this;
52-
with<T,J>(expr: (value: T, otherValue: J, ...param: any) => any, params?: any): this;
74+
with<T,J>(expr: (value: T, otherValue: J, ...params: any[]) => any, ...params: any[]): this;
5375
orderBy(expr: string | any): this;
5476
orderBy<T>(expr: QueryFunc<T>, ...params: any[]): this;
5577
orderByDescending(expr: string | any): this;

query.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,17 @@ QueryExpression.prototype.distinct = function(value)
370370

371371
/**
372372
* @param {*} field
373-
* @param {*=} params
373+
* @param {...*} params
374374
* @returns {QueryExpression}
375375
*/
376+
// eslint-disable-next-line no-unused-vars
376377
QueryExpression.prototype.where = function(field, params)
377378
{
378379
if (typeof field === 'function') {
379380
// parse closure
380381
const closureParser = this.getClosureParser();
381-
this.$where = closureParser.parseFilter(field, params);
382+
const funcWithArgs = Array.from(arguments);
383+
this.$where = closureParser.parseFilter.apply(closureParser, funcWithArgs);
382384
return this;
383385
}
384386
if (_.isNil(field))

spec/ClosureParser.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,4 +801,26 @@ describe('ClosureParser', () => {
801801
expect(results.length).toBeTruthy();
802802
});
803803

804+
it('should use select with multiple params', async () => {
805+
const Products = new QueryEntity('ProductData');
806+
let a = new QueryExpression().select(({id, name, price, category}, num, percent) => ({
807+
id,
808+
name,
809+
price,
810+
rounded: round(price, num),
811+
offer: round(price * percent, num),
812+
category
813+
}), 2, 0.75)
814+
.from(Products).where((x, maxPrice, category) => {
815+
return x.category === category && x.price < maxPrice;
816+
}, 700, 'Laptops');
817+
const result = await db.executeAsync(a);
818+
expect(result).toBeTruthy();
819+
for(const item of result) {
820+
expect(item.price).toBeLessThan(700);
821+
expect(item.category).toBe('Laptops');
822+
expect(item.rounded).toBe(round(item.price, 2));
823+
}
824+
});
825+
804826
});

0 commit comments

Comments
 (0)