@@ -156,7 +156,7 @@ DatabaseController.prototype.update = function(className, query, update, {
156156 . then ( ( ) => this . adapter . adaptiveCollection ( className ) )
157157 . then ( collection => {
158158 if ( ! isMaster ) {
159- query = this . addPointerPermissions ( schema , className , 'update' , query , aclGroup ) ;
159+ query = this . addPointerPermissions ( schema , className , 'update' , query , aclGroup ) ;
160160 }
161161 if ( ! query ) {
162162 return Promise . resolve ( ) ;
@@ -284,40 +284,56 @@ DatabaseController.prototype.removeRelation = function(key, fromClassName, fromI
284284// acl: a list of strings. If the object to be updated has an ACL,
285285// one of the provided strings must provide the caller with
286286// 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 || [ ] ;
290290
291- var schema ;
292291 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 ;
306298 }
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+ } ) ;
319334 }
320335 } ) ;
336+ } ) ;
321337} ;
322338
323339// Inserts an object into the database.
@@ -612,7 +628,7 @@ DatabaseController.prototype.find = function(className, query, {
612628 . then ( ( ) => this . adapter . adaptiveCollection ( className ) )
613629 . then ( collection => {
614630 if ( ! isMaster ) {
615- query = this . addPointerPermissions ( schema , className , op , query , aclGroup ) ;
631+ query = this . addPointerPermissions ( schema , className , op , query , aclGroup ) ;
616632 }
617633 if ( ! query ) {
618634 if ( op == 'get' ) {
0 commit comments