UNPKG

firestore-auto-sync

Version:

<a href="https://www.npmjs.com/package/firestore-auto-sync"><img src="https://img.shields.io/npm/v/firestore-auto-sync.svg" alt="Total Downloads"></a> <a href="https://www.npmjs.com/package/firestore-auto-sync"><img src="https://img.shields.io/npm/dw/fire

55 lines 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateInfoToRefs = exports.getUpdatePath = void 0; const firestore_1 = require("firebase-admin/firestore"); const path_to_prop_1 = require("path-to-prop"); const db = (0, firestore_1.getFirestore)(); /** * Returns the refs to be used to update/increment/... */ function getUpdatePath(newOrOldData, update) { const foundOriginVal = "keyOrigin" in update ? (0, path_to_prop_1.getProp)(newOrOldData, update.keyOrigin) : ""; if ("propToIncrement" in update) { return update.propToIncrement.replace(/{keyTarget}/g, foundOriginVal); } if ("propToUpdate" in update) { return update.propToUpdate.replace(/{keyTarget}/g, foundOriginVal); } return ""; } exports.getUpdatePath = getUpdatePath; /** * Returns the refs to be used to update/increment/... */ async function updateInfoToRefs(newOrOldData, update) { let docsToUpdate = []; const foundOriginVal = "keyOrigin" in update ? (0, path_to_prop_1.getProp)(newOrOldData, update.keyOrigin) : ""; if ("doc" in update) { const docPath = update.doc.replace(/{keyTarget}/g, foundOriginVal); docsToUpdate.push(db.doc(docPath)); } if ("collection" in update) { // get collection const collectionPath = update.collection.replace(/{keyTarget}/g, foundOriginVal); const collection = db.collection(collectionPath); let collectionOrQuery = collection; if (update.keyTarget) { // add WHERE to collection collectionOrQuery = collection.where(update.keyTarget, "==", foundOriginVal); } if (update.where) { // add WHERE to collection for (const whereClause of update.where) { collectionOrQuery = collection.where(whereClause[0].replace(/{keyTarget}/g, foundOriginVal), whereClause[1], whereClause[2]); } } // get docs to update const targetSnap = await collectionOrQuery.get(); targetSnap.docs.forEach((doc) => { docsToUpdate.push(collection.doc(doc.id)); }); } return docsToUpdate; } exports.updateInfoToRefs = updateInfoToRefs; //# sourceMappingURL=updateInfoToRefs.js.map