Skip to content

Instantly share code, notes, and snippets.

@josser
Created July 27, 2017 09:45
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 josser/908d2d94b644cbf2bd2eedb8d5640245 to your computer and use it in GitHub Desktop.
Save josser/908d2d94b644cbf2bd2eedb8d5640245 to your computer and use it in GitHub Desktop.
import fs from 'fs'
import path from 'path'
import Sequelize from 'sequelize'
import config from '../config'
const sequelize = new Sequelize(
config.db.database, config.db.username, config.db.password, config.db.options
)
const db = {}
fs
.readdirSync(__dirname)
// Skip hidden files and index.js itself
.filter(file => file.indexOf('.') !== 0 && file !== 'index.js')
.forEach((file) => {
const 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)
}
})
export { Sequelize, sequelize }
export default db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment