Skip to content

Commit

Permalink
More flexible DB initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Reis committed Dec 14, 2016
1 parent f18b950 commit fae552d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
71 changes: 41 additions & 30 deletions app/init/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
});
};
6 changes: 4 additions & 2 deletions scripts/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 4 additions & 2 deletions scripts/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit fae552d

Please sign in to comment.