-
Notifications
You must be signed in to change notification settings - Fork 369
Closed
Description
Hey, say I have a model like:
User = db.define('user',{
email: String
},{
validations: {
email: orm.validators.unique()
},
hooks: {
beforeSave: function() {
this.email = email.toLowerCase();
}
}
});All is jolly, however:
user1 = db.models.user.new({email: 'a@b.c'});
user1.save(...);
...
user2 = db.models.user.new({email: 'A@b.c'});
user2.save(...);We now have two duplicate users.
(I actually have a unique index on the table to prevent this, but it throws an ugly error when saving)
I can think of two ways of solving this:
- Add a
beforeValidatehook. - Make it possible to add custom setters/getters for model properties.
I imagine it working something like this:
User = db.define('user',{
email: String
},{
setters: {
email: function(val) {
return val.toLowerCase();
}
}
});Or maybe even more fancy:
User = db.define('user',{
email: String
},{
methods: {
email: {
set: function(val) {
this.setAttribute('email', val.toLowerCase());
}
}
}
});where it defaults to the standard getter as one isn't specified, and setAttribute calls the default setter.
What do you think?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels