-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbinit.js
More file actions
40 lines (31 loc) · 1.05 KB
/
dbinit.js
File metadata and controls
40 lines (31 loc) · 1.05 KB
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
40
/*Note: In javascript , primitives as passed by value, and objects are passed by reference.However, changing the object's reference will not affect the passed value.*/
module.exports = {
init: _init
};
function _init(mongoose, DBURI) {
/*Connect to database*/
mongoose.connect(DBURI);
/*When successfully connected*/
mongoose.connection.on('connected', onconnect);
/*If the connection throws an error*/
mongoose.connection.on('error', onerror);
/*When the connection is disconnected*/
mongoose.connection.on('disconnected', ondisconnect);
/*If the Node process ends, close the Mongoose connection*/
process.on('SIGINT', () => { CtrlCHandler(mongoose); });
}
function onconnect() {
console.log("Connected to database");
}
function onerror(error) {
console.log(`Couldn't connect to database.\n Error : ${error}`);
}
function ondisconnect() {
console.log("Disconnected from database");
}
function CtrlCHandler(mongoose) {
mongoose.connection.close(function() {
console.log("Mongoose connection closed through app termination.");
process.exit(0);
});
}