forked from JawherKl/graphql-nodejs-ecom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestDatabase.js
More file actions
25 lines (21 loc) · 825 Bytes
/
testDatabase.js
File metadata and controls
25 lines (21 loc) · 825 Bytes
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
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
let mongoServer;
const connect = async () => {
if (mongoose.connection.readyState === 0) { // Check if no connection is active
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();
await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
console.log('MongoDB in-memory server connected...');
} else {
console.log('Already connected to MongoDB');
}
};
const close = async () => {
if (mongoose.connection.readyState !== 0) { // Check if a connection is active
await mongoose.disconnect();
console.log('MongoDB in-memory server disconnected...');
}
};
// module.exports = { connect, close };
module.exports = { connect, close };