-
Notifications
You must be signed in to change notification settings - Fork 4
/
initDB.js
39 lines (32 loc) · 1.02 KB
/
initDB.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
const { connect, connection } = require('mongoose');
module.exports = () => {
connect(process.env.MONGODB_URI || "mongodb://localhost/resumeDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
connectTimeoutMS: 10000,
})
// Mongoose connection lifecycles
.then(() => {
console.log('///////////', 'Connection established with MongoDB');
})
.catch(error => {
connection.on('connected', () => {
console.log('///////////', 'Mongoose connected to DB Cluster');
})
connection.on('error', (error) => {
console.error(error.message);
})
connection.on('disconnected', () => {
console.log('///////////', 'Mongoose Disconnected');
});
});
// Kill the database connection if the app is stopped
process.on('SIGINT', function () {
connection.close(function () {
console.log('///////////', 'Mongoose disconnected on app termination');
process.exit(0);
});
});
};