mongoose Schema to sequelize model mongoose Schema to sequelize model mongoose mongoose

mongoose Schema to sequelize model


I will focus on defining a model with instance methods, some logic specific to passportjs and mongoose you might have to implement differently.

There are 2 ways to define a model.

  1. With sequelize.define like the way you implemented, you can attach instance methods using User.prototype.yourMethod since User is a ES6 class
var User = sequelize.define('user', {  name: DataTypes.STRING,  mail: DataTypes.STRING,  hash: DataTypes.STRING,  salt: DataTypes.STRING,/* ... */});User.prototype.validPassword = function(password) {  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');  return this.hash === hash;};/*If you want an equivalent of User.statics.yourStaticMethod = function() {}or User.static('yourstaticmethod', function() {})You can use the following*/User.yourStaticMethod = function() {};
  1. You can also extend Model
class User extends Model {  static yourStaticMethod() {} // in mongoose equivalent to User.statics.yourStaticMethod = function() {}  validPassword(password) {    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');    return this.hash === hash;  }};User.init({  name: DataTypes.STRING,  mail: DataTypes.STRING,  hash: DataTypes.STRING,  salt: DataTypes.STRING,/* ... */}, {  sequelize,  modelName: 'user'});