January 2019
Intermediate to advanced
460 pages
10h 33m
English
We want to import all of our database models at once, in a central file. Our database connection instantiator will then use this file on the other side.
Create an index.js file in the models folder, and fill in the following code:
import Sequelize from 'sequelize';if (process.env.NODE_ENV === 'development') { require('babel-plugin-require-context-hook/register')()}export default (sequelize) => { let db = {}; const context = require.context('.', true, /^\.\/(?!index\.js).*\.js$/, 'sync') context.keys().map(context).forEach(module => { const model = module(sequelize, Sequelize); db[model.name] = model; }); Object.keys(db).forEach((modelName) => { if (db[modelName].associate) { db[modelName].associate(db);Read now
Unlock full access