-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectionSingleton.js
More file actions
45 lines (35 loc) · 1.17 KB
/
Copy pathconnectionSingleton.js
File metadata and controls
45 lines (35 loc) · 1.17 KB
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
const MongoClient = require('mongodb').MongoClient;
function geturl(params) {
const {
username,
password,
server,
port,
authentication
} = params;
let url = "";
if (authentication) {
url = 'mongodb://' + username + ':' + password + '@' + server + ':' + port + '/test?authMechanism=SCRAM-SHA-1&authSource=admin';
} else {
url = 'mongodb://' + server + ':' + port + '/test';
}
const urlEncoded = encodeURI(url);
return urlEncoded;
}
module.exports = {
connect: async function (params) {
let { username, password, server, port, authentication, name } = params;
// validate params
// if all good connect to db and get info
let url = geturl({ username, password, server, port, authentication });
const client = await MongoClient.connect(url, {
useNewUrlParser: true
});
// save the connection
this.connection = client;
this.config = { username, password, server, port, authentication, name };
},
// the connection will be established when the method connect is invoked
connection: null,
config: null,
}