This repository was archived by the owner on Jun 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathFindQueryBuilder.js
More file actions
535 lines (480 loc) · 15.7 KB
/
Copy pathFindQueryBuilder.js
File metadata and controls
535 lines (480 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
'use strict';
const _ = require('lodash');
const utils = require('./utils');
const filters = require('./filters');
const PropertyRef = require('./PropertyRef');
const QueryParameter = require('./QueryParameter');
const SPECIAL_PARAMETERS = Object.freeze({
eager: 'eager',
rangeEnd: 'rangeEnd',
rangeStart: 'rangeStart',
orderBy: 'orderBy',
orderByAsc: 'orderByAsc',
orderByDesc: 'orderByDesc',
groupBy: 'groupBy',
count: 'count'
});
/**
* A class for building HTTP query parameter controlled find queries for objection.js models.
*
* Usage example:
*
* ```js
* var findQuery = require('objection-find');
* var Person = require('../models/Person');
*
* expressApp.get('/api/persons', function (req, res, next) {
* findQuery(Person).build(req.query).then(function (persons) {
* res.send(persons);
* }).catch(next);
* });
* ```
*
* This class understands two kinds of query parameters: filters and special parameters.
*
*
* ## Filters
*
* A filter parameter has the following format:
*
* ```
* <propertyReference>|<propertyReference>|...:<filterName>=<value>
* ```
*
* A <propertyReference> is either simply a property name like `firstName` or a reference to a
* relation's property like `pets.name` (`pets` is the name of the relation).
*
* <filterName> is one of the built-in filters `eq`, `lt`, `lte`, `gt`, `gte`, `like`, `likeLower`
* `in`, `notNull` or `isNull`. Filter can also be a custom filter registered using the
* `registerFilter` method.
*
* The following examples explain how filter parameters work:
*
* | Filter query parameter | Explanation |
* |------------------------------------|---------------------------------------------------------------------------------------------------------|
* | `firstName=Jennifer` | Returns all Persons whose firstName is 'Jennifer'. |
* | `firstName:eq=Jennifer` | Returns all Persons whose firstName is 'Jennifer'. |
* | `pets.name:like=Fluf%` | Returns all Persons that have at least one pet whose name starts with 'Fluf'. |
* | `lastName|movies.name:like=%Gump%` | Returns all Persons whose last name contains 'Gump' or who acted in a movie whose name contains 'Gump'. |
* | `parent.age:lt=60` | Returns all persons whose parent's age is less than 60. |
* | `parent.age:in=20,22,24` | Returns all persons whose parent's age is 20, 22 or 24. |
*
* Filter query parameters are joined with `AND` operator so for example the query string:
*
* ```
* firstName:eq=Jennifer&parent.age:lt=60&pets.name:like=Fluf%
* ```
*
* would return the Persons whose firstName is 'Jennifer' and whose parent's age is less than 60 and who have
* at least one pet whose name starts with 'Fluf'.
*
*
* ## Special parameters
*
* In addition to the filter parameters, there is a set of query parameters that have a special meaning:
*
* | Special parameter | Explanation |
* |-------------------------------|----------------------------------------------------------------------------------------------|
* | `eager=[pets, parent.movies]` | Which relations to fetch eagerly for the result models. An objection.js relation expression. |
* | `orderBy=firstName` | Sort the result by certain property. |
* | `orderByDesc=firstName` | Sort the result by certain property in descending order. |
* | `rangeStart=10` | The start of the result range. The result will be `{total: 12343, results: [ ... ]}`. |
* | `rangeEnd=50` | The end of the result range. The result will be `{total: 12343, results: [ ... ]}`. |
*
* @param {Model} modelClass
* @constructor
*/
class FindQueryBuilder {
constructor(modelClass) {
/**
* An objection.js `Model` subclass constructor.
*
* The model for which the find query is created.
*
* @type {Model}
* @private
*/
this._modelClass = modelClass;
/**
* If this is true (default) all property references are allowed.
*
* @type {boolean}
* @private
*/
this._allowAll = true;
/**
* Hash of allowed property references.
*
* @type {Object.<string, PropertyRef>}
* @private
*/
this._allow = Object.create(null);
/**
* Which relations of the result models can be fetched eagerly.
*
* See objection.js for information about relation expressions.
*
* @type {string|RelationExpression}
* @private
*/
this._allowEager = null;
/**
* Registered filter functions.
*
* @type {Object.<string, function>}
* @private
*/
this._filters = Object.create(null);
/**
* Query parameter names for `special` parameters.
*
* @type {Object.<string, string>}
* @private
*/
this._specialParameterMap = Object.create(null);
/**
* The inverse of `_specialParameterMap`.
*
* @type {Object.<string, string>}
* @private
*/
this._inverseSpecialParameterMap = Object.create(null);
/**
* Cache for `PropertyRef` objects.
*
* @type {Object.<string, PropertyRef>}
* @private
*/
this._propertyRefCache = Object.create(null);
// Register default filters.
_.each(filters, (filter, name) => {
this.registerFilter(name, filter);
});
// Give default names for the special parameters.
_.each(SPECIAL_PARAMETERS, (parameterName, name) => {
this.specialParameter(name, parameterName);
});
}
/**
* Allow all property references.
*
* This is true by default.
*
* @returns {FindQueryBuilder}
*/
allowAll() {
this._allowAll = true;
this._allow = [];
return this;
}
/**
* Sets/gets the allowed eager expression.
*
* Calls the `allowEager` method of a objection.js `QueryBuilder`. See the objection.js
* documentation for more information.
*
* @param {String|RelationExpression=} exp
* @returns {String|RelationExpression|FindQueryBuilder}
*/
allowEager(exp) {
if (arguments.length === 0) {
return this._allowEager;
} else {
this._allowEager = exp;
return this;
}
}
/**
* Use this method to whitelist property references.
*
* By default all properties and relations' properties can be used in the filters
* and in orderBy. This method can be used to whitelist only a subset of them.
*
* ```js
* findQuery(Person).allow('firstName', 'parent.firstName', 'pets.name');
* ```
*
* @returns {FindQueryBuilder}
*/
allow() {
this._allowAll = false;
_.merge(this._allow, this._parsePropertyRefs(toArray(arguments)));
return this;
}
/**
* Registers a filter function.
*
* Given a query parameter `someProp:eq=10` the `eq` part is the filter. The filter name
* (in this case 'eq') is mapped to a function that performs the filtering.
*
* Filter functions take in a `PropertyRef` instance of the property to be filtered,
* the filter value and the objection.js model class constructor. The filter functions
* must return an object `{method: string, args: *}`. For example:
*
* ```js
* function lowercaseEq(propertyRef, value, modelClass) {
* return {
* method: 'where',
* // You can access the name of the column we are filtering through
* // `propertyRef.fullColumnName()`.
* args: [propertyRef.fullColumnName(), '=', value.toLowerCase()]
* };
* }
* ```
*
* A better `lowercaseEq` would also lowercase the column value:
*
* ```js
* function lowercaseEq(propertyRef, value, modelClass) {
* // Always use knex columnization for column references when building raw queries to make sure column names are escaped.
* return {
* method: 'whereRaw',
* // Always escape the user input when building raw queries.
* args: ['lower(' + columnName + ') = ?', value.toLowerCase()];
* args: ['lower(??) = ?', [propertyRef.fullColumnName(), value.toLowerCase()]]
* };
* }
* ```
*
* The `method` must be the name of one of the knex.js where methods. `args` is the array
* of arguments for the method. The filter is invoked somewhat like this:
*
* ```js
* const filter = lowercaseEq(propertyRef, value, modelClass);
* queryBuilder[filter.method].apply(queryBuilder, filter.args);
* ```
*
* The args array can be anything the given where method accepts as an argument. Check
* out the knex.js documentation.
*
* To register `lowercaseEq`:
*
* ```js
* builder.registerFilter('leq', lowercaseEq);
* ```
*
* Now you could use your filter in the query parameters like this `someProperty:leq=Hello`.
*
* @param {string} filterName
* @param {function} filter
* @returns {FindQueryBuilder}
*/
registerFilter(filterName, filter) {
this._filters[filterName] = filter;
return this;
}
/**
* Give names for the special parameters.
*
* This can be used to rename a special parameter for example if it collides with a property name.
* The following example you can fetch relations eagerly by giving a `withRelated=[pets, movies]`
* query parameter instead of `eager=[pets, movies]`.
*
* ```js
* builder.specialParameter('eager', 'withRelated');
* ```
*
* @param name
* @param parameterName
* @returns {FindQueryBuilder}
*/
specialParameter(name, parameterName) {
this._specialParameterMap[name] = parameterName;
this._inverseSpecialParameterMap = _.invert(this._specialParameterMap);
return this;
}
/**
* Builds the find query for the given query parameters.
*
* ```js
* var findQuery = require('objection-find');
* var Person = require('../models/Person');
*
* expressApp.get('/api/persons', function (req, res, next) {
* findQuery(Person).build(req.query).then(function (persons) {
* res.send(persons);
* }).catch(next);
* });
* ```
*
* @param {Object<string, string|Array.<string>>} params
* Query parameter hash. For example express's `req.query`.
*
* @param {QueryBuilder=} builder
* Optional objection.js QueryBuilder instance. If not given,
* modelClass.query() is used.
*
* @returns {QueryBuilder}
*/
build(params, builder) {
builder = builder || this._modelClass.query();
params = this._parseQueryParameters(params);
this._buildCount(params, builder);
this._buildJoins(params, builder);
this._buildFilters(params, builder);
this._buildGroupBy(params, builder);
this._buildOrderBy(params, builder);
this._buildRange(params, builder);
this._buildEager(params, builder);
return builder;
}
_parseQueryParameters(params) {
const parsed = [];
_.each(params, (value, key) => {
if (_.isArray(value)) {
return _.each(value, value => {
parsed.push(new QueryParameter(value, key, this));
});
} else {
parsed.push(new QueryParameter(value, key, this));
}
});
// Check that we only have allowed property references in the query parameters.
if (!this._allowAll) {
_.each(parsed, param => {
_.each(param.propertyRefs, ref => {
if (!this._allow[ref.str]) {
utils.throwError('Property reference "' + ref.str + '" not allowed');
}
});
});
}
return parsed;
}
_buildCount(params, builder) {
const countParam = _.find(params, { key: 'count' });
if (countParam) {
builder.count(countParam.value);
}
}
_buildJoins(params, builder) {
// Array of Objection `Relation` subclass instances.
const relationsToJoin = [];
_.each(params, function(param) {
_.each(param.propertyRefs, ref => {
const rel = ref.relation;
if (rel && rel.isOneToOne()) {
relationsToJoin.push(rel);
}
});
});
_.each(_.uniq(relationsToJoin, 'name'), relation => {
relation.join(builder, {
joinOperation: 'leftJoin',
relatedTableAlias: this._modelClass.tableName + '_rel_' + relation.name
});
});
if (!_.isEmpty(relationsToJoin)) {
builder.select(this._modelClass.tableName + '.*');
}
}
_buildFilters(params, builder) {
const filterParams = _.filter(params, 'filter');
_.each(filterParams, param => {
this._buildFilter(param, builder);
});
}
/**
* @private
*/
_buildFilter(param, builder) {
const refNames = _.keys(param.propertyRefs);
if (refNames.length === 1) {
const ref = param.propertyRefs[refNames[0]];
ref.buildFilter(param, builder);
} else {
// If there are multiple property refs, they are combined with an `OR` operator.
builder.where(function() {
const builder = this;
_.each(param.propertyRefs, function(ref) {
ref.buildFilter(param, builder, 'or');
});
});
}
}
/**
* @private
*/
_buildGroupBy(params, builder) {
const groupByParam = _.find(params, { key: 'groupBy' });
if (groupByParam) {
builder.select(groupByParam.value.split(','));
builder.groupBy(groupByParam.value.split(','));
}
}
/**
* @private
*/
_buildOrderBy(params, builder) {
const self = this;
_.each(params, param => {
const orderType = param.specialParameter;
if (orderType && orderType.indexOf('orderBy') !== -1) {
let dir = 'asc';
if (orderType === 'orderByDesc') {
dir = 'desc';
}
_.each(param.propertyRefs, propertyRef => {
const rel = propertyRef.relation;
if (rel) {
if (!rel.isOneToOne()) {
utils.throwError(
"Can only order by model's own properties and by BelongsToOneRelation relations' properties"
);
}
const columnNameAlias = rel.name + _.capitalize(propertyRef.propertyName);
builder.select(propertyRef.fullColumnName() + ' as ' + columnNameAlias);
builder.orderBy(columnNameAlias, dir);
} else {
builder.orderBy(propertyRef.columnName, dir);
}
});
}
});
}
_buildRange(params, builder) {
let rangeStart = _.find(params, { specialParameter: 'rangeStart' });
let rangeEnd = _.find(params, { specialParameter: 'rangeEnd' });
if (rangeStart && rangeEnd) {
rangeStart = _.parseInt(rangeStart.value);
rangeEnd = _.parseInt(rangeEnd.value);
if (_.isNaN(rangeStart) || _.isNaN(rangeEnd)) {
utils.throwError('Invalid range start or end "' + rangeStart + ' - ' + rangeEnd + '"');
}
builder.range(rangeStart, rangeEnd);
}
}
_buildEager(params, builder) {
let eager = _.find(params, { specialParameter: 'eager' });
if (!eager) {
return;
}
if (this._allowEager) {
builder.allowEager(this._allowEager);
}
builder.eager(eager.value);
}
_parsePropertyRefs(refs) {
return _.reduce(
refs,
(output, ref) => {
output[ref] = this._parsePropertyRef(ref);
return output;
},
{}
);
}
_parsePropertyRef(ref) {
if (!this._propertyRefCache[ref]) {
this._propertyRefCache[ref] = new PropertyRef(ref, this);
}
return this._propertyRefCache[ref];
}
}
function toArray() {
return _(arguments)
.flattenDeep()
.compact()
.value();
}
module.exports = FindQueryBuilder;