@@ -28,16 +28,6 @@ DatabaseController.prototype.connect = function() {
28
28
return this . adapter . connect ( ) ;
29
29
} ;
30
30
31
- // Returns a promise for a Mongo collection.
32
- // Generally just for internal use.
33
- DatabaseController . prototype . collection = function ( className ) {
34
- if ( ! Schema . classNameIsValid ( className ) ) {
35
- throw new Parse . Error ( Parse . Error . INVALID_CLASS_NAME ,
36
- 'invalid className: ' + className ) ;
37
- }
38
- return this . adapter . collection ( this . collectionPrefix + className ) ;
39
- } ;
40
-
41
31
DatabaseController . prototype . adaptiveCollection = function ( className ) {
42
32
return this . adapter . adaptiveCollection ( this . collectionPrefix + className ) ;
43
33
} ;
@@ -54,15 +44,23 @@ function returnsTrue() {
54
44
return true ;
55
45
}
56
46
47
+ DatabaseController . prototype . validateClassName = function ( className ) {
48
+ if ( ! Schema . classNameIsValid ( className ) ) {
49
+ const error = new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , 'invalid className: ' + className ) ;
50
+ return Promise . reject ( error ) ;
51
+ }
52
+ return Promise . resolve ( ) ;
53
+ } ;
54
+
57
55
// Returns a promise for a schema object.
58
56
// If we are provided a acceptor, then we run it on the schema.
59
57
// If the schema isn't accepted, we reload it at most once.
60
58
DatabaseController . prototype . loadSchema = function ( acceptor = returnsTrue ) {
61
59
62
60
if ( ! this . schemaPromise ) {
63
- this . schemaPromise = this . collection ( '_SCHEMA' ) . then ( ( coll ) => {
61
+ this . schemaPromise = this . adaptiveCollection ( '_SCHEMA' ) . then ( collection => {
64
62
delete this . schemaPromise ;
65
- return Schema . load ( coll ) ;
63
+ return Schema . load ( collection ) ;
66
64
} ) ;
67
65
return this . schemaPromise ;
68
66
}
@@ -71,9 +69,9 @@ DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
71
69
if ( acceptor ( schema ) ) {
72
70
return schema ;
73
71
}
74
- this . schemaPromise = this . collection ( '_SCHEMA' ) . then ( ( coll ) => {
72
+ this . schemaPromise = this . adaptiveCollection ( '_SCHEMA' ) . then ( collection => {
75
73
delete this . schemaPromise ;
76
- return Schema . load ( coll ) ;
74
+ return Schema . load ( collection ) ;
77
75
} ) ;
78
76
return this . schemaPromise ;
79
77
} ) ;
@@ -230,30 +228,28 @@ DatabaseController.prototype.handleRelationUpdates = function(className,
230
228
231
229
// Adds a relation.
232
230
// Returns a promise that resolves successfully iff the add was successful.
233
- DatabaseController . prototype . addRelation = function ( key , fromClassName ,
234
- fromId , toId ) {
235
- var doc = {
231
+ DatabaseController . prototype . addRelation = function ( key , fromClassName , fromId , toId ) {
232
+ let doc = {
236
233
relatedId : toId ,
237
- owningId : fromId
234
+ owningId : fromId
238
235
} ;
239
- var className = ' _Join:' + key + ':' + fromClassName ;
240
- return this . collection ( className ) . then ( ( coll ) => {
241
- return coll . update ( doc , doc , { upsert : true } ) ;
236
+ let className = ` _Join:${ key } : ${ fromClassName } ` ;
237
+ return this . adaptiveCollection ( className ) . then ( ( coll ) => {
238
+ return coll . upsertOne ( doc , doc ) ;
242
239
} ) ;
243
240
} ;
244
241
245
242
// Removes a relation.
246
243
// Returns a promise that resolves successfully iff the remove was
247
244
// successful.
248
- DatabaseController . prototype . removeRelation = function ( key , fromClassName ,
249
- fromId , toId ) {
245
+ DatabaseController . prototype . removeRelation = function ( key , fromClassName , fromId , toId ) {
250
246
var doc = {
251
247
relatedId : toId ,
252
248
owningId : fromId
253
249
} ;
254
- var className = ' _Join:' + key + ':' + fromClassName ;
255
- return this . collection ( className ) . then ( ( coll ) => {
256
- return coll . remove ( doc ) ;
250
+ let className = ` _Join:${ key } : ${ fromClassName } ` ;
251
+ return this . adaptiveCollection ( className ) . then ( coll => {
252
+ return coll . deleteOne ( doc ) ;
257
253
} ) ;
258
254
} ;
259
255
@@ -269,40 +265,36 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
269
265
var aclGroup = options . acl || [ ] ;
270
266
271
267
var schema ;
272
- return this . loadSchema ( ) . then ( ( s ) => {
273
- schema = s ;
274
- if ( ! isMaster ) {
275
- return schema . validatePermission ( className , aclGroup , 'delete' ) ;
276
- }
277
- return Promise . resolve ( ) ;
278
- } ) . then ( ( ) => {
279
-
280
- return this . collection ( className ) ;
281
- } ) . then ( ( coll ) => {
282
- var mongoWhere = transform . transformWhere ( schema , className , query ) ;
283
-
284
- if ( options . acl ) {
285
- var writePerms = [
286
- { _wperm : { '$exists' : false } }
287
- ] ;
288
- for ( var entry of options . acl ) {
289
- writePerms . push ( { _wperm : { '$in' : [ entry ] } } ) ;
268
+ return this . loadSchema ( )
269
+ . then ( s => {
270
+ schema = s ;
271
+ if ( ! isMaster ) {
272
+ return schema . validatePermission ( className , aclGroup , 'delete' ) ;
290
273
}
291
- mongoWhere = { '$and' : [ mongoWhere , { '$or' : writePerms } ] } ;
292
- }
293
-
294
- return coll . remove ( mongoWhere ) ;
295
- } ) . then ( ( resp ) => {
296
- //Check _Session to avoid changing password failed without any session.
297
- if ( resp . result . n === 0 && className !== "_Session" ) {
298
- return Promise . reject (
299
- new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,
300
- 'Object not found.' ) ) ;
274
+ return Promise . resolve ( ) ;
275
+ } )
276
+ . then ( ( ) => this . adaptiveCollection ( className ) )
277
+ . then ( collection => {
278
+ let mongoWhere = transform . transformWhere ( schema , className , query ) ;
301
279
302
- }
303
- } , ( error ) => {
304
- throw error ;
305
- } ) ;
280
+ if ( options . acl ) {
281
+ var writePerms = [
282
+ { _wperm : { '$exists' : false } }
283
+ ] ;
284
+ for ( var entry of options . acl ) {
285
+ writePerms . push ( { _wperm : { '$in' : [ entry ] } } ) ;
286
+ }
287
+ mongoWhere = { '$and' : [ mongoWhere , { '$or' : writePerms } ] } ;
288
+ }
289
+ return collection . deleteMany ( mongoWhere ) ;
290
+ } )
291
+ . then ( resp => {
292
+ //Check _Session to avoid changing password failed without any session.
293
+ // TODO: @nlutsenko Stop relying on `result.n`
294
+ if ( resp . result . n === 0 && className !== "_Session" ) {
295
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
296
+ }
297
+ } ) ;
306
298
} ;
307
299
308
300
// Inserts an object into the database.
@@ -312,21 +304,21 @@ DatabaseController.prototype.create = function(className, object, options) {
312
304
var isMaster = ! ( 'acl' in options ) ;
313
305
var aclGroup = options . acl || [ ] ;
314
306
315
- return this . loadSchema ( ) . then ( ( s ) => {
316
- schema = s ;
317
- if ( ! isMaster ) {
318
- return schema . validatePermission ( className , aclGroup , 'create' ) ;
319
- }
320
- return Promise . resolve ( ) ;
321
- } ) . then ( ( ) => {
322
-
323
- return this . handleRelationUpdates ( className , null , object ) ;
324
- } ) . then ( ( ) => {
325
- return this . collection ( className ) ;
326
- } ) . then ( ( coll ) => {
327
- var mongoObject = transform . transformCreate ( schema , className , object ) ;
328
- return coll . insert ( [ mongoObject ] ) ;
329
- } ) ;
307
+ return this . validateClassName ( className )
308
+ . then ( ( ) => this . loadSchema ( ) )
309
+ . then ( s => {
310
+ schema = s ;
311
+ if ( ! isMaster ) {
312
+ return schema . validatePermission ( className , aclGroup , 'create' ) ;
313
+ }
314
+ return Promise . resolve ( ) ;
315
+ } )
316
+ . then ( ( ) => this . handleRelationUpdates ( className , null , object ) )
317
+ . then ( ( ) => this . adaptiveCollection ( className ) )
318
+ . then ( coll => {
319
+ var mongoObject = transform . transformCreate ( schema , className , object ) ;
320
+ return coll . insertOne ( mongoObject ) ;
321
+ } ) ;
330
322
} ;
331
323
332
324
// Runs a mongo query on the database.
@@ -386,14 +378,14 @@ DatabaseController.prototype.owningIds = function(className, key, relatedIds) {
386
378
// equal-to-pointer constraints on relation fields.
387
379
// Returns a promise that resolves when query is mutated
388
380
DatabaseController . prototype . reduceInRelation = function ( className , query , schema ) {
389
-
381
+
390
382
// Search for an in-relation or equal-to-relation
391
383
// Make it sequential for now, not sure of paralleization side effects
392
384
if ( query [ '$or' ] ) {
393
385
let ors = query [ '$or' ] ;
394
386
return Promise . all ( ors . map ( ( aQuery , index ) => {
395
387
return this . reduceInRelation ( className , aQuery , schema ) . then ( ( aQuery ) => {
396
- query [ '$or' ] [ index ] = aQuery ;
388
+ query [ '$or' ] [ index ] = aQuery ;
397
389
} )
398
390
} ) ) ;
399
391
}
@@ -413,14 +405,14 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
413
405
relatedIds = [ query [ key ] . objectId ] ;
414
406
}
415
407
return this . owningIds ( className , key , relatedIds ) . then ( ( ids ) => {
416
- delete query [ key ] ;
408
+ delete query [ key ] ;
417
409
this . addInObjectIdsIds ( ids , query ) ;
418
410
return Promise . resolve ( query ) ;
419
411
} ) ;
420
412
}
421
413
return Promise . resolve ( query ) ;
422
414
} )
423
-
415
+
424
416
return Promise . all ( promises ) . then ( ( ) => {
425
417
return Promise . resolve ( query ) ;
426
418
} )
@@ -429,13 +421,13 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
429
421
// Modifies query so that it no longer has $relatedTo
430
422
// Returns a promise that resolves when query is mutated
431
423
DatabaseController . prototype . reduceRelationKeys = function ( className , query ) {
432
-
424
+
433
425
if ( query [ '$or' ] ) {
434
426
return Promise . all ( query [ '$or' ] . map ( ( aQuery ) => {
435
427
return this . reduceRelationKeys ( className , aQuery ) ;
436
428
} ) ) ;
437
429
}
438
-
430
+
439
431
var relatedTo = query [ '$relatedTo' ] ;
440
432
if ( relatedTo ) {
441
433
return this . relatedIds (
0 commit comments