Question


I need a way to add classMethods and instanceMethods to already defined Sequelize model

Something which can be achieved in mongoose using the Plugins feature


I have a model defined for User in models/user.js.

Now, I want to add classMethods like findByUsername, signup, login to this User model BUT within my custom nodejs library.

If Plugins are not supported at the moment, is there any workaround like redefining the User model within the custom library ?

I tried attaching a function directly to the User model using

User.findByUsername = User.options.classMethods.findByUsername = function(username, callback) {
    ...
}

Which acts like a classMethod and kind of works but I don't think that's the right way.

Was it helpful?

Solution

Though there isn't any documented way to achieve this (yet), as per mickhansen's comment on github issue here the following workarounds work great.

classMethod

User.findByUsername = function(username, callback) {
  ...
}

instanceMethod

User.DAO.prototype.authenticate = function(password, callback) {
  ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top