UNPKG

@learnlink/firestore-toolbox

Version:

NPM package for easier cleaning and management of Firestore data. Backup, copy, convert, migrate, sort and move data between different accounts.

206 lines 8.39 kB
/* * firestore-toolbox * * Copyright © 2019-2020 Learnlink AS <engineering@learnlink.no> * MIT Licensed * */ "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.renameCollection = exports.replaceValuesForAllDocumentsWhere = exports.deletePropFromAllDocumentsInCollection = exports.deleteDocumentsFromCollection = exports.convertPropertyType = exports.addPropertyToAllDocumentsInCollection = void 0; const firebase_admin_1 = require("firebase-admin"); /** * Lets you specify a collection, a property name and a value, * and populate all documents in the collection with the property. * * @param {string} collectionName * @param {string} propertyName * @param {Object|string|number} propertyValue * @returns {Promise<Object[]>} Array containing all the responses from Firestore. */ function addPropertyToAllDocumentsInCollection(firestoreInstance, collectionName, propertyName, propertyValue) { return __awaiter(this, void 0, void 0, function* () { const updated = []; const collectionSnapshot = yield firestoreInstance.collection(collectionName).get(); yield Promise.all(collectionSnapshot.docs.map(documentSnapshot => { const document = documentSnapshot.data(); if (document[propertyName] === undefined) { const updateObject = {}; updateObject[propertyName] = propertyValue; updated.push(documentSnapshot.id); return firestoreInstance.collection(collectionName).doc(documentSnapshot.id).update(updateObject); } })); return updated; }); } exports.addPropertyToAllDocumentsInCollection = addPropertyToAllDocumentsInCollection; ; /** * Convert the type of a specific field with a specific type to a new type. * Also possible to pass in a new Initial value to initate the new type in the field * If unspecified it will set the new value of the field to be the "empty" equivalent (e.g "" for strings and 0 for number types) * @param firestoreInstance * @param collectionName * @param propertyName * @param fromType * @param toType * @param newInitialValue */ function convertPropertyType(firestoreInstance, collectionName, propertyName, fromType, toType, newInitialValue) { return __awaiter(this, void 0, void 0, function* () { const collectionSnapshot = yield firestoreInstance.collection(collectionName).get(); const updated = []; yield Promise.all(collectionSnapshot.docs.map(doc => { const ID = doc.id, data = doc.data(); if (getType(data[propertyName]) === fromType) { if (newInitialValue) { data[propertyName] = newInitialValue; } else { data[propertyName] = getInitialValue(toType); } updated.push(ID); return firestoreInstance.collection(collectionName).doc(ID).update(data); } })); return updated; }); } exports.convertPropertyType = convertPropertyType; /** * * @param {string} collectionName * @param {Array} idList * @returns {Promise<Object[]>} Array containing all the responses from Firestore. */ function deleteDocumentsFromCollection(firestoreInstance, collectionName, idList) { return __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(idList)) { throw new Error("Parameter idList must be array of strings."); } return Promise.all(idList.map(documentID => { return firestoreInstance.collection(collectionName).doc(documentID + "").delete(); })); }); } exports.deleteDocumentsFromCollection = deleteDocumentsFromCollection; ; function deletePropFromAllDocumentsInCollection(firestoreInstance, collectionName, propertyName) { return __awaiter(this, void 0, void 0, function* () { const collectionSnapshot = yield firestoreInstance.collection(collectionName).get(); const updated = []; yield Promise.all(collectionSnapshot.docs.map((doc) => __awaiter(this, void 0, void 0, function* () { const updateObject = {}; updateObject[propertyName] = firebase_admin_1.firestore.FieldValue.delete(); updated.push(doc.id); return firestoreInstance.collection(collectionName).doc(doc.id + "").update(updateObject); }))); return updated; }); } exports.deletePropFromAllDocumentsInCollection = deletePropFromAllDocumentsInCollection; ; function replaceValuesForAllDocumentsWhere(firestoreInstance, collectionName, propertyName, comparison, equalTo, newValue) { return __awaiter(this, void 0, void 0, function* () { const updated = []; const documentSnapshot = yield firestoreInstance.collection(collectionName).where(propertyName, comparison, equalTo).get(); console.log("Snapshot size " + documentSnapshot.size); yield Promise.all(documentSnapshot.docs.map((doc) => { const documentID = doc.id; const documentData = doc.data(); documentData[propertyName] = newValue; updated.push(documentID); return firestoreInstance.collection(collectionName).doc(documentID).update(documentData); })); console.log("Updated " + updated.length + " documents."); return updated; }); } exports.replaceValuesForAllDocumentsWhere = replaceValuesForAllDocumentsWhere; ; /** * Renames a collection by running through the current collection, copying all documents * to a collection given the new name, and deleting all the documents from the old * collection that where successfully copied to the new collection. * * @param {string} currentName * @param {string} newName * @returns {Promise<[]>} Promise that resolves with a string-array of all docIDs moved. */ function renameCollection(firestoreInstance, currentName, newName) { return __awaiter(this, void 0, void 0, function* () { const collectionSnapshot = yield firestoreInstance.collection(currentName).get(); collectionSnapshot.docs.map((doc) => __awaiter(this, void 0, void 0, function* () { try { yield firestoreInstance.collection(newName).doc(doc.id + "").set(doc.data()); yield firestoreInstance.collection(currentName).doc(doc.id + "").delete(); } catch (e) { console.error(e); } })); }); } exports.renameCollection = renameCollection; ; /** * Returns an initatorvalue for a field based on type input. * @param type */ function getInitialValue(type) { if (type === "string") { return ""; } else if (type === "number") { return 0; } else if (type === "boolean") { return false; } else if (type === "array") { return []; } else if (type === "object") { return {}; } const errorMessage = "Cannot init value of type " + type; throw new Error(errorMessage); } ; function getType(val) { if (val === null) { return "null"; } else if (typeof val === "undefined") { return "undefined"; } else if (typeof val === "string") { return "string"; } else if (typeof val === "number" && !isNaN(val)) { if (Number.isInteger(val)) { return "integer"; } return "float"; } else if (typeof val === "boolean") { return "boolean"; } else if (Array.isArray(val)) { return "array"; } else if (typeof val === "object" && Object.keys(val).length) { return "object"; } } //# sourceMappingURL=index.js.map