-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
53 lines (39 loc) · 1.35 KB
/
connect.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const glob = require('glob');
const mongoose = require('mongoose');
const config = require('./config');
mongoose.Promise = global.Promise;
const db = mongoose.connection;
// If the connection throws an error
db.on('error', function () {
OUT.error(`unable to connect to database at ${config.db}`);
throw new Error('unable to connect to database at ' + config.db);
});
// When the connection is disconnected
db.on('disconnected', function () {
OUT.info('Mongoose default connection to DB :' + config.db + ' disconnected');
});
const gracefulExit = function() {
db.close(function () {
console.log('Mongoose default connection with DB :' + config.mongodb + ' is disconnected through app termination');
process.exit(0);
});
};
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
try {
OUT.info(`connet to mongodb : ${config.db}`);
mongoose.connect(config.db, {
auto_reconnect: true,
poolSize: 10,
native_parser: true,
replset: {socketOptions: {keepAlive: 1}},
server: {socketOptions: {keepAlive: 1}}
});
} catch (err) {
OUT.info("Sever initialization failed " , err.message);
}
const models = glob.sync(config.root + '/models/*.js');
models.forEach(function (model) {
require(model);
});
module.exports = db;