UNPKG

pg-cache

Version:

PostgreSQL connection pool LRU cache manager

18 lines (17 loc) 692 B
import pg from 'pg'; import { getPgEnvOptions } from 'pg-env'; import { pgCache } from './lru'; const getDbString = (user, password, host, port, database) => `postgres://${user}:${password}@${host}:${port}/${database}`; export const getPgPool = (pgConfig) => { const config = getPgEnvOptions(pgConfig); const { user, password, host, port, database, } = config; if (pgCache.has(database)) { const cached = pgCache.get(database); if (cached) return cached; } const connectionString = getDbString(user, password, host, port, database); const pgPool = new pg.Pool({ connectionString }); pgCache.set(database, pgPool); return pgPool; };