From e9baed16dcac117500002e201c1d2a6722e8ec6a Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 16 May 2013 17:28:56 +0200 Subject: [PATCH] Can upsert with modifiers --- lib/datastore.js | 2 +- package.json | 2 +- test/db.test.js | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 28ab4a4..24902e8 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -214,7 +214,7 @@ Datastore.prototype.update = function (query, newDoc, options, cb) { if (doc) { return cb(); } else { - return self.insert(newDoc, function (err) { + return self.insert(model.modify({}, newDoc), function (err) { if (err) { return callback(err); } return callback(null, 1, true); }); diff --git a/package.json b/package.json index 5a06803..b937cb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nedb", - "version": "0.3.1", + "version": "0.3.2", "author": { "name": "tldr.io", "email": "hello@tldr.io" diff --git a/test/db.test.js b/test/db.test.js index 756c141..a9a4caa 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -464,7 +464,23 @@ describe('Database', function () { }); }); }); + }); + + it('Can upsert a document even with modifiers', function (done) { + d.update({ bloup: 'blap' }, { $set: { hello: 'world' } }, { upsert: true }, function (err, nr, upsert) { + assert.isNull(err); + nr.should.equal(1); + upsert.should.equal(true); + + d.find({}, function (err, docs) { + docs.length.should.equal(1); + Object.keys(docs[0]).length.should.equal(2); + docs[0].hello.should.equal('world'); + assert.isDefined(docs[0]._id); + done(); + }); + }); }); }); // ==== End of 'Update' ==== //