Skip to content

Commit 80316ed

Browse files
committed
Object.destroyAll use batched requests
1 parent d3faa1e commit 80316ed

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/object.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,35 +1242,31 @@ module.exports = function(AV) {
12421242
return result;
12431243
};
12441244
/**
1245-
* Delete objects in batch.The objects className must be the same.
1245+
* Delete objects in batch.
12461246
* @param {AV.Object[]} objects The <code>AV.Object</code> array to be deleted.
12471247
* @param {AuthOptions} options
12481248
* @return {Promise} A promise that is fulfilled when the save
12491249
* completes.
12501250
*/
1251-
AV.Object.destroyAll = function(objects, options){
1252-
options = options || {};
1251+
AV.Object.destroyAll = function(objects, options = {}){
12531252
if (!objects || objects.length === 0){
12541253
return AV.Promise.resolve();
12551254
}
1256-
var className = objects[0].className;
1257-
var id = "";
1258-
var wasFirst = true;
1259-
objects.forEach(function(obj){
1260-
if(obj.className != className)
1261-
throw "AV.Object.destroyAll requires the argument object array's classNames must be the same";
1262-
if(!obj.id)
1263-
throw "Could not delete unsaved object";
1264-
if(wasFirst){
1265-
id = obj.id;
1266-
wasFirst = false;
1267-
}else{
1268-
id = id + ',' + obj.id;
1255+
const objectsByClassNameAndFlags = _.groupBy(objects, object => JSON.stringify({
1256+
className: object.className,
1257+
flags: object._flags
1258+
}));
1259+
const body = {
1260+
requests: _.map(objectsByClassNameAndFlags, objects => {
1261+
const ids = _.map(objects, 'id').join(',');
1262+
return {
1263+
method: 'DELETE',
1264+
path: `/1.1/classes/${objects[0].className}/${ids}`,
1265+
body: objects[0]._flags
12691266
}
1270-
});
1271-
var request =
1272-
AVRequest('classes', className, id, 'DELETE', null, options);
1273-
return request;
1267+
})
1268+
};
1269+
return AVRequest('batch', null, null, 'POST', body, options);
12741270
};
12751271

12761272
/**

0 commit comments

Comments
 (0)