Comments (9)
I've got Webpack to include the models into the bundle, here my index.js
:
import Sequelize from 'sequelize'
const env = process.env.NODE_ENV || 'development';
const config = require(`${__dirname}/../config/config.js`)[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
const context = require.context('.', true, /^\.\/(?!index\.js).*\.js$/, 'sync')
context.keys().map(context).forEach(module => {
const sequelizeModel = module(sequelize, Sequelize);
db[sequelizeModel.name] = sequelizeModel;
})
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
export default db;
from express-example.
@incompletude you can do like this:
context.keys().map(context).forEach(({ default: module }) => {...
from express-example.
@papb I'm still running into trouble with associations on lambda using webpack & sequelize. I've submitted an issue here with a PoC repo: sequelize/sequelize#12874
Unsure if it's the way my models are created/associated, or something a bit more sinister. I've tried the various different methods so far and no luck.
from express-example.
I'm having the same issue. Were you able to figure this out?
from express-example.
@zainzafar Sadly, no :(
I eventually gave up on using webpack/babel completely since node v8 is like 99% ES6 compliant already and so we no longer really need babel for the most part.
from express-example.
Thanks for working on Sequelize. I like it a lot and mean to provide constructive feedback.
I'm having the same problem. This also applies to Umzug migrations. From what I've learned, some Node.js-centric dependencies expect to be able to crawl a directory and dynamically require() things at runtime (i.e. Umzug migrations/ directory, models/index.js in Sequelize) are a world of pain in a Webpack/Babel/Electron world.
For migrations, I just gave up and just built my own small migration framework that doesn't have dynamic require()'s and following your simple but annoying strategy of import
ing each migration.
Once I got past that, I hit the same exact thing with Sequelize models being dynamically imported. I'll probably just use the workaround for now so I can use models.
It would be nice if Sequelize and Umzug were a little less aggressive about assuming things about the environment they operate in. require(), import, import() all seem to be a mess between various JavaScript transpilers and environments. "Use pure Node.js 10.x" is a fine solution, but all my other code is using Webpack, so it's really fragments my codebase into 2 styles (some Webpack/Babel, some pure Node.js, can't import between).
I know "it's Webpack's fault", but there's still not a great solution. With Umzug, I almost got customResolver working with babel/ES6, until I realized that webpack-dev-server screws with module, making this method not work in a webpack-dev-server environment (module.constructor
is undefined in webpack-dev-server environment, and I couldn't figure out a workaround to convert my transpiled code into an object).
from express-example.
I've got Webpack to include the models into the bundle, here my
index.js
:import Sequelize from 'sequelize' const env = process.env.NODE_ENV || 'development'; const config = require(`${__dirname}/../config/config.js`)[env]; const db = {}; let sequelize; if (config.use_env_variable) { sequelize = new Sequelize(process.env[config.use_env_variable], config); } else { sequelize = new Sequelize(config.database, config.username, config.password, config); } const context = require.context('.', true, /^\.\/(?!index\.js).*\.js$/, 'sync') context.keys().map(context).forEach(module => { const sequelizeModel = module(sequelize, Sequelize); db[sequelizeModel.name] = sequelizeModel; }) Object.keys(db).forEach(modelName => { if (db[modelName].associate) { db[modelName].associate(db); } }); db.sequelize = sequelize; db.Sequelize = Sequelize; export default db;
This does not work for me. At const model = module(sequelize, Sequelize)
I get an error stating module is not a function.
from express-example.
Hello everyone! Sorry to take long to address this issue.
It would be nice if Sequelize and Umzug were a little less aggressive about assuming things about the environment they operate in.
I agree.
I have replaced the previous example in this repository with a new, modern example that no longer uses sequelize.import
nor reads the filesystem to detect modules. Things should just work now.
Let me know if anyone has further questions, thank you!!
from express-example.
Hi @AwolDes, I've just replied you in that issue 😀
@ everyone else: please also look at sequelize/sequelize#13169 about using Sequelize with Webpack.
from express-example.
Related Issues (20)
- I'm receiving a ERROR: sequelize is not defined HOT 2
- How to use the transaction? HOT 1
- Add TypeScript Example HOT 2
- Add examples of associations HOT 1
- nodemon index.js doesn't work
- upsert route HOT 1
- Should the directory structure be updated to adapt to the directory structure of Sequelize CLI?
- Contributing and Maintaining information HOT 2
- dynamic imports do not work for defining models HOT 2
- "node_modules/.bin/sequelize" is not work on windows HOT 2
- Unit test hanging or sequelize doesn't close connection HOT 1
- enable Travis CI / Circle CI
- Why we need to define structure of database TWICE? HOT 8
- how to use DataType from req.body HOT 2
- npm install error HOT 4
- duplicate foreignKey when using sequelize.sync() HOT 3
- What does Model.associate() do in model/index.js ? HOT 4
- Please provide an example using sequelize v5 and TypeScript. HOT 6
- Update this repo HOT 3
- Stop using `sequelize.import` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from express-example.