-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnections.js
More file actions
31 lines (27 loc) · 881 Bytes
/
Copy pathconnections.js
File metadata and controls
31 lines (27 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { Pool } = require("pg");
const pools = {};
const getConnection = async (connStr) => {
if (!connStr) return null;
const connectionString =
typeof connStr === "object" ? getConnStr(connStr) : connStr;
if (!pools[connectionString]) {
const poolOpt = { connectionString };
if (connStr.ssl) {
poolOpt.ssl = {
ca: poolOpt.ca || undefined,
key: poolOpt.key || undefined,
cert: poolOpt.cert || undefined,
};
}
pools[connectionString] = new Pool(poolOpt);
}
return pools[connectionString];
};
const getConnStr = ({ host, user, password, port, database }) => {
if (!password)
return `postgresql://${user}:${
process.env[`SC_EXTPG_PASS_${database}`]
}@${host}:${port}/${database}`;
else return `postgresql://${user}:${password}@${host}:${port}/${database}`;
};
module.exports = { getConnection };