Consolidate all models (in models folder)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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