Skip to content

Instantly share code, notes, and snippets.

@AlekseyA
Created August 6, 2018 13:59
Show Gist options
  • Select an option

  • Save AlekseyA/e805c058c32320f71f59a0ee5f3871b6 to your computer and use it in GitHub Desktop.

Select an option

Save AlekseyA/e805c058c32320f71f59a0ee5f3871b6 to your computer and use it in GitHub Desktop.
Models entry point
const Sequelize = require('sequelize');
const models = require('require-dir')();
const env = process.env.NODE_ENV || 'development';
const config = require('../../config/database')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config.database);
} else {
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
}
Object.keys(models).forEach((path) => {
const model = sequelize.import(path);
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
module.exports = db;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment