Skip to content

Instantly share code, notes, and snippets.

@wcoder
Last active July 31, 2022 14:18
Show Gist options
  • Save wcoder/9bb44ffe709397f657864f6a404cf7cc to your computer and use it in GitHub Desktop.
Save wcoder/9bb44ffe709397f657864f6a404cf7cc to your computer and use it in GitHub Desktop.
Utility method to update more than 500 docs in Firestore using Batch. TypeScript.
import admin from 'firebase-admin';
/**
* Firestore does not accept more than 500 writes in a transaction or batch write.
*/
const MAX_TRANSACTION_WRITES = 499;
type WriteBigBatchOperationType = (batch: admin.firestore.WriteBatch) => void;
type WriteBigBatchCallbackType = (operation: WriteBigBatchOperationType) => void;
/* Usage:
await writeBigBatch(
admin.firestore(), // firestore instance
(resolveActiveBatch) => {
// const docRef = ...
// const data = ...
resolveActiveBatch((writeBatch) => {
writeBatch.set(docRef, data, {merge: true});
});
//...
}
);
*/
// Based on solution: https://stackoverflow.com/a/56145214/5925490
export const writeBigBatch = async (
db: admin.firestore.Firestore,
callback: (_:WriteBigBatchCallbackType) => void
) => {
const batchPool: admin.firestore.WriteBatch[] = [];
batchPool.push(db.batch());
let operationCounter = 0;
let batchIndex = 0;
callback((operation: WriteBigBatchOperationType) => {
operation(batchPool[batchIndex]);
operationCounter++;
if (operationCounter === MAX_TRANSACTION_WRITES) {
batchPool.push(db.batch());
batchIndex++;
operationCounter = 0;
}
});
batchPool.forEach(async (batch) => {
await batch.commit();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment