28

For example I have a Client model. I want to add new function "sendEmail"

The function needs to work send email to one client, or to send email to many clients at once?

Where to define those functions?

4
  • 5
    Probably you are looking for: docs.sequelizejs.com/en/stable/docs/models/#expansion-of-models Commented May 7, 2015 at 12:28
  • No. This is to run a method only on one instance (row). Commented May 7, 2015 at 16:08
  • But classMethods are for global model. Commented May 7, 2015 at 16:09
  • I want Client.FindAll(...).sendEmail... || It is not documentated How I make the classMethods to work on the returned rows from the FindAll Commented May 7, 2015 at 16:11

4 Answers 4

91

Version 4 of sequelize has changed this and the other solutions with instanceMethods and classMethods do not work anymore. See Upgrade to V4 / Breaking changes

The new way of doing it is as follows:

const Model = sequelize.define('Model', {
    ...
});

// Class Method
Model.myCustomQuery = function (param, param2) {  };

// Instance Method
Model.prototype.myCustomSetter = function (param, param2) {  }
Sign up to request clarification or add additional context in comments.

6 Comments

This answer should be the accepted since it's the new way of adding both class methods and instance methods.
What could be the reasons that myCustomSetter is undefined when accessed from outside the Model? I am having this problem. Any hint?
It's an instance method, not a class method and therefore you have to have an instance of Model in order to access it @RameshPareek
thanks@Yehonatan. What could be the reasons that even after initializing with new keyword, I can't access these methods?
Link is dead @jlh
|
13

Use instanceMethods as Jan Meier pointed out.

In your client sample:

// models/Client.js
'use strict';

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('Client', {
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
  }, {
    instanceMethods: {
      getFullName: function() {
        return this.first_name + ' ' + this.last_name;
      }
    }
  });
};

Comments

2

https://sequelize.org/v4/manual/tutorial/upgrade-to-v4.html#config-options

Removed classMethods and instanceMethods options from sequelize.define. Sequelize models are now ES6 classes. You can set class / instance level methods like this

Old

const Model = sequelize.define('Model', {
    ...
}, {
    classMethods: {
        associate: function (model) {...}
    },
    instanceMethods: {
        someMethod: function () { ...}
    }
});

New

const Model = sequelize.define('Model', {
    ...
});

// Class Method
Model.associate = function (models) {
    ...associate the models
};

// Instance Method
Model.prototype.someMethod = function () {..}

Comments

-3

I had the same problem, for me it worked to add the method in the classMethods object

// models/Client.js
'use strict';

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('Client', {
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
  }, {
    classMethods: {
      getFullName: function() {
        return this.first_name + ' ' + this.last_name;
      }
    }
  });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.