-
Notifications
You must be signed in to change notification settings - Fork 70
/
url-builder.js
59 lines (55 loc) · 1.25 KB
/
url-builder.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
// Generated by CoffeeScript 1.10.0
(function() {
var _, buildHost;
_ = require('lodash');
buildHost = function(opts) {
var host;
host = opts.host;
if (opts.port) {
host += ':' + opts.port;
}
return host;
};
module.exports = {
buildMongoConnString: function(config) {
var hasUser, params, replicaset, s;
hasUser = !!config.user;
if (config.url) {
return config.url;
}
s = "mongodb://";
if (hasUser) {
s += config.user;
}
if (config.password) {
if (!hasUser) {
throw new Error('Password provided but Username is not');
}
s += ':' + config.password;
}
if (hasUser) {
s += '@';
}
if (replicaset = config.replicaset) {
s += replicaset.members.map(buildHost).join(',');
} else {
s += buildHost(config);
}
s += '/';
if (config.db) {
s += config.db;
}
params = [];
if (config.replicaset) {
params.push('replicaSet=' + config.replicaset.name);
}
if (config.ssl) {
params.push('ssl=true');
}
if (params.length > 0) {
s += '?' + params.join('&');
}
return s;
}
};
}).call(this);