UNPKG

firebase-tools-extra

Version:

Extra functionality for firebase-tools with support for emulators and auth through service account.

145 lines (144 loc) 6.35 kB
import { __awaiter, __generator } from "tslib"; import { initializeFirebase, readJsonFile, tryToJsonParse, writeFilePromise, } from '../utils'; import { error } from '../logger'; /** * Add query options to RTDB reference * @param baseRef - Base RTDB reference * @param options - Options for ref * @returns RTDB Reference */ function optionsToRtdbRef(baseRef, options) { var optionsToAdd = [ 'orderByChild', 'orderByKey', 'orderByValue', 'equalTo', 'limitToFirst', 'limitToLast', 'startAt', 'endAt', ]; return optionsToAdd.reduce(function (acc, optionName) { if (options && options[optionName]) { return acc[optionName](options[optionName]); } return acc; }, baseRef); } /** * Write data to path of Real Time Database * @param actionPath - Pat of get * @param options - Get options object */ export function rtdbGet(actionPath, options) { return __awaiter(this, void 0, void 0, function () { var _a, emulator, debug, fbInstance, baseRef, ref, res, dataToWrite, err_1; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = options || {}, emulator = _a.emulator, debug = _a.debug; fbInstance = initializeFirebase({ emulator: emulator, debug: debug }); _b.label = 1; case 1: _b.trys.push([1, 6, , 7]); baseRef = fbInstance .database() .ref(actionPath); ref = optionsToRtdbRef(baseRef, options); return [4 /*yield*/, ref.once('value')]; case 2: res = _b.sent(); dataToWrite = (options === null || options === void 0 ? void 0 : options.shallow) ? Object.keys(res.val()) : res.val(); if (!(options === null || options === void 0 ? void 0 : options.output)) return [3 /*break*/, 4]; // Write results to file at path provided in options.output return [4 /*yield*/, writeFilePromise(process.cwd() + "/" + options.output, JSON.stringify(dataToWrite, null, 2))]; case 3: // Write results to file at path provided in options.output _b.sent(); return [3 /*break*/, 5]; case 4: // Write results to stdout (console.log was used instead of process.stdout.write so that newline is appended) /* eslint-disable no-console */ console.log((options === null || options === void 0 ? void 0 : options.pretty) ? JSON.stringify(dataToWrite, null, 2) : JSON.stringify(dataToWrite)); _b.label = 5; case 5: return [2 /*return*/, dataToWrite]; case 6: err_1 = _b.sent(); error("Error with database:get at path \"" + actionPath + "\": ", err_1.message); throw err_1; case 7: return [2 /*return*/]; } }); }); } /** * Write data to path of Real Time Database. Also works with server timestamps * passed as {.sv: "timestamp"}. * @param action - Write action to run * @param actionPath - Path of action * @param filePath - Path of file to write to RTDB * @param options - Options */ export function rtdbWrite(action, actionPath, filePath, options) { if (action === void 0) { action = 'set'; } return __awaiter(this, void 0, void 0, function () { var _a, emulator, debug, fbInstance, errMsg, dataToWrite, ref, res, err_2; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = options || {}, emulator = _a.emulator, debug = _a.debug; fbInstance = initializeFirebase({ emulator: emulator, debug: debug }); if (!filePath && !(options === null || options === void 0 ? void 0 : options.data)) { errMsg = "File path or data is required to run " + action + " at path \"" + actionPath + "\""; error(errMsg); throw new Error(errMsg); } dataToWrite = (options === null || options === void 0 ? void 0 : options.data) ? tryToJsonParse(options.data) : readJsonFile(filePath); _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); ref = fbInstance.database().ref(actionPath); return [4 /*yield*/, ref[action](dataToWrite)]; case 2: res = _b.sent(); return [2 /*return*/, res]; case 3: err_2 = _b.sent(); error("Error with database:" + action + " at path \"" + actionPath + "\": ", err_2.message); throw err_2; case 4: return [2 /*return*/]; } }); }); } /** * Remove data from path of Real Time Database * @param actionPath - Path to remove from database * @param options - Options */ export function rtdbRemove(actionPath, options) { return __awaiter(this, void 0, void 0, function () { var _a, emulator, debug, fbInstance, res, err_3; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = options || {}, emulator = _a.emulator, debug = _a.debug; fbInstance = initializeFirebase({ emulator: emulator, debug: debug }); _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); return [4 /*yield*/, fbInstance.database().ref(actionPath).remove()]; case 2: res = _b.sent(); return [2 /*return*/, res]; case 3: err_3 = _b.sent(); error("Error with database:remove at path \"" + actionPath + "\": ", err_3.message); throw err_3; case 4: return [2 /*return*/]; } }); }); }