From 5be5b27b138070b2fa3109c66010a624a713a5cd Mon Sep 17 00:00:00 2001 From: Geoff Cox Date: Wed, 11 Oct 2017 14:17:42 -0700 Subject: [PATCH] feat(doc): bulk create or update --- scripts/doc.js | 11 ++++++++++ test/spec/doc.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/scripts/doc.js b/scripts/doc.js index d8738f1..ad5a589 100644 --- a/scripts/doc.js +++ b/scripts/doc.js @@ -336,4 +336,15 @@ Doc.prototype.setDestroyed = function (doc) { doc._deleted = true; }; +Doc.prototype.bulkCreateOrUpdate = function (dbName, docs) { + return this._slouch._req({ + uri: this._slouch._url + '/' + dbName + '/_bulk_docs', + method: 'POST', + json: { + docs: docs + }, + parseBody: true + }); +}; + module.exports = Doc; diff --git a/test/spec/doc.js b/test/spec/doc.js index ae6223f..30a022c 100644 --- a/test/spec/doc.js +++ b/test/spec/doc.js @@ -506,4 +506,61 @@ describe('doc', function () { }); }); + it('should bulk create or update', function () { + // Bulk create with a single doc + return slouch.doc.bulkCreateOrUpdate(utils.createdDB, [{ + _id: '1', + thing: 'read' + }, { + _id: '2', + thing: 'jam' + }]).then(function (docs) { + docs.length.should.eql(2); + + docs[0].id.should.eql('1'); + docs[0].ok.should.eql(true); + (docs[0].rev === undefined).should.eql(false); + + docs[1].id.should.eql('2'); + docs[1].ok.should.eql(true); + (docs[1].rev === undefined).should.eql(false); + + // Edit doc so that we can prepare for conflict + return slouch.doc.update(utils.createdDB, { + _id: '1', + _rev: docs[0].rev, + thing: 'read books' + }).then(function () { + // Create 1 doc and update 2 docs. The first update should result in a conflict + return slouch.doc.bulkCreateOrUpdate(utils.createdDB, [{ + _id: '1', + _rev: docs[0].rev, // outdated rev + thing: 'read many books' + }, { + thing: 'play' + }, { + _id: '2', + _rev: docs[1].rev, + thing: 'jam on guitar' + }]); + }).then(function (docs) { + docs.length.should.eql(3); + + // 1st operation should have resulted in conflict + docs[0].id.should.eql('1'); + docs[0].error.should.eql('conflict'); + + // 2nd operation should have succeeded + (docs[1].id === undefined).should.eql(false); + docs[1].ok.should.eql(true); + (docs[1].rev === undefined).should.eql(false); + + // 3rd operation should have succeeded and rev should have changed + (docs[2].id === undefined).should.eql(false); + docs[2].ok.should.eql(true); + docs[2].rev.should.not.eql(docs[1].rev); + }); + }); + }); + });