-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
90 lines (82 loc) · 2.78 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
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
var async = require('async')
var get = require('lodash.get')
var format = require('util').format
module.exports = function(options) {
var pg = get(options, 'pg') || require('pg')
var pool
var config
var logger
function init(dependencies, cb) {
config = dependencies.config
logger = dependencies.logger || console
cb()
}
function validate(cb) {
if (!config) return cb(new Error('config is required'))
cb()
}
function start(cb) {
logger.info(format('Connecting to %s', getConnectionUrl()))
pool = new pg.Pool(config)
pool.on('connect', client => {
/*
pg maintains the order of queued requests. Since the connect event is synchronous
we can be sure the following statements will be executed before any other queries
*/
client.on('notice', function(notice) {
switch (notice.severity) {
case 'DEBUG': {
logger.debug(notice.message)
break;
}
case 'LOG': {
logger.info(notice.message)
break;
}
case 'INFO': {
logger.info(notice.message)
break;
}
case 'NOTICE': {
logger.info(notice.message)
break;
}
case 'WARNING': {
logger.warn(notice.message)
break;
}
case 'EXCEPTION': {
logger.error(notice.message)
break;
}
default: {
logger.error(notice.message)
break;
}
}
})
async.eachSeries(config.onConnect || [], function(query, cb) {
client.query(query, function(err) {
if (err) logger.error(format('Error running query: %s', query), err)
cb()
})
})
})
pool.on('error', function(err) {
logger.warn(format('An idle client has experienced an error'), err)
})
cb(null, pool);
}
function stop(cb) {
if (!pool) return cb()
logger.info(format('Disconnecting from %s', getConnectionUrl()))
pool.end(cb)
}
function getConnectionUrl() {
return format('postgres://%s:%s/%s', config.host || 'localhost', config.port || 5432, config.database || '')
}
return {
start: async.seq(init, validate, start),
stop: stop
}
}