-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdefaults.ts
39 lines (36 loc) · 1.18 KB
/
defaults.ts
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
32
33
34
35
36
37
38
39
import { userInfo } from 'node:os';
/** Environment variables providing connection configuration defaults.
*
* See {@link https://www.postgresql.org/docs/current/libpq-envars.html} for a detailed description.
*/
export type Environment = keyof {
PGCLIENTENCODING: string;
PGCONNECT_TIMEOUT: string;
PGDATABASE: string;
PGHOST: string;
PGPASSWORD: string;
PGPORT: string;
PGSSLMODE: string;
PGUSER: string;
}
function secToMsec(value?: number) {
if (typeof value === 'number' && !isNaN(value)) {
return value * 1000;
}
}
export class Defaults {
constructor(
env: Partial<Record<Environment, string>>,
readonly host = env.PGHOST || 'localhost',
readonly port = parseInt(env.PGPORT as string, 10) || 5432,
readonly user = env.PGUSER || userInfo().username,
readonly database = env.PGDATABASE,
readonly password = env.PGPASSWORD,
readonly preparedStatementPrefix = 'tsp_',
readonly sslMode = env.PGSSLMODE,
readonly connectionTimeout = secToMsec(
parseInt(env.PGCONNECT_TIMEOUT as string, 10),
),
readonly clientEncoding = env.PGCLIENTENCODING,
) {}
}