-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
executable file
·56 lines (41 loc) · 1.07 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
52
53
54
55
56
'use strict'
const mongoose = require('mongoose')
var connectionString, options
const env = process.env.NODE_ENV || "development";
const config = $config.database[env]
const port = config.port
var db = config.db
var host
const isDebug = config.is_debug
if (isDebug) {
log('提醒:debug状态连接数据库:')
host = config.host
} else {
log('警告:非debug状态连接数据库:')
host = config.host
}
connectionString = 'mongodb://' + host + ':' + port + '/' + db + ''
options = {
db: {
native_parser: true
},
server: {
auto_reconnect: true,
poolSize: 5
}
}
log(connectionString)
mongoose.connect(connectionString, options, function (err, res) {
if (err) {
log('[mongoose log] Error connecting to: ', +connectionString + '. ' + err)
return process.exit(1)
} else {
return 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 log('mongoose open success')
})
module.exports = db