Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequelize and Webpack not playing nice together #68

Open
michaelyiu opened this issue Dec 14, 2018 · 11 comments
Open

Sequelize and Webpack not playing nice together #68

michaelyiu opened this issue Dec 14, 2018 · 11 comments

Comments

@michaelyiu
Copy link

michaelyiu commented Dec 14, 2018

I have to say, this tutorial is a godsend. I love the breakdowns and very clear-cut explanations and I just so happen to need to use most of the packages.

I was following the guide fairly well until the Sequelize part of the tutorial and I was getting an error: "Cannot find module C:\PROJECT_DIR\\.webpack\service\src\user" for the user file. I've tried the following:

const models = [ 'User', 'Message' ];

models.forEach(function(model) { module.exports[model] = sequelize.import(__dirname + '/' + model); });

But when I ran it again, "Cannot find module 'C:\User'"

  1. Sequelize as a nodeExternal

  2. Adding this piece to webpack.config.js
    node: { __dirname: false },

Error: "Cannot find module C:\PROJECT_DIR\\.webpack\service\src\user"

  1. There has been some examples around google such as https://gist.github.com/ihavenoidea14/0dab8b461c057c427829fdc99bfb6743
    fail to bundle sequelize webpack/webpack#4879

that uses module(sequelize, Sequelize); When I tried the examples in the links above, I get a 'module is not a function'.

I'm at a loss and am wondering if you had tried with Webpack before and if you have any recommendations I'd love to hear it.

Cheers.

@rwieruch
Copy link
Owner

Can you maybe link your GitHub repository here? Then I can check whether I can help with it!

@michaelyiu
Copy link
Author

michaelyiu commented Dec 14, 2018

https://github.com/michaelyiu/apollo-server-tutorial

I forgot to mention I'm using it with serverless as well

Turns out finding a different way to import the models seem to do the trick and I didn't have to play around with webpack too much

@rwieruch
Copy link
Owner

@michaelyiu did you find the cause? Sorry for not being able to fix this in time.

@radcapitalist
Copy link

@michaelyiu, could you by any chance elaborate on "different way to import the models"?

@michaelyiu
Copy link
Author

michaelyiu commented Mar 6, 2019

@michaelyiu, could you by any chance elaborate on "different way to import the models"?

@radcapitalist sorry for the extremely late reply. So for whatever reason, I couldn't mimic what @rwieruch had in his post because webpack was complaining:

 User: sequelize.import('./user'),
 Message: sequelize.import('./message'),
};

Object.keys(models).forEach(key => {
 if ('associate' in models[key]) {
   models[key].associate(models);
 }
});`

I had to require the files in the /model/index first:

`const userModel = require("./User.js");
const messageModel = require("./Message.js");

const models = {
   User: sequelize.import("user", userModel),
   Message: sequelize.import("message", messageModel),
}

Object.keys(models).forEach(key => {
   if ("associate" in models[key]) {
   	models[key].associate(models);
   }
})

It gets a bit tedious the more models I had to put in but hey it works.

@radcapitalist
Copy link

radcapitalist commented Mar 7, 2019

Okay, I ended up going with the require.context() approach suggested here:

sequelize/express-example#74

It keeps you from having to enumerate every individual model file, allowing you instead to process *.js (excluding index.js) from your models folder just like you probably did before Webpack. I have models from two folders (one that is shared with another project), so my code looks like this:

const modelsContext = require.context('.', false, /^\.\/(?!index\.js).*\.js$/, 'sync');
debug(`modelsContext: ${JSON.stringify(modelsContext.keys())}`);
const sharedModelsContext = require.context('../../../../../shared/models/postgres', false, /^\.\/(?!index\.js).*\.js$/, 'sync');
debug(`sharedModelsContext: ${JSON.stringify(sharedModelsContext.keys())}`);

modelsContext.keys().map(modelsContext).forEach(module => {
	const model = module(sequelize, Sequelize);
	debug(`postgres: About to import model: ${model.name}`);
	db[model.name] = model;
});

sharedModelsContext.keys().map(sharedModelsContext).forEach(module => {
	const model = module(sequelize, Sequelize);
	debug(`postgres: About to import model: ${model.name}`);
	db[model.name] = model;
});


Object.keys(db).forEach(function(modelName) {
	if ("associate" in db[modelName]) {
		db[modelName].associate(db);
	}
});

@michaelyiu
Copy link
Author

Clean, I'm gonna try this out!

@rwieruch
Copy link
Owner

Do you think more people using Webpack are running into this issue? Then I would keep it open for now. Let me know!

@michaelyiu
Copy link
Author

michaelyiu commented Mar 17, 2019

Yeah, I definitely think a large majority running Webpack would run into this issue. Also I've been running into several issues trying to get the above by @radcapitalist to work because I added helper methods inside the model itself in case anyone stumbles onto this and wonders why. Refactoring that code into the resolvers or some helper file is probably better.

I also changed the index files in resolvers/schemas which was much simpler

const context = require.context('.', true, /^./(?!index.js).*.js$/, 'sync');

let schemas = [];

const linkSchema = gql`
  scalar Date

  type Query {
    _: Boolean
  }
  type Mutation {
    _: Boolean
  }
  type Subscription {
    _: Boolean
  }
`;
schemas.push(linkSchema)

context.keys().forEach(module => {
  schemas.push(context(module).default)
});

export default schemas

Thanks again @radcapitalist for the super clean solution

edit: It wasn't about the helper methods that's preventing this solution, it's actually the associations. Which I'm having issues circumventing.

@rwieruch rwieruch reopened this Mar 19, 2019
@AwolDes
Copy link

AwolDes commented Nov 29, 2020

@michaelyiu did you ever get around the associations issues you had? I submitted a similar issue to what you're describing on the sequelize repo sequelize/sequelize#12874

@papb
Copy link

papb commented Apr 4, 2021

Hello, everyone! I've just created a new related issue: sequelize/sequelize#13169

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants