UNPKG

firebase-tools-extra

Version:

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

150 lines (149 loc) 6.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var utils_1 = require("../utils"); var logger_1 = require("../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 */ function rtdbGet(actionPath, options) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, emulator, debug, fbInstance, baseRef, ref, res, dataToWrite, err_1; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _a = options || {}, emulator = _a.emulator, debug = _a.debug; fbInstance = utils_1.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*/, utils_1.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(); logger_1.error("Error with database:get at path \"" + actionPath + "\": ", err_1.message); throw err_1; case 7: return [2 /*return*/]; } }); }); } exports.rtdbGet = rtdbGet; /** * 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 */ function rtdbWrite(action, actionPath, filePath, options) { if (action === void 0) { action = 'set'; } return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, emulator, debug, fbInstance, errMsg, dataToWrite, ref, res, err_2; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _a = options || {}, emulator = _a.emulator, debug = _a.debug; fbInstance = utils_1.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 + "\""; logger_1.error(errMsg); throw new Error(errMsg); } dataToWrite = (options === null || options === void 0 ? void 0 : options.data) ? utils_1.tryToJsonParse(options.data) : utils_1.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(); logger_1.error("Error with database:" + action + " at path \"" + actionPath + "\": ", err_2.message); throw err_2; case 4: return [2 /*return*/]; } }); }); } exports.rtdbWrite = rtdbWrite; /** * Remove data from path of Real Time Database * @param actionPath - Path to remove from database * @param options - Options */ function rtdbRemove(actionPath, options) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, emulator, debug, fbInstance, res, err_3; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _a = options || {}, emulator = _a.emulator, debug = _a.debug; fbInstance = utils_1.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(); logger_1.error("Error with database:remove at path \"" + actionPath + "\": ", err_3.message); throw err_3; case 4: return [2 /*return*/]; } }); }); } exports.rtdbRemove = rtdbRemove;