0

I am new to sequelize.js and basically am trying to refactor code that I've written in the controller and came across classMethods and instanceMethods. I see instance methods defined like so:

/lib/model/db/users.js

module.exports = function(sequelize, DataTypes) {

  var instance_methods = get_instance_methods(sequelize);

  var User = sequelize.define("User", {
      email : {
          type      : DataTypes.STRING,
          allowNull : false
      },
  }, {
      classMethods: class_methods,
      instanceMethods : instance_methods,
    });

    return User;
};

function get_instance_methods(sequelize) {
  return {
    is_my_password : function( password ) {
        return sequelize.models.User.hashify_password( password ) === this.password;
    },   
};

function get_class_methods(sequelize) {
  return {
    hashify_password : function( password ) {
      return crypto
        .createHash('md5')
        .update(
          password + config.get('crypto_secret'),
          (config.get('crypto_hash_encoding') || 'binary')
        )
        .digest('hex');
    },
}; 

My understanding of the above is that classMethods are generic functions defined for the whole model and instanceMethods are basically a reference to a given row in a table/model, am I right in assuming this ? This would be my primary question.

Also I don't see any reference of classMethods and instanceMethods in the docs. I only found this previous answer. That provides a somewhat comprehensive understanding of the difference between instanceMethods and classMethods.

Basically I'm just trying to confirm whether my understanding matches the intended usage for class vs instance methods.

2 Answers 2

1

The official way to add both static and instance methods is using classes like this:

class User extends Model {
  static classLevelMethod() {
    return 'foo';
  }
  instanceLevelMethod() {
    return 'bar';
  }
  getFullname() {
    return [this.firstname, this.lastname].join(' ');
  }
}
User.init({
  firstname: Sequelize.TEXT,
  lastname: Sequelize.TEXT
}, { sequelize });

See Models as classes

Sign up to request clarification or add additional context in comments.

3 Comments

thanks can you share some light on this My understanding of the above is that classMethods are generic functions defined for the whole model and instanceMethods are basically a reference to a given row in a table/model, am i right in assuming this ?
Yes, you're quite right
thanks a ton ! cheers
1

Your understand is correct. In short: classes can have instances. Models are classes. So, Models can have instances. When working with an instance method, you will notice the this — which is the context, which refers to that particular class/model instance.

Hence, if you have a User model that has:

  • an instance method called is_my_password
  • a class model called hashify_password

User.hashify_password('123') will return the hashed version of 123. The User instance is not needed here. hashify_password is general function attached to the User model (class).

Now, if you'd like to call is_my_password() you do need a User instance:

User.findOne({...}).then(function (user) {
  if (user.is_my_password('123')) {
     // ^ Here we call `is_my_password` as a method of the user instance.
     ...
  }
}).catch(console.error)

In general, when you have functions that do not need the particular model instance data, you will define them as class methods. They are static methods.

And when the function works with the instance data, you define it as instance method to make it easier and nicer to call.

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.