-
Notifications
You must be signed in to change notification settings - Fork 92
/
connection_params.ts
61 lines (54 loc) · 1.88 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
import { env } from "deno";
import { parseDsn } from "./utils.ts";
const DEFAULT_CONNECTION_PARAMS = {
host: "127.0.0.1",
port: "5432",
user: "postgres",
database: "postgres",
password: "",
application_name: "deno_postgres"
};
export interface IConnectionParams {
database?: string;
host?: string;
port?: string;
user?: string;
password?: string;
application_name?: string;
}
export class ConnectionParams {
database?: string;
host?: string;
port?: string;
user?: string;
password?: string;
application_name?: string;
// TODO: support other params
constructor(config?: string | IConnectionParams) {
// TODO: I don't really like that we require access to environment
// by default, maybe it should be flag-controlled?
const envVars = env();
if (!config) {
config = {};
}
if (typeof config === "string") {
const dsn = parseDsn(config);
if (dsn.driver !== "postgres") {
throw new Error(`Supplied DSN has invalid driver: ${dsn.driver}.`);
}
this.database = dsn.database || envVars.PGDATABASE;
this.host = dsn.host || envVars.PGHOST;
this.port = dsn.port || envVars.PGPORT;
this.user = dsn.user || envVars.PGUSER;
this.password = dsn.password || envVars.PGPASSWORD;
this.application_name = dsn.params.application_name || envVars.PGAPPNAME;
} else {
this.database = config.database || envVars.PGDATABASE;
this.host = config.host || envVars.PGHOST;
this.port = config.port || envVars.PGPORT;
this.user = config.user || envVars.PGUSER;
this.password = config.password || envVars.PGPASSWORD;
this.application_name = config.application_name || envVars.PGAPPNAME;
}
}
}