@@ -1315,31 +1315,30 @@ module.exports = function(AV) {
13151315 * @return {Promise } A promise that is fulfilled when the save
13161316 * completes.
13171317 */
1318- AV . Object . destroyAll = function ( objects , options = { } ) {
1319- if ( ! objects || objects . length === 0 ) {
1320- return AV . Promise . resolve ( ) ;
1321- }
1322- const body = {
1323- requests : _ . map ( objects , object => {
1324- if ( ! object . className ) throw new Error ( 'object must have className to destroy' ) ;
1325- if ( ! object . id ) throw new Error ( 'object must have id to destroy' ) ;
1326- return {
1327- method : 'DELETE' ,
1328- path : `/1.1/classes/${ object . className } /${ object . id } ` ,
1329- body : object . _flags
1330- } ;
1331- } ) ,
1332- } ;
1333- return _request ( 'batch' , null , null , 'POST' , body , options ) . then ( function ( response ) {
1334- const results = _ . map ( objects , function ( object , i ) {
1335- if ( response [ i ] . success ) {
1336- return null ;
1337- }
1338- return new AVError ( response [ i ] . error . code , response [ i ] . error . error ) ;
1339- } ) ;
1340- return handleBatchResults ( results ) ;
1341- } ) ;
1342- } ;
1318+ AV . Object . destroyAll = function ( objects , options = { } ) {
1319+ if ( ! objects || objects . length === 0 ) {
1320+ return AV . Promise . resolve ( ) ;
1321+ }
1322+ const objectsByClassNameAndFlags = _ . groupBy ( objects , object => JSON . stringify ( {
1323+ className : object . className ,
1324+ flags : object . _flags
1325+ } ) ) ;
1326+ const body = {
1327+ requests : _ . map ( objectsByClassNameAndFlags , objects => {
1328+ const ids = _ . map ( objects , 'id' ) . join ( ',' ) ;
1329+ return {
1330+ method : 'DELETE' ,
1331+ path : `/1.1/classes/${ objects [ 0 ] . className } /${ ids } ` ,
1332+ body : objects [ 0 ] . _flags
1333+ }
1334+ } )
1335+ } ;
1336+ return _request ( 'batch' , null , null , 'POST' , body , options ) . then ( response => {
1337+ const firstError = _ . find ( response , result => ! result . success ) ;
1338+ if ( firstError ) throw new AVError ( firstError . error . code , firstError . error . error ) ;
1339+ return undefined ;
1340+ } ) ;
1341+ } ;
13431342
13441343 /**
13451344 * Returns the appropriate subclass for making new instances of the given
0 commit comments