-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathPostgresConfigParser.js
93 lines (81 loc) · 2.98 KB
/
PostgresConfigParser.js
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
const fs = require('fs');
function getDatabaseOptionsFromURI(uri) {
const databaseOptions = {};
const parsedURI = new URL(uri);
const queryParams = parseQueryParams(parsedURI.searchParams.toString());
databaseOptions.host = parsedURI.hostname || 'localhost';
databaseOptions.port = parsedURI.port ? parseInt(parsedURI.port) : 5432;
databaseOptions.database = parsedURI.pathname ? parsedURI.pathname.substr(1) : undefined;
databaseOptions.user = parsedURI.username;
databaseOptions.password = parsedURI.password;
if (queryParams.ssl && queryParams.ssl.toLowerCase() === 'true') {
databaseOptions.ssl = true;
}
if (
queryParams.ca ||
queryParams.pfx ||
queryParams.cert ||
queryParams.key ||
queryParams.passphrase ||
queryParams.rejectUnauthorized ||
queryParams.secureOptions
) {
databaseOptions.ssl = {};
if (queryParams.ca) {
databaseOptions.ssl.ca = fs.readFileSync(queryParams.ca).toString();
}
if (queryParams.pfx) {
databaseOptions.ssl.pfx = fs.readFileSync(queryParams.pfx).toString();
}
if (queryParams.cert) {
databaseOptions.ssl.cert = fs.readFileSync(queryParams.cert).toString();
}
if (queryParams.key) {
databaseOptions.ssl.key = fs.readFileSync(queryParams.key).toString();
}
if (queryParams.passphrase) {
databaseOptions.ssl.passphrase = queryParams.passphrase;
}
if (queryParams.rejectUnauthorized) {
databaseOptions.ssl.rejectUnauthorized =
queryParams.rejectUnauthorized.toLowerCase() === 'true' ? true : false;
}
if (queryParams.secureOptions) {
databaseOptions.ssl.secureOptions = parseInt(queryParams.secureOptions);
}
}
databaseOptions.binary =
queryParams.binary && queryParams.binary.toLowerCase() === 'true' ? true : false;
databaseOptions.client_encoding = queryParams.client_encoding;
databaseOptions.application_name = queryParams.application_name;
databaseOptions.fallback_application_name = queryParams.fallback_application_name;
if (queryParams.poolSize) {
databaseOptions.max = parseInt(queryParams.poolSize) || 10;
}
if (queryParams.max) {
databaseOptions.max = parseInt(queryParams.max) || 10;
}
if (queryParams.query_timeout) {
databaseOptions.query_timeout = parseInt(queryParams.query_timeout);
}
if (queryParams.idleTimeoutMillis) {
databaseOptions.idleTimeoutMillis = parseInt(queryParams.idleTimeoutMillis);
}
if (queryParams.keepAlive) {
databaseOptions.keepAlive = queryParams.keepAlive.toLowerCase() === 'true' ? true : false;
}
return databaseOptions;
}
function parseQueryParams(queryString) {
queryString = queryString || '';
return queryString.split('&').reduce((p, c) => {
const parts = c.split('=');
p[decodeURIComponent(parts[0])] =
parts.length > 1 ? decodeURIComponent(parts.slice(1).join('=')) : '';
return p;
}, {});
}
module.exports = {
parseQueryParams: parseQueryParams,
getDatabaseOptionsFromURI: getDatabaseOptionsFromURI,
};