UNPKG

multibridge

Version:

A multi-database connection framework with centralized configuration

94 lines (93 loc) 3.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchDBConfig = fetchDBConfig; exports.invalidateConfigCache = invalidateConfigCache; exports.clearConfigCache = clearConfigCache; exports.closeCentralDB = closeCentralDB; const pg_1 = require("pg"); const envConfig_1 = require("./envConfig"); const loggers_1 = __importDefault(require("../utils/loggers")); const lruCache_1 = require("../utils/lruCache"); const errors_1 = require("../utils/errors"); const centralDB = new pg_1.Pool({ host: envConfig_1.envConfig.CENTRAL_DB_HOST, port: envConfig_1.envConfig.CENTRAL_DB_PORT, user: envConfig_1.envConfig.CENTRAL_DB_USER, password: envConfig_1.envConfig.CENTRAL_DB_PASSWORD, database: envConfig_1.envConfig.CENTRAL_DB_NAME, }); // Cache for DB config lookups const configCache = new lruCache_1.LRUCache(1000, // Max 1000 cached configs envConfig_1.envConfig.CONFIG_CACHE_TTL_MS || undefined); function generateConfigCacheKey(appId, orgId) { return `${appId}:${orgId}`; } async function fetchDBConfig(appId, orgId) { const cacheKey = generateConfigCacheKey(appId, orgId); // Check cache first const cached = configCache.get(cacheKey); if (cached) { loggers_1.default.debug(`Using cached config for appid: ${appId}, orgid: ${orgId}`); return cached; } try { const tableName = envConfig_1.envConfig.CENTRAL_DB_TABLE; if (!tableName) { throw new errors_1.ConfigurationError("CENTRAL_DB_TABLE is not set in the environment"); } // Ensure tableName is a safe SQL identifier (alphanumeric and underscores) if (!/^[a-zA-Z0-9_]+$/.test(tableName)) { throw new errors_1.ConfigurationError("CENTRAL_DB_TABLE contains invalid characters"); } const query = `SELECT * FROM ${tableName} WHERE app_id = $1 AND org_id = $2`; const result = await centralDB.query(query, [appId, orgId]); if (result.rows.length === 0) { loggers_1.default.error(`No connection config found for app ${appId} and org ${orgId} in the DB`); return null; } const config = result.rows[0]; // Cache the result configCache.set(cacheKey, config); loggers_1.default.info(`Fetched configuration for appid: ${appId}, orgid: ${orgId}`); return config; } catch (error) { loggers_1.default.error(`Error fetching DB config: ${JSON.stringify(error, Object.getOwnPropertyNames(error))}`, { appId, orgId, }); throw error; } } /** * Invalidate cached config for a specific tenant */ function invalidateConfigCache(appId, orgId) { const cacheKey = generateConfigCacheKey(appId, orgId); configCache.delete(cacheKey); loggers_1.default.debug(`Invalidated config cache for appid: ${appId}, orgid: ${orgId}`); } /** * Clear all cached configs */ function clearConfigCache() { configCache.clear(); loggers_1.default.debug("Cleared all config cache"); } /** * Close the central database pool * Should be called during graceful shutdown */ async function closeCentralDB() { try { await centralDB.end(); loggers_1.default.info("Central database pool closed"); } catch (error) { loggers_1.default.error(`Error closing central database pool: ${error.message}`); throw error; } }