Skip to content

Commit c0534c9

Browse files
committed
fixup for ssl options and deep merge of options
1 parent fdd27d9 commit c0534c9

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

index.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ let standardOptions = {
2323
keepAlive: 1
2424
},
2525
ssl: false,
26-
authenticationDatabase: '',
2726
auto_reconnect: true,
2827
reconnectInterval: RECONNECT_TIMEOUT
2928
},
@@ -32,8 +31,7 @@ let standardOptions = {
3231
keepAlive: 1
3332
}
3433
},
35-
user: '',
36-
pass: ''
34+
logger: stdLogger
3735
}
3836

3937
function consoleLogger (level) {
@@ -47,17 +45,12 @@ function _isOk () {
4745
return isOk === true
4846
}
4947

50-
function _connect (options, sslOptions) {
48+
function _connect (options) {
5149
mongoose.Promise = global.Promise
5250
var log = options.logger || stdLogger
5351
// Mongoose connect is called once by the app.js & connection established
5452
var dbUri = options.dbUri
55-
var dbOptions
56-
if (typeof sslOptions === 'object') {
57-
dbOptions = getMongoOptionsSsl(options, sslOptions)
58-
} else {
59-
dbOptions = getMongoOptions(options)
60-
}
53+
var dbOptions = getMongoOptions(options)
6154

6255
return new Promise((resolve, reject) => {
6356
mongoose.connect(dbUri, dbOptions)
@@ -92,19 +85,16 @@ function _connect (options, sslOptions) {
9285
}
9386

9487
function getMongoOptions (options) {
95-
var dbOptions = standardOptions
96-
dbOptions.reconnectInterval = options.reconnectInterval || RECONNECT_TIMEOUT
97-
dbOptions.reconnectTries = options.reconnectTries || 0
98-
dbOptions.user = options.dbUsername
99-
dbOptions.pass = options.dbPassword
100-
88+
var dbOptions = _mergeOptions(standardOptions, options)
10189
return dbOptions
10290
}
10391

104-
function getMongoOptionsSsl (options, sslOptions) {
105-
var dbOptions = getMongoOptions(options)
106-
dbOptions.ssl = sslOptions.ssl
107-
dbOptions.authenticationDatabase = sslOptions.authDatabase
108-
109-
return dbOptions
92+
// merge all options objects front to back, i.e. later overriding earlier objects
93+
function _mergeOptions () {
94+
var options = {}
95+
for (var i = 0; i < arguments.length; ++i) {
96+
let obj = arguments[i]
97+
for (var attr in obj) { options[attr] = obj[attr] }
98+
}
99+
return options
110100
}

0 commit comments

Comments
 (0)