-
Notifications
You must be signed in to change notification settings - Fork 92
/
connection_params.ts
167 lines (143 loc) · 3.99 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { parseDsn } from "./utils.ts";
function getPgEnv(): ConnectionOptions {
try {
const env = Deno.env;
const port = env.get("PGPORT");
return {
database: env.get("PGDATABASE"),
hostname: env.get("PGHOST"),
port: port ? parseInt(port, 10) : undefined,
user: env.get("PGUSER"),
password: env.get("PGPASSWORD"),
applicationName: env.get("PGAPPNAME"),
};
} catch (e) {
// PermissionDenied (--allow-env not passed)
return {};
}
}
function isDefined<T>(value: T): value is NonNullable<T> {
return value !== undefined && value !== null;
}
export class ConnectionParamsError extends Error {
constructor(message: string) {
super(message);
this.name = "ConnectionParamsError";
}
}
export interface ConnectionOptions {
database?: string;
hostname?: string;
port?: number;
user?: string;
password?: string;
applicationName?: string;
}
export interface ConnectionParams {
database: string;
hostname: string;
port: number;
user: string;
password?: string;
applicationName: string;
// TODO: support other params
}
function select<T extends keyof ConnectionOptions>(
sources: ConnectionOptions[],
key: T,
): ConnectionOptions[T] {
return sources.map((s) => s[key]).find(isDefined);
}
function selectRequired<T extends keyof ConnectionOptions>(
sources: ConnectionOptions[],
key: T,
): NonNullable<ConnectionOptions[T]> {
const result = select(sources, key);
if (!isDefined(result)) {
throw new ConnectionParamsError(`Required parameter ${key} not provided`);
}
return result;
}
function assertRequiredOptions(
sources: ConnectionOptions[],
requiredKeys: (keyof ConnectionOptions)[],
) {
const missingParams: (keyof ConnectionOptions)[] = [];
for (const key of requiredKeys) {
if (!isDefined(select(sources, key))) {
missingParams.push(key);
}
}
if (missingParams.length) {
// deno-lint-ignore camelcase
const missing_params_message = formatMissingParams(missingParams);
// deno-lint-ignore camelcase
let permission_error_thrown = false;
try {
Deno.env.toObject();
} catch (e) {
if (e instanceof Deno.errors.PermissionDenied) {
permission_error_thrown = true;
} else {
throw e;
}
}
if (permission_error_thrown) {
throw new ConnectionParamsError(
missing_params_message +
"\nConnection parameters can be read from environment only if Deno is run with env permission",
);
} else {
throw new ConnectionParamsError(missing_params_message);
}
}
}
function formatMissingParams(missingParams: string[]) {
return `Missing connection parameters: ${
missingParams.join(
", ",
)
}`;
}
const DEFAULT_OPTIONS: ConnectionOptions = {
hostname: "127.0.0.1",
port: 5432,
applicationName: "deno_postgres",
};
function parseOptionsFromDsn(connString: string): ConnectionOptions {
const dsn = parseDsn(connString);
if (dsn.driver !== "postgres") {
throw new Error(`Supplied DSN has invalid driver: ${dsn.driver}.`);
}
return {
...dsn,
port: dsn.port ? parseInt(dsn.port, 10) : undefined,
applicationName: dsn.params.application_name,
};
}
export function createParams(
config: string | ConnectionOptions = {},
): ConnectionParams {
if (typeof config === "string") {
const dsn = parseOptionsFromDsn(config);
return createParams(dsn);
}
const pgEnv = getPgEnv();
const sources = [config, pgEnv, DEFAULT_OPTIONS];
assertRequiredOptions(
sources,
["database", "hostname", "port", "user", "applicationName"],
);
const params = {
database: selectRequired(sources, "database"),
hostname: selectRequired(sources, "hostname"),
port: selectRequired(sources, "port"),
applicationName: selectRequired(sources, "applicationName"),
user: selectRequired(sources, "user"),
password: select(sources, "password"),
};
if (isNaN(params.port)) {
throw new ConnectionParamsError(`Invalid port ${params.port}`);
}
return params;
}