-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
60 lines (48 loc) · 1.32 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { MongoClient } = require('mongodb');
module.exports = (options) => {
const MongoClientInstance = options && options.MongoClient ? options.MongoClient : MongoClient;
let client;
let config;
let logger;
const start = async (dependencies) => {
config = { ...dependencies.config };
logger = ({ ...dependencies.logger }) || console;
if (!dependencies.logger) {
logger.info = console.log;
}
if (!config || !config.url) {
throw new Error('config.url is required');
}
if (!config.options) {
config.options = {};
}
const mongoOptions = { ...config.options };
if( config.showConnectionString ){
logger.info(`Connecting to ${config.url}`);
}else{
logger.info(`Connecting to server <hidden connection url>`);
}
try {
client = await MongoClientInstance.connect(config.url, mongoOptions );
return client;
} catch (error) {
throw new Error(error);
}
};
const stop = async () => {
if (!client) {
logger.info('No database has been opened.');
return;
}
if (config.showConnectionString) {
logger.info(`Disconnecting from ${config.url}`);
} else {
logger.info(`Disconnecting from server <hidden connection url>`);
}
await client.close();
};
return {
start,
stop,
};
};