/ database.js
database.js
1 const mongoose = require('mongoose'); 2 const config = require('./config'); 3 4 class Database { 5 6 constructor(events, config){ 7 this.events = events; 8 this.config = config; 9 10 this.db = null; 11 this.client = null; 12 } 13 14 init(logger){ 15 if (config.DB_CONNECTION == undefined) { 16 logger.error('DB - Unable to find MongoDB URI in DB_CONNECTION env variable!') 17 process.exit(1); 18 } 19 20 mongoose.Promise = global.Promise; 21 22 mongoose.connect(config.DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true }, err => { 23 if(err) { 24 throw err; 25 } 26 }).then(() => { 27 this.db = mongoose; 28 logger.log('info', "Connected successfully to db") 29 this.events.emit('db:connected', this.db); 30 }).catch(err => { 31 logger.error("DB - ", err) 32 process.exit(1); 33 }); 34 } 35 } 36 37 module.exports = Database;