Batch Operations (App)

Version compatibility

This feature was introduced in version 4.20 of the JourneyApps Container.

You can also use Batch operations when using CloudCode or the v4 API.

JourneyApps includes a way for the app to batch together many save or destroy operations to significantly increase the performance of a function. Instead of using one network request per operation, it batches all of them into a single network request.

Example usage:

var batch = new DB.Batch(); // Can also use OnlineDB.Batch or LocalDB.Batch
for(var i = 0; i < 200; i++) {
    var item = DB.item.create({message: 'Item ' + i}); 
    batch.save(item);
}
batch.execute();

Methods

NameUsage

save(item)

Adds an item to be saved. Equivalent to item.save()

destroy(item)

Adds an item to be destroyed. Equivalent to item.destroy()

execute()

Executes the batch

Large batches

If the batch contains more than 1000 operations, it will internally be split into separate smaller network requests.

Error handling

The execute() function can throw an Error, typically in these scenarios:

  1. A network error occurred. This could be before or after the server received and processed the batch.

  2. An attempt is made to update (save) an object that has been deleted elsewhere.

  3. There is a data-model mismatch

It is safe to attempt recovery from these errors by performing batch.execute() again. Any objects that have been confirmed as saved (e.g. the first part of a large batch) will not be saved again. Duplicate objects will not be created, since the IDs for new objects are generated on the device (not on the server).

Note that error types (2) and (3) above are generally not recoverable by retrying the same batch.

Last updated