Pre-save capture and instance methods in Sequelize?

Are there pre-saving and instance methods in Sequelize.js?

In particular, I need to convert this Mongoose code to equivalent Sequelize code:

Scheme

var userSchema = new mongoose.Schema({ username: { type: String, unique: true }, email: { type: String, unique: true }, password: String, token: String }); 

Pre save

 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(); }); }); }); 

Instance method

 userSchema.methods.comparePassword = function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { if(err) return cb(err); cb(null, isMatch); }); }; 
+7
source share
3 answers

The best way is to extend the model using class or instance methods:

 var User = sequelize.define('User', { username: { type: Sequelize.STRING, unique: true }, email: { type: Sequelize.STRING, unique: true }, password: Sequelize.STRING, token: Sequelize.STRING }, { instanceMethods: { comparePassword : function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.getDataValue('password'), function(err, isMatch) { if(err) return cb(err); cb(null, isMatch); }); }, setToken: function(){ // bla bla bla // bla bla bla }, getFullname: function() { return [this.firstname, this.lastname].join(' '); } } }) 

So when you do

 User.build({ firstname: 'foo', lastname: 'bar' }).getFullname(); // 'foo bar' 

So, to set the token, you can do it like this:

 User.build({ ... }).setToken().save(); 

Or, to use the comparePassword function:

 User.find({ ... }).success(function(user) { user.comparePassword('the password to check', function(err, isMatch) { ... } }); 

This can be seen in Sequelize docs.

Edit

The latest versions have hooks for each model, you can check the documentation for hooks that are very simple and complement their class or instance.

+8
source share

I ran into the same problem, but at least in version 2.0, to continue this feature, full documentation is available at Hooks .

The following is sample code that uses beforeValidate binding:

 "use strict"; var md5 = require('blueimp-md5').md5; module.exports = function(sequelize, DataTypes) { var Sms = sequelize.define("sms", { senderName: DataTypes.STRING, smsBody : { type : DataTypes.STRING, allowNull:false }, userId : { type: DataTypes.INTEGER, allowNull:false }, hash : { type:DataTypes.CHAR(32), unique:true, allowNull:false } }); Sms.beforeValidate(function(sms){ sms.hash = md5(sms.smsBody+sms.userId); return sequelize.Promise.resolve(sms) }); return Sms; }; 

The requirement here was to create a hash using smsBody and userId, so I created a hook ie beforeValidate , this hook will be executed before any checks are performed by Sequelize on the model. There are many other hooks available, and best of all, you do not need to write additional code, while you save your data, these hooks will take care of this.

You should choose wisely between hooks and instance methods. But in your case, I think hooks would be a better choice

+4
source share

What you are looking for in the sequilize world is a hook. It will look something like this:

 module.exports = function(sequelize, DataTypes) { var User = sequelize.define('users', { username: { type: String, unique: true }, email: { type: String, unique: true }, password: String, token: String }, hooks: { beforeCreate: function(user){ // do your hashing here to user.password } }); 
0
source share

All Articles