Skip to content

beforeCreate and Model Properties #69

@markandrus

Description

@markandrus

Hi,

Thanks a lot for this project. I was wondering if I'm doing something wrong here—I expect to be able to modify the properties of a model in beforeCreate, but Postgres shows the following in the database:

 id |        email        | password | salt | created 
----+---------------------+----------+------+---------
  1 | test@email.com      | visible? |      | 
var orm    = require('orm'),
    bcrypt = require('bcrypt');

orm.connect('postgres://localhost/database', function (err, db) {
  var Person = db.define('Person', {
    email: String,
    password: String,
    salt: String,
    created: Date
  }, {
    validations: {
      email: orm.validators.unique()
    },
    hooks: {
      beforeCreate: function () {
        if (!this.salt) {
          console.log('Generating salt...');
          this.salt = bcrypt.genSaltSync(10);
          this.created = new Date();
        }
        if (this.password) {
          console.log('Hashing password...');
          this.password = bcrypt.hashSync(this.password, this.salt);
        }
      }
    }
  });
  // Person.sync(function (err) { console.log(err); });
  Person.create([{
    email: 'test@email.com',
    password: 'visible?'
  }], function (err, items) {
    console.log(err);
    // console.log(items);
  });
});

Testing with beforeSave yields different results for me, too: the password gets hashed, but salt and created go unset in the database.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions