@@ -156,7 +156,7 @@ DatabaseController.prototype.update = function(className, query, update, {
156
156
. then ( ( ) => this . adapter . adaptiveCollection ( className ) )
157
157
. then ( collection => {
158
158
if ( ! isMaster ) {
159
- query = this . addPointerPermissions ( schema , className , 'update' , query , aclGroup ) ;
159
+ query = this . addPointerPermissions ( schema , className , 'update' , query , aclGroup ) ;
160
160
}
161
161
if ( ! query ) {
162
162
return Promise . resolve ( ) ;
@@ -284,40 +284,56 @@ DatabaseController.prototype.removeRelation = function(key, fromClassName, fromI
284
284
// acl: a list of strings. If the object to be updated has an ACL,
285
285
// one of the provided strings must provide the caller with
286
286
// write permissions.
287
- DatabaseController . prototype . destroy = function ( className , query , { acl } = { } ) {
288
- var isMaster = acl !== undefined ;
289
- var aclGroup = acl || [ ] ;
287
+ DatabaseController . prototype . destroy = function ( className , { objectId , ... query } , { acl } = { } ) {
288
+ const isMaster = acl !== undefined ;
289
+ const aclGroup = acl || [ ] ;
290
290
291
- var schema ;
292
291
return this . loadSchema ( )
293
- . then ( s => {
294
- schema = s ;
295
- if ( ! isMaster ) {
296
- return schema . validatePermission ( className , aclGroup , 'delete' ) ;
297
- }
298
- return Promise . resolve ( ) ;
299
- } )
300
- . then ( ( ) => this . adapter . adaptiveCollection ( className ) )
301
- . then ( collection => {
302
- if ( ! isMaster ) {
303
- query = this . addPointerPermissions ( schema , className , 'delete' , query , aclGroup ) ;
304
- if ( ! query ) {
305
- throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
292
+ . then ( schemaController => {
293
+ return ( isMaster ? Promise . resolve ( ) : schemaController . validatePermission ( className , aclGroup , 'delete' ) )
294
+ . then ( ( ) => {
295
+ if ( query !== { } || ! objectId ) {
296
+ if ( objectId ) {
297
+ query . objectId = objectId ;
306
298
}
307
- }
308
- let mongoWhere = this . transform . transformWhere ( schema , className , query , { validate : ! this . skipValidation } ) ;
309
- if ( acl ) {
310
- mongoWhere = this . transform . addWriteACL ( mongoWhere , acl ) ;
311
- }
312
- return collection . deleteMany ( mongoWhere ) ;
313
- } )
314
- . then ( resp => {
315
- //Check _Session to avoid changing password failed without any session.
316
- // TODO: @nlutsenko Stop relying on `result.n`
317
- if ( resp . result . n === 0 && className !== "_Session" ) {
318
- throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
299
+
300
+ // delete by query
301
+ return this . adapter . adaptiveCollection ( className )
302
+ . then ( collection => {
303
+ if ( ! isMaster ) {
304
+ query = this . addPointerPermissions ( schema , className , 'delete' , query , aclGroup ) ;
305
+ if ( ! query ) {
306
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
307
+ }
308
+ }
309
+ let mongoWhere = this . transform . transformWhere ( schemaController , className , query , { validate : ! this . skipValidation } ) ;
310
+ if ( acl ) {
311
+ mongoWhere = this . transform . addWriteACL ( mongoWhere , acl ) ;
312
+ }
313
+ return collection . deleteMany ( mongoWhere )
314
+ . then ( resp => {
315
+ //Check _Session to avoid changing password failed without any session.
316
+ // TODO: @nlutsenko Stop relying on `result.n`
317
+ if ( resp . result . n === 0 && className !== "_Session" ) {
318
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
319
+ }
320
+ } ) ;
321
+ } ) ;
322
+ } else {
323
+ // delete by objectId
324
+ return this . adapter . deleteObject ( className , objectId )
325
+ . catch ( error => {
326
+ if ( className === '_Session' && error . code === Parse . Error . OBJECT_NOT_FOUND ) {
327
+ // When deleting sessions while changing passwords, don't throw an error.
328
+ // I suspect this isn't necessary, as sessions should only be deleted by query
329
+ // when changing password, but we'll see.
330
+ return Promise . resolve ( ) ;
331
+ }
332
+ throw error ;
333
+ } ) ;
319
334
}
320
335
} ) ;
336
+ } ) ;
321
337
} ;
322
338
323
339
// Inserts an object into the database.
@@ -612,7 +628,7 @@ DatabaseController.prototype.find = function(className, query, {
612
628
. then ( ( ) => this . adapter . adaptiveCollection ( className ) )
613
629
. then ( collection => {
614
630
if ( ! isMaster ) {
615
- query = this . addPointerPermissions ( schema , className , op , query , aclGroup ) ;
631
+ query = this . addPointerPermissions ( schema , className , op , query , aclGroup ) ;
616
632
}
617
633
if ( ! query ) {
618
634
if ( op == 'get' ) {
0 commit comments