-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
34 lines (25 loc) · 958 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
const mongoose = require('mongoose');
require('dotenv').config();
// define the mongoDb connection URL
const mongoURL = process.env.MONGODB_URL_LOCAL // Replace 'myDatabase' with your database name
// const mongoURL = process.env.MONGODB_URL; // this is connect with mongoDb Atlas
// Set up mongoDb connection
mongoose.connect(mongoURL, {
// useNewUrlParser: true, // these two are not used in latest version of nodejs
// useUnifiedTopology:true
})
// get the default connection
// Mongoose maintains a default connection object representing the mongoDb connection.
const db = mongoose.connection;
// define EventListenerfor 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;