Last active
June 3, 2018 05:36
-
-
Save Cretezy/dc54fc859e307ef5e80f3e752c5987b0 to your computer and use it in GitHub Desktop.
Helper for batching operations in Firestore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FirestoreBatcher { | |
batches = []; | |
index = 0; | |
constructor(firestore) { | |
this.firestore = firestore; | |
} | |
getNext() { | |
if (this.index++ % 500 === 0) { | |
this.batches.push(this.firestore.batch()); | |
} | |
return this.batches[this.batches.length - 1]; | |
} | |
async commit() { | |
await Promise.all(this.batches.map(batch => batch.commit())); | |
} | |
delete(ref) { | |
this.getNext().delete(ref); | |
} | |
set(ref, data) { | |
this.getNext().set(ref, data); | |
} | |
update(ref, data) { | |
this.getNext().update(ref, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: