UNPKG

@magnetarjs/plugin-firestore-admin

Version:

Magnetar plugin firestore-admin

42 lines (41 loc) 1.41 kB
import { FieldValue } from 'firebase-admin/firestore'; function deleteField() { return FieldValue.delete(); } export function createWriteBatch(db) { return db.batch(); } /** * A function that applies everything in the `SyncBatch` to a Firestore's `WriteBatch`. * It mutates the passed `batch`. */ export function applySyncBatch(writeBatch, batch, db) { batch.insert.forEach((payload, documentPath) => { const ref = db.doc(documentPath); writeBatch.set(ref, payload); }); batch.assign.forEach((payload, documentPath) => { const ref = db.doc(documentPath); writeBatch.set(ref, payload, { mergeFields: Object.keys(payload) }); }); batch.merge.forEach((payload, documentPath) => { const ref = db.doc(documentPath); writeBatch.set(ref, payload, { merge: true }); }); batch.replace.forEach((payload, documentPath) => { const ref = db.doc(documentPath); writeBatch.set(ref, payload); }); batch.deleteProp.forEach((payload, documentPath) => { const ref = db.doc(documentPath); const _payload = [...payload].reduce((carry, propPath) => ({ ...carry, [propPath]: deleteField(), }), {}); writeBatch.update(ref, _payload); }); batch.delete.forEach((documentPath) => { const ref = db.doc(documentPath); writeBatch.delete(ref); }); }