FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
parse-server/src/RestQuery.js at master · mabowgithub/parse-server · GitHub
mabowgithub
/
parse-server
Public
forked from
parse-community/parse-server
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
parse-server
/
src
/
RestQuery.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
532 lines (482 loc) · 15.2 KB
Breadcrumbs
parse-server
/
src
/
RestQuery.js
Copy path
File metadata and controls
532 lines (482 loc) · 15.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// An object that encapsulates everything we need to run a 'find'
// operation, encoded in the REST API format.
var
Parse
=
require
(
'parse/node'
)
.
Parse
;
import
{
default
as
FilesController
}
from
'./Controllers/FilesController'
;
// restOptions can include:
// skip
// limit
// order
// count
// include
// keys
// redirectClassNameForKey
function
RestQuery
(
config
,
auth
,
className
,
restWhere
=
{
}
,
restOptions
=
{
}
)
{
this
.
config
=
config
;
this
.
auth
=
auth
;
this
.
className
=
className
;
this
.
restWhere
=
restWhere
;
this
.
response
=
null
;
this
.
findOptions
=
{
}
;
if
(
!
this
.
auth
.
isMaster
)
{
this
.
findOptions
.
acl
=
this
.
auth
.
user
?
[
this
.
auth
.
user
.
id
]
:
null
;
if
(
this
.
className
==
'_Session'
)
{
if
(
!
this
.
findOptions
.
acl
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_SESSION_TOKEN
,
'This session token is invalid.'
)
;
}
this
.
restWhere
=
{
'$and'
:
[
this
.
restWhere
,
{
'user'
:
{
__type
:
'Pointer'
,
className
:
'_User'
,
objectId
:
this
.
auth
.
user
.
id
}
}
]
}
;
}
}
this
.
doCount
=
false
;
// The format for this.include is not the same as the format for the
// include option - it's the paths we should include, in order,
// stored as arrays, taking into account that we need to include foo
// before including foo.bar. Also it should dedupe.
// For example, passing an arg of include=foo.bar,foo.baz could lead to
// this.include = [['foo'], ['foo', 'baz'], ['foo', 'bar']]
this
.
include
=
[
]
;
for
(
var
option
in
restOptions
)
{
switch
(
option
)
{
case
'keys'
:
this
.
keys
=
new
Set
(
restOptions
.
keys
.
split
(
','
)
)
;
this
.
keys
.
add
(
'objectId'
)
;
this
.
keys
.
add
(
'createdAt'
)
;
this
.
keys
.
add
(
'updatedAt'
)
;
break
;
case
'count'
:
this
.
doCount
=
true
;
break
;
case
'skip'
:
case
'limit'
:
this
.
findOptions
[
option
]
=
restOptions
[
option
]
;
break
;
case
'order'
:
var
fields
=
restOptions
.
order
.
split
(
','
)
;
var
sortMap
=
{
}
;
for
(
var
field
of
fields
)
{
if
(
field
[
0
]
==
'-'
)
{
sortMap
[
field
.
slice
(
1
)
]
=
-
1
;
}
else
{
sortMap
[
field
]
=
1
;
}
}
this
.
findOptions
.
sort
=
sortMap
;
break
;
case
'include'
:
var
paths
=
restOptions
.
include
.
split
(
','
)
;
var
pathSet
=
{
}
;
for
(
var
path
of
paths
)
{
// Add all prefixes with a .-split to pathSet
var
parts
=
path
.
split
(
'.'
)
;
for
(
var
len
=
1
;
len
<=
parts
.
length
;
len
++
)
{
pathSet
[
parts
.
slice
(
0
,
len
)
.
join
(
'.'
)
]
=
true
;
}
}
this
.
include
=
Object
.
keys
(
pathSet
)
.
sort
(
(
a
,
b
)
=>
{
return
a
.
length
-
b
.
length
;
}
)
.
map
(
(
s
)
=>
{
return
s
.
split
(
'.'
)
;
}
)
;
break
;
case
'redirectClassNameForKey'
:
this
.
redirectKey
=
restOptions
.
redirectClassNameForKey
;
this
.
redirectClassName
=
null
;
break
;
default
:
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_JSON
,
'bad option: '
+
option
)
;
}
}
}
// A convenient method to perform all the steps of processing a query
// in order.
// Returns a promise for the response - an object with optional keys
// 'results' and 'count'.
// TODO: consolidate the replaceX functions
RestQuery
.
prototype
.
execute
=
function
(
)
{
return
Promise
.
resolve
(
)
.
then
(
(
)
=>
{
return
this
.
getUserAndRoleACL
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
redirectClassNameForKey
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
replaceSelect
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
replaceDontSelect
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
replaceInQuery
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
replaceNotInQuery
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
runFind
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
runCount
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
handleInclude
(
)
;
}
)
.
then
(
(
)
=>
{
return
this
.
response
;
}
)
;
}
;
// Uses the Auth object to get the list of roles, adds the user id
RestQuery
.
prototype
.
getUserAndRoleACL
=
function
(
)
{
if
(
this
.
auth
.
isMaster
||
!
this
.
auth
.
user
)
{
return
Promise
.
resolve
(
)
;
}
return
this
.
auth
.
getUserRoles
(
)
.
then
(
(
roles
)
=>
{
roles
.
push
(
this
.
auth
.
user
.
id
)
;
this
.
findOptions
.
acl
=
roles
;
return
Promise
.
resolve
(
)
;
}
)
;
}
;
// Changes the className if redirectClassNameForKey is set.
// Returns a promise.
RestQuery
.
prototype
.
redirectClassNameForKey
=
function
(
)
{
if
(
!
this
.
redirectKey
)
{
return
Promise
.
resolve
(
)
;
}
// We need to change the class name based on the schema
return
this
.
config
.
database
.
redirectClassNameForKey
(
this
.
className
,
this
.
redirectKey
)
.
then
(
(
newClassName
)
=>
{
this
.
className
=
newClassName
;
this
.
redirectClassName
=
newClassName
;
}
)
;
}
;
// Replaces a $inQuery clause by running the subquery, if there is an
// $inQuery clause.
// The $inQuery clause turns into an $in with values that are just
// pointers to the objects returned in the subquery.
RestQuery
.
prototype
.
replaceInQuery
=
function
(
)
{
var
inQueryObject
=
findObjectWithKey
(
this
.
restWhere
,
'$inQuery'
)
;
if
(
!
inQueryObject
)
{
return
;
}
// The inQuery value must have precisely two keys - where and className
var
inQueryValue
=
inQueryObject
[
'$inQuery'
]
;
if
(
!
inQueryValue
.
where
||
!
inQueryValue
.
className
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_QUERY
,
'improper usage of $inQuery'
)
;
}
var
subquery
=
new
RestQuery
(
this
.
config
,
this
.
auth
,
inQueryValue
.
className
,
inQueryValue
.
where
)
;
return
subquery
.
execute
(
)
.
then
(
(
response
)
=>
{
var
values
=
[
]
;
for
(
var
result
of
response
.
results
)
{
values
.
push
(
{
__type
:
'Pointer'
,
className
:
inQueryValue
.
className
,
objectId
:
result
.
objectId
}
)
;
}
delete
inQueryObject
[
'$inQuery'
]
;
inQueryObject
[
'$in'
]
=
values
;
// Recurse to repeat
return
this
.
replaceInQuery
(
)
;
}
)
;
}
;
// Replaces a $notInQuery clause by running the subquery, if there is an
// $notInQuery clause.
// The $notInQuery clause turns into a $nin with values that are just
// pointers to the objects returned in the subquery.
RestQuery
.
prototype
.
replaceNotInQuery
=
function
(
)
{
var
notInQueryObject
=
findObjectWithKey
(
this
.
restWhere
,
'$notInQuery'
)
;
if
(
!
notInQueryObject
)
{
return
;
}
// The notInQuery value must have precisely two keys - where and className
var
notInQueryValue
=
notInQueryObject
[
'$notInQuery'
]
;
if
(
!
notInQueryValue
.
where
||
!
notInQueryValue
.
className
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_QUERY
,
'improper usage of $notInQuery'
)
;
}
var
subquery
=
new
RestQuery
(
this
.
config
,
this
.
auth
,
notInQueryValue
.
className
,
notInQueryValue
.
where
)
;
return
subquery
.
execute
(
)
.
then
(
(
response
)
=>
{
var
values
=
[
]
;
for
(
var
result
of
response
.
results
)
{
values
.
push
(
{
__type
:
'Pointer'
,
className
:
notInQueryValue
.
className
,
objectId
:
result
.
objectId
}
)
;
}
delete
notInQueryObject
[
'$notInQuery'
]
;
notInQueryObject
[
'$nin'
]
=
values
;
// Recurse to repeat
return
this
.
replaceNotInQuery
(
)
;
}
)
;
}
;
// Replaces a $select clause by running the subquery, if there is a
// $select clause.
// The $select clause turns into an $in with values selected out of
// the subquery.
// Returns a possible-promise.
RestQuery
.
prototype
.
replaceSelect
=
function
(
)
{
var
selectObject
=
findObjectWithKey
(
this
.
restWhere
,
'$select'
)
;
if
(
!
selectObject
)
{
return
;
}
// The select value must have precisely two keys - query and key
var
selectValue
=
selectObject
[
'$select'
]
;
if
(
!
selectValue
.
query
||
!
selectValue
.
key
||
typeof
selectValue
.
query
!==
'object'
||
!
selectValue
.
query
.
className
||
!
selectValue
.
query
.
where
||
Object
.
keys
(
selectValue
)
.
length
!==
2
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_QUERY
,
'improper usage of $select'
)
;
}
var
subquery
=
new
RestQuery
(
this
.
config
,
this
.
auth
,
selectValue
.
query
.
className
,
selectValue
.
query
.
where
)
;
return
subquery
.
execute
(
)
.
then
(
(
response
)
=>
{
var
values
=
[
]
;
for
(
var
result
of
response
.
results
)
{
values
.
push
(
result
[
selectValue
.
key
]
)
;
}
delete
selectObject
[
'$select'
]
;
selectObject
[
'$in'
]
=
values
;
// Keep replacing $select clauses
return
this
.
replaceSelect
(
)
;
}
)
}
;
// Replaces a $dontSelect clause by running the subquery, if there is a
// $dontSelect clause.
// The $dontSelect clause turns into an $nin with values selected out of
// the subquery.
// Returns a possible-promise.
RestQuery
.
prototype
.
replaceDontSelect
=
function
(
)
{
var
dontSelectObject
=
findObjectWithKey
(
this
.
restWhere
,
'$dontSelect'
)
;
if
(
!
dontSelectObject
)
{
return
;
}
// The dontSelect value must have precisely two keys - query and key
var
dontSelectValue
=
dontSelectObject
[
'$dontSelect'
]
;
if
(
!
dontSelectValue
.
query
||
!
dontSelectValue
.
key
||
typeof
dontSelectValue
.
query
!==
'object'
||
!
dontSelectValue
.
query
.
className
||
!
dontSelectValue
.
query
.
where
||
Object
.
keys
(
dontSelectValue
)
.
length
!==
2
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_QUERY
,
'improper usage of $dontSelect'
)
;
}
var
subquery
=
new
RestQuery
(
this
.
config
,
this
.
auth
,
dontSelectValue
.
query
.
className
,
dontSelectValue
.
query
.
where
)
;
return
subquery
.
execute
(
)
.
then
(
(
response
)
=>
{
var
values
=
[
]
;
for
(
var
result
of
response
.
results
)
{
values
.
push
(
result
[
dontSelectValue
.
key
]
)
;
}
delete
dontSelectObject
[
'$dontSelect'
]
;
dontSelectObject
[
'$nin'
]
=
values
;
// Keep replacing $dontSelect clauses
return
this
.
replaceDontSelect
(
)
;
}
)
}
;
// Returns a promise for whether it was successful.
// Populates this.response with an object that only has 'results'.
RestQuery
.
prototype
.
runFind
=
function
(
)
{
return
this
.
config
.
database
.
find
(
this
.
className
,
this
.
restWhere
,
this
.
findOptions
)
.
then
(
(
results
)
=>
{
if
(
this
.
className
==
'_User'
)
{
for
(
var
result
of
results
)
{
delete
result
.
password
;
}
}
this
.
config
.
filesController
.
expandFilesInObject
(
this
.
config
,
results
)
;
if
(
this
.
keys
)
{
var
keySet
=
this
.
keys
;
results
=
results
.
map
(
(
object
)
=>
{
var
newObject
=
{
}
;
for
(
var
key
in
object
)
{
if
(
keySet
.
has
(
key
)
)
{
newObject
[
key
]
=
object
[
key
]
;
}
}
return
newObject
;
}
)
;
}
if
(
this
.
redirectClassName
)
{
for
(
var
r
of
results
)
{
r
.
className
=
this
.
redirectClassName
;
}
}
this
.
response
=
{
results
:
results
}
;
}
)
;
}
;
// Returns a promise for whether it was successful.
// Populates this.response.count with the count
RestQuery
.
prototype
.
runCount
=
function
(
)
{
if
(
!
this
.
doCount
)
{
return
;
}
this
.
findOptions
.
count
=
true
;
delete
this
.
findOptions
.
skip
;
return
this
.
config
.
database
.
find
(
this
.
className
,
this
.
restWhere
,
this
.
findOptions
)
.
then
(
(
c
)
=>
{
this
.
response
.
count
=
c
;
}
)
;
}
;
// Augments this.response with data at the paths provided in this.include.
RestQuery
.
prototype
.
handleInclude
=
function
(
)
{
if
(
this
.
include
.
length
==
0
)
{
return
;
}
var
pathResponse
=
includePath
(
this
.
config
,
this
.
auth
,
this
.
response
,
this
.
include
[
0
]
)
;
if
(
pathResponse
.
then
)
{
return
pathResponse
.
then
(
(
newResponse
)
=>
{
this
.
response
=
newResponse
;
this
.
include
=
this
.
include
.
slice
(
1
)
;
return
this
.
handleInclude
(
)
;
}
)
;
}
return
pathResponse
;
}
;
// Adds included values to the response.
// Path is a list of field names.
// Returns a promise for an augmented response.
function
includePath
(
config
,
auth
,
response
,
path
)
{
var
pointers
=
findPointers
(
response
.
results
,
path
)
;
if
(
pointers
.
length
==
0
)
{
return
response
;
}
var
className
=
null
;
var
objectIds
=
{
}
;
for
(
var
pointer
of
pointers
)
{
if
(
className
===
null
)
{
className
=
pointer
.
className
;
}
else
{
if
(
className
!=
pointer
.
className
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_JSON
,
'inconsistent type data for include'
)
;
}
}
objectIds
[
pointer
.
objectId
]
=
true
;
}
if
(
!
className
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_JSON
,
'bad pointers'
)
;
}
// Get the objects for all these object ids
var
where
=
{
'objectId'
:
{
'$in'
:
Object
.
keys
(
objectIds
)
}
}
;
var
query
=
new
RestQuery
(
config
,
auth
,
className
,
where
)
;
return
query
.
execute
(
)
.
then
(
(
includeResponse
)
=>
{
var
replace
=
{
}
;
for
(
var
obj
of
includeResponse
.
results
)
{
obj
.
__type
=
'Object'
;
obj
.
className
=
className
;
if
(
className
==
"_User"
)
{
delete
obj
.
sessionToken
;
}
replace
[
obj
.
objectId
]
=
obj
;
}
var
resp
=
{
results
:
replacePointers
(
response
.
results
,
path
,
replace
)
}
;
if
(
response
.
count
)
{
resp
.
count
=
response
.
count
;
}
return
resp
;
}
)
;
}
// Object may be a list of REST-format object to find pointers in, or
// it may be a single object.
// If the path yields things that aren't pointers, this throws an error.
// Path is a list of fields to search into.
// Returns a list of pointers in REST format.
function
findPointers
(
object
,
path
)
{
if
(
object
instanceof
Array
)
{
var
answer
=
[
]
;
for
(
var
x
of
object
)
{
answer
=
answer
.
concat
(
findPointers
(
x
,
path
)
)
;
}
return
answer
;
}
if
(
typeof
object
!==
'object'
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_QUERY
,
'can only include pointer fields'
)
;
}
if
(
path
.
length
==
0
)
{
if
(
object
.
__type
==
'Pointer'
)
{
return
[
object
]
;
}
throw
new
Parse
.
Error
(
Parse
.
Error
.
INVALID_QUERY
,
'can only include pointer fields'
)
;
}
var
subobject
=
object
[
path
[
0
]
]
;
if
(
!
subobject
)
{
return
[
]
;
}
return
findPointers
(
subobject
,
path
.
slice
(
1
)
)
;
}
// Object may be a list of REST-format objects to replace pointers
// in, or it may be a single object.
// Path is a list of fields to search into.
// replace is a map from object id -> object.
// Returns something analogous to object, but with the appropriate
// pointers inflated.
function
replacePointers
(
object
,
path
,
replace
)
{
if
(
object
instanceof
Array
)
{
return
object
.
map
(
(
obj
)
=>
replacePointers
(
obj
,
path
,
replace
)
)
;
}
if
(
typeof
object
!==
'object'
)
{
return
object
;
}
if
(
path
.
length
==
0
)
{
if
(
object
.
__type
==
'Pointer'
&&
replace
[
object
.
objectId
]
)
{
return
replace
[
object
.
objectId
]
;
}
return
object
;
}
var
subobject
=
object
[
path
[
0
]
]
;
if
(
!
subobject
)
{
return
object
;
}
var
newsub
=
replacePointers
(
subobject
,
path
.
slice
(
1
)
,
replace
)
;
var
answer
=
{
}
;
for
(
var
key
in
object
)
{
if
(
key
==
path
[
0
]
)
{
answer
[
key
]
=
newsub
;
}
else
{
answer
[
key
]
=
object
[
key
]
;
}
}
return
answer
;
}
// Finds a subobject that has the given key, if there is one.
// Returns undefined otherwise.
function
findObjectWithKey
(
root
,
key
)
{
if
(
typeof
root
!==
'object'
)
{
return
;
}
if
(
root
instanceof
Array
)
{
for
(
var
item
of
root
)
{
var
answer
=
findObjectWithKey
(
item
,
key
)
;
if
(
answer
)
{
return
answer
;
}
}
}
if
(
root
&&
root
[
key
]
)
{
return
root
;
}
for
(
var
subkey
in
root
)
{
var
answer
=
findObjectWithKey
(
root
[
subkey
]
,
key
)
;
if
(
answer
)
{
return
answer
;
}
}
}
module
.
exports
=
RestQuery
;
Back
|
FazBrowse Home
|
New Git URL