-
Notifications
You must be signed in to change notification settings - Fork 92
/
connection_params.ts
120 lines (104 loc) · 2.81 KB
/
connection_params.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { parseDsn } from "./utils.ts";
function getPgEnv(): IConnectionParams {
try {
const env = Deno.env();
return {
database: env.PGDATABASE,
host: env.PGHOST,
port: env.PGPORT,
user: env.PGUSER,
password: env.PGPASSWORD,
application_name: env.PGAPPNAME,
};
} catch (e) {
// PermissionDenied (--allow-env not passed)
return {};
}
}
function selectFrom(
sources: Array<IConnectionParams>,
key: keyof IConnectionParams,
): string | undefined {
for (const source of sources) {
if (source[key]) {
return source[key];
}
}
return undefined;
}
function selectFromWithDefault(
sources: Array<IConnectionParams>,
key: keyof typeof DEFAULT_CONNECTION_PARAMS,
): string {
return selectFrom(sources, key) || DEFAULT_CONNECTION_PARAMS[key];
}
const DEFAULT_CONNECTION_PARAMS = {
host: "127.0.0.1",
port: "5432",
application_name: "deno_postgres",
};
export interface IConnectionParams {
database?: string;
host?: string;
port?: string;
user?: string;
password?: string;
application_name?: string;
}
class ConnectionParamsError extends Error {
constructor(message: string) {
super(message);
this.name = "ConnectionParamsError";
}
}
export class ConnectionParams {
database!: string;
host: string;
port: string;
user!: string;
password?: string;
application_name: string;
// TODO: support other params
constructor(config?: string | IConnectionParams) {
if (!config) {
config = {};
}
const pgEnv = getPgEnv();
if (typeof config === "string") {
const dsn = parseDsn(config);
if (dsn.driver !== "postgres") {
throw new Error(`Supplied DSN has invalid driver: ${dsn.driver}.`);
}
config = dsn;
}
let potentiallyNull: { [K in keyof IConnectionParams]?: string } = {
database: selectFrom([config, pgEnv], "database"),
user: selectFrom([config, pgEnv], "user"),
};
this.host = selectFromWithDefault([config, pgEnv], "host");
this.port = selectFromWithDefault([config, pgEnv], "port");
this.application_name = selectFromWithDefault(
[config, pgEnv],
"application_name",
);
this.password = selectFrom([config, pgEnv], "password");
const missingParams: string[] = [];
(["database", "user"] as Array<keyof IConnectionParams>).forEach(
(param) => {
if (potentiallyNull[param]) {
this[param] = potentiallyNull[param]!;
} else {
missingParams.push(param);
}
},
);
if (missingParams.length) {
throw new ConnectionParamsError(
`Missing connection parameters: ${missingParams.join(
", ",
)}. Connection parameters can be read
from environment only if Deno is run with env permission (deno run --allow-env)`,
);
}
}
}