title: Migrating from a Mongoose Model to Postgres NodeJS
body: <p>I have a node project which I use twillo to verify Phone number and it works with MongoDB, now I want to transfer the same logic to an old project which uses Posgres and Bookshelf to create tables, I have tried adding the necessary fields and but it does not reflect in the database as I would want it to, and How to effectively write methods with the Bookshelf model. Below is my postgres code and my Mongo model that works which I want to replicate with postgres</p>
  
  <pre><code>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);
    });
  };
  </code></pre>
  
  <p>here is my current Postgres model</p>
  
  <pre><code>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')
      ]}
  </code></pre>
  
  <p>I just added the <code>authyId</code> and <code>verified</code> fields and still, they do not reflect in my postgres table. further code would be supplied on request, I am equally new to NodeJS</p>