From 4e32c399e14f7fe354c844bbe1e372e362748a32 Mon Sep 17 00:00:00 2001 From: Tal Beja Date: Wed, 23 Aug 2017 10:38:40 +0300 Subject: [PATCH] merge db_options into default options --- lib/db.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/db.js b/lib/db.js index 309c13c..44e7e4f 100644 --- a/lib/db.js +++ b/lib/db.js @@ -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) { @@ -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, @@ -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)