@@ -54,6 +54,14 @@ function returnsTrue() {
54
54
return true ;
55
55
}
56
56
57
+ DatabaseController . prototype . validateClassName = function ( className ) {
58
+ if ( ! Schema . classNameIsValid ( className ) ) {
59
+ const error = new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , 'invalid className: ' + className ) ;
60
+ return Promise . reject ( error ) ;
61
+ }
62
+ return Promise . resolve ( ) ;
63
+ } ;
64
+
57
65
// Returns a promise for a schema object.
58
66
// If we are provided a acceptor, then we run it on the schema.
59
67
// If the schema isn't accepted, we reload it at most once.
@@ -230,30 +238,28 @@ DatabaseController.prototype.handleRelationUpdates = function(className,
230
238
231
239
// Adds a relation.
232
240
// Returns a promise that resolves successfully iff the add was successful.
233
- DatabaseController . prototype . addRelation = function ( key , fromClassName ,
234
- fromId , toId ) {
235
- var doc = {
241
+ DatabaseController . prototype . addRelation = function ( key , fromClassName , fromId , toId ) {
242
+ let doc = {
236
243
relatedId : toId ,
237
- owningId : fromId
244
+ owningId : fromId
238
245
} ;
239
- var className = ' _Join:' + key + ':' + fromClassName ;
240
- return this . collection ( className ) . then ( ( coll ) => {
241
- return coll . update ( doc , doc , { upsert : true } ) ;
246
+ let className = ` _Join:${ key } : ${ fromClassName } ` ;
247
+ return this . adaptiveCollection ( className ) . then ( ( coll ) => {
248
+ return coll . upsertOne ( doc , doc ) ;
242
249
} ) ;
243
250
} ;
244
251
245
252
// Removes a relation.
246
253
// Returns a promise that resolves successfully iff the remove was
247
254
// successful.
248
- DatabaseController . prototype . removeRelation = function ( key , fromClassName ,
249
- fromId , toId ) {
255
+ DatabaseController . prototype . removeRelation = function ( key , fromClassName , fromId , toId ) {
250
256
var doc = {
251
257
relatedId : toId ,
252
258
owningId : fromId
253
259
} ;
254
- var className = ' _Join:' + key + ':' + fromClassName ;
255
- return this . collection ( className ) . then ( ( coll ) => {
256
- return coll . remove ( doc ) ;
260
+ let className = ` _Join:${ key } : ${ fromClassName } ` ;
261
+ return this . adaptiveCollection ( className ) . then ( coll => {
262
+ return coll . deleteOne ( doc ) ;
257
263
} ) ;
258
264
} ;
259
265
@@ -269,40 +275,36 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
269
275
var aclGroup = options . acl || [ ] ;
270
276
271
277
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 ] } } ) ;
278
+ return this . loadSchema ( )
279
+ . then ( s => {
280
+ schema = s ;
281
+ if ( ! isMaster ) {
282
+ return schema . validatePermission ( className , aclGroup , 'delete' ) ;
290
283
}
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.' ) ) ;
284
+ return Promise . resolve ( ) ;
285
+ } )
286
+ . then ( ( ) => this . adaptiveCollection ( className ) )
287
+ . then ( collection => {
288
+ let mongoWhere = transform . transformWhere ( schema , className , query ) ;
301
289
302
- }
303
- } , ( error ) => {
304
- throw error ;
305
- } ) ;
290
+ if ( options . acl ) {
291
+ var writePerms = [
292
+ { _wperm : { '$exists' : false } }
293
+ ] ;
294
+ for ( var entry of options . acl ) {
295
+ writePerms . push ( { _wperm : { '$in' : [ entry ] } } ) ;
296
+ }
297
+ mongoWhere = { '$and' : [ mongoWhere , { '$or' : writePerms } ] } ;
298
+ }
299
+ return collection . deleteMany ( mongoWhere ) ;
300
+ } )
301
+ . then ( resp => {
302
+ //Check _Session to avoid changing password failed without any session.
303
+ // TODO: @nlutsenko Stop relying on `result.n`
304
+ if ( resp . result . n === 0 && className !== "_Session" ) {
305
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
306
+ }
307
+ } ) ;
306
308
} ;
307
309
308
310
// Inserts an object into the database.
@@ -312,21 +314,21 @@ DatabaseController.prototype.create = function(className, object, options) {
312
314
var isMaster = ! ( 'acl' in options ) ;
313
315
var aclGroup = options . acl || [ ] ;
314
316
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
- } ) ;
317
+ return this . validateClassName ( className )
318
+ . then ( ( ) => this . loadSchema ( ) )
319
+ . then ( s => {
320
+ schema = s ;
321
+ if ( ! isMaster ) {
322
+ return schema . validatePermission ( className , aclGroup , 'create' ) ;
323
+ }
324
+ return Promise . resolve ( ) ;
325
+ } )
326
+ . then ( ( ) => this . handleRelationUpdates ( className , null , object ) )
327
+ . then ( ( ) => this . adaptiveCollection ( className ) )
328
+ . then ( coll => {
329
+ var mongoObject = transform . transformCreate ( schema , className , object ) ;
330
+ return coll . insertOne ( mongoObject ) ;
331
+ } ) ;
330
332
} ;
331
333
332
334
// Runs a mongo query on the database.
@@ -386,14 +388,14 @@ DatabaseController.prototype.owningIds = function(className, key, relatedIds) {
386
388
// equal-to-pointer constraints on relation fields.
387
389
// Returns a promise that resolves when query is mutated
388
390
DatabaseController . prototype . reduceInRelation = function ( className , query , schema ) {
389
-
391
+
390
392
// Search for an in-relation or equal-to-relation
391
393
// Make it sequential for now, not sure of paralleization side effects
392
394
if ( query [ '$or' ] ) {
393
395
let ors = query [ '$or' ] ;
394
396
return Promise . all ( ors . map ( ( aQuery , index ) => {
395
397
return this . reduceInRelation ( className , aQuery , schema ) . then ( ( aQuery ) => {
396
- query [ '$or' ] [ index ] = aQuery ;
398
+ query [ '$or' ] [ index ] = aQuery ;
397
399
} )
398
400
} ) ) ;
399
401
}
@@ -413,14 +415,14 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
413
415
relatedIds = [ query [ key ] . objectId ] ;
414
416
}
415
417
return this . owningIds ( className , key , relatedIds ) . then ( ( ids ) => {
416
- delete query [ key ] ;
418
+ delete query [ key ] ;
417
419
this . addInObjectIdsIds ( ids , query ) ;
418
420
return Promise . resolve ( query ) ;
419
421
} ) ;
420
422
}
421
423
return Promise . resolve ( query ) ;
422
424
} )
423
-
425
+
424
426
return Promise . all ( promises ) . then ( ( ) => {
425
427
return Promise . resolve ( query ) ;
426
428
} )
@@ -429,13 +431,13 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
429
431
// Modifies query so that it no longer has $relatedTo
430
432
// Returns a promise that resolves when query is mutated
431
433
DatabaseController . prototype . reduceRelationKeys = function ( className , query ) {
432
-
434
+
433
435
if ( query [ '$or' ] ) {
434
436
return Promise . all ( query [ '$or' ] . map ( ( aQuery ) => {
435
437
return this . reduceRelationKeys ( className , aQuery ) ;
436
438
} ) ) ;
437
439
}
438
-
440
+
439
441
var relatedTo = query [ '$relatedTo' ] ;
440
442
if ( relatedTo ) {
441
443
return this . relatedIds (
0 commit comments