Created
August 1, 2017 20:41
-
-
Save ihavenoidea14/0dab8b461c057c427829fdc99bfb6743 to your computer and use it in GitHub Desktop.
Using Sequelize with Webpack can be interesting. This snippet should help...
This file contains 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
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; |
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
A similar solution to this gist was posted in this issue. sequelize/sequelize#4974