-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
33 lines (26 loc) · 839 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
//only for establishing db connection with node.js
const mongoose = require('mongoose');
require('dotenv').config();
//define mongo db connection
const mongoURL = process.env.MONGODB_URL_LOCAL;
//const mongoURL = process.env.MONGODB_URL;
//set up mondodb connection
mongoose.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
//get the default connection
//moongose maintains a deafult connection object representing the mongose connection
const db = mongoose.connection;
//define event listener for database connection
db.on('connected', () =>{
console.log('Connected to Mongodb server');
});
db.on('error', (err) =>{
console.log('Mongodb connection error', err);
});
db.on('disconnected', () =>{
console.log('Mongodb disconnected');
});
//export the database connection
module.exports = db;