Skip to content

Commit 303e4af

Browse files
committed
fix: revert batched delete
1 parent e32941b commit 303e4af

File tree

4 files changed

+28
-49
lines changed

4 files changed

+28
-49
lines changed

changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# 3.3.0 (2017-10-24)
22

33
### Features
4-
- `AV.Object` 的批量操作 API(`.saveAll``.fetchAll` `deleteAll`失败时,抛出的异常 error 对象增加 `results` 属性,类型为 `Array<AV.Object|Error>`。通过这个属性,开发者可以知道哪些对象的操作成功了,哪些失败了。
4+
- `AV.Object.saveAll``AV.Object.fetchAll` 失败时,抛出的异常 error 对象增加 `results` 属性,类型为 `Array<AV.Object|Error>`。通过这个属性,开发者可以知道哪些对象的操作成功了,哪些失败了。
55
- `AV.User` 增加了 `#associateWithAuthData``#disassociateAuthData` 用于关联、解绑第三方平台。原有的静态方法 `AV.User.associateWithAuthData` 已废弃。
66

77
### Bug Fixes
8-
- 优化了关联对象保存时的逻辑,减少了一些不必要的保存请求,避免了在关联对象有循环依赖时可能会出现死循环的问题。
8+
- 优化了关联对象保存时的逻辑,减少了一些不必要的保存请求,避免了在关联对象有循环依赖时可能会出现死循环的问题。
99
- 修复了使用应用内社交模块 `inboxQuery` 查询时可能出现 `URI too long` 异常的问题。
1010

1111

src/object.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/hooks.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,7 @@ describe('hooks', function() {
186186
return Promise.all([object, objectIgnoreBefore].map(function(object) {
187187
return object.save();
188188
})).then(function() {
189-
return AV.Object.destroyAll([object, objectIgnoreBefore]);
190-
}).catch(error => {
191-
error.results.should.be.length(2);
192-
error.results[0].should.be.instanceof(Error);
193-
expect(error.results[1]).to.equal(null);
194-
throw new Error('handled error');
195-
}).should.be.rejectedWith('handled error');
189+
return AV.Object.destroyAll([objectIgnoreBefore, object]);
190+
}).should.be.rejectedWith(/Error from beforeDelete/);
196191
});
197192
});

test/object.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,6 @@ describe('Objects', function(){
420420
)
421421
)
422422
});
423-
it("batch delete",function(){
424-
const acl = new AV.ACL();
425-
acl.setPublicWriteAccess(false);
426-
return new GameScore({ACL: acl}).save().then(gameScore =>
427-
AV.Object.destroyAll([
428-
AV.Object.createWithoutData('GameScore', 'fakeId'),
429-
gameScore,
430-
]).catch(error => {
431-
error.results.should.be.length(2);
432-
const [_, err] = error.results;
433-
err.should.be.instanceof(Error);
434-
throw new Error('handled error');
435-
}).should.be.rejectedWith('handled error')
436-
);
437-
});
438423
});
439424

440425
describe('Array Data', function () {

0 commit comments

Comments
 (0)