从猫鼬模型迁移到Postgres NodeJS

时间:2019-06-05 11:26:15

标签: node.js mongodb postgresql mongoose bookshelf.js

我有一个节点项目,该项目使用twillo验证电话号码,并且可与MongoDB一起使用,现在我想将相同的逻辑转移到一个使用Posgres和Bookshelf创建表的旧项目中,我尝试添加必要的字段但是它并没有像我希望的那样反映在数据库中,以及如何使用Bookshelf模型有效地编写方法。下面是我的postgres代码和可正常使用的Mongo模型,我想用postgres复制它

const UserSchemax = new mongoose.Schema({
  fullName: {
    type: String,
    required: true,
  },
  countryCode: {
    type: String,
    required: true,
  },
  phone: {
    type: String,
    required: true,
  },
  verified: {
    type: Boolean,
    default: false,
  },
  authyId: String,
  email: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
});
// Middleware executed before save - hash the user's password
UserSchemax.pre('save', function (next) {
  const self = this;
  // only hash the password if it has been modified (or is new)
  if (!self.isModified('password')) return next();
  // generate a salt
  bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
    if (err) return next(err);
    // hash the password using our new salt
    bcrypt.hash(self.password, salt, function (err, hash) {
      if (err) return next(err);
      // override the cleartext password with the hashed one
      self.password = hash;
      next();
    });
  });
});
// Test candidate password
UserSchemax.methods.comparePassword = function (candidatePassword, cb) {
  const self = this;
  bcrypt.compare(candidatePassword, self.password, function (err, isMatch) {
    if (err) return cb(err);
    cb(null, isMatch);
  });
};
// Send a verification token to this user
UserSchemax.methods.sendAuthyToken = function (cb) {
  var self = this;
  if (!self.authyId) {
    // Register this user if it's a new user
    authy.register_user(self.email, self.phone, self.countryCode,
      function (err, response) {
        if (err || !response.user) return cb.call(self, err);
        self.authyId = response.user.id;
        self.save(function (err, doc) {
          if (err || !doc) return cb.call(self, err);
          self = doc;
          sendToken();
        });
      });
  } else {
    // Otherwise send token to a known user
    sendToken();
  }
  // With a valid Authy ID, send the 2FA token for this user
  function sendToken() {
    authy.request_sms(self.authyId, true, function (err, response) {
      cb.call(self, err);
    });
  }
};
// Test a 2FA token
UserSchemax.methods.verifyAuthyToken = function (otp, cb) {
  const self = this;
  authy.verify(self.authyId, otp, function (err, response) {
    cb.call(self, err, response);
  });
};

这是我当前的Postgres模型

const User = models.Model.extend({
    tableName: 'users',
    uuid: true,

    schema: [
        fields.StringField('first_name'),
        fields.StringField('last_name'),
        fields.StringField('photo'),
        fields.StringField('phone'),
        fields.EmailField('email'),
        fields.BooleanField('verified')
        fields.StringField('authyId')
        fields.EncryptedStringField('password', {
            min_length: {
                value: 8,
                message: '{{label}} must be at least 8 characters long!'
            }
        }),
        fields.BooleanField('old_account')
    ]}

我刚刚添加了authyIdverified字段,但它们仍未反映在我的postgres表中。进一步的代码将应要求提供,我对NodeJS还是陌生的

0 个答案:

没有答案