Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql mongoose模式对模型进行续集_Mysql_Node.js_Mongodb_Mongoose_Sequelize.js - Fatal编程技术网

Mysql mongoose模式对模型进行续集

Mysql mongoose模式对模型进行续集,mysql,node.js,mongodb,mongoose,sequelize.js,Mysql,Node.js,Mongodb,Mongoose,Sequelize.js,我用mongodb(mongoose作为ODM)制作了一个应用程序,但现在我想用MySQL(工作义务),所以我用了Sequelize模块,但我真的不知道如何用它的所有方法将我的用户模式转换成用户模型(我正在使用passportJs进行身份验证,因此我使用了一些方法,例如setpassword…) 这里是我的userSchema(mongoose),它工作得非常好 var mongoose = require('mongoose'); var crypto = require('crypto');

我用
mongodb
(mongoose作为ODM)制作了一个应用程序,但现在我想用
MySQL
(工作义务),所以我用了
Sequelize
模块,但我真的不知道如何用它的所有方法将我的用户模式转换成用户模型(我正在使用
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;
我没有测试它,因为我需要开发另一个部分,但我需要知道,你认为呢,我觉得它充满了错误


提前感谢

我将重点介绍如何使用实例方法定义模型,一些特定于passportjs和mongoose的逻辑可能需要以不同的方式实现

有很多方法可以定义一个模型

  • 与您实现的方式类似,您可以使用
    User.prototype.yourMethod
    since
    User
  • var User=sequelize.define('User'){
    名称:DataTypes.STRING,
    邮件:DataTypes.STRING,
    hash:DataTypes.STRING,
    salt:DataTypes.STRING,
    /* ... */
    });
    User.prototype.validPassword=函数(密码){
    var hash=crypto.pbkdf2Sync(密码,this.salt,1000,64).toString('hex');
    返回this.hash==hash;
    };
    /*
    如果您想要等效的User.statics.yourStaticMethod=function(){}
    或User.static('yourstaticmethod',function(){})
    您可以使用以下命令
    */
    User.yourStaticMethod=function(){};
    
  • 你也可以

  • 很好的问题,但没有答案:(你能让Mongoose向你展示生成的SQL吗?
    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'
    });