mongoose模式序列化模型

我做了一个与MongoDB(mongoose作为ODM)的应用程序,但现在想与MySQL(工作义务),所以我拿了Sequelize模块,但我真的不明白如何将我的userSchema转换为用户模型的所有方法与passportJsauthentication,所以我有一些方法,即时通讯使用例如setpassword …)

在这里我的userSchema(mongoose),完美的作品。

var mongoose = require('mongoose'); var crypto = require('crypto'); var jwt = require('jsonwebtoken'); var validator = require('node-mongoose-validator'); var Schema = mongoose.Schema; var userSchema = new Schema({ name: { type: String, maxlength: 50}, mail: { type: String, required: true, maxlength: 50, index: { unique: true } }, hash: String, salt: String, { collection: "user" } ); userSchema.methods.setPassword = function(password) { this.salt = crypto.randomBytes(16).toString('hex'); this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex'); }; userSchema.methods.validPassword = function(password) { var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex'); return this.hash === hash; }; userSchema.methods.generateJwt = function() { var expiry = new Date(); expiry.setDate(expiry.getDate() + 7); return jwt.sign({ _id: this._id, mail: this.mail, name: this.name, exp: parseInt(expiry.getTime() / 1000), }, process.env.JWT_SECRET); // secret code from .env }; module.exports = mongoose.model('user', userSchema); 

在这里,我尝试了sequelize:

  var crypto = require('crypto'); var jwt = require('jsonwebtoken'); var User = sequelize.define('user', { name: Sequelize.STRING, mail: Sequelize.STRING, hash: Sequelize.STRING, salt: Sequelize.STRING }); User.methods.setPassword = function(password) { this.salt = crypto.randomBytes(16).toString('hex'); this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex'); }; User.methods.validPassword = function(password) { var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex'); return this.hash === hash; }; User.methods.generateJwt = function() { var expiry = new Date(); expiry.setDate(expiry.getDate() + 7); return jwt.sign({ _id: this._id, mail: this.mail, name: this.name, exp: parseInt(expiry.getTime() / 1000), }, process.env.JWT_SECRET); // DO NOT KEEP YOUR SECRET IN THE CODE! }; module.exports = User; 

我没有testing,因为ineed开发另一个部分,但我需要知道,你是否考虑到这一点,我觉得它充满了错误。

先谢谢你

       

网上收集的解决方案 "mongoose模式序列化模型"