-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
168 lines (135 loc) · 4.82 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
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
require('localenvironment')
const { EventEmitter } = require('events')
const { Pool } = require('pg')
const Utility = require('./utility')
const MustHave = require('musthave')
const mh = new MustHave()
class PostgresClient extends EventEmitter {
constructor (cfg = {}) {
super()
let conn = {}
// Make sure the DB host is identified.
if (mh.hasAny(process.env, 'DB_HOST', 'PGHOST') || cfg.host) {
conn.host = cfg.host || process.env.DB_HOST || process.env.PGHOST
} else {
conn.host = 'localhost:5432'
}
// If a port is specified, use it.
if (conn.host.indexOf(':') >= 0) {
let port = conn.host.split(':').pop()
if (typeof port !== 'number') {
port = parseInt(port, 10)
}
if (conn.port && parseInt(conn.port, 10) !== port) {
this.emit('warning', `Port ${conn.port} was explicitly configured, which overrides the port identified in the host configuration string (port ${conn.port}). Port ${conn.port} will be used to connect to ${conn.host}.`)
} else {
conn.port = port
}
conn.host = conn.host.split(':')[0]
}
// Make sure the DB name is identified.
if (mh.hasAny(process.env, 'DB', 'PGDATABASE') || cfg.database || cfg.db) {
conn.database = cfg.db || cfg.database || process.env.DB || process.env.PGDATABASE
} else {
conn.database = 'postgres'
}
// Make sure the user is specified.
if (mh.hasAny(process.env, 'DB_USER', 'PGUSER') || cfg.user) {
conn.user = cfg.user || process.env.DB_USER || process.env.PGUSER
} else {
conn.user = 'postgres'
}
// Make sure the user password is specified.
if (mh.hasAny(process.env, 'DB_PASSWORD', 'PGPASSWORD') || cfg.password) {
conn.password = cfg.password || process.env.DB_PASSWORD || process.env.PGPASSWORD
} else {
conn.password = 'postgres'
}
conn.port = conn.port || 5432
const me = this
Object.defineProperties(this, {
METADATA: Utility.privateconst({
connection: new Pool(conn),
host: conn.host,
port: conn.port,
user: conn.user,
database: conn.database,
pass: conn.password.replace(/./gi, '*')
}),
PRIVATE: Utility.privateconst({
connected: null,
createResultSet (rawResults) {
let fields = rawResults.fields.map(field => field.name)
return {
recordcount: rawResults.rowCount,
fields: rawResults.fields.slice(),
command: rawResults.command,
results: rawResults.rows.map(data => {
let record = {}
fields.forEach((field, index) => { record[field] = data[index] })
return record
})
}
},
reachable () {
if (me.PRIVATE.connected !== true) {
me.emit('connected', `Now communicating with //${me.host}:${me.port}/${me.database}.`)
}
me.PRIVATE.connected = true
},
unreachable () {
me.PRIVATE.connected = false
me.emit('warning', `Cannot communicate with //${me.host}:${me.port}/${me.database}.`)
},
monitor: null
})
})
cfg.autoconnect = cfg.hasOwnProperty('autoconnect') ? cfg.autoconnect : true
// this.METADATA.connection.on('notice', (msg) => console.warn('notice:', msg))
this.METADATA.connection.on('error', e => console.error(e.message))
}
get host () {
return this.METADATA.host
}
get port () {
return this.METADATA.port
}
get user () {
return this.METADATA.user
}
get database () {
return this.METADATA.database
}
get connectionString () {
return `postgresql://${this.user}` + (this.METADATA.pass.length > 0 ? `:${this.METADATA.pass}` : '') + `@${this.METADATA.host}:${this.METADATA.port}/${this.METADATA.database}`
}
async disconnect () {
await this.METADATA.connection.end().catch(e => {})
this.PRIVATE.connected = false
}
async query (statement = 'SELECT \'No Query Statement\';', parameters = []) {
const result = await this.METADATA.connection.query({ rowMode: 'array', text: statement }, parameters)
// await this.METADATA.connection.end()
return this.PRIVATE.createResultSet(result)
}
ping (next) {
this.query('SELECT NOW();')
.then(() => next(true))
.catch(() => next(false))
}
monitor (interval = 15000) {
if (this.PRIVATE.monitor === null) {
this.PRIVATE.monitor = setInterval(() =>
this.ping(reachable => !reachable ? this.PRIVATE.unreachable() : this.PRIVATE.reachable())
, interval)
this.ping(reachable => !reachable ? this.PRIVATE.unreachable() : this.PRIVATE.reachable())
}
}
unmonitor () {
if (this.PRIVATE.monitor !== null) {
clearTimeout(this.PRIVATE.monitor)
this.PRIVATE.monitor = null
}
}
}
module.exports = PostgresClient