Skip to content

Instantly share code, notes, and snippets.

@csorlandi
Last active June 14, 2018 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csorlandi/ebea45e62f6501da9ccb505183ae8c16 to your computer and use it in GitHub Desktop.
Save csorlandi/ebea45e62f6501da9ccb505183ae8c16 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const config = require('../../config/database.js');
const db = {};
const sequelize = new Sequelize(config);
fs
.readdirSync(__dirname)
.filter(file => (file.indexOf('.') !== 0) && (file !== path.basename(__filename)) && (file.slice(-3) === '.js'))
.forEach((file) => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment