-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
45 lines (42 loc) · 1.33 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
var mongoose = require("mongoose");
var {
MongoMemoryServer
} = require("mongodb-memory-server")
const mongod = new MongoMemoryServer()
function mongoDb(usingDB) {
return mongoose.connect(usingDB, {
useNewUrlParser: true,
useUnifiedTopology: true
})
}
module.exports = {
connect: function () {
return new Promise(function (resolve, reject) {
if (process.env.NODE_ENV === "test") {
mongod.getUri().then(function (res) {
mongoDb(res).then(function () {
resolve("testing mongoose DB conncted")
}).catch(function () {
reject()
})
})
} else {
mongoose
.connect(
"mongodb://localhost:27017/test", {
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(function () {
console.log("Mongo db compass connected")
resolve("Mongo db compass connected")
}).catch(function () {
reject()
})
}
})
},
disconnect: function () {
mongoose.disconnect()
}
}