forked from Axolotl-Beats-52/axolotl-beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
32 lines (26 loc) · 785 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
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
let mongo = undefined;
module.exports.setUp = async () => {
mongo = await MongoMemoryServer.create();
const url = mongo.getUri();
await mongoose.connect(url, {
useNewUrlParser: true,
});
};
module.exports.dropDatabase = async () => {
if (mongo) {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await mongo.stop();
}
};
module.exports.dropCollections = async () => {
if (mongo) {
const collections = mongoose.connection.collections;
for (const key in collections) {
const collection = collections[key];
await collection.deleteMany();
}
}
};