Madhur
Madhur

Reputation: 2219

Sequelize - mongoose like plugins


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.

Upvotes: 0

Views: 1087

Answers (1)

Madhur
Madhur

Reputation: 2219

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) {
  ...
}

Upvotes: 3

Related Questions