Last active
June 14, 2018 13:27
-
-
Save csorlandi/ebea45e62f6501da9ccb505183ae8c16 to your computer and use it in GitHub Desktop.
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/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