【问题标题】:mongoose Schema to sequelize model用于续集模型的猫鼬模式
【发布时间】:2016-07-03 16:55:56
【问题描述】:

我用mongodb(猫鼬作为ODM)制作了一个应用程序,但现在我想使用MySQL(工作义务)所以我为此使用了Sequelize模块,但我真的不明白如何转换我的 userSchema 到用户模型及其所有方法(我正在使用 passportJs 进行身份验证,所以我有一些我正在使用的方法,例如 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;

我没有测试,因为我需要开发另一部分,但我需要知道你是否考虑过,我觉得它充满了错误

提前谢谢你

【问题讨论】:

  • 很好的问题,但没有答案:(
  • 你能让 Mongoose 向你展示生成的 SQL 吗?

标签: mysql node.js mongodb mongoose sequelize.js


【解决方案1】:

我将专注于使用实例方法定义模型,一些特定于 passportjs 和 mongoose 的逻辑,您可能需要以不同的方式实现。

2 ways定义模型。

  1. 使用sequelize.define 就像您实现的方式一样,您可以使用User.prototype.yourMethod 附加实例方法,因为User is a ES6 class
var User = sequelize.define('user', {
  name: DataTypes.STRING,
  mail: DataTypes.STRING,
  hash: DataTypes.STRING,
  salt: DataTypes.STRING,
/* ... */
});

User.prototype.validPassword = function(password) {
  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  return this.hash === hash;
};

/*
If you want an equivalent of User.statics.yourStaticMethod = function() {}
or User.static('yourstaticmethod', function() {})
You can use the following
*/

User.yourStaticMethod = function() {};
  1. 您也可以extend Model
class User extends Model {
  static yourStaticMethod() {} // in mongoose equivalent to User.statics.yourStaticMethod = function() {}
  validPassword(password) {
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash === hash;
  }
};

User.init({
  name: DataTypes.STRING,
  mail: DataTypes.STRING,
  hash: DataTypes.STRING,
  salt: DataTypes.STRING,
/* ... */
}, {
  sequelize,
  modelName: 'user'
});


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 2013-06-23
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多