-
Notifications
You must be signed in to change notification settings - Fork 55
/
db.js
51 lines (39 loc) · 1.13 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var connectionString, db, mongoose, options;
mongoose = require('mongoose');
var config = require('./config/mongodb');
var port = config.port;
var db = config.db;
var host;
var is_debug = config.is_debug;
if(is_debug) {
console.log('\033[32m提醒:debug状态连接数据库:\033[39m');
host = config.host;
}else{
console.log('\033[91m警告:非debug状态连接数据库:\033[39m');
host = config.host;
}
connectionString = 'mongodb://' + host + ':' + port + '/' + db + '';
options = {
db: {
native_parser: true
},
server: {
auto_reconnect: true,
poolSize: 5
}
};
console.log(connectionString);
mongoose.connect(connectionString, options, function(err, res) {
if (err) {
console.log('[mongoose log] Error connecting to: ', +connectionString + '. ' + err);
return process.exit(1);
} else {
return console.log('[mongoose log] Successfully connected to: ', +connectionString);
}
});
db = mongoose.connection;
db.on('error', console.error.bind(console, 'mongoose connection error:'));
db.once('open', function() {
return console.log('mongoose open success');
});
module.exports = db;