Skip to content

Add batchSize to saveAll / destroyAll #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type SaveParams = {
body: AttributeMap;
};

const DEFAULT_BATCH_SIZE = 20;

// Mapping of class names to constructors, so we can populate objects from the
// server with appropriate subclasses of ParseObject
var classMap = {};
Expand Down Expand Up @@ -1339,6 +1341,7 @@ class ParseObject {
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>batchSize: Number of objects to process per request
* </ul>
* @return {Promise} A promise that is fulfilled when the destroyAll
* completes.
Expand All @@ -1351,6 +1354,9 @@ class ParseObject {
if (options.hasOwnProperty('sessionToken')) {
destroyOptions.sessionToken = options.sessionToken;
}
if (options.hasOwnProperty('batchSize') && typeof options.batchSize === 'number') {
destroyOptions.batchSize = options.batchSize;
}
return CoreManager.getObjectController().destroy(
list,
destroyOptions
Expand Down Expand Up @@ -1378,6 +1384,7 @@ class ParseObject {
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>batchSize: Number of objects to process per request
* </ul>
*/
static saveAll(list: Array<ParseObject>, options = {}) {
Expand All @@ -1388,6 +1395,9 @@ class ParseObject {
if (options.hasOwnProperty('sessionToken')) {
saveOptions.sessionToken = options.sessionToken;
}
if (options.hasOwnProperty('batchSize') && typeof options.batchSize === 'number') {
saveOptions.batchSize = options.batchSize;
}
return CoreManager.getObjectController().save(
list,
saveOptions
Expand Down Expand Up @@ -1729,6 +1739,8 @@ var DefaultController = {
},

destroy(target: ParseObject | Array<ParseObject>, options: RequestOptions): Promise {
const batchSize = (options && options.batchSize) ? options.batchSize : DEFAULT_BATCH_SIZE;

var RESTController = CoreManager.getRESTController();
if (Array.isArray(target)) {
if (target.length < 1) {
Expand All @@ -1740,7 +1752,7 @@ var DefaultController = {
return;
}
batches[batches.length - 1].push(obj);
if (batches[batches.length - 1].length >= 20) {
if (batches[batches.length - 1].length >= batchSize) {
batches.push([]);
}
});
Expand Down Expand Up @@ -1796,6 +1808,8 @@ var DefaultController = {
},

save(target: ParseObject | Array<ParseObject | ParseFile>, options: RequestOptions) {
const batchSize = (options && options.batchSize) ? options.batchSize : DEFAULT_BATCH_SIZE;

var RESTController = CoreManager.getRESTController();
var stateController = CoreManager.getObjectStateController();
if (Array.isArray(target)) {
Expand Down Expand Up @@ -1831,7 +1845,7 @@ var DefaultController = {
var batch = [];
var nextPending = [];
pending.forEach((el) => {
if (batch.length < 20 && canBeSerialized(el)) {
if (batch.length < batchSize && canBeSerialized(el)) {
batch.push(el);
} else {
nextPending.push(el);
Expand Down
1 change: 1 addition & 0 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type RequestOptions = {
useMasterKey?: boolean;
sessionToken?: string;
installationId?: string;
batchSize?: number;
include?: any;
};

Expand Down
138 changes: 138 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,69 @@ describe('ParseObject', () => {
jest.runAllTicks();
});

it('can saveAll with batchSize', async (done) => {
const xhrs = [];
for (let i = 0; i < 2; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
readyState: 4
};
}
let current = 0;
RESTController._setXHR(function() { return xhrs[current++]; });
const objects = [];
for (let i = 0; i < 22; i++) {
objects[i] = new ParseObject('Person');
}
ParseObject.saveAll(objects, { batchSize: 20 }).then(() => {
expect(xhrs[0].open.mock.calls[0]).toEqual(
['POST', 'https://api.parse.com/1/batch', true]
);
expect(xhrs[1].open.mock.calls[0]).toEqual(
['POST', 'https://api.parse.com/1/batch', true]
);
done();
});
jest.runAllTicks();
await flushPromises();

xhrs[0].responseText = JSON.stringify([
{ success: { objectId: 'pid0' } },
{ success: { objectId: 'pid1' } },
{ success: { objectId: 'pid2' } },
{ success: { objectId: 'pid3' } },
{ success: { objectId: 'pid4' } },
{ success: { objectId: 'pid5' } },
{ success: { objectId: 'pid6' } },
{ success: { objectId: 'pid7' } },
{ success: { objectId: 'pid8' } },
{ success: { objectId: 'pid9' } },
{ success: { objectId: 'pid10' } },
{ success: { objectId: 'pid11' } },
{ success: { objectId: 'pid12' } },
{ success: { objectId: 'pid13' } },
{ success: { objectId: 'pid14' } },
{ success: { objectId: 'pid15' } },
{ success: { objectId: 'pid16' } },
{ success: { objectId: 'pid17' } },
{ success: { objectId: 'pid18' } },
{ success: { objectId: 'pid19' } },
]);
xhrs[0].onreadystatechange();
jest.runAllTicks();
await flushPromises();

xhrs[1].responseText = JSON.stringify([
{ success: { objectId: 'pid20' } },
{ success: { objectId: 'pid21' } },
]);
xhrs[1].onreadystatechange();
jest.runAllTicks();
});

it('returns the first error when saving an array of objects', async (done) => {
const xhrs = [];
for (let i = 0; i < 2; i++) {
Expand Down Expand Up @@ -1714,6 +1777,81 @@ describe('ObjectController', () => {
await result;
});

it('can destroy an array of objects with batchSize', async () => {
const objectController = CoreManager.getObjectController();
const xhrs = [];
for (let i = 0; i < 3; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn()
};
xhrs[i].status = 200;
xhrs[i].responseText = JSON.stringify({});
xhrs[i].readyState = 4;
}
let current = 0;
RESTController._setXHR(function() { return xhrs[current++]; });
let objects = [];
for (let i = 0; i < 5; i++) {
objects[i] = new ParseObject('Person');
objects[i].id = 'pid' + i;
}
const result = objectController.destroy(objects, { batchSize: 20}).then(async () => {
expect(xhrs[0].open.mock.calls[0]).toEqual(
['POST', 'https://api.parse.com/1/batch', true]
);
expect(JSON.parse(xhrs[0].send.mock.calls[0]).requests).toEqual([
{
method: 'DELETE',
path: '/1/classes/Person/pid0',
body: {}
}, {
method: 'DELETE',
path: '/1/classes/Person/pid1',
body: {}
}, {
method: 'DELETE',
path: '/1/classes/Person/pid2',
body: {}
}, {
method: 'DELETE',
path: '/1/classes/Person/pid3',
body: {}
}, {
method: 'DELETE',
path: '/1/classes/Person/pid4',
body: {}
}
]);

objects = [];
for (let i = 0; i < 22; i++) {
objects[i] = new ParseObject('Person');
objects[i].id = 'pid' + i;
}
const destroy = objectController.destroy(objects, { batchSize: 20 });
jest.runAllTicks();
await flushPromises();
xhrs[1].onreadystatechange();
jest.runAllTicks();
await flushPromises();
expect(xhrs[1].open.mock.calls.length).toBe(1);
xhrs[2].onreadystatechange();
jest.runAllTicks();
return destroy;
}).then(() => {
expect(JSON.parse(xhrs[1].send.mock.calls[0]).requests.length).toBe(20);
expect(JSON.parse(xhrs[2].send.mock.calls[0]).requests.length).toBe(2);
});
jest.runAllTicks();
await flushPromises();

xhrs[0].onreadystatechange();
jest.runAllTicks();
await result;
});

it('can destroy an array of objects', async () => {
const objectController = CoreManager.getObjectController();
const xhrs = [];
Expand Down