Created
July 6, 2011 16:20
-
-
Save danmactough/1067650 to your computer and use it in GitHub Desktop.
Modify the record (working)
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 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.findById( process.argv[2], function(err, doc) { | |
if (err) sys.puts('Error: '+ JSON.stringify(err)); | |
else if (doc) { | |
sys.puts('Original user: '+ JSON.stringify(doc)); | |
var newStuff = {}; | |
newStuff.email = '[email protected]'; | |
newStuff.prefs = { sharing: false, https: true }; | |
Object.keys(newStuff).forEach (function(key) { | |
if (doc[key] != newStuff[key]) doc[key] = newStuff[key]; | |
}); | |
doc.save(function(err){ | |
if(!err) { | |
sys.puts('Updated user per doc.save(): '+ JSON.stringify(doc)); | |
User.find( {}, function(err2, doc2) { | |
sys.puts('Actual doc...: '+ JSON.stringify(doc2)); | |
process.exit(0); | |
}); | |
} | |
else sys.puts('Error: '+ JSON.stringify(err)); | |
}); | |
} else sys.puts('User '+process.argv[2]+' not found'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment