forked from Sequal32/yavm-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
40 lines (34 loc) · 757 Bytes
/
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
/* eslint-disable no-undef */
var MongoClient = require('mongodb').MongoClient
var state = {
db: null,
}
exports.connect = function(url, done) {
if (state.db) return done()
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) return done(err)
state.db = db
done()
})
}
exports.get = function() {
if(process.env.NODE_ENV === 'production' && state.db !== null){
return state.db.db(process.env.DB_NAME);
} else {
try {
const db = state.db.db('yaam')
return db
} catch (error) {
return null
}
}
}
exports.close = function(done) {
if (state.db) {
state.db.close(function(err, result) {
state.db = null
state.mode = null
done(err)
})
}
}