Skip to content

Instantly share code, notes, and snippets.

@natmegs
Created May 1, 2018 23:17
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 natmegs/23352adb72127f1a56dbe33ff4539263 to your computer and use it in GitHub Desktop.
Save natmegs/23352adb72127f1a56dbe33ff4539263 to your computer and use it in GitHub Desktop.
Consolidate all models (in models folder)
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const config = require('../config/db');
let db = {};
let sequelize = new Sequelize(config.database, config.username, config.password, config.options);
fs
.readdirSync(__dirname)
.filter(file => (file.indexOf(".") !== 0) && (file !== 'index.js'))
.forEach(file => {
let model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if("associate" in db[modelName]) {
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