diff --git a/app/app.js b/app/app.js index 375f371..808987c 100644 --- a/app/app.js +++ b/app/app.js @@ -29,7 +29,7 @@ const ERROR_MIDDLEWARE = config.ERROR_MIDDLEWARE; * Initialization */ require('./init/error-handling'); -require('./init/db'); +require('./init/db')(); require('./init/jwt'); require('./init/auth'); require('./init/i18n'); diff --git a/app/init/db.js b/app/init/db.js index 077cbdd..90b87dd 100644 --- a/app/init/db.js +++ b/app/init/db.js @@ -15,7 +15,6 @@ const config = require('../config'); mongoose.Promise = require('bluebird'); mongoose.plugin(require('meanie-mongoose-to-json')); mongoose.plugin(require('meanie-mongoose-set-properties')); -mongoose.set('debug', config.DB_DEBUG); /** * Helper to check if an ID is an object ID @@ -24,33 +23,45 @@ mongoose.isObjectId = function(id) { return (id instanceof mongoose.Types.ObjectId); }; -//Connect to database -console.log('Connecting to database', chalk.magenta(config.DB_URI), '...'); -mongoose.connect(config.DB_URI, { - user: config.DB_USER, - pass: config.DB_PASS, - debug: config.DB_DEBUG, - config: { +/** + * Wrapper to enable overriding of configuration options + */ +module.exports = function(options) { + + //Extend main config options + const cfg = Object.assign({}, { + uri: config.DB_URI, + user: config.DB_USER, + pass: config.DB_PASS, + debug: config.DB_DEBUG, autoIndex: config.DB_AUTO_INDEX, - }, -}); - -//Handle connection events -mongoose.connection.on('error', error => { - console.log(chalk.red('Database error:')); - console.log(chalk.red(error.stack || error)); - process.exit(-1); -}); -mongoose.connection.on('connected', () => { - console.log( - chalk.green('Database connected @'), - chalk.magenta(config.DB_URI) - ); -}); - -//Load models -console.log('Loading model files...'); -glob.sync('./app/**/*.model.js').forEach(modelPath => { - console.log(chalk.grey(' - %s'), modelPath.replace('./app/', '')); - require(path.resolve(modelPath)); -}); + }, options || {}); + + //Connect to database + console.log('Connecting to database', chalk.magenta(cfg.uri), '...'); + mongoose.set('debug', cfg.debug); + mongoose.connect(cfg.uri, { + user: cfg.user, + pass: cfg.pass, + config: { + autoIndex: cfg.autoIndex, + }, + }); + + //Handle connection events + mongoose.connection.on('error', error => { + console.log(chalk.red('Database error:')); + console.log(chalk.red(error.stack || error)); + process.exit(-1); + }); + mongoose.connection.on('connected', () => { + console.log(chalk.green('Database connected @'), chalk.magenta(cfg.uri)); + }); + + //Load models + console.log('Loading model files...'); + glob.sync('./app/**/*.model.js').forEach(modelPath => { + console.log(chalk.grey(' - %s'), modelPath.replace('./app/', '')); + require(path.resolve(modelPath)); + }); +}; diff --git a/scripts/migrate.js b/scripts/migrate.js index 6133f76..29aa512 100644 --- a/scripts/migrate.js +++ b/scripts/migrate.js @@ -29,8 +29,10 @@ if (scripts.length === 0) { } //Initialize database -require('../app/init/db'); -mongoose.set('debug', (typeof argv.debug !== 'undefined')); +require('../app/init/db')({ + debug: (typeof argv.debug !== 'undefined'), + autoIndex: false, +}); //Run when DB connected mongoose.connection.on('connected', () => { diff --git a/scripts/task.js b/scripts/task.js index 5208b8b..01775e4 100644 --- a/scripts/task.js +++ b/scripts/task.js @@ -29,8 +29,10 @@ if (scripts.length === 0) { } //Initialize database -require('../app/init/db'); -mongoose.set('debug', (typeof argv.debug !== 'undefined')); +require('../app/init/db')({ + debug: (typeof argv.debug !== 'undefined'), + autoIndex: false, +}); //Run when DB connected mongoose.connection.on('connected', () => {