Skip to content

Commit

Permalink
merge db_options into default options
Browse files Browse the repository at this point in the history
  • Loading branch information
bejavu committed Aug 23, 2017
1 parent 5c6aa65 commit 4e32c39
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var timestamps = require('mongoose-time')
var fs = require('fs')
var merge = require('mongoose-merge-plugin')
var mongoUri = require('mongodb-uri')
var _ = require('lodash')

function load_models (model_path) {
fs.readdirSync(model_path).forEach(function (file) {
Expand Down Expand Up @@ -39,14 +40,7 @@ module.exports = {
throw new Error('Can\'t connect to Database')
}
uri = uri || mongoUri.formatMongoose({username: username, password: password, hosts: [{host: host, port: port}], database: database})
if (options) {
try {
options = JSON.parse(options)
} catch (e) {
options = null
}
}
options = options || {
var default_options = {
server: {
socketOptions: {
keepAlive: 1,
Expand All @@ -60,7 +54,18 @@ module.exports = {
}
}
}
console.log('options', JSON.stringify(options))
if (options) {
try {
options = JSON.parse(options)
options = _.merge(default_options, options)
} catch (e) {
console.warning('cant resolve db options:', e)
options = default_options
}
} else {
options = default_options
}
console.log('db_options', JSON.stringify(options))
// CONNECTION EVENTS
mongoose.connection.on('connected', function () {
console.log('Database connected to: ' + uri)
Expand Down

0 comments on commit 4e32c39

Please sign in to comment.