-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
43 lines (36 loc) · 1.14 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
/**
* Mongoose Initialization and initial connection establishment
*/
const Mongoose = require('mongoose');
const log = require('./log').log;
const connRetriesLimit = 3;
const connRetriesInterval = 3000;
let connString = 'mongodb://';
let connRetries = 0;
Mongoose.Promise = global.Promise;
if (process.env.IDC_API_DB_USER) {
connString += `${process.env.IDC_API_DB_USER}:${process.env.IDC_API_DB_PASSWORD}@`;
}
connString += `${process.env.IDC_API_DB_HOST}:${process.env.IDC_API_DB_PORT}/idchain`;
/**
* Connect to MongoDB
*/
function connect() {
Mongoose.connect(connString)
.then(() => log.info('connection to database established'))
.catch(error => {
log.error(error);
if (connRetries < connRetriesLimit) {
connRetries++;
log.info(
`Retrying to connect to MongoDB in ${connRetriesInterval}ms` +
`- [${connRetries}/${connRetriesLimit}]`
);
setTimeout(connect, connRetriesInterval);
} else {
process.exit(1);
}
});
}
connect();
module.exports = Mongoose;