-
Notifications
You must be signed in to change notification settings - Fork 3
/
spawnpoint-rethinkdb.js
65 lines (62 loc) · 1.81 KB
/
spawnpoint-rethinkdb.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
'use strict';
const fs = require('fs');
const _ = require('lodash'),
rethinkdbdash = require('rethinkdbdash');
const errors = require('rethinkdbdash/lib/error');
module.exports = require('spawnpoint').registerPlugin({
dir: __dirname,
name: "RethinkDB",
namespace: "rethinkdb",
errors: {
'rethinkdb.driver_error': errors.ReqlDriverError,
'rethinkdb.server_error': errors.ReqlServerError,
'rethinkdb.runtime_error': errors.ReqlRuntimeError,
'rethinkdb.compile_error': errors.ReqlCompileError,
'rethinkdb.client_error': errors.ReqlClientError
},
callback: true,
exports: function(app, initCallback){
// fetch SSL files, because JSON + keys != fun
if(app.config.rethinkdb.servers){
app.config.rethinkdb.servers.map(function(server){
if(server.ssl_files && typeof(server.ssl_files) === 'object'){
if(!server.ssl){
server.ssl = {};
}
_.each(server.ssl_files, function(file, key){
server.ssl[key] = fs.readFileSync(file, 'utf8');
});
}
return server;
});
}
app.r = rethinkdbdash(app.config.rethinkdb);
if(app.config.rethinkdb.pool !== false){
let initConnection = false;
const master = app.r.getPoolMaster();
master.on('log', console.log);
master.on('healthy', function(active){
if(!initConnection && !active){
if(app.config.rethinkdb.requireConnection){
return initCallback(app.errorCode('rethinkdb.failed_to_connect'));
}
}
});
master.on('available-size', function(size){
if(!initConnection && size > 0){
initConnection = true;
if(app.config.rethinkdb.requireConnection){
return initCallback();
}
}
});
// allowing reconnection is always an option
if(!app.config.rethinkdb.requireConnection){
return initCallback();
}
}else{
// no guarantee can be made?
return initCallback();
}
}
});