title: Pre-save hook and instance methods in Sequelize?
body: <p>Are there pre-save hooks and instance methods in <a href="http://sequelizejs.com" rel="noreferrer">Sequelize.js?</a></p>
  
  <p>Specifically I need to convert this Mongoose code into an equivalent Sequelize code:</p>
  
  <h2>Schema</h2>
  
  <pre><code>var userSchema = new mongoose.Schema({
    username: { type: String, unique: true },
    email: { type: String, unique: true },
    password: String,
    token: String
  });
  </code></pre>
  
  <h2>Pre-save</h2>
  
  <pre><code>userSchema.pre('save', function(next) {
    var user = this;
  
    var hashContent = user.username + user.password + Date.now() + Math.random();
    user.token = crypto.createHash('sha1').update(hashContent).digest('hex');
  
    if (!user.isModified('password')) return next();
    bcrypt.genSalt(5, function(err, salt) {
      if (err) return next(err);
      bcrypt.hash(user.password, salt, function(err, hash) {
        if (err) return next(err);
        user.password = hash;
        next();
      });
    });
  });
  </code></pre>
  
  <h2>Instance method</h2>
  
  <pre><code>userSchema.methods.comparePassword = function(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
      if(err) return cb(err);
      cb(null, isMatch);
    });
  };
  </code></pre>