Merge pull request #323 from cttttt/patch-1
body should be optional in db.atomic(design, updater, id, [body], [callback])
diff --git a/lib/nano.js b/lib/nano.js
index dc819ac..81d12d7 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -563,6 +563,10 @@
// http://docs.couchdb.org/en/latest/api/ddoc/render.html#put--db-_design-ddoc-_update-func-docid
function updateWithHandler(ddoc, viewName, docName, body, callback) {
+ if (typeof body === 'function') {
+ callback = body;
+ body = undefined;
+ }
return view(ddoc, viewName + '/' + encodeURIComponent(docName), {
type: 'update',
method: 'PUT',
diff --git a/tests/fixtures/design/atomic.json b/tests/fixtures/design/atomic.json
index 5a58b91..5712da0 100644
--- a/tests/fixtures/design/atomic.json
+++ b/tests/fixtures/design/atomic.json
@@ -22,6 +22,10 @@
, "response" : "{\"foo\": \"bar\"}"
}
, { "method" : "put"
+ , "path" : "/design_atomic/_design/update/_update/addbaz/baz"
+ , "response" : "{\"baz\": \"biz\"}"
+ }
+, { "method" : "put"
, "status" : 201
, "path" : "/design_atomic/wat%2Fwat"
, "body" : "{\"wat\":\"wat\"}"
diff --git a/tests/integration/design/atomic.js b/tests/integration/design/atomic.js
index 8fedaf1..f948387 100644
--- a/tests/integration/design/atomic.js
+++ b/tests/integration/design/atomic.js
@@ -26,6 +26,10 @@
var body = JSON.parse(req.body);
doc[body.field] = body.value;
return [doc, JSON.stringify(doc)];
+ },
+ addbaz: function (doc, req) {
+ doc.baz = "biz";
+ return [doc, JSON.stringify(doc)];
}
}
}, '_design/update', function(err) {
@@ -51,6 +55,16 @@
});
});
+it('should be able to update atomically without a body', function (assert) {
+ db.insert({}, 'baz', function (error, doc) {
+ db.atomic('update', 'addbaz', 'baz', function (error, response) {
+ assert.equal(error, null, 'should be able to update');
+ assert.equal(response.baz, 'biz', 'and the new field is present');
+ assert.end();
+ });
+ });
+});
+
it('should be able to update with slashes on the id', function(assert) {
db.insert({'wat': 'wat'}, 'wat/wat', function(error, foo) {
assert.equal(error, null, 'stores `wat`');