Sequelize classMethods vs instanceMethods

梦想的初衷 提交于 2019-12-04 16:05:43

问题


So starting my adventure into all things Node. One of the tools I am trying to learn is Sequelize. So I will start off what I was trying to do:

'use strict';
var crypto = require('crypto');

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    username: DataTypes.STRING,
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    salt: DataTypes.STRING,
    hashed_pwd: DataTypes.STRING
  }, {
    classMethods: {

    },
    instanceMethods: {
      createSalt: function() {
        return crypto.randomBytes(128).toString('base64');
      },
      hashPassword: function(salt, pwd) {
        var hmac = crypto.createHmac('sha1', salt);

        return hmac.update(pwd).digest('hex');
      },
      authenticate: function(passwordToMatch) {
        return this.hashPassword(this.salt, passwordToMatch) === this.hashed_pwd;
      }
    }
  });
  return User;
};

I am confused on when to use classMethods vs instanceMethods. To me when I think about createSalt() and hashPassword() should be class methods. They are general and for the most part dont really have anything to do with the specific instance they are just used in general. But when I have createSalt() and hashPassword() in classMethods I cannot call them from instanceMethods.

I have tried variations of the following:

this.createSalt();
this.classMethods.createSalt();
createSalt();

Something like below wont work and I am probably just not understanding something simple.

authenticate: function(passwordToMatch) {
  console.log(this.createSalt());
  return this.hashPassword(this.salt, passwordToMatch) === this.hashed_pwd;
}

Any hints/tips/direction would be very much so appreciated!


回答1:


All the method who don't modify or check any type of instance should be classMethod and the rest instanceMethod

ex:

// Should be a classMethods
function getMyFriends() {
  return this.find({where{...}})
}

// Should be a instanceMethods
function checkMyName() {
  return this.name === "george";
}



回答2:


Although the basics are that instance methods should be used when you want to modify your instance ( ergo row ). I would rather not pollute the classMethods with methods that don't use the class ( ergo the table ) itself.

In your example I would put hashPassword function outside your class and leave it as a helper function somewhere in my utilities module ( or why not the same module but as a normal defined function ) ... like

var hashPassword = function(...) { ... }

...

...

  instanceMethods: { 
     authenticate: function( ... ) { hashPassword( ... ) }
  }



回答3:


I found this worked for me as of sequelize 3.14

var myModel = sequelize.define('model', {

}, {
  classMethods: {
    someClassMethod: function() {
      return true;
    }
}, {
  instanceMethods: {
    callClassMethod: function() {
      myModel.someClassMethod();
    }
  }
});


来源:https://stackoverflow.com/questions/34258938/sequelize-classmethods-vs-instancemethods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!