UNPKG

firebase-admin-ql

Version:

A powerful library that bridges Firebase Admin SDK with PostgreSQL, simplifies interaction with stored procedures, facilitates seamless third-party API calls using fetch, and provides utility functions to streamline backend operations.

69 lines 3.16 kB
"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.PgDatabase = void 0; const v2_1 = require("firebase-functions/v2"); class PgDatabase { constructor(schema, dbInstance) { this.db = dbInstance; this.schema = schema; } procedureExists(methodName) { return __awaiter(this, void 0, void 0, function* () { var _a; const query = ` SELECT EXISTS ( SELECT 1 FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid WHERE n.nspname = ? AND p.proname = ? ) AS exists; `; try { const result = yield this.db.raw(query, [this.schema, methodName]); return ((_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.exists) || false; } catch (error) { v2_1.logger.error(`Error checking procedure existence: ${error}`); return false; } }); } runStoredMethod(methodName, parameters) { return __awaiter(this, void 0, void 0, function* () { var _a; const exists = yield this.procedureExists(methodName); if (!exists) { throw new Error(`Stored procedure ${this.schema}.${methodName} does not exist.`); } if (!Array.isArray(parameters) || parameters.length === 0) { throw new Error(`Stored procedure ${this.schema}.${methodName} expects parameters but received none.`); } const placeholders = parameters.map(() => "?").join(","); const query = `CALL ${this.schema}.${methodName}(${placeholders})`; v2_1.logger.info(`Executing SQL: ${query}`); v2_1.logger.info(`With parameters: ${JSON.stringify(parameters)}`); try { const result = yield this.db.raw(query, parameters); v2_1.logger.info("Stored procedure executed successfully:", result); return (_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.f_return_value; } catch (error) { v2_1.logger.error(`Error executing stored method: ${error}`); throw new Error("Unable to execute transaction: " + error); } finally { this.db.destroy(); } }); } } exports.PgDatabase = PgDatabase; //# sourceMappingURL=postgres.config.js.map