-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.js
More file actions
37 lines (30 loc) · 867 Bytes
/
database.js
File metadata and controls
37 lines (30 loc) · 867 Bytes
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
const mongoose = require('mongoose');
const config = require('./config');
class Database {
constructor(events, config){
this.events = events;
this.config = config;
this.db = null;
this.client = null;
}
init(logger){
if (config.DB_CONNECTION == undefined) {
logger.error('DB - Unable to find MongoDB URI in DB_CONNECTION env variable!')
process.exit(1);
}
mongoose.Promise = global.Promise;
mongoose.connect(config.DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true }, err => {
if(err) {
throw err;
}
}).then(() => {
this.db = mongoose;
logger.log('info', "Connected successfully to db")
this.events.emit('db:connected', this.db);
}).catch(err => {
logger.error("DB - ", err)
process.exit(1);
});
}
}
module.exports = Database;