pg-cache
Version:
PostgreSQL connection pool LRU cache manager
25 lines (24 loc) • 1.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPgPool = void 0;
const pg_1 = __importDefault(require("pg"));
const pg_env_1 = require("pg-env");
const lru_1 = require("./lru");
const getDbString = (user, password, host, port, database) => `postgres://${user}:${password}@${host}:${port}/${database}`;
const getPgPool = (pgConfig) => {
const config = (0, pg_env_1.getPgEnvOptions)(pgConfig);
const { user, password, host, port, database, } = config;
if (lru_1.pgCache.has(database)) {
const cached = lru_1.pgCache.get(database);
if (cached)
return cached;
}
const connectionString = getDbString(user, password, host, port, database);
const pgPool = new pg_1.default.Pool({ connectionString });
lru_1.pgCache.set(database, pgPool);
return pgPool;
};
exports.getPgPool = getPgPool;