Skip to content

Instantly share code, notes, and snippets.

@calmdev calmdev/user.js
Created May 28, 2013

Embed
What would you like to do?
sequelize.define('User', {
id: DataTypes.INTEGER,
email: {
type: DataTypes.STRING,
validate: {
isEmail: {
msg: 'Invalid email address.'
}
}
}
}, {
classMethods: {
// User.add({ email: 'test@email.com' }, callback(user, error));
add: function(attributes, callback) {
var self = this;
var user = this.build(attributes);
var errors = user.validate();
if(errors) {
callback(null, errors[Object.keys(errors)[0]]);
}else{
this.find({
where: { email: user.email }
}).success(function(account) {
if(account) {
callback(null, 'Email already registered.');
}else{
user.save().success(function(user) {
self.getById(user.id, function(user) {
callback(user);
});
});
}
});
}
},
// User.getById(1, callback(user));
getById: function(id, callback) {
this.find({
where: { id: id }
}).success(function(user) {
callback(user);
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.