tg-client-query-builder 2.13.0 → 2.14.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/query-builder/index.js +34 -20
- package/package.json +7 -5
- package/query-builder/index.js +86 -155
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
// valid filters:
|
3
|
+
// valid filters:
|
4
4
|
// greaterThan
|
5
5
|
// inList
|
6
6
|
// lessThan
|
@@ -16,11 +16,14 @@
|
|
16
16
|
// endsWithExactly
|
17
17
|
// containsExactly
|
18
18
|
// startsWith
|
19
|
+
// notStartsWith
|
19
20
|
// endsWith
|
21
|
+
// notEndsWith
|
20
22
|
// contains
|
23
|
+
// notContains
|
21
24
|
// upperCase
|
22
25
|
// lowerCase
|
23
|
-
// matchesRegex (note: this takes a string like "thomas.*is.*cool", don't include the outer slashes like /asdf/ and don't pass a regex object )
|
26
|
+
// matchesRegex (note: this takes a string like "thomas.*is.*cool", don't include the outer slashes like /asdf/ and don't pass a regex object )
|
24
27
|
// fuzzy
|
25
28
|
var FilterExpression = require("./filter-expression");
|
26
29
|
var _ = require("lodash");
|
@@ -35,7 +38,7 @@ function isDateOrNumber(opName) {
|
|
35
38
|
if (args.some(function (arg) {
|
36
39
|
return !(_.isDate(arg) || _.isString(arg) || _.isNumber(arg));
|
37
40
|
})) {
|
38
|
-
throw new Error("QueryBuilderError: You must pass a date or number as args to " + opName + ". You passed: Args " + args.join(
|
41
|
+
throw new Error("QueryBuilderError: You must pass a date or number as args to " + opName + ". You passed: Args " + args.join(","));
|
39
42
|
}
|
40
43
|
};
|
41
44
|
}
|
@@ -107,9 +110,15 @@ var expressionOperators = [{
|
|
107
110
|
}, {
|
108
111
|
opName: "startsWith",
|
109
112
|
sanityChecks: [numberOfArgs("startsWith", 1), isString("startsWith")]
|
113
|
+
}, {
|
114
|
+
opName: "notStartsWith",
|
115
|
+
sanityChecks: [numberOfArgs("notStartsWith", 1), isString("notStartsWith")]
|
110
116
|
}, {
|
111
117
|
opName: "endsWith",
|
112
118
|
sanityChecks: [numberOfArgs("endsWith", 1), isString("endsWith")]
|
119
|
+
}, {
|
120
|
+
opName: "notEndsWith",
|
121
|
+
sanityChecks: [numberOfArgs("notEndsWith", 1), isString("notEndsWith")]
|
113
122
|
}, {
|
114
123
|
opName: "contains",
|
115
124
|
sanityChecks: [numberOfArgs("contains", 1), isString("contains")]
|
@@ -125,26 +134,26 @@ var expressionOperators = [{
|
|
125
134
|
}, {
|
126
135
|
opName: "matchesRegex",
|
127
136
|
sanityChecks: [numberOfArgs("matchesRegex", 1), isString("matchesRegex")]
|
137
|
+
}, {
|
138
|
+
opName: "matchesSimilar",
|
139
|
+
sanityChecks: [numberOfArgs("matchesSimilar", 1), isString("matchesSimilar")]
|
128
140
|
}, {
|
129
141
|
opName: "fuzzy",
|
130
142
|
sanityChecks: [numberOfArgs("fuzzy", 1), isString("fuzzy")],
|
131
143
|
transform: function transform(arg) {
|
132
|
-
// Build Regex String
|
133
|
-
var matchTerm =
|
144
|
+
// Build Regex String
|
145
|
+
var matchTerm = "";
|
134
146
|
// Split all the search terms
|
135
|
-
var terms = arg.replace(/\W/g,
|
147
|
+
var terms = arg.replace(/\W/g, "").replace(" ", "").split("");
|
136
148
|
for (var i = 0; i < terms.length; i++) {
|
137
|
-
matchTerm +=
|
149
|
+
matchTerm += ".*" + terms[i];
|
138
150
|
}
|
139
|
-
matchTerm +=
|
151
|
+
matchTerm += ".*";
|
140
152
|
return {
|
141
|
-
newOpName:
|
153
|
+
newOpName: "matchesRegex",
|
142
154
|
newArgs: [matchTerm]
|
143
155
|
};
|
144
156
|
}
|
145
|
-
|
146
|
-
// 'subString', //tnr: not yet implemented
|
147
|
-
// 'dateOnly', //tnr: not yet implemented
|
148
157
|
}];
|
149
158
|
|
150
159
|
module.exports = function () {
|
@@ -198,6 +207,7 @@ module.exports = function () {
|
|
198
207
|
entity: subQuery.entity,
|
199
208
|
foreignKey: subQuery.foreignKey,
|
200
209
|
modifier: subQuery.modifier,
|
210
|
+
isArrayRelation: subQuery.isArrayRelation,
|
201
211
|
filters: [],
|
202
212
|
countExpression: undefined
|
203
213
|
};
|
@@ -218,7 +228,7 @@ module.exports = function () {
|
|
218
228
|
};
|
219
229
|
};
|
220
230
|
|
221
|
-
QueryBuilder.prototype.related = function (relatedEntity) {
|
231
|
+
QueryBuilder.prototype.related = function (relatedEntity, isArrayRelation) {
|
222
232
|
var tokens = relatedEntity.split(".");
|
223
233
|
var entity = tokens[0];
|
224
234
|
var key = tokens[1];
|
@@ -226,10 +236,10 @@ module.exports = function () {
|
|
226
236
|
// log("FilterBuilder in related");
|
227
237
|
// log(FilterBuilder);
|
228
238
|
|
229
|
-
return createSubQueryBuilder(this, entity, key);
|
239
|
+
return createSubQueryBuilder(this, entity, key, isArrayRelation);
|
230
240
|
};
|
231
241
|
|
232
|
-
QueryBuilder.prototype.notRelated = function (relatedEntity) {
|
242
|
+
QueryBuilder.prototype.notRelated = function (relatedEntity, isArrayRelation) {
|
233
243
|
var tokens = relatedEntity.split(".");
|
234
244
|
var entity = tokens[0];
|
235
245
|
var key = tokens[1];
|
@@ -237,7 +247,7 @@ module.exports = function () {
|
|
237
247
|
// log("FilterBuilder in notRelated");
|
238
248
|
// log(FilterBuilder);
|
239
249
|
|
240
|
-
return createSubQueryBuilder(this, entity, key, "not");
|
250
|
+
return createSubQueryBuilder(this, entity, key, isArrayRelation, "not");
|
241
251
|
};
|
242
252
|
|
243
253
|
QueryBuilder.prototype.toJSON = function () {
|
@@ -245,8 +255,8 @@ module.exports = function () {
|
|
245
255
|
if (qry.filters.length > 1) {
|
246
256
|
qry.filters = [{
|
247
257
|
type: "group",
|
248
|
-
operator:
|
249
|
-
chainedWith:
|
258
|
+
operator: "and",
|
259
|
+
chainedWith: "and",
|
250
260
|
filters: qry.filters
|
251
261
|
}];
|
252
262
|
}
|
@@ -359,12 +369,16 @@ module.exports = function () {
|
|
359
369
|
|
360
370
|
return QueryBuilder;
|
361
371
|
|
362
|
-
function createSubQueryBuilder(qb, entity, key
|
372
|
+
function createSubQueryBuilder(qb, entity, key) {
|
373
|
+
var isArrayRelation = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
374
|
+
var modifier = arguments[4];
|
375
|
+
|
363
376
|
return new QueryBuilder({
|
364
377
|
parentBuilder: qb,
|
365
378
|
entity: entity,
|
366
379
|
key: key,
|
367
|
-
modifier: modifier
|
380
|
+
modifier: modifier,
|
381
|
+
isArrayRelation: isArrayRelation
|
368
382
|
});
|
369
383
|
}
|
370
384
|
|
package/package.json
CHANGED
@@ -1,23 +1,25 @@
|
|
1
1
|
{
|
2
2
|
"name": "tg-client-query-builder",
|
3
|
-
"version": "2.
|
3
|
+
"version": "2.14.2",
|
4
4
|
"description": "Teselagen Client Side (browser) SQL Query Builder",
|
5
5
|
"main": "build/query-builder/index.js",
|
6
6
|
"repository": "https://github.com/TeselaGen/tg-query.git",
|
7
7
|
"author": "TeselaGen",
|
8
8
|
"license": "ISC",
|
9
9
|
"scripts": {
|
10
|
-
"
|
10
|
+
"prepublishOnly": "yarn build",
|
11
|
+
"build": "babel --presets=es2015 query-builder/ --out-dir build/query-builder/",
|
12
|
+
"test": "jest"
|
11
13
|
},
|
12
|
-
"prepublish": "yarn build",
|
13
14
|
"dependencies": {
|
14
15
|
"lodash": "^4.17.4"
|
15
16
|
},
|
16
17
|
"devDependencies": {
|
17
|
-
"chai": "^4.0.2",
|
18
|
-
"fs-extra": "^3.0.1",
|
19
18
|
"babel-cli": "^6.26.0",
|
20
19
|
"babel-preset-latest": "^6.24.1",
|
20
|
+
"chai": "^4.0.2",
|
21
|
+
"fs-extra": "^3.0.1",
|
22
|
+
"jest": "^28.1.2",
|
21
23
|
"json-beautify": "^1.0.1",
|
22
24
|
"json-format": "^1.0.1",
|
23
25
|
"jsonfile": "^3.0.0"
|
package/query-builder/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
// valid filters:
|
1
|
+
// valid filters:
|
2
2
|
// greaterThan
|
3
3
|
// inList
|
4
4
|
// lessThan
|
@@ -21,232 +21,163 @@
|
|
21
21
|
// notContains
|
22
22
|
// upperCase
|
23
23
|
// lowerCase
|
24
|
-
// matchesRegex (note: this takes a string like "thomas.*is.*cool", don't include the outer slashes like /asdf/ and don't pass a regex object )
|
24
|
+
// matchesRegex (note: this takes a string like "thomas.*is.*cool", don't include the outer slashes like /asdf/ and don't pass a regex object )
|
25
25
|
// fuzzy
|
26
26
|
const FilterExpression = require("./filter-expression");
|
27
27
|
const _ = require("lodash");
|
28
28
|
|
29
|
-
|
30
29
|
const combineQueries = require("./combine-queries");
|
31
30
|
|
32
31
|
function isDateOrNumber(opName) {
|
33
32
|
return () => {
|
34
33
|
var args = [].slice.call(arguments);
|
35
|
-
if (
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
);
|
34
|
+
if (
|
35
|
+
args.some((arg) => {
|
36
|
+
return !(_.isDate(arg) || _.isString(arg) || _.isNumber(arg));
|
37
|
+
})
|
38
|
+
) {
|
39
|
+
throw new Error(`QueryBuilderError: You must pass a date or number as args to ${opName}. You passed: Args ${args.join(",")}`);
|
41
40
|
}
|
42
|
-
}
|
41
|
+
};
|
43
42
|
}
|
44
43
|
function isArray(opName) {
|
45
44
|
return (arg) => {
|
46
45
|
if (!_.isArray(arg)) {
|
47
|
-
throw new Error(
|
48
|
-
`QueryBuilderError: You must pass an array for ${opName} filters. You passed: ${arg}`
|
49
|
-
);
|
46
|
+
throw new Error(`QueryBuilderError: You must pass an array for ${opName} filters. You passed: ${arg}`);
|
50
47
|
}
|
51
|
-
}
|
48
|
+
};
|
52
49
|
}
|
53
50
|
function isString(opName) {
|
54
51
|
return (arg) => {
|
55
52
|
if (!_.isString(arg)) {
|
56
|
-
throw new Error(
|
57
|
-
`QueryBuilderError: You must pass a string for ${opName} filters. You passed: ${arg}`
|
58
|
-
);
|
53
|
+
throw new Error(`QueryBuilderError: You must pass a string for ${opName} filters. You passed: ${arg}`);
|
59
54
|
}
|
60
|
-
}
|
55
|
+
};
|
61
56
|
}
|
62
57
|
|
63
58
|
function numberOfArgs(opName, argLength) {
|
64
59
|
return (...args) => {
|
65
60
|
if (args.length !== argLength) {
|
66
|
-
throw new Error(
|
67
|
-
`QueryBuilderError: Args for ${opName} are of length ${args.length}, but they should be of length ${argLength}`
|
68
|
-
);
|
61
|
+
throw new Error(`QueryBuilderError: Args for ${opName} are of length ${args.length}, but they should be of length ${argLength}`);
|
69
62
|
}
|
70
63
|
};
|
71
64
|
}
|
72
65
|
|
73
|
-
|
74
66
|
const expressionOperators = [
|
75
67
|
{
|
76
68
|
opName: "greaterThan",
|
77
|
-
sanityChecks: [
|
78
|
-
numberOfArgs("greaterThan", 1),
|
79
|
-
isDateOrNumber("greaterThan")
|
80
|
-
]
|
69
|
+
sanityChecks: [numberOfArgs("greaterThan", 1), isDateOrNumber("greaterThan")],
|
81
70
|
},
|
82
71
|
{
|
83
72
|
opName: "inList",
|
84
|
-
sanityChecks: [numberOfArgs("inList", 1), isArray("inList")]
|
73
|
+
sanityChecks: [numberOfArgs("inList", 1), isArray("inList")],
|
85
74
|
},
|
86
75
|
{
|
87
76
|
opName: "lessThan",
|
88
|
-
sanityChecks: [
|
89
|
-
numberOfArgs("lessThan", 1),
|
90
|
-
isDateOrNumber("lessThan"),
|
91
|
-
]
|
77
|
+
sanityChecks: [numberOfArgs("lessThan", 1), isDateOrNumber("lessThan")],
|
92
78
|
},
|
93
79
|
{
|
94
80
|
opName: "lessThanOrEqual",
|
95
|
-
sanityChecks: [
|
96
|
-
numberOfArgs("lessThanOrEqual", 1),
|
97
|
-
isDateOrNumber("lessThanOrEqual"),
|
98
|
-
]
|
81
|
+
sanityChecks: [numberOfArgs("lessThanOrEqual", 1), isDateOrNumber("lessThanOrEqual")],
|
99
82
|
},
|
100
83
|
{
|
101
84
|
opName: "equals",
|
102
|
-
sanityChecks: [
|
103
|
-
numberOfArgs("equals", 1),
|
104
|
-
]
|
85
|
+
sanityChecks: [numberOfArgs("equals", 1)],
|
105
86
|
},
|
106
87
|
{
|
107
88
|
opName: "greaterThanOrEqual",
|
108
|
-
sanityChecks: [
|
109
|
-
numberOfArgs("greaterThanOrEqual", 1),
|
110
|
-
isDateOrNumber("greaterThanOrEqual"),
|
111
|
-
]
|
89
|
+
sanityChecks: [numberOfArgs("greaterThanOrEqual", 1), isDateOrNumber("greaterThanOrEqual")],
|
112
90
|
},
|
113
91
|
{
|
114
92
|
opName: "notEquals",
|
115
|
-
sanityChecks: [
|
116
|
-
numberOfArgs("notEquals", 1),
|
117
|
-
]
|
93
|
+
sanityChecks: [numberOfArgs("notEquals", 1)],
|
118
94
|
},
|
119
95
|
{
|
120
96
|
opName: "notNull",
|
121
|
-
sanityChecks: [
|
122
|
-
numberOfArgs("notNull", 0),
|
123
|
-
]
|
97
|
+
sanityChecks: [numberOfArgs("notNull", 0)],
|
124
98
|
},
|
125
99
|
{
|
126
100
|
opName: "isNull",
|
127
|
-
sanityChecks: [
|
128
|
-
numberOfArgs("isNull", 0),
|
129
|
-
]
|
101
|
+
sanityChecks: [numberOfArgs("isNull", 0)],
|
130
102
|
},
|
131
103
|
{
|
132
104
|
opName: "between",
|
133
|
-
sanityChecks: [
|
134
|
-
numberOfArgs("between", 2),
|
135
|
-
isDateOrNumber("between"),
|
136
|
-
]
|
105
|
+
sanityChecks: [numberOfArgs("between", 2), isDateOrNumber("between")],
|
137
106
|
},
|
138
107
|
{
|
139
108
|
opName: "notInList",
|
140
|
-
sanityChecks: [
|
141
|
-
numberOfArgs("notInList", 1),
|
142
|
-
isArray("notInList"),
|
143
|
-
]
|
109
|
+
sanityChecks: [numberOfArgs("notInList", 1), isArray("notInList")],
|
144
110
|
},
|
145
111
|
{
|
146
112
|
opName: "startsWithExactly",
|
147
|
-
sanityChecks: [
|
148
|
-
numberOfArgs("startsWith", 1),
|
149
|
-
isString("startsWith")
|
150
|
-
]
|
113
|
+
sanityChecks: [numberOfArgs("startsWith", 1), isString("startsWith")],
|
151
114
|
},
|
152
115
|
{
|
153
116
|
opName: "endsWithExactly",
|
154
|
-
sanityChecks: [
|
155
|
-
numberOfArgs("endsWith", 1),
|
156
|
-
isString("endsWith")
|
157
|
-
]
|
117
|
+
sanityChecks: [numberOfArgs("endsWith", 1), isString("endsWith")],
|
158
118
|
},
|
159
119
|
{
|
160
120
|
opName: "containsExactly",
|
161
|
-
sanityChecks: [
|
162
|
-
numberOfArgs("contains", 1),
|
163
|
-
isString("contains")
|
164
|
-
]
|
121
|
+
sanityChecks: [numberOfArgs("contains", 1), isString("contains")],
|
165
122
|
},
|
166
123
|
{
|
167
124
|
opName: "startsWith",
|
168
|
-
sanityChecks: [
|
169
|
-
numberOfArgs("startsWith", 1),
|
170
|
-
isString("startsWith")
|
171
|
-
]
|
125
|
+
sanityChecks: [numberOfArgs("startsWith", 1), isString("startsWith")],
|
172
126
|
},
|
173
127
|
{
|
174
128
|
opName: "notStartsWith",
|
175
|
-
sanityChecks: [
|
176
|
-
numberOfArgs("notStartsWith", 1),
|
177
|
-
isString("notStartsWith")
|
178
|
-
]
|
129
|
+
sanityChecks: [numberOfArgs("notStartsWith", 1), isString("notStartsWith")],
|
179
130
|
},
|
180
131
|
{
|
181
132
|
opName: "endsWith",
|
182
|
-
sanityChecks: [
|
183
|
-
numberOfArgs("endsWith", 1),
|
184
|
-
isString("endsWith")
|
185
|
-
]
|
133
|
+
sanityChecks: [numberOfArgs("endsWith", 1), isString("endsWith")],
|
186
134
|
},
|
187
135
|
{
|
188
136
|
opName: "notEndsWith",
|
189
|
-
sanityChecks: [
|
190
|
-
numberOfArgs("notEndsWith", 1),
|
191
|
-
isString("notEndsWith")
|
192
|
-
]
|
137
|
+
sanityChecks: [numberOfArgs("notEndsWith", 1), isString("notEndsWith")],
|
193
138
|
},
|
194
139
|
{
|
195
140
|
opName: "contains",
|
196
|
-
sanityChecks: [
|
197
|
-
numberOfArgs("contains", 1),
|
198
|
-
isString("contains")
|
199
|
-
]
|
141
|
+
sanityChecks: [numberOfArgs("contains", 1), isString("contains")],
|
200
142
|
},
|
201
143
|
{
|
202
144
|
opName: "notContains",
|
203
|
-
sanityChecks: [
|
204
|
-
numberOfArgs("notContains", 1),
|
205
|
-
isString("notContains")
|
206
|
-
]
|
145
|
+
sanityChecks: [numberOfArgs("notContains", 1), isString("notContains")],
|
207
146
|
},
|
208
147
|
{
|
209
148
|
opName: "upperCase",
|
210
|
-
sanityChecks: [
|
211
|
-
numberOfArgs("upperCase", 1),
|
212
|
-
isString("upperCase")
|
213
|
-
]
|
149
|
+
sanityChecks: [numberOfArgs("upperCase", 1), isString("upperCase")],
|
214
150
|
},
|
215
151
|
{
|
216
152
|
opName: "lowerCase",
|
217
|
-
sanityChecks: [
|
218
|
-
numberOfArgs("lowerCase", 1),
|
219
|
-
isString("lowerCase")
|
220
|
-
]
|
153
|
+
sanityChecks: [numberOfArgs("lowerCase", 1), isString("lowerCase")],
|
221
154
|
},
|
222
155
|
{
|
223
156
|
opName: "matchesRegex",
|
224
|
-
sanityChecks: [
|
225
|
-
|
226
|
-
|
227
|
-
|
157
|
+
sanityChecks: [numberOfArgs("matchesRegex", 1), isString("matchesRegex")],
|
158
|
+
},
|
159
|
+
{
|
160
|
+
opName: "matchesSimilar",
|
161
|
+
sanityChecks: [numberOfArgs("matchesSimilar", 1), isString("matchesSimilar")],
|
228
162
|
},
|
229
163
|
{
|
230
164
|
opName: "fuzzy",
|
231
|
-
sanityChecks: [
|
232
|
-
numberOfArgs("fuzzy", 1),
|
233
|
-
isString("fuzzy")
|
234
|
-
],
|
165
|
+
sanityChecks: [numberOfArgs("fuzzy", 1), isString("fuzzy")],
|
235
166
|
transform: (arg) => {
|
236
|
-
// Build Regex String
|
237
|
-
var matchTerm =
|
167
|
+
// Build Regex String
|
168
|
+
var matchTerm = "";
|
238
169
|
// Split all the search terms
|
239
|
-
var terms = arg.replace(/\W/g,
|
170
|
+
var terms = arg.replace(/\W/g, "").replace(" ", "").split("");
|
240
171
|
for (var i = 0; i < terms.length; i++) {
|
241
|
-
matchTerm +=
|
172
|
+
matchTerm += ".*" + terms[i];
|
242
173
|
}
|
243
|
-
matchTerm +=
|
174
|
+
matchTerm += ".*";
|
244
175
|
return {
|
245
|
-
newOpName:
|
246
|
-
newArgs: [matchTerm]
|
247
|
-
}
|
248
|
-
}
|
249
|
-
}
|
176
|
+
newOpName: "matchesRegex",
|
177
|
+
newArgs: [matchTerm],
|
178
|
+
};
|
179
|
+
},
|
180
|
+
},
|
250
181
|
|
251
182
|
// 'subString', //tnr: not yet implemented
|
252
183
|
// 'dateOnly', //tnr: not yet implemented
|
@@ -286,15 +217,14 @@ module.exports = (function () {
|
|
286
217
|
function QueryBuilder(entity) {
|
287
218
|
this.query = {};
|
288
219
|
|
289
|
-
if (entity == null)
|
290
|
-
throw new Error("You must pass the name of the model being filtered!");
|
220
|
+
if (entity == null) throw new Error("You must pass the name of the model being filtered!");
|
291
221
|
|
292
222
|
if (typeof entity === "string") {
|
293
223
|
this.query = {
|
294
224
|
__objectType: "query",
|
295
225
|
type: "root",
|
296
226
|
entity,
|
297
|
-
filters: []
|
227
|
+
filters: [],
|
298
228
|
};
|
299
229
|
} else {
|
300
230
|
let subQuery = entity;
|
@@ -304,8 +234,9 @@ module.exports = (function () {
|
|
304
234
|
entity: subQuery.entity,
|
305
235
|
foreignKey: subQuery.foreignKey,
|
306
236
|
modifier: subQuery.modifier,
|
237
|
+
isArrayRelation: subQuery.isArrayRelation,
|
307
238
|
filters: [],
|
308
|
-
countExpression: undefined
|
239
|
+
countExpression: undefined,
|
309
240
|
};
|
310
241
|
this.parentBuilder = subQuery.parentBuilder;
|
311
242
|
this.toFilter = function (filterBuilder, name) {
|
@@ -320,11 +251,11 @@ module.exports = (function () {
|
|
320
251
|
QueryBuilder.prototype.field = function (fieldName) {
|
321
252
|
return {
|
322
253
|
__objectType: "field",
|
323
|
-
field: fieldName
|
254
|
+
field: fieldName,
|
324
255
|
};
|
325
256
|
};
|
326
257
|
|
327
|
-
QueryBuilder.prototype.related = function (relatedEntity) {
|
258
|
+
QueryBuilder.prototype.related = function (relatedEntity, isArrayRelation) {
|
328
259
|
var tokens = relatedEntity.split(".");
|
329
260
|
var entity = tokens[0];
|
330
261
|
var key = tokens[1];
|
@@ -332,10 +263,10 @@ module.exports = (function () {
|
|
332
263
|
// log("FilterBuilder in related");
|
333
264
|
// log(FilterBuilder);
|
334
265
|
|
335
|
-
return createSubQueryBuilder(this, entity, key);
|
266
|
+
return createSubQueryBuilder(this, entity, key, isArrayRelation);
|
336
267
|
};
|
337
268
|
|
338
|
-
QueryBuilder.prototype.notRelated = function (relatedEntity) {
|
269
|
+
QueryBuilder.prototype.notRelated = function (relatedEntity, isArrayRelation) {
|
339
270
|
var tokens = relatedEntity.split(".");
|
340
271
|
var entity = tokens[0];
|
341
272
|
var key = tokens[1];
|
@@ -343,7 +274,7 @@ module.exports = (function () {
|
|
343
274
|
// log("FilterBuilder in notRelated");
|
344
275
|
// log(FilterBuilder);
|
345
276
|
|
346
|
-
return createSubQueryBuilder(this, entity, key, "not");
|
277
|
+
return createSubQueryBuilder(this, entity, key, isArrayRelation, "not");
|
347
278
|
};
|
348
279
|
|
349
280
|
QueryBuilder.prototype.toJSON = function () {
|
@@ -352,10 +283,10 @@ module.exports = (function () {
|
|
352
283
|
qry.filters = [
|
353
284
|
{
|
354
285
|
type: "group",
|
355
|
-
operator:
|
356
|
-
chainedWith:
|
357
|
-
filters: qry.filters
|
358
|
-
}
|
286
|
+
operator: "and",
|
287
|
+
chainedWith: "and",
|
288
|
+
filters: qry.filters,
|
289
|
+
},
|
359
290
|
];
|
360
291
|
}
|
361
292
|
return qry;
|
@@ -374,7 +305,7 @@ module.exports = (function () {
|
|
374
305
|
// }
|
375
306
|
if (!isFilterExpresionOrSubQuery(name, arg)) {
|
376
307
|
if (Array.isArray(arg)) {
|
377
|
-
filters.push(this.inList(arg).toFilter(this, name))
|
308
|
+
filters.push(this.inList(arg).toFilter(this, name));
|
378
309
|
} else {
|
379
310
|
//log("Is Where Filter: " + name);
|
380
311
|
whereArgs[name] = arg;
|
@@ -387,7 +318,7 @@ module.exports = (function () {
|
|
387
318
|
if (_.keys(whereArgs).length > 0) {
|
388
319
|
filters.unshift({
|
389
320
|
type: "where",
|
390
|
-
args: whereArgs
|
321
|
+
args: whereArgs,
|
391
322
|
});
|
392
323
|
}
|
393
324
|
|
@@ -397,7 +328,7 @@ module.exports = (function () {
|
|
397
328
|
var filterDef = {
|
398
329
|
type: "group",
|
399
330
|
operator: operator || "and",
|
400
|
-
filters
|
331
|
+
filters,
|
401
332
|
};
|
402
333
|
return filterDef;
|
403
334
|
};
|
@@ -451,9 +382,7 @@ module.exports = (function () {
|
|
451
382
|
var args = [].slice.call(arguments);
|
452
383
|
if (this.query.type === "subquery") {
|
453
384
|
if (this.query.countExpression) {
|
454
|
-
throw new Error(
|
455
|
-
"QueryBuilder subquery can only have one count expression"
|
456
|
-
);
|
385
|
+
throw new Error("QueryBuilder subquery can only have one count expression");
|
457
386
|
}
|
458
387
|
this.query.countExpression = args[0].toFilter(this, "count");
|
459
388
|
} else {
|
@@ -467,30 +396,33 @@ module.exports = (function () {
|
|
467
396
|
|
468
397
|
return QueryBuilder;
|
469
398
|
|
470
|
-
function createSubQueryBuilder(qb, entity, key, modifier) {
|
399
|
+
function createSubQueryBuilder(qb, entity, key, isArrayRelation = true, modifier) {
|
471
400
|
return new QueryBuilder({
|
472
401
|
parentBuilder: qb,
|
473
402
|
entity,
|
474
403
|
key,
|
475
|
-
modifier
|
404
|
+
modifier,
|
405
|
+
isArrayRelation,
|
476
406
|
});
|
477
407
|
}
|
478
408
|
|
479
409
|
function attachExpressionFunctions() {
|
480
410
|
expressionOperators.forEach(({ opName, sanityChecks, transform }) => {
|
481
|
-
const filter = (...args)=> {
|
482
|
-
let argsToUse = args
|
483
|
-
let opNameToUse = opName
|
411
|
+
const filter = (...args) => {
|
412
|
+
let argsToUse = args;
|
413
|
+
let opNameToUse = opName;
|
484
414
|
if (transform) {
|
485
|
-
let { newOpName, newArgs } = transform(...args)
|
486
|
-
argsToUse = newArgs
|
487
|
-
opNameToUse = newOpName
|
415
|
+
let { newOpName, newArgs } = transform(...args);
|
416
|
+
argsToUse = newArgs;
|
417
|
+
opNameToUse = newOpName;
|
488
418
|
}
|
489
|
-
sanityChecks.forEach((sanityCheck) => {
|
419
|
+
sanityChecks.forEach((sanityCheck) => {
|
420
|
+
sanityCheck(...args);
|
421
|
+
});
|
490
422
|
return new FilterExpression(opNameToUse, argsToUse);
|
491
423
|
};
|
492
|
-
QueryBuilder.prototype[opName] = filter
|
493
|
-
QueryBuilder.ExpressionJson[opName] = filter
|
424
|
+
QueryBuilder.prototype[opName] = filter;
|
425
|
+
QueryBuilder.ExpressionJson[opName] = filter;
|
494
426
|
});
|
495
427
|
}
|
496
428
|
|
@@ -509,17 +441,16 @@ module.exports = (function () {
|
|
509
441
|
}
|
510
442
|
|
511
443
|
function where(filterBuilder, operator, whereArgs, chainedWith) {
|
512
|
-
if (!Array.isArray(whereArgs))
|
513
|
-
return where(filterBuilder, operator, [whereArgs], chainedWith);
|
444
|
+
if (!Array.isArray(whereArgs)) return where(filterBuilder, operator, [whereArgs], chainedWith);
|
514
445
|
|
515
446
|
var filterDef = {
|
516
447
|
type: "group",
|
517
448
|
operator,
|
518
449
|
chainedWith,
|
519
|
-
filters: []
|
450
|
+
filters: [],
|
520
451
|
};
|
521
452
|
|
522
|
-
whereArgs.forEach(arg => {
|
453
|
+
whereArgs.forEach((arg) => {
|
523
454
|
//add check for object type TODO
|
524
455
|
var filter = filterBuilder.convertToFilter(arg, operator);
|
525
456
|
filterDef.filters.push(filter);
|