1 | 'use strict';
|
2 |
|
3 | const mongo = require('mongodb');
|
4 |
|
5 | const getMongoUri = ({
|
6 | url, servers, host, port, username, password, authSource,
|
7 | }) => {
|
8 | if (url)
|
9 | return url;
|
10 |
|
11 | const member = (servers || [{ host, port }]).map(m => `${m.host}:${m.port}`);
|
12 | const auth = (username && password) ? `${username}:${password}@` : '';
|
13 | const options = authSource ? `?authSource=${authSource}` : '';
|
14 |
|
15 | return `mongodb://${auth}${member}/${options}`;
|
16 | };
|
17 |
|
18 | module.exports = {
|
19 | type: 'mongodb',
|
20 | connect: ({ mongodb }) => new mongo.MongoClient(getMongoUri(mongodb), { useNewUrlParser: true }).connect(),
|
21 | };
|