UNPKG

@js-ak/db-manager

Version:
190 lines (189 loc) 6.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.shutdown = exports.removeTransactionPool = exports.removeStandardPool = exports.getTransactionPool = exports.getStandardPool = exports.createClient = void 0; const pg_1 = __importDefault(require("pg")); const pools = new Map(); /** * Creates a new PostgreSQL client instance. * * @param config - The connection configuration as a connection string or a client configuration object. * * @returns The PostgreSQL client instance. */ const createClient = (config) => { return new pg_1.default.Client(config); }; exports.createClient = createClient; /** * Retrieves or creates a standard connection pool. * * @param config - The database credentials configuration object. * @param [poolName="shared"] - The name suffix for the pool, default is "shared". * * @returns The retrieved or newly created standard connection pool. */ const getStandardPool = (config, poolName = "shared") => { const poolNameResult = "st-" + poolName; return getPool(config, poolNameResult); }; exports.getStandardPool = getStandardPool; /** * Retrieves or creates a transaction-specific connection pool. * * @param config - The database credentials configuration object. * @param [poolName="shared"] - The name suffix for the pool, default is "shared". * * @returns The retrieved or newly created transaction connection pool. */ const getTransactionPool = (config, poolName = "shared") => { const poolNameResult = "tr-" + poolName; return getPool(config, poolNameResult); }; exports.getTransactionPool = getTransactionPool; /** * Removes and closes a standard connection pool. * * @param config - The database credentials configuration object. * @param [poolName="shared"] - The name suffix for the pool, default is "shared". * * @returns A promise that resolves when the pool is closed. */ const removeStandardPool = async (config, poolName = "shared") => { const poolNameResult = "st-" + poolName; return removePool(config, poolNameResult); }; exports.removeStandardPool = removeStandardPool; /** * Removes and closes a transaction-specific connection pool. * * @param config - The database credentials configuration object. * @param [poolName="shared"] - The name suffix for the pool, default is "shared". * * @returns A promise that resolves when the pool is closed. */ const removeTransactionPool = async (config, poolName = "shared") => { const poolNameResult = "tr-" + poolName; return removePool(config, poolNameResult); }; exports.removeTransactionPool = removeTransactionPool; /** * Gracefully shuts down all active connection pools. * * @param options - The options object. * @param options.poolName - The pool name to match. * * @returns A promise that resolves when all pools are closed. */ const shutdown = async (options) => { const poolShutdowns = []; const poolNames = []; const poolName = options?.poolName; if (poolName) { for (const entry of pools) { const credsString = entry[0]; const pool = entry[1]; if (credsString !== `st-${poolName}` && credsString !== `tr-${poolName}`) { continue; } poolShutdowns.push(pool.end()); poolNames.push(credsString); } } else { for (const entry of pools) { const credsString = entry[0]; const pool = entry[1]; poolShutdowns.push(pool.end()); poolNames.push(credsString); } } if (poolShutdowns.length === 0) { return; } for (const poolName of poolNames) { pools.delete(poolName); } await Promise.all(poolShutdowns); }; exports.shutdown = shutdown; /** * Creates a connection credentials string. * This string is used as a key in the pools map to identify different pools. * * @param poolName - The name prefix for the pool. * @param creds - The credentials object. * @param creds.user - The database user. * @param creds.password - The database password. * @param creds.host - The database host. * @param creds.port - The database port. * @param creds.database - The database name. * * @returns The constructed credentials string. */ const createCredsString = (poolName, creds) => { return `${poolName}#${creds.user}:${creds.password}@${creds.host}:${creds.port}/${creds.database}`; }; /** * Masks sensitive information in a connection string. * * @param credsString - The connection credentials string. * * @returns The credentials string with sensitive information masked. */ const _maskSensitiveInfo = (credsString) => { return credsString.replace(/:.+@/, ":<hidden>@"); }; /** * Retrieves or creates a named connection pool. * * @param config - The database credentials configuration object. * @param poolName - The name suffix for the pool. * * @returns The retrieved or newly created standard connection pool. */ const getPool = (config, poolName) => { const { database, host, password, port, user } = config; const credsString = createCredsString(poolName, { database, host, password, port, user, }); const poolCandidate = pools.get(credsString); if (poolCandidate) { return poolCandidate; } else { const pool = new pg_1.default.Pool(config); pools.set(credsString, pool); return pool; } }; /** * Removes and closes a named connection pool. * * @param config - The database credentials configuration object. * @param poolName - The name suffix for the pool. * * @returns A promise that resolves when the pool is closed. */ const removePool = async (config, poolName) => { const { database, host, password, port, user } = config; const credsString = createCredsString(poolName, { database, host, password, port, user, }); const pool = pools.get(credsString); if (pool) { pools.delete(credsString); await pool.end(); } };