-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
64 lines (57 loc) · 1.75 KB
/
index.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
const assert = require('assert');
// if DATABASE_URL Environment Variable is unset halt the server.start
assert(process.env.DATABASE_URL, 'Please set DATABASE_URL Env Variable');
const pg = require('pg');
const pkg = require('./package.json');
const PG_CON = []; // this "global" is local to the plugin.
let run_once = false;
// create a pool
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.DATABASE_SSL || false
});
const createPoolConnection = async () => {
try {
const client = await pool.connect();
PG_CON.push({ client });
} catch (err) {
assert(!err, pkg.name + ' ERROR Connecting to PostgreSQL!');
}
}
async function assign_connection (request, h) { // DRY
request.pg = await module.exports.getCon();
return h.continue;
}
const HapiPostgresConnection = {
pkg,
name: 'HapiPostgresConnection',
version: '1.0.0',
register: async function (server) {
// connection using created pool
await createPoolConnection();
server.ext({
type: 'onPreAuth',
method: async function (request, h) {
// each connection created is shut down when the server stops (e.g tests)
if(!run_once) {
run_once = true;
server.events.on('stop', function () { // only one server.on('stop') listener
PG_CON.forEach(async function (con) { // close all the connections
await con.client.end();
});
server.log(['info', pkg.name], 'DB Connection Closed');
});
}
return assign_connection(request, h);
}
});
}
};
module.exports = HapiPostgresConnection;
module.exports.getCon = async function () {
if (!PG_CON[0]) {
await createPoolConnection();
return PG_CON[0];
}
return PG_CON[0];
};