Repository navigation
Make Instances instances of Model - #5877
felixfbecker wants to merge 0 commit into
Conversation
|
Hmm interesting. However, under this new scheme, is there a way to add a method universally to all instances of all models? At present you can do How would you do that under the new scheme? Being able to add (or shim) methods on the universal Model Instance prototype is very useful for plugins. Also...
|
|
It's tricky. Depending on the circumstances, you want Which should take precedence is probably a matter of opinion. Which feels "right" will be different for different users depending on what they use Sequelize for. As someone who maintains a few Sequelize plugins, I see the schema more in the abstract so the power that |
|
By the way, would |
Sure, simply do
It is possible to have both,
Yes. |
|
The tests fail because I try to add the |
|
I fixed the issue with the name property, will fallback to using eval() if |
Sorry, I was talking about var User = sequelize.define( 'User', /* blah */ );
assert( User instanceof Sequelize.Model == true );This will no longer work right? After this PR, you'd have to use So after this PR, to add a static method to all models, you add a method on var myMethod = function () { /* blah */ };
Sequelize.Model.prototype.myMethod = myMethod;
var User = sequelize.define( 'User', /* blah */ );
assert( User.myMethod == myMethod );After this PR: var myMethod = function () { /* blah */ };
Sequelize.Model.myMethod = myMethod;
var User = sequelize.define( 'User', /* blah */ );
assert( User.myMethod == myMethod );Have I got this right? I think this goes a little bit beyond basic OOP! |
|
NB Isn't In node v4 and below |
Yes.
Yes. But imo globally patching an imported class from a module is a very bad pattern. The better option, that is now possible, would be if you subclass abstract class MyBaseModel extends Sequelize.Model {
static myStaticMethod() { ... }
myMethod() { ... }
}
class User extends MyBaseModel { ... }
Model.init(User.name, ...)And extending classes really is basic OOP. Of course Javascript's prototype concept is a bit hard to grasp, but imo having instances be instances of the model classes is what a user expects.
If you look at my code you see I do the same. But that is only so the instances inherit the prototype properties. But you also have make the new Model class inherit the static methods, like function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}Since this PR is kind of a breaking change it will land in 4.0... If the decision is made in 4.0 to only support Node 4 and up, that would be awesome, because we could use native classes which of course have better performance. |
|
OK, let's start again from the beginning... I'm not against what you're doing here. I think generally speaking, having an instance of I'd say that this should definitely be considered a breaking change - any user code like Concerning OOP, yes In an app, as you say, it'd be better to subclass My only real concern about this PR is whether altering the prototype of each model constructor with My point about Anyway, the question of performance will become clearer once Travis starts running. I'd also suggest adding 2 static methods to Sequelize.isModel = function(item) { return item && item.prototype instanceof Sequelize.Model; };
Sequelize.isInstance = function(item) { return item && item instanceof Sequelize.Model; };These could be used internally - easier to understand than |
|
@overlookmotel I was in no way trying to insult your programming skills. Sorry if I sounded a bit rude, just wanted to answer your questions :) I would still argue though that a subclass inheriting not only instance methods, but also static methods, is expected behaviour - it's how it works in every other OOP language, the ES6 spec and Babel. Altering the prototype of an object definitely has a performance impact because it destroys the "prediction" V8 can make about objects. Still, it is the only spec-compliant way to make a class inherit from another. As I said in the original post, this PR is mostly to generate discussion. I would propose to drop support for Node <4 in 4.0 and then use real classes. I'm just a bit worried that 4.0 will take a long time to release. I would rather keep the API changes small (make Adding |
|
@felixfbecker Yes you're right Concerning performance, well I guess we'll have to see what impact this has once you've rebased and Travis runs. You still can't subclass |
|
@overlookmotel well if 4.0 will only support Node >4 I would redo my PR to use classes, and then no return class extends Model {} |
|
I think most of what needs to be said has already been said here - I'm just waiting to see the performance impact ;-). One problem with extending (and overlookmotel might already have mentioned this) is plugins which are working on |
|
I will make a new PR that uses classes instead of rebasing this (that would be a pain). I originally commited on master (stupid me), I kept the commits in the instance-instanceof-master branch |
- check instanceof on the prototype see sequelize/sequelize#5877 - update dependencies
- check instanceof on the prototype see sequelize/sequelize#5877 - update dependencies
This PR does some changes I suggested in #5205 (comment) and #4728 (comment). I could not get the tests to run so I open this to see what CI says and to generate some discussion.
Instanceclass, it essentially becomes theModelclass.Modelclass (find(),build(), ...) are now static methods on the newModelclass.Modelclass is added to the static initializerModel.init().sequelize.define()is basically now a class factory, that returns a new class that inherits fromModel, initialized with the passed options.User.build() instanceof Useris now true.instanceMethods,classMethodsgetterMethods,setterMethodsyou can now simply set those methods on the Model / the Model's prototype directly which is awesome for strict typing (TypeScript)Model.build()you can also just usenew Model()to get an instanceModel, you have to checkx.prototype instanceof Model, to check if a variable is an instance of aModelyou have to checkx instanceof Modelinstance.Modelis now exactly the same asinstance.constructor(a reference to the class that created the object).Instanceare still there and set to the Model class (since theModelclass replaces theInstanceclass)Model.nameis set to the model name insequelize.define()viaObject.defineProperty. Only in the super-old Node versions, whereFunction.prototype.nameis notconfigurable,eval()is used to dynamically create a class with the model name.Changes in semantic, expressed in TypeScript definitions:
Model creation with
define()as a class factory is supported just like before, but it is in theory also possible to subclassModeldirectly for type-safety and callModel.init()yourself:Model.init()is of course low-level API that is called bydefine(), but it would finally allow third-party wrappers to do this in decorators: A model would extendSequelize.Model(similar to how it is today), property/method decorators would add metadata about types, hooks, associations and a class decorator would read them and call the static initializerModel.init()with the info.