-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
30 lines (26 loc) · 966 Bytes
/
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
var mongoose = require('mongoose');
var connectionStr = process.env.MONGO_DB_CONNECTION_STR;
if (!connectionStr) {
connectionStr = 'mongodb://127.0.0.1:27017/dataex';
}
console.log('Connection string: ' + connectionStr);
const options = {
autoIndex: false, // Don't build indexes
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0
};
var connectWithRetry = function () {
return mongoose.connect(connectionStr, options).then(
() => {
console.log('Connected to: ' + connectionStr);
},
err => {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
setTimeout(connectWithRetry, 5000);
}
);
};
connectWithRetry();