-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (33 loc) · 1001 Bytes
/
index.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
const get = require('lodash.get');
const { format } = require('util');
const { MongoMemoryServer } = require('mongodb-memory-server');
module.exports = options => {
const MongoClient = get(options, 'MongoClient') || require('mongodb'); // eslint-disable-line global-require
let mongod;
let db;
let config;
let logger;
let url;
const start = async dependencies => {
config = dependencies.config;
logger = dependencies.logger || console;
mongod = new MongoMemoryServer(config.mongodbMemoryServer || {});
url = await mongod.getUri();
logger.info(format('Connecting to temporal server on %s', url));
db = await MongoClient.connect(url, config.options || { useUnifiedTopology: true });
return db;
};
// eslint-disable-next-line consistent-return
const stop = async cb => {
if (!db) {
return cb();
}
logger.info(format('Disconnecting from %s', url));
db.close();
await mongod.stop();
};
return {
start,
stop,
};
};