GithubHelp home page GithubHelp logo

Comments (9)

mexx avatar mexx commented on April 27, 2024 27

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.

astiag avatar astiag commented on April 27, 2024 3

@incompletude you can do like this:

context.keys().map(context).forEach(({ default: module }) => {...

from express-example.

AwolDes avatar AwolDes commented on April 27, 2024 2

@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.

zainzafar avatar zainzafar commented on April 27, 2024

I'm having the same issue. Were you able to figure this out?

from express-example.

Aditya94A avatar Aditya94A commented on April 27, 2024

@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.

aguynamedben avatar aguynamedben commented on April 27, 2024

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 importing 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.

incompletude avatar incompletude commented on April 27, 2024

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.

papb avatar papb commented on April 27, 2024

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.

papb avatar papb commented on April 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.