Skip to content

Instantly share code, notes, and snippets.

@calmdev
Created May 28, 2013 15:34
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 calmdev/5663640 to your computer and use it in GitHub Desktop.
Save calmdev/5663640 to your computer and use it in GitHub Desktop.
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