-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
44 lines (26 loc) · 979 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module.exports = (function () {
'use strict';
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const ON_DEATH = require('death')({ uncaughtException: true });
const { env } = require('process');
const { NODE_ENV } = env;
let db;
if (NODE_ENV === 'PRODUCTION') {
// Database access credentials in production.
const username = env.USERNAME;
const password = env.PASSWORD;
const host = env.HOST;
const port = env.PORT;
const database = env.DATABASE;
db = mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`)
};
if (NODE_ENV === 'DEVELOPMENT' || NODE_ENV === 'TEST') {
const devDB = env.DEV_DB
db = mongoose.connect(`mongodb://localhost/${devDB}`);
};
// Gracefully cleaning up when exiting..
const cleanUp = (signal, err) => db.disconnect();
// The DEATH package helps in dealing with exceptions and termination signals.
ON_DEATH(cleanUp);
})();