Sah*_*bov 10 mongoose node.js sequelize.js
Sequelize.js中有预先保存的钩子和实例方法吗?
具体来说,我需要将此Mongoose代码转换为等效的Sequelize代码:
var userSchema = new mongoose.Schema({
username: { type: String, unique: true },
email: { type: String, unique: true },
password: String,
token: String
});
Run Code Online (Sandbox Code Playgroud)
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();
});
});
});
Run Code Online (Sandbox Code Playgroud)
userSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if(err) return cb(err);
cb(null, isMatch);
});
};
Run Code Online (Sandbox Code Playgroud)
最好的方法是使用类或实例方法扩展模型:
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(' ');
}
}
})
Run Code Online (Sandbox Code Playgroud)
所以,当你这样做
User.build({ firstname: 'foo', lastname: 'bar' }).getFullname(); // 'foo bar'
Run Code Online (Sandbox Code Playgroud)
因此,要设置令牌,您可以这样做:
User.build({ ... }).setToken().save();
Run Code Online (Sandbox Code Playgroud)
或者,使用comparePassword函数:
User.find({ ... }).success(function(user) { user.comparePassword('the password to check', function(err, isMatch) { ... } });
Run Code Online (Sandbox Code Playgroud)
您可以在Sequelize文档中看到这一点
编辑
在最新版本的每个模型都有钩子,你可以查看非常简单的钩子文档,并用类或实例方法补充它们.
我遇到了同样的问题,但是至少在2.0版本的sequelize中可以使用此功能,完整的文档可在Hooks上获得。
以下是使用beforeValidate挂钩的示例代码:
"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;
};
Run Code Online (Sandbox Code Playgroud)
这里的要求是,使用smsBody和userId创建一个哈希,因此我创建了一个钩子,即beforeValidate,该钩子将在Sequelize对模型执行任何验证之前执行。还有许多其他可用的挂钩,最好的部分是您在保存数据时不必编写任何其他代码,这些挂钩将解决这一问题。
您应该在钩子和instanceMethods之间明智地选择。但是在您的情况下,我想钩子会是一个更好的选择