Skip to content

Commit

Permalink
fix(sequelize): fixing error handling in in sequelize lib instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Dec 28, 2016
1 parent f73ec2b commit 02608a2
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions server/config/lib/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,49 @@ const config = require('../config'),
var orm = {};
var sequelize;

// Instantiate a sequelize connection to an SQL database based on configuration
// from server/config/env/*
try {
sequelize = new Sequelize(config.orm.dbname, config.orm.user, config.orm.pass, config.orm.options);
} catch (e) {
throw new Error(e);
}
// Only read config.orm if it is defined, if not just return empty orm object
if (config.orm) {

// Instantiate sequelize models
config.files.server.sequelizeModels.forEach(function (modelPath) {
// Instantiate a sequelize connection to an SQL database based on configuration
// from `server/config/env/*`
try {
let model = sequelize.import(path.resolve(modelPath));
orm[model.name] = model;
sequelize = new Sequelize(config.orm.dbname, config.orm.user, config.orm.pass, config.orm.options);
} catch (e) {
throw new Error(e);
}
});

// Once all models have been loaded, establish the associations between them
Object.keys(orm).forEach(function(modelName) {
if (orm[modelName].associate) {
orm[modelName].associate(orm);
}
});

// Expose the instantiated sequelize connection object
orm.sequelize = sequelize;
// Instantiate sequelize models
config.files.server.sequelizeModels.forEach(function (modelPath) {
try {
let model = sequelize.import(path.resolve(modelPath));
orm[model.name] = model;
} catch (e) {
throw new Error(e);
}
});

// Once all models have been loaded, establish the associations between them
Object.keys(orm).forEach(function (modelName) {
if (orm[modelName].associate) {
orm[modelName].associate(orm);
}
});

// Expose the instantiated sequelize connection object
orm.sequelize = sequelize;

// Expose the global Sequelize library
orm.Sequelize = Sequelize;

orm.sync = function () {
// Sync makes sure the database tables are created if they don't exist
// and the `force` parameter will also drop the tables before re-creating them
return this.sequelize.sync({
force: (config.seedDB.reset || false)
});
};

// Expose the global Sequelize library
orm.Sequelize = Sequelize;

orm.sync = function() {
return this.sequelize.sync();
};
}

// Export this ORM module
module.exports = orm;

0 comments on commit 02608a2

Please sign in to comment.