Created
October 28, 2013 22:47
-
-
Save alduro/7206150 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sequelize = require("sequelize"); | |
module.exports = { | |
// These are shortcuts to type and validatation definations commonly used in our models | |
unique_email: { | |
type: Sequelize.STRING, | |
allowNull: false, | |
validate: { | |
notEmpty: true, | |
isEmail: { | |
msg: "must be a valid email" | |
}, | |
} | |
// , unique: true | |
}, | |
module.exports = function(sequelize, Sequelize) { | |
return sequelize.define('User', { | |
firstName : ts.unempty_name, | |
lastName : ts.unempty_name, | |
email : ts.unique_email, | |
password : ts.unempty_password, | |
lastLogin : { type: Sequelize.DATE, allowNull: false, validate: {notEmpty: true}, defaultValue: Sequelize.NOW }, | |
confidence : { type: Sequelize.INTEGER, allowNull: false, validate: {notEmpty: true}, defaultValue: 100, validate: {min: 0, max: 100} }, | |
role : { | |
type: Sequelize.INTEGER, | |
allowNull: false, | |
validate: { | |
notEmpty: true | |
}, | |
defaultValue: 1 | |
} | |
} | |
, { | |
// validate: { | |
// emailAlreadyTaken: function() { | |
// this.find({ where: { email: this.email } }) | |
// .success( function(user) { | |
// if (user) { | |
// throw new Error('already taken!'); | |
// } | |
// }); | |
// } | |
// } | |
}); |
inerte
commented
Oct 28, 2013
unempty_password: {
type: Sequelize.STRING,
allowNull: false,
validate: {
notEmpty: true,
len: {
args: [ 8, 20 ],
msg: "must have 8 to 20 characters"
}
}
},
AccountController.create = function() {
var this_ = this;
var params = this.req.body;
User.create(params)
.success(function(user) {
if (!user)
return this_.res.json(generateResponse(false, 'Error creating user.' , null));
this_.req.logIn(user, function(err) {
if (err) {
return this_.res.json(generateResponse(false, 'Error creating user.' , { err: err } ));
}
return this_.res.json(generateResponse(true, 'Welcome' , { user: user } ));
});
})
.error(function(error) {
if (error) {
var msg = '';
_.each(error, function(message, key) {
msg += key + ': ' + message + '<br/>';
});
return this_.res.json(generateResponse(false, msg , { error: error } ));
}
});
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment