-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
QueryTools.js
401 lines (384 loc) · 11.3 KB
/
QueryTools.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
401
var equalObjects = require('./equalObjects');
var Id = require('./Id');
var Parse = require('parse/node');
/**
* Query Hashes are deterministic hashes for Parse Queries.
* Any two queries that have the same set of constraints will produce the same
* hash. This lets us reliably group components by the queries they depend upon,
* and quickly determine if a query has changed.
*/
/**
* Convert $or queries into an array of where conditions
*/
function flattenOrQueries(where) {
if (!Object.prototype.hasOwnProperty.call(where, '$or')) {
return where;
}
var accum = [];
for (var i = 0; i < where.$or.length; i++) {
accum = accum.concat(where.$or[i]);
}
return accum;
}
/**
* Deterministically turns an object into a string. Disregards ordering
*/
function stringify(object): string {
if (typeof object !== 'object' || object === null) {
if (typeof object === 'string') {
return '"' + object.replace(/\|/g, '%|') + '"';
}
return object + '';
}
if (Array.isArray(object)) {
var copy = object.map(stringify);
copy.sort();
return '[' + copy.join(',') + ']';
}
var sections = [];
var keys = Object.keys(object);
keys.sort();
for (var k = 0; k < keys.length; k++) {
sections.push(stringify(keys[k]) + ':' + stringify(object[keys[k]]));
}
return '{' + sections.join(',') + '}';
}
/**
* Generate a hash from a query, with unique fields for columns, values, order,
* skip, and limit.
*/
function queryHash(query) {
if (query instanceof Parse.Query) {
query = {
className: query.className,
where: query._where,
};
}
var where = flattenOrQueries(query.where || {});
var columns = [];
var values = [];
var i;
if (Array.isArray(where)) {
var uniqueColumns = {};
for (i = 0; i < where.length; i++) {
var subValues = {};
var keys = Object.keys(where[i]);
keys.sort();
for (var j = 0; j < keys.length; j++) {
subValues[keys[j]] = where[i][keys[j]];
uniqueColumns[keys[j]] = true;
}
values.push(subValues);
}
columns = Object.keys(uniqueColumns);
columns.sort();
} else {
columns = Object.keys(where);
columns.sort();
for (i = 0; i < columns.length; i++) {
values.push(where[columns[i]]);
}
}
var sections = [columns.join(','), stringify(values)];
return query.className + ':' + sections.join('|');
}
/**
* contains -- Determines if an object is contained in a list with special handling for Parse pointers.
*/
function contains(haystack: Array, needle: any): boolean {
if (needle && needle.__type && needle.__type === 'Pointer') {
for (const i in haystack) {
const ptr = haystack[i];
if (typeof ptr === 'string' && ptr === needle.objectId) {
return true;
}
if (ptr.className === needle.className && ptr.objectId === needle.objectId) {
return true;
}
}
return false;
}
if (Array.isArray(needle)) {
for (const need of needle) {
if (contains(haystack, need)) {
return true;
}
}
}
return haystack.indexOf(needle) > -1;
}
/**
* matchesQuery -- Determines if an object would be returned by a Parse Query
* It's a lightweight, where-clause only implementation of a full query engine.
* Since we find queries that match objects, rather than objects that match
* queries, we can avoid building a full-blown query tool.
*/
function matchesQuery(object: any, query: any): boolean {
if (query instanceof Parse.Query) {
var className = object.id instanceof Id ? object.id.className : object.className;
if (className !== query.className) {
return false;
}
return matchesQuery(object, query._where);
}
for (var field in query) {
if (!matchesKeyConstraints(object, field, query[field])) {
return false;
}
}
return true;
}
function equalObjectsGeneric(obj, compareTo, eqlFn) {
if (Array.isArray(obj)) {
for (var i = 0; i < obj.length; i++) {
if (eqlFn(obj[i], compareTo)) {
return true;
}
}
return false;
}
return eqlFn(obj, compareTo);
}
/**
* Determines whether an object matches a single key's constraints
*/
function matchesKeyConstraints(object, key, constraints) {
if (constraints === null) {
return false;
}
if (key.indexOf('.') >= 0) {
// Key references a subobject
var keyComponents = key.split('.');
var subObjectKey = keyComponents[0];
var keyRemainder = keyComponents.slice(1).join('.');
return matchesKeyConstraints(object[subObjectKey] || {}, keyRemainder, constraints);
}
var i;
if (key === '$or') {
for (i = 0; i < constraints.length; i++) {
if (matchesQuery(object, constraints[i])) {
return true;
}
}
return false;
}
if (key === '$and') {
for (i = 0; i < constraints.length; i++) {
if (!matchesQuery(object, constraints[i])) {
return false;
}
}
return true;
}
if (key === '$nor') {
for (i = 0; i < constraints.length; i++) {
if (matchesQuery(object, constraints[i])) {
return false;
}
}
return true;
}
if (key === '$relatedTo') {
// Bail! We can't handle relational queries locally
return false;
}
// Decode Date JSON value
if (object[key] && object[key].__type == 'Date') {
object[key] = new Date(object[key].iso);
}
// Equality (or Array contains) cases
if (typeof constraints !== 'object') {
if (Array.isArray(object[key])) {
return object[key].indexOf(constraints) > -1;
}
return object[key] === constraints;
}
var compareTo;
if (constraints.__type) {
if (constraints.__type === 'Pointer') {
return equalObjectsGeneric(object[key], constraints, function (obj, ptr) {
return (
typeof obj !== 'undefined' &&
ptr.className === obj.className &&
ptr.objectId === obj.objectId
);
});
}
return equalObjectsGeneric(object[key], Parse._decode(key, constraints), equalObjects);
}
// More complex cases
for (var condition in constraints) {
compareTo = constraints[condition];
if (compareTo?.__type) {
compareTo = Parse._decode(key, compareTo);
}
switch (condition) {
case '$lt':
if (object[key] >= compareTo) {
return false;
}
break;
case '$lte':
if (object[key] > compareTo) {
return false;
}
break;
case '$gt':
if (object[key] <= compareTo) {
return false;
}
break;
case '$gte':
if (object[key] < compareTo) {
return false;
}
break;
case '$eq':
if (!equalObjects(object[key], compareTo)) {
return false;
}
break;
case '$ne':
if (equalObjects(object[key], compareTo)) {
return false;
}
break;
case '$in':
if (!contains(compareTo, object[key])) {
return false;
}
break;
case '$nin':
if (contains(compareTo, object[key])) {
return false;
}
break;
case '$all':
if (!object[key]) {
return false;
}
for (i = 0; i < compareTo.length; i++) {
if (object[key].indexOf(compareTo[i]) < 0) {
return false;
}
}
break;
case '$exists': {
const propertyExists = typeof object[key] !== 'undefined';
const existenceIsRequired = constraints['$exists'];
if (typeof constraints['$exists'] !== 'boolean') {
// The SDK will never submit a non-boolean for $exists, but if someone
// tries to submit a non-boolean for $exits outside the SDKs, just ignore it.
break;
}
if ((!propertyExists && existenceIsRequired) || (propertyExists && !existenceIsRequired)) {
return false;
}
break;
}
case '$regex':
if (typeof compareTo === 'object') {
return compareTo.test(object[key]);
}
// JS doesn't support perl-style escaping
var expString = '';
var escapeEnd = -2;
var escapeStart = compareTo.indexOf('\\Q');
while (escapeStart > -1) {
// Add the unescaped portion
expString += compareTo.substring(escapeEnd + 2, escapeStart);
escapeEnd = compareTo.indexOf('\\E', escapeStart);
if (escapeEnd > -1) {
expString += compareTo
.substring(escapeStart + 2, escapeEnd)
.replace(/\\\\\\\\E/g, '\\E')
.replace(/\W/g, '\\$&');
}
escapeStart = compareTo.indexOf('\\Q', escapeEnd);
}
expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
var exp = new RegExp(expString, constraints.$options || '');
if (!exp.test(object[key])) {
return false;
}
break;
case '$nearSphere':
if (!compareTo || !object[key]) {
return false;
}
var distance = compareTo.radiansTo(object[key]);
var max = constraints.$maxDistance || Infinity;
return distance <= max;
case '$within':
if (!compareTo || !object[key]) {
return false;
}
var southWest = compareTo.$box[0];
var northEast = compareTo.$box[1];
if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
// Invalid box, crosses the date line
return false;
}
return (
object[key].latitude > southWest.latitude &&
object[key].latitude < northEast.latitude &&
object[key].longitude > southWest.longitude &&
object[key].longitude < northEast.longitude
);
case '$containedBy': {
for (const value of object[key]) {
if (!contains(compareTo, value)) {
return false;
}
}
return true;
}
case '$geoWithin': {
if (compareTo.$polygon) {
const points = compareTo.$polygon.map(geoPoint => [
geoPoint.latitude,
geoPoint.longitude,
]);
const polygon = new Parse.Polygon(points);
return polygon.containsPoint(object[key]);
}
if (compareTo.$centerSphere) {
const [WGS84Point, maxDistance] = compareTo.$centerSphere;
const centerPoint = new Parse.GeoPoint({
latitude: WGS84Point[1],
longitude: WGS84Point[0],
});
const point = new Parse.GeoPoint(object[key]);
const distance = point.radiansTo(centerPoint);
return distance <= maxDistance;
}
break;
}
case '$geoIntersects': {
const polygon = new Parse.Polygon(object[key].coordinates);
const point = new Parse.GeoPoint(compareTo.$point);
return polygon.containsPoint(point);
}
case '$options':
// Not a query type, but a way to add options to $regex. Ignore and
// avoid the default
break;
case '$maxDistance':
// Not a query type, but a way to add a cap to $nearSphere. Ignore and
// avoid the default
break;
case '$select':
return false;
case '$dontSelect':
return false;
default:
return false;
}
}
return true;
}
var QueryTools = {
queryHash: queryHash,
matchesQuery: matchesQuery,
};
module.exports = QueryTools;