Skip to content

Instantly share code, notes, and snippets.

@waggertron
Forked from pranildasika/gist:2964211
Created December 18, 2018 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waggertron/0956fd107a4683fc381e0e603785387d to your computer and use it in GitHub Desktop.
Save waggertron/0956fd107a4683fc381e0e603785387d to your computer and use it in GitHub Desktop.
Virtual fields using getter and setter methods in sequelize
var Sequelize = require('sequelize')
var sequelize = new Sequelize('sequelize_test', 'root')
//Note that the model definition does not have "fullName"
var User = sequelize.define('User', {
email: Sequelize.STRING,
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
},
{
getterMethods: {
fullName: function() {
return this.firstName + '' + this.lastName
}
},
setterMethods: {
fullName: function(fullname) {
var split = fullname.split(''),
this.firstName = split[0],
this.lastName = split[1]
}
}
})
sequelize.sync().success(function() {
User
.create({ email: 'john@smith.com', fullName: 'John Smith' })
.success(function(jsmith){ console.log(jsmith.fullName) })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment