Skip to content

Instantly share code, notes, and snippets.

@alduro
Created October 28, 2013 22:47
Show Gist options
  • Save alduro/7206150 to your computer and use it in GitHub Desktop.
Save alduro/7206150 to your computer and use it in GitHub Desktop.
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
Copy link

inerte commented Oct 28, 2013

var User = sequelize.define('User', {
    email: {
        type: Sequelize.STRING,
        allowNull: false,
        validate: {
            notEmpty: true,
            isEmail: true,
        },
    }
}, {
    validate: {
        emailAlreadyTaken: function (done) {
            return this.daoFactory.find({
                where: {
                    email: this.email
                }
            }).success(function (user) {
                if (user) {
                    throw new Error('already taken!');
                }
            }).then(done, done);
        }
    }
});

return sequelize.sync().then(function () {
    User.create({
        email: "inerte@gmail.com",
    }).then(function () {
        console.log("first user created");

        return User.create({
            email: "inerte@gmail.com",
        });
    }).then(null, function (error) {
        console.log("failed to create user!");
        console.log("error", error);
    });
});

@alduro
Copy link
Author

alduro 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