The BulkWriter class was made public in version 4.1.0. however, it has some use disadvantages compared to using a BatchWriter.
The problem lies in the fact that close never fails, and the fact that writing (update, delete, set) returns a promise.
This makes it difficult and awkward to correctly handle errors, which is absolutely necessary on cloud functions, as UnhandledRejections will make the whole function to fail with a very cryptic error.
It'd be great if we could write something like:
const writer = db.bulkWriter();
writer.update(ref1, {...});
writer.update(ref2, {...});
try {
await writer.commit();
} catch (error) {
// decide whether to continue, fail, log or do something else based on the error.
// error could be an object having a `failures` and `succeses` fields, where each failure or success has the all the arguments of the write operation.
// Or something similar to the result of promiseAllSettled. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
}
Note that using WriteBatch resembles the proposed API.
Compare that to what one needs to do today.
let sharedState = ...;
const writer = db.bulkWriter();
// depending on whether we want to skip failures or actually fail what the catch function will be
writer.update(ref1, {...}).catch(...);
writer.update(ref2, {...}).catch(...);
await writer.close();
// Check shared state
The
BulkWriterclass was made public in version 4.1.0. however, it has some use disadvantages compared to using aBatchWriter.The problem lies in the fact that
closenever fails, and the fact that writing (update,delete,set) returns a promise.This makes it difficult and awkward to correctly handle errors, which is absolutely necessary on cloud functions, as
UnhandledRejectionswill make the whole function to fail with a very cryptic error.It'd be great if we could write something like:
Note that using
WriteBatchresembles the proposed API.Compare that to what one needs to do today.