Skip to content

Commit 02608a2

Browse files
committed
fix(sequelize): fixing error handling in in sequelize lib instantiation
1 parent f73ec2b commit 02608a2

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

server/config/lib/sequelize.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,49 @@ const config = require('../config'),
1010
var orm = {};
1111
var sequelize;
1212

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

21-
// Instantiate sequelize models
22-
config.files.server.sequelizeModels.forEach(function (modelPath) {
16+
// Instantiate a sequelize connection to an SQL database based on configuration
17+
// from `server/config/env/*`
2318
try {
24-
let model = sequelize.import(path.resolve(modelPath));
25-
orm[model.name] = model;
19+
sequelize = new Sequelize(config.orm.dbname, config.orm.user, config.orm.pass, config.orm.options);
2620
} catch (e) {
2721
throw new Error(e);
2822
}
29-
});
30-
31-
// Once all models have been loaded, establish the associations between them
32-
Object.keys(orm).forEach(function(modelName) {
33-
if (orm[modelName].associate) {
34-
orm[modelName].associate(orm);
35-
}
36-
});
3723

38-
// Expose the instantiated sequelize connection object
39-
orm.sequelize = sequelize;
24+
// Instantiate sequelize models
25+
config.files.server.sequelizeModels.forEach(function (modelPath) {
26+
try {
27+
let model = sequelize.import(path.resolve(modelPath));
28+
orm[model.name] = model;
29+
} catch (e) {
30+
throw new Error(e);
31+
}
32+
});
33+
34+
// Once all models have been loaded, establish the associations between them
35+
Object.keys(orm).forEach(function (modelName) {
36+
if (orm[modelName].associate) {
37+
orm[modelName].associate(orm);
38+
}
39+
});
40+
41+
// Expose the instantiated sequelize connection object
42+
orm.sequelize = sequelize;
43+
44+
// Expose the global Sequelize library
45+
orm.Sequelize = Sequelize;
46+
47+
orm.sync = function () {
48+
// Sync makes sure the database tables are created if they don't exist
49+
// and the `force` parameter will also drop the tables before re-creating them
50+
return this.sequelize.sync({
51+
force: (config.seedDB.reset || false)
52+
});
53+
};
4054

41-
// Expose the global Sequelize library
42-
orm.Sequelize = Sequelize;
43-
44-
orm.sync = function() {
45-
return this.sequelize.sync();
46-
};
55+
}
4756

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

0 commit comments

Comments
 (0)