multibridge
Version:
A multi-database connection framework with centralized configuration
48 lines (47 loc) • 2.38 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDBConfig = fetchDBConfig;
const pg_1 = require("pg");
const envConfig_1 = require("./envConfig");
const loggers_1 = __importDefault(require("../utils/loggers"));
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,
});
function fetchDBConfig(appId, orgId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const tableName = envConfig_1.envConfig.CENTRAL_DB_TABLE;
if (!tableName) {
throw new Error("CENTRAL_DB_TABLE is not set in the environment");
}
const query = `SELECT * FROM ${tableName} WHERE app_id = $1 AND org_id = $2`;
const result = yield 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;
}
loggers_1.default.info(`Fetched configuration for appid: ${appId}, orgid: ${orgId}`);
return result.rows[0];
}
catch (error) {
loggers_1.default.error(`Error fetching DB config: ${JSON.stringify(error, Object.getOwnPropertyNames(error))}`);
throw error;
}
});
}