-
Notifications
You must be signed in to change notification settings - Fork 13
/
db.js
57 lines (48 loc) · 1.33 KB
/
db.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
'use strict';
const Thinky = require('thinky');
const config = require('config');
const debug = require('./util/debug');
const thinky = Thinky({
servers: config.dbServers,
db: config.dbName,
user: config.dbUser,
password: config.dbPassword,
silent: true
});
thinky.r.getPoolMaster().on('healthy', healthy => {
if (healthy) {
debug.db('RethinkDB database pool is healthy.');
} else {
debug.db('RethinkDB database pool is not healthy. Is the database up?');
}
});
let droppingMsgs, lastMsg;
thinky.r.getPoolMaster().on('log', msg => {
if (lastMsg !== msg) {
debug.db(msg);
droppingMsgs = false;
} else {
if (!droppingMsgs)
debug.db('Additional identical message(s) received, ignoring.');
droppingMsgs = true;
}
lastMsg = msg;
});
thinky.r.getPoolMaster()._flushErrors = () => {
// Overriding default behavior in driver -- pool master does not gracefully
// handle inability to (re-)connect and spews errors
};
thinky.getDB = () => {
let connected = false;
return new Promise(resolve => {
debug.db('Waiting for database connection.');
thinky.r.getPoolMaster().on('available-size', size => {
if (size === 1 && !connected) {
connected = true;
debug.db('Connection to RethinkDB established.');
resolve(thinky);
}
});
});
};
module.exports = thinky;