-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresql.ts
223 lines (188 loc) · 5.89 KB
/
postgresql.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as url from 'url';
import * as fs from 'fs';
// import pg from 'pg';
// import type { Pool } from 'pg';
import knex from 'knex';
import * as dotenv from 'dotenv';
dotenv.config();
//Parse method copied from https://github.com/brianc/node-postgres
//Copyright (c) 2010-2014 Brian Carlson ([email protected])
//MIT License
//parses a connection string
function parse(str) {
//unix socket
if (str.charAt(0) === '/') {
var config = str.split(' ')
return { host: config[0], database: config[1] }
}
// url parse expects spaces encoded as %20
var result = url.parse(
/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str) ? encodeURI(str).replace(/\%25(\d\d)/g, '%$1') : str,
true
)
var config: any = result.query
for (var k in config) {
if (Array.isArray(config[k])) {
config[k] = config[k][config[k].length - 1]
}
}
var auth = (result.auth || ':').split(':')
config.user = auth[0]
config.password = auth.splice(1).join(':')
config.port = result.port
if (result.protocol == 'socket:') {
config.host = decodeURI(result.pathname)
config.database = result.query.db
config.client_encoding = result.query.encoding
return config
}
if (!config.host) {
// Only set the host if there is no equivalent query param.
config.host = result.hostname
}
// If the host is missing it might be a URL-encoded path to a socket.
var pathname = result.pathname
if (!config.host && pathname && /^%2f/i.test(pathname)) {
var pathnameSplit = pathname.split('/')
config.host = decodeURIComponent(pathnameSplit[0])
pathname = pathnameSplit.splice(1).join('/')
}
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
// only strip the slash if it is present.
if (pathname && pathname.charAt(0) === '/') {
pathname = pathname.slice(1) || null
}
config.database = pathname && decodeURI(pathname)
if (config.ssl === 'true' || config.ssl === '1') {
config.ssl = true
}
if (config.ssl === '0') {
config.ssl = false
}
if (config.sslcert || config.sslkey || config.sslrootcert || config.sslmode) {
config.ssl = {}
}
if (config.sslcert) {
config.ssl.cert = fs.readFileSync(config.sslcert).toString()
}
if (config.sslkey) {
config.ssl.key = fs.readFileSync(config.sslkey).toString()
}
if (config.sslrootcert) {
config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
}
switch (config.sslmode) {
case 'disable': {
config.ssl = false
break
}
case 'prefer':
case 'require':
case 'verify-ca':
case 'verify-full': {
break
}
case 'no-verify': {
config.ssl.rejectUnauthorized = false
break
}
}
return config
}
// const parse = PgConnStr.default;
type PgUrl = ReturnType<typeof parse>;
// console.log(PgConnStr.default);
// console.log(PgConnStr.parse);
export function requireVar(name: string): string {
const envVar = process.env[name] || null;
if (!envVar) {
throw Error(`ENV var ${name} must be defined`);
} else if (envVar === '') {
throw Error(`ENV var ${name} was defined, but was empty`);
}
return envVar;
}
export const ENVS = {
development: 'development',
preview : 'preview',
production : 'production'
};
export type Env = keyof typeof ENVS;
export const mode = (process.env.NODE_ENV || 'development').toLowerCase() as Env;
// export const pool: Pool = new pg.Pool(
// {
// connectionString: requireVar('DATABASE_URL'),
// ssl: mode == 'development' ? false : { rejectUnauthorized : false } ,
// options: '-c TimeZone=utc',
// }
// );
type ConnectionConfig = {
host : string,
port : number,
user : string,
password: string,
database: string,
ssl : boolean,
charset : string,
timezone: string,
createRetryIntervalMillis: number
}
declare type Maybe<T> = T | null | undefined;
const dbAndEnv = process.env.DB_NAME ? `${process.env.DB_NAME}_${mode}` : void(0);
const dbHost = (urlConfig: Maybe<PgUrl>) => (urlConfig?.host || process.env.DB_HOST || '127.0.0.1').toLowerCase();
const databaseUrl = requireVar('DATABASE_URL');
// console.log(parse(databaseUrl));
// env.DATABASE_URL takes precedence.
export function connectionConfig(): ConnectionConfig {
let urlConfig;
// console.log('the database url is', databaseUrl);
// Heroku PG uses a pg connection string - parse it and only enforce SSL in heroku hosted environments
if(databaseUrl) {
urlConfig = parse(databaseUrl) as any;
const isLocalDB = [ 'localhost', '127.0.0.1', '0.0.0.0' ].includes(dbHost(urlConfig));
urlConfig.ssl = isLocalDB ? false : { rejectUnauthorized: false } ;
}
const connection = {
host : dbHost(urlConfig),
port : Number(urlConfig?.port || process.env.DB_PORT),
user : urlConfig?.user || process.env.DB_USER || dbAndEnv,
password: urlConfig?.password || process.env.DB_PASS || '',
database: urlConfig?.database || dbAndEnv,
ssl : urlConfig?.ssl || false,
charset : 'utf8',
timezone: 'utc',
createRetryIntervalMillis: 200
};
return connection;
}
export const dbConfig = {
debug: false, //env === ENVS.development,
client: 'pg',
pool: {
min: 0,
max: 10,
afterCreate: function(connection: any, callback: any) {
connection.query('set timezone=\'utc\';', function(err: any) {
callback(err, connection);
});
}
},
connection: connectionConfig()
};
export const db = knex(dbConfig);
export const cleanup = async function() {
await db.destroy();
};
export function wrapPromiseMain(m: NodeModule, entryPoint: any): void {
if (require.main !== m) {
// console.log('⏭ not main, skipping wrapPromiseMain exec', m.id);
return;
}
const pollTime = 1000000;
const interval = setInterval(() => { 0; }, pollTime);
return entryPoint().then(async () => {
clearInterval(interval);
console.log('cleaning up!');
await cleanup();
});
}