couchdb-force
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -11,2 +11,7 @@ 'use strict'; | ||
function bulkInsert(couchdbAddr, docs, options) { | ||
// Short-circuit if there are no docs | ||
if (!docs.length) { | ||
return Promise.resolve(docs); | ||
} | ||
docs = docs.map((doc) => Object.assign({}, doc)); // We will be mutating the docs, so shallow clone them | ||
@@ -40,2 +45,4 @@ options = Object.assign({ | ||
doc._rev = undefined; // Remove the rev so that it is retrieved when retrying | ||
} else { | ||
remainingDocs.splice(index, 1); | ||
} | ||
@@ -42,0 +49,0 @@ } |
@@ -13,2 +13,8 @@ 'use strict'; | ||
function bulkPatch(couchdbAddr, patches, options) { | ||
// Short-circuit if there are no patches | ||
if (!patches.length) { | ||
return Promise.resolve(patches); | ||
} | ||
// Check if any of the patches has _rev | ||
const hasRevs = patches.some((patch) => patch._rev !== undefined); | ||
@@ -74,2 +80,4 @@ | ||
docsHash[doc._id] = null; // Remove the doc so that it is retrieved when retrying | ||
} else { | ||
remainingPatches.splice(index, 1); | ||
} | ||
@@ -76,0 +84,0 @@ } |
{ | ||
"name": "couchdb-force", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Updates documents in CouchDB without having to fetch them", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -161,3 +161,3 @@ # couchdb-force | ||
- `create`: Create the document in case it doesn't yet exists (defaults to `true`). | ||
- `docs`: If you already have the documents, you may pass them to avoid having to fetch them in the first try (defaults to `[]`); the order must be the same as `patches`. | ||
- `docs`: If you already have the documents, you may pass them to avoid having to fetch them in the first try (defaults to `null`); the order must be the same as `patches`. | ||
- `nano`: Custom options to be used when creating the [nano]((https://www.npmjs.com/package/nano)) instance, defaults to `null`. | ||
@@ -164,0 +164,0 @@ |
@@ -123,2 +123,23 @@ 'use strict'; | ||
it('should fail if couchdb fails when inserting the doc (non-conflict)', () => { | ||
const betrayBulk = betrayed(couchdb, 'bulk', (key, callback) => callback(null, [ | ||
{ id: 'bulk-insert-insert-error', error: 'foo', reason: 'bar' }, | ||
])); | ||
return couchdbForce.bulkInsert(couchdb, [{ _id: 'bulk-insert-insert-error' }]) | ||
.then(() => { | ||
throw new Error('Expected to fail'); | ||
}, (err) => { | ||
expect(betrayBulk.invoked).to.equal(1); | ||
expect(err.message).to.match(/failed.+1/i); | ||
expect(Object.keys(err.errors)).to.have.length(1); | ||
const docError = err.errors['bulk-insert-insert-error']; | ||
expect(docError.message).to.equal('bar'); | ||
expect(docError.error).to.equal('foo'); | ||
expect(docError.reason).to.equal('bar'); | ||
}); | ||
}); | ||
it('should maintain the input order', () => { | ||
@@ -135,3 +156,12 @@ return couchdb.insertAsync({ _id: 'bulk-insert-input-order-2' }) | ||
}); | ||
it('should not do any request to CouchDB on empty arrays', () => { | ||
const betrayBulk = betrayed(couchdb, 'bulk'); | ||
return couchdbForce.bulkInsert(couchdb, []) | ||
.then(() => { | ||
expect(betrayBulk.invoked).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
}; |
@@ -16,3 +16,3 @@ 'use strict'; | ||
it('should force patch document', () => { | ||
it('should force patch documents', () => { | ||
return Promise.resolve() | ||
@@ -179,2 +179,23 @@ // Test create docs | ||
it('should fail if couchdb fails when inserting the doc (non-conflict)', () => { | ||
const betrayBulk = betrayed(couchdb, 'bulk', (key, callback) => callback(null, [ | ||
{ id: 'bulk-patch-insert-error', error: 'foo', reason: 'bar' }, | ||
])); | ||
return couchdbForce.bulkPatch(couchdb, [{ _id: 'bulk-patch-insert-error' }]) | ||
.then(() => { | ||
throw new Error('Expected to fail'); | ||
}, (err) => { | ||
expect(betrayBulk.invoked).to.equal(1); | ||
expect(err.message).to.match(/failed.+1/i); | ||
expect(Object.keys(err.errors)).to.have.length(1); | ||
const docError = err.errors['bulk-patch-insert-error']; | ||
expect(docError.message).to.equal('bar'); | ||
expect(docError.error).to.equal('foo'); | ||
expect(docError.reason).to.equal('bar'); | ||
}); | ||
}); | ||
it('should maintain the input order', () => { | ||
@@ -194,3 +215,14 @@ return couchdb.insertAsync({ _id: 'bulk-patch-input-order-2' }) | ||
}); | ||
it('should not do any request to CouchDB on empty arrays', () => { | ||
const betrayFetch = betrayed(couchdb, 'fetch'); | ||
const betrayBulk = betrayed(couchdb, 'bulk'); | ||
return couchdbForce.bulkPatch(couchdb, []) | ||
.then(() => { | ||
expect(betrayFetch.invoked).to.equal(0); | ||
expect(betrayBulk.invoked).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
}; |
54017
939