Created
October 19, 2012 15:56
-
-
Save aheckmann/3919001 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'); | |
var Schema = mongoose.Schema; | |
var assert = require('assert') | |
console.log('\n==========='); | |
console.log(' mongoose version: %s', mongoose.version); | |
console.log('========\n\n'); | |
var dbname = 'testing_neverMarkModified'; | |
console.log('dbname: %s', dbname); | |
mongoose.connect('localhost', dbname); | |
mongoose.connection.on('error', function () { | |
console.error('connection error', arguments); | |
}); | |
var schema = new Schema({ | |
_name: String | |
}); | |
schema.virtual('name').set(function (v) { | |
console.log('ignoring', v); | |
}).get(function () { | |
return this._name; | |
}); | |
schema.pre('save', function (next) { | |
this.set('_name', 'set from pre-save only'); | |
next(); | |
}) | |
var A = mongoose.model('A', schema); | |
mongoose.connection.on('open', function () { | |
var a = new A({ name: 'neverMarkModified' }); | |
console.log(a); | |
a.name = 'trying to set directly'; | |
console.log('name is %s', a.name); | |
a.save(function (err, a) { | |
if (err) return done(err); | |
console.log('name is %s', a.name); | |
A.findById(a, function (err, doc) { | |
console.error('found', doc); | |
done(err); | |
}); | |
}) | |
}); | |
function done (err) { | |
if (err) console.error(err.stack); | |
mongoose.connection.db.dropDatabase(function () { | |
mongoose.connection.close(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment