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

Support v5 instance methods on models #1

Open
Trevoke opened this issue Jun 26, 2020 · 6 comments
Open

Support v5 instance methods on models #1

Trevoke opened this issue Jun 26, 2020 · 6 comments

Comments

@Trevoke
Copy link

Trevoke commented Jun 26, 2020

Thanks for modernizing sequelize-mock!

sequelize v5 requires that instance methods be on the prototype: https://sequelizedocs.fullstackacademy.com/instance-and-class-methods/ - I'm working on a codebase that needs some love, and I'd be very happy if this were supported :)

@philtweir
Copy link

I've got a basic version of this running, tested using classes of the form:

class User extends sequelize.Model {
   [instance methods]
}
return User.init({...}, { sequelize, ... });

The modifications are on this branch - it's pretty rough at the moment, with minimal alterations, but I would be keen to get some feedback before tidying up. Really, it's a bit of a sticking plaster, but it might help gradually migrate to the newer sequelize approach.

@SippieCup
Copy link
Member

SippieCup commented Jul 13, 2020

Sorry I didn't see this issue earlier, we are currently doing something similar to philwier posted. We have a couple blog posts that I will prioritize putting up tomorrow to show how we use this in production. In addition, we have a few more updates coming to this now we are working on internally to provide more functionality (such as transaction support) which wasn't in the original sequelize-mock repo and some refactoring to allow it to work.

@philtweir we would love a pull request from you to add your work to this project while providing credit to you and will continue to support your changes in the future.

I'll update this tomorrow with these updates and a link to how we use it.

Edit: for now this is our "base model" which we then extend our actual models from

@philtweir
Copy link

Awesome and apologies for the long delay, but great news!

@Shadowstep33
Copy link

Shadowstep33 commented Dec 30, 2020

Hi, I came up to a similar problem and was able to fix it with a 1 liner:

Sequelize.prototype.define = function (name, obj, opts) {
	opts = _.extend({
		sequelize: this,
	}, opts || {})
	
	var model = new Model(name, obj, opts);
	model.prototype = {}; //This line forces `prototype` onto the returned instance
	this.models[name] = model;
	return model;
};

Edit: it needed to be an empty object or else every new model will overwrite the prototype methods

@SippieCup
Copy link
Member

Edit: it needed to be an empty object or else every new model will overwrite the prototype methods

We were experiencing this when doing deep recursion. now I understand whats happening. Thanks for the insight.

If you want to make a pull request to adapt this into the core package I'd love to see it!

@Shadowstep33
Copy link

Hope you had a good new year @SippieCup can do! Will link here when done

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

4 participants