Skip to content

Instantly share code, notes, and snippets.

@sdepold
Created June 20, 2012 06:36
Show Gist options
  • Save sdepold/2958440 to your computer and use it in GitHub Desktop.
Save sdepold/2958440 to your computer and use it in GitHub Desktop.
"virtual fields" in sequelize
var Sequelize = require('sequelize')
var sequelize = new Sequelize('sequelize_test', 'root')
var Person = sequelize.define('Person', {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING
}, {
instanceMethods: {
getFullName: function() {
return [this.firstName, this.lastName].join(' ')
}
}
})
sequelize.sync().success(function() {
Person
.create({ firstName: 'Sascha', lastName: 'Depold' })
.success(function(sdepold){ console.log(sdepold.getFullName()) })
})
@pdasika82
Copy link

Hello Sascha!

The use case I am referring to is the one mentioned in https://github.com/1602/express-on-railway/issues/77 . While creating the User we should be able to set the password field even though it is not part of the schema whereas the salt and hash would be part of the schema.

For e.g the schema could be defined as

sequelize.define('Users', {
email: DataTypes.STRING,
salt: DataTypes.STRING,
hash : DataTypes.STRING
})

Whereas it can be created as:
User.create(email: '[email protected]' ,
password: userInfo.password)

The setter method abstracts out the population of salt and hash fields and the getter method should return the original password.

There seems to be a pull request which addresses this: https://github.com/sdepold/sequelize/pull/70.

  • Thanks

@sdepold
Copy link
Author

sdepold commented Jun 20, 2012 via email

@pdasika82
Copy link

Hello Sascha!

Done. Added https://gist.github.com/2964211 to the pull request.

Thanks,
Pranil

@sdepold
Copy link
Author

sdepold commented Jun 21, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment