@js-ak/db-manager
Version:
178 lines (177 loc) • 5.58 kB
JavaScript
import pg from "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.
*/
export const createClient = (config) => {
return new pg.Client(config);
};
/**
* 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.
*/
export const getStandardPool = (config, poolName = "shared") => {
const poolNameResult = "st-" + poolName;
return getPool(config, poolNameResult);
};
/**
* 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.
*/
export const getTransactionPool = (config, poolName = "shared") => {
const poolNameResult = "tr-" + poolName;
return getPool(config, poolNameResult);
};
/**
* 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.
*/
export const removeStandardPool = async (config, poolName = "shared") => {
const poolNameResult = "st-" + poolName;
return removePool(config, poolNameResult);
};
/**
* 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.
*/
export const removeTransactionPool = async (config, poolName = "shared") => {
const poolNameResult = "tr-" + poolName;
return removePool(config, poolNameResult);
};
/**
* 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.
*/
export 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);
};
/**
* 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.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();
}
};