Skip to content

Instantly share code, notes, and snippets.

@sdepold
Created October 29, 2013 18:24
Show Gist options
  • Save sdepold/7220001 to your computer and use it in GitHub Desktop.
Save sdepold/7220001 to your computer and use it in GitHub Desktop.
virtual attributes with sequelize
var Sequelize = require('sequelize')
, sequelize = new Sequelize('sequelize_test', 'root')
var User = sequelize.define('User', {
username: Sequelize.STRING,
password_hash: Sequelize.STRING
}, {
validate: {
password: function(next) {
if (((this._password || "").trim() !== '') && (this._password === this._password_confirmation)) {
next()
} else {
next('Password not matching!')
}
}
},
getterMethods: {
password: function() { return this._password },
password_confirmation: function() { return this._password_confirmation }
},
setterMethods: {
password: function(v) { this._password = v },
password_confirmation: function(v) { this._password_confirmation = v }
}
})
User.sync({ force: true }).success(function() {
User
.create({ username: 'foo', password: 'something', password_confirmation: 'different' })
.complete(function(err, user) {
console.log(err)
User
.create({ username: 'foo', password: 'something', password_confirmation: 'something' })
.complete(function(err, user) {
// should be empty
console.log(err)
console.log(user.values)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment