Created
February 6, 2019 17:30
-
-
Save vonvick/c23cc4fd3de7400ed16115eb1073c422 to your computer and use it in GitHub Desktop.
Sample model index file
This file contains hidden or 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
require('dotenv').config(); | |
const fs = require('fs'); | |
const path = require('path'); | |
const Sequelize = require('sequelize'); | |
const basename = path.basename(__filename); | |
const config = require('<path-to-sequelize-config>'); | |
const db = {}; | |
const sequelize = new Sequelize(config.url, config); | |
fs | |
.readdirSync(__dirname) | |
.filter(file => { | |
return (file.indexOf('.') !== 0) && (file !== basename) && (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