UNPKG

@magnetarjs/plugin-firestore

Version:
39 lines (38 loc) 1.38 kB
import { deleteField, doc, writeBatch } from 'firebase/firestore'; export function createWriteBatch(db) { return writeBatch(db); } /** * 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 = doc(db, documentPath); writeBatch.set(ref, payload); }); batch.assign.forEach((payload, documentPath) => { const ref = doc(db, documentPath); writeBatch.set(ref, payload, { mergeFields: Object.keys(payload) }); }); batch.merge.forEach((payload, documentPath) => { const ref = doc(db, documentPath); writeBatch.set(ref, payload, { merge: true }); }); batch.replace.forEach((payload, documentPath) => { const ref = doc(db, documentPath); writeBatch.set(ref, payload); }); batch.deleteProp.forEach((payload, documentPath) => { const ref = doc(db, documentPath); const _payload = [...payload].reduce((carry, propPath) => ({ ...carry, [propPath]: deleteField(), }), {}); writeBatch.update(ref, _payload); }); batch.delete.forEach((documentPath) => { const ref = doc(db, documentPath); writeBatch.delete(ref); }); }