-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
144 lines (115 loc) · 3.38 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
var mongoose = require('mongoose');
var muri = require('muri');
var rgxReplSet = /^.+,.+$/;
var log = require('debug')('democracyos:db');
exports.getDefaultConnection = function getDefaultConnection () {
return mongoose.connection;
}
exports.createConnection = function createConnection (mongoUri) {
return performConnection(mongoose.createConnection(), buildOpts(muri(mongoUri)));
}
exports.connect = function connect (mongoUri) {
return performConnection(mongoose.connection, buildOpts(muri(mongoUri)));;
}
function performConnection (connection, opts) {
if (/mongoose|^\*$/.test(process.env.DEBUG)) {
mongoose.set('debug', true);
}
var dbOptions = {
db: {
journal: true,
retryMiliSeconds: 5000,
numberOfRetries: 1000000,
read_preference: 'secondaryPreferred'
},
auto_reconnect: true,
server: {
poolSize: parseInt(process.env.MONGO_POOL_SIZE || 5, 10),
auto_reconnect: true,
socketOptions: {
connectTimeoutMS: 20000,
keepAlive: 1
}
}
};
var uri = [];
if (opts.hosts) {
var nodes = opts.hosts.split(',');
log('Using mongodb hosts: %s', nodes);
for (var index = 0; index < nodes.length; index++) {
uri.push('mongodb://' + nodes[index]);
if (index === 0) {
uri.push('/');
uri.push(opts.dbname);
}
if (index + 1 < nodes.length) {
uri.push(',');
}
}
uri = uri.join('');
log('Using mongodb URI: %s', uri);
// TODO: according to docs, this defaults to '1'
// dbOptions.db.w = 'majority';
dbOptions.server.slaveOk = true;
dbOptions.replset = {
haInterval: 500,
reconnectWait: 5000,
poolSize: parseInt(process.env.MONGO_POOL_SIZE || 5, 10),
retries: 10000000,
readPreference: 'secondaryPreferred',
rs_name: opts.replSetName || 'rs0',
slaveOk: true,
socketOptions: {
connectTimeoutMS: 20000,
keepAlive: 1
}
};
} else {
uri = 'mongodb://' + process.env.MONGO_HOST + ':' +
(process.env.MONGO_PORT || 27017) + '/' + opts.dbname;
}
if (opts.mongoUser && opts.mongoPassword) {
dbOptions.user = opts.mongoUser;
dbOptions.pass = opts.mongoPassword;
}
connection.on('error', function(err) {
log('mongo %s error: %s', uri, err);
log(err);
});
connection.on('reconnect', function() {
log('Recconecting to MongoDB on URI: %s', uri);
});
connection.on('connected', function() {
log('Connected to MongoDB on URI: %s', uri);
});
connection.on('connecting', function() {
log('Connecting to MongoDB database %s', opts.dbname);
});
connection.on('disconnected', function(err) {
log('Discnnected from MongoDB on URI: %s - Error: %s', uri, err);
});
connection.on('open', function() {
log('Connection to MongoDB is now OPEN on URI: %s', uri);
});
if (isReplSet(uri)){
connection.openSet(uri, dbOptions);
} else {
connection.open(uri, dbOptions);
}
return connection;
}
function isReplSet(mongoUri) {
return rgxReplSet.test(mongoUri);
}
function buildOpts(uriObj) {
return {
hosts: uriObj.hosts.map(serialize).join(','),
dbname: uriObj.db,
replSetName: uriObj.options.replicaSet,
mongoUser: uriObj.auth ? uriObj.auth.user : undefined,
mongoPassword: uriObj.auth ? uriObj.auth.pass : undefined,
};
}
function serialize(conn) {
return conn.host + ':' + conn.port;
}