Skip to content

Commit

Permalink
fixed; allow updating versionKey
Browse files Browse the repository at this point in the history
closes #1265
  • Loading branch information
aheckmann committed Feb 12, 2013
1 parent 556ab76 commit 3af5152
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,15 @@ Model._getSchema = function _getSchema (path) {
*/

Model.compile = function compile (name, schema, collectionName, connection, base) {
var versioningEnabled = false !== schema.options.versionKey;

if (versioningEnabled && !schema.paths[schema.options.versionKey]) {
// add versioning to top level documents only
var o = {};
o[schema.options.versionKey] = Number;
schema.add(o);
}

// generate new class
function model (doc, fields, skipId) {
if (!(this instanceof model))
Expand Down
5 changes: 1 addition & 4 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ function Schema (obj, options) {
if (autoid) {
this.virtual('id').get(idGetter);
}

// versioning not directly added to schema b/c we only want
// it in the top level document, not embedded ones.
};
}

/*!
* Returns this documents _id cast to a string.
Expand Down
6 changes: 6 additions & 0 deletions test/versioning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ var BlogPost = new Schema({
mongoose.model('Versioning', BlogPost);

describe('versioning', function(){
it('is only added to parent schema (gh-1265)', function(done){
assert.ok(BlogPost.path('__v'));
assert.ok(!BlogPost.path('comments').__v);
assert.ok(!BlogPost.path('meta.nested').__v);
done();
})

it('works', function (done) {
var db = start()
Expand Down

0 comments on commit 3af5152

Please sign in to comment.