Skip to content

Instantly share code, notes, and snippets.

@ihavenoidea14
Created August 1, 2017 20:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihavenoidea14/0dab8b461c057c427829fdc99bfb6743 to your computer and use it in GitHub Desktop.
Save ihavenoidea14/0dab8b461c057c427829fdc99bfb6743 to your computer and use it in GitHub Desktop.
Using Sequelize with Webpack can be interesting. This snippet should help...
import Sequelize from 'sequelize';
import config from '../config';
// Face, meet desk. Sequelize models and Webpack do not play nicely
let models = {};
(function(config) {
if (Object.keys(models).length && !force) {
return models;
}
const sequelize = new Sequelize(config.byteTestDb, config.byteTestUser, config.byteTestPwd, { dialect: 'mssql', logging: false, host: config.byteTestDbHost, define: { timestamps: false }});
// Importing each module for now, can change to readDirSync and require models dynamically
let modules = [
require('./FileData.js'),
];
modules.forEach((module) => {
const model = module(sequelize, Sequelize, config);
models[model.name] = model;
});
Object.keys(models).forEach((key) => {
if ('associate' in models[key]) {
models[key].associate(models);
}
});
models.sequelize = sequelize;
models.Sequelize = Sequelize;
return models;
})(config);
module.exports = models;
@Gerst20051
Copy link

A similar solution to this gist was posted in this issue. sequelize/sequelize#4974

@Sujina-Saddival
Copy link

SOLVED - This worked for me. If you want to make webpack config for all sequelize models dynamically check this out. - webpack/webpack#4879

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment