sequelize.js Modify attributes in beforeCreate hook Example working with a library that doesn't use Promise


Example

function cryptPassword(password, callback) {
  bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
    if (err)
      return callback(err);

    bcrypt.hash(password, salt, null, function(err, hash) {
      return callback(err, hash);
    });
  });
}

User.beforeCreate((user, options, cb) => {
  cryptPassword(user.password, (err, hash) => {
    if (err) return cb(err);

    user.password = hash;
    // invoking the finish callback is important!
    return cb(null, options);
  });    
});