From fd6bc22e5636a4429700ffc0fc6a23aba78bd16a Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 16 May 2013 17:05:00 +0200 Subject: [PATCH] Dont throw an error if updateQuery contains an _id that is the same as obj --- lib/model.js | 2 +- package.json | 2 +- test/model.test.js | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/model.js b/lib/model.js index 0aebe56..af7c306 100644 --- a/lib/model.js +++ b/lib/model.js @@ -184,7 +184,7 @@ function modify (obj, updateQuery) { , newDoc, modifiers ; - if (keys.indexOf('_id') !== -1) { throw "You cannot change a document's _id"; } + if (keys.indexOf('_id') !== -1 && updateQuery._id !== obj._id) { throw "You cannot change a document's _id"; } if (dollarFirstChars.length !== 0 && dollarFirstChars.length !== firstChars.length) { throw "You cannot mix modifiers and normal fields"; diff --git a/package.json b/package.json index d440778..5a06803 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nedb", - "version": "0.3.0", + "version": "0.3.1", "author": { "name": "tldr.io", "email": "hello@tldr.io" diff --git a/test/model.test.js b/test/model.test.js index df9c08b..758787d 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -212,7 +212,7 @@ describe('Model', function () { t._id.should.equal('keepit'); }); - it('Throw an error if trying to replace the _id field in a copy-type modification', function () { + it('Throw an error if trying to change the _id field in a copy-type modification', function () { var obj = { some: 'thing', _id: 'keepit' } , updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' } ; @@ -220,6 +220,9 @@ describe('Model', function () { (function () { model.modify(obj, updateQuery); }).should.throw(); + + updateQuery._id = 'keepit'; + model.modify(obj, updateQuery); // No error thrown }); it('Throw an error if trying to use modify in a mixed copy+modify way', function () {