-
Notifications
You must be signed in to change notification settings - Fork 55
/
query.js
400 lines (353 loc) · 12.2 KB
/
query.js
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
var query = {}
, Query
, utils = require('utilities')
, model = require('../index')
, operation = require('./operation')
, comparison = require('./comparison');
Query = function (model, conditions, options) {
this.model = null;
this.conditions = null;
this.initialize.apply(this, arguments);
};
Query.comparisonTypes = {
'eql': 'EqualTo'
, 'ne': 'NotEqualTo'
, 'in': 'Inclusion'
, 'like': 'Like'
, 'gt': 'GreaterThan'
, 'lt': 'LessThan'
, 'gte': 'GreaterThanOrEqual'
, 'lte': 'LessThanOrEqual'
};
Query.prototype = new (function () {
var _operationTypes = {
'and': true
, 'or': true
, 'not': true
, 'null': true
}
, _isQueryObject = function (obj) {
// Just generic query object -- not null, Date, or Boolean, or Array
return (obj && typeof obj == 'object' && !(obj instanceof Date) &&
!(obj instanceof Boolean) && !Array.isArray(obj));
}
, _extractComparisonType = function (obj) {
// Just generic query object -- not null, Date, or Boolean
if (_isQueryObject(obj)) {
// Return any valid comparison type used as key
for (var p in obj) {
if (Query.comparisonTypes[p]) {
return p;
}
}
}
}
, _createFieldValidator = function () {
var self = this
, baseProps = {
id: true
, createdAt: true
, updatedAt: true
};
return function (k) {
var keyName = k
, keyNameArr
, modelName
, reg
, assnTokens = [];
// Users.loginId, Teams.name
// Sort on nested association property
modelName = self.model.modelName
// Walks through the associations and updates modelName
// until we reach the property name
if (keyName.indexOf('.') > -1) {
if(self.opts != null && self.opts.limit != null) {
throw new Error('It is not possible to query on an association when there is a limit clause')
}
keyNameArr = keyName.split('.');
while(keyNameArr.length > 1) {
var tempAssnName = keyNameArr.shift()
, assn = model.getAssociation(modelName, tempAssnName)
assnTokens.push(tempAssnName)
if(!assn) {
throw new Error('The association "' + tempAssnName + '" is not a valid ' +
'property on the ' + modelName + ' model.');
}
else {
modelName = assn.model
}
}
keyName = keyNameArr[0]
}
reg = model.descriptionRegistry[modelName].properties;
if (baseProps[keyName] || reg[keyName]) {
return {
modelName: modelName
, propertyName: keyName
, assnTokens: assnTokens
};
}
else {
return null;
}
};
}
, _createOperation = function (conditions, key) {
var self = this
, type = key || 'and'
, cond
, item
, op = operation.create(type)
, notOperand
, operand
, keys;
// TODO: Handle associations
for (var k in conditions) {
cond = conditions[k];
// Operation type, can contain other operations/conditions
if (_operationTypes[k]) {
// Base operation-type to create: if the type is a 'not',
// create a single 'and' with the same conditions to wrap
// in a 'not'
type = k == 'not' ? 'and' : k;
// If the conditions are an array, create a single 'and'
// op that wraps each set of conditions in each item, and
// add to the wrapper
if (Array.isArray(cond)) {
// Create empty wrapper
operand = operation.create(type);
cond.forEach(function (c) {
operand.add(_createOperation.apply(self, [c, 'and']));
});
}
// Simple object-literal, just create an operation
else {
operand = _createOperation.apply(this, [cond, type]);
}
// If this was a 'not' operation, create a wrapping 'not'
// to contain the single operation created
if (k == 'not') {
notOperand = operation.create(k);
notOperand.add(operand);
operand = notOperand;
}
}
// Condition, may be leaf-node or multiple comparisions
// ANDed on the same field
else {
// Exclude null, exclude array-values, only consider actual objects
if (_isQueryObject(cond)) {
keys = Object.keys(cond);
}
// If there are multiple keys, means it's multiple comparisons on
// the same field
if (keys && keys.length > 1) {
// Create wrapper operation
operand = operation.create('and');
// Go through each of the comparision keys in the object
// and create single comparisions which can be ANDed together.
// E.g.: {foo: {gte: 1, lte: 5}} =>
// {and: [{foo: {gte: 1}}, {foo: {lte: 5}}]}
keys.forEach(function (kk) {
var q = {};
q[k] = {};
q[k][kk] = cond[kk];
if (!Query.comparisonTypes[kk]) {
throw new Error(kk + ' is not a valid type of comparison');
}
operand.add(_createOperation.apply(self, [q, 'and']));
});
}
// Simple condition (leaf-node)
// {foo: {ne: 'asdf'} or {foo: 1} or {foo: [1, 2, 3]}
else {
operand = _createComparison.apply(this, [cond, k]);
}
}
op.add(operand);
}
return op;
}
, _createComparison = function (val, key) {
var type
, keyName = key
, keyNameArr
, modelName
, props
, descr
, datatype
, opts
, assnTokens = [];
modelName = this.model.modelName
assnTokens.push(modelName)
// Walks through the associations and updates modelName
// until we reach the property name
if (keyName.indexOf('.') > -1) {
if(this.opts != null && this.opts.limit != null) {
throw new Error('It is not possible to query on an association when there is a limit clause')
}
keyNameArr = keyName.split('.');
modelName = this.model.modelName;
while(keyNameArr.length > 1) {
var tempAssnName = keyNameArr.shift()
, assn = model.getAssociation(modelName, tempAssnName)
assnTokens.push(tempAssnName)
if(!assn) {
throw new Error('The association "' + tempAssnName + '" is not a valid ' +
'property on the ' + modelName + ' model.');
}
else {
modelName = assn.model
}
}
keyName = keyNameArr[0]
}
props = model.descriptionRegistry[modelName].properties;
descr = props[keyName];
// {id: ['foo', 'bar', 'baz']}, shorthand for Inclusion
if (Array.isArray(val)) {
type = 'in';
}
else {
// Single query objects -- not null, Date, Boolean
// e.g., {id: {'like': 'foo'}}
type = _extractComparisonType(val);
if (type) {
val = val[type];
}
// Simple scalar value, default to equality
else {
type = 'eql';
}
}
// FIXME: How the fuck to handle IDs?
// id isn't in the defined props
if (keyName == 'id') {
if (model.config.autoIncrementId) {
datatype = 'int';
}
else {
datatype = 'string';
}
}
else {
if (typeof descr === 'undefined') {
throw new Error('The property "' + keyName + '" is not a valid ' +
'property on the ' + modelName + ' model.');
}
datatype = descr.datatype;
}
opts = _createComparisonOpts.apply(this, [keyName, datatype, assnTokens]);
// TODO: Validate the value for both the particular field
// (e.g., must be a numeric) and the type of comparison
// (e.g., 'IN' must be a collection, etc)
return comparison.create(Query.comparisonTypes[type], modelName,
keyName, val, datatype, opts);
}
, _createComparisonOpts = function (key, datatype, assnTokens) {
var opts = this.opts
, nocase = opts.nocase
, ret = {};
if (nocase && (datatype == 'string' || datatype == 'text')) {
if (Array.isArray(nocase)) {
if (nocase.some(function (o) {
return o == key;
})) {
ret.nocase = true;
}
}
else {
ret.nocase = true;
}
}
if(Array.isArray(assnTokens) && assnTokens.length) {
ret.assnTokens = assnTokens
}
return ret;
}
, _parseOpts = function (options) {
var opts = options || {}
, ret = {}
, val
, parsed
, validatedField
, validated = {}
, defaultDir = 'asc';
for (var prop in opts) {
val = opts[prop];
switch (prop) {
case 'sort':
// 'foo,bar,baz'
if (typeof val == 'string') {
val = val.split(',');
}
// ['foo', 'bar', 'baz']
if (Array.isArray(val)) {
parsed = {};
val.forEach(function (v) {
parsed[v] = defaultDir;
});
}
else {
parsed = val;
}
// Now there's a well-formed obj, validate fields
for (var p in parsed) {
val = parsed[p].toLowerCase();
validatedField = this.getValidField(p);
if (!validatedField) {
throw new Error(p + ' is not a valid field for ' +
this.model.modelName);
}
if (!(val == 'asc' || val == 'desc')) {
throw new Error('Sort directon for ' + p +
' field on ' + validatedField.modelName + ' must be ' +
'either "asc" or "desc"');
}
if (validatedField.assnTokens.length) {
var temp = validatedField.assnTokens.concat(validatedField.propertyName).join('.');
validated[temp] = val;
}
else {
validated[p] = val;
}
}
ret[prop] = validated;
break;
case 'limit':
case 'skip':
if (isNaN(val)) {
throw new Error('"' + prop + '" must be a number.');
}
ret[prop] = Number(val);
break;
default:
ret[prop] = val;
}
}
return ret;
}
// If there's an 'id' property in the top-level of the query
// object, allow non-relational stores to optimize
, _isByIdQuery = function (params) {
// Don't optimize if there is more than one property
if(Object.keys(params).length > 1) {
return null;
}
// Don't optimize if it's a list of ids
if (Array.isArray(params.id)) {
return null;
}
return params.id ? params.id : null;
};
this.initialize = function (model, conditionParams, opts) {
this.model = model;
this.byId = _isByIdQuery(conditionParams);
this.getValidField = _createFieldValidator.apply(this);
this.opts = _parseOpts.apply(this, [opts || {}]);
this.conditions = _createOperation.apply(this, [conditionParams]);
this.rawConditions = conditionParams;
};
})();
query.Query = Query;
module.exports = query;