Skip to content

Instantly share code, notes, and snippets.

@natmegs

natmegs/index.js

Created May 1, 2018
Embed
What would you like to do?
Consolidate all models (in models folder)
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
You can’t perform that action at this time.