Created
August 6, 2018 13:59
-
-
Save AlekseyA/e805c058c32320f71f59a0ee5f3871b6 to your computer and use it in GitHub Desktop.
Models entry point
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
| const Sequelize = require('sequelize'); | |
| const models = require('require-dir')(); | |
| const env = process.env.NODE_ENV || 'development'; | |
| const config = require('../../config/database')[env]; | |
| const db = {}; | |
| let sequelize; | |
| if (config.use_env_variable) { | |
| sequelize = new Sequelize(process.env[config.use_env_variable], config.database); | |
| } else { | |
| sequelize = new Sequelize( | |
| config.database, | |
| config.username, | |
| config.password, | |
| config | |
| ); | |
| } | |
| Object.keys(models).forEach((path) => { | |
| const model = sequelize.import(path); | |
| db[model.name] = model; | |
| }); | |
| Object.keys(db).forEach((modelName) => { | |
| if (db[modelName].associate) { | |
| db[modelName].associate(db); | |
| } | |
| }); | |
| db.sequelize = sequelize; | |
| module.exports = db; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment