firestore-large-batch
Version:
A library for limitless batching operations in Firestore
120 lines (119 loc) • 4.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LargeBatch = void 0;
/**
* LargeBatch allows for batching operations in Firestore.
*/
class LargeBatch {
firestoreInstance;
batches;
operationCount = 0;
/**
* Creates a new instance of the `LargeBatch` class.
* @param firestoreInstance An instance of Firestore.
*/
constructor(firestoreInstance) {
this.firestoreInstance = firestoreInstance;
this.batches = [firestoreInstance.batch()];
}
/**
* Returns the current batch and if the number of operations in that batch reaches 500, a new batch is added to the array
*/
currentBatch() {
this.operationCount++;
const batch = this.batches.at(-1);
if (!batch) {
const newBatch = this.firestoreInstance.batch();
this.batches.push(newBatch);
return newBatch;
}
if (this.operationCount == 500) {
this.batches.push(this.firestoreInstance.batch());
this.operationCount = 0;
}
return batch;
}
/**
* Adds a create operation to the current batch.
* @param documentRef A Firestore DocumentReference.
* @param data The data to be added to the document.
*/
create(documentRef, data) {
this.currentBatch().create(documentRef, data);
}
/**
* Adds a set operation to the current batch.
* @param documentRef A Firestore DocumentReference.
* @param data The data to be set on the document.
* @param options Optional settings to use when setting the document.
*/
set(documentRef, data, options) {
if (options) {
this.currentBatch().set(documentRef, data, options);
}
else {
this.currentBatch().set(documentRef, data);
}
}
/**
* Adds an update operation to the current batch.
* @param documentRef A Firestore DocumentReference.
* @param data The data to be updated on the document.
* @param precondition An optional precondition to use when updating the document.
*/
update(documentRef, data, precondition) {
if (precondition) {
this.currentBatch().update(documentRef, data, precondition);
}
else {
this.currentBatch().update(documentRef, data);
}
}
/**
* Adds an update field operation to the current batch.
* @param documentRef A Firestore DocumentReference.
* @param field The field to update on the document.
* @param value The new value of the field.
* @param fieldsOrPrecondition Additional fields to update on the document, or an optional precondition to use when updating the document.
*/
updateField(documentRef, field, value, ...fieldsOrPrecondition) {
this.currentBatch().update(documentRef, field, value, ...fieldsOrPrecondition);
}
/**
* Adds a delete operation to the current batch.
* @param documentRef A Firestore DocumentReference.
* @param precondition An optional precondition to use when deleting the document.
*/
delete(documentRef, precondition) {
this.currentBatch().delete(documentRef, precondition);
}
/**
* Commits all batches. Optionally, you can pass a commit unit which controls the number of batches that are committed at a time.
* @param commitUnit Number of batches to commit at a time.
*/
async commit(options) {
if (!options?.commitUnit) {
await this.commitBatches(this.batches);
}
else {
const slicedBatches = Array();
for (let i = 0; i < this.batches.length; i += options.commitUnit) {
const chunk = this.batches.slice(i, i + options.commitUnit);
slicedBatches.push(chunk);
}
for (const batches of slicedBatches) {
await this.commitBatches(batches);
}
}
this.batches = [];
this.operationCount = 0;
}
/**
* Commits all batches and reset the batch array and operation count
* @param batches Array of batches to be committed
*/
async commitBatches(batches) {
await Promise.all(batches.map((batch) => batch.commit()));
}
}
exports.LargeBatch = LargeBatch;