Skip to content

Instantly share code, notes, and snippets.

@danmactough
Created July 6, 2011 16:15
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 danmactough/1067637 to your computer and use it in GitHub Desktop.
Save danmactough/1067637 to your computer and use it in GitHub Desktop.
Create a new User record
// Create the record we will test modify
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, crypto = require('crypto')
, sys = require('sys')
, User
;
function defineModels(mongoose, fn) {
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
;
function validatePresenceOf(value) {
return value && value.length;
}
function trim (str) {
var str = str.replace(/^\s\s*/, '')
, ws = /\s/
, i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
var User = new Schema ({
'email' : { type: String, set: trim, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } }
, 'hashed_password': String
, 'salt': String
, 'username': { type: String, set: trim, validate: [validatePresenceOf, 'a username is required'], index: { unique: true } }
, 'fullname': { type: String, set: trim }
, 'homepage': { type: String, set: trim }
, 'prefs' : {
'sharing' : { type: Boolean, default: true }
, 'https' : { type: Boolean, default: false }
}
, 'role' : { type: String, enum: [ 'admin', 'user' ], default: 'user' }
, 'date': Date
, 'deleted': { type: Boolean, default: false }
, 'count' : { type: Number, min: 0, default: 1 }
}
, {use$SetOnSave: false}
);
User.path('date')
.default(function() {
return new Date()
})
.set(function(v){
return v == 'now' ? new Date() : v;
});
User.virtual('id')
.get(function() {
return this._id.toHexString();
});
User.virtual('password')
.set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
})
.get(function() { return this._password; });
User.method('authenticate', function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
});
User.method('makeSalt', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
User.method('encryptPassword', function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
});
User.pre('save', function(next) {
if (!this.isNew) next();
if (!validatePresenceOf(this.password)) {
next(new Error('Invalid password'));
} else {
next();
}
});
mongoose.model('User', User);
fn();
};
defineModels(mongoose, function() {
User = mongoose.model('User');
db = mongoose.connect('mongodb://localhost/use-set-test');
});
User.remove({}, function(){
sys.puts('All users removed');
});
var user = new User({email:'dan@foo.com', username:'dan', password:'foobar'});
user.save(function(err){
if(!err) {
sys.puts('Created new user: '+ JSON.stringify(user));
}
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment