From 01600515cbd6ba1e1e1d839e9a2cedec9edadc01 Mon Sep 17 00:00:00 2001 From: Nuno Job Date: Tue, 7 Aug 2012 18:25:37 +0100 Subject: [PATCH] [fix api] changes api to handle params in insert * handles param in insert * backwards compatible * fixes for 500s when no rev is passed in bulk docs --- README.md | 4 ++-- nano.js | 32 ++++++++++++++++---------------- tests/doc/insert.js | 9 ++++++--- tests/fixtures/doc/insert.json | 4 ++-- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 760b4b9b..c4c43925 100644 --- a/README.md +++ b/README.md @@ -281,9 +281,9 @@ an object containing the nano configurations, possible keys are: ## document functions -### db.insert(doc, [docname], [callback]) +### db.insert(doc, [params], [callback]) -inserts `doc` in the database with an optional `docname`. +inserts `doc` in the database with optional `params`. if params is a string, its assumed as the intended document name. if params is an object, its passed as query string parameters and `doc_name` is checked for defining the document name. ``` js var alice = nano.use('alice'); diff --git a/nano.js b/nano.js index ad207d42..a103a26b 100644 --- a/nano.js +++ b/nano.js @@ -565,27 +565,27 @@ module.exports = exports = nano = function database_module(cfg) { * * @see relax */ - function insert_doc(doc,doc_name,params,callback) { + function insert_doc(doc,params,callback) { var opts = {db: db_name, body: doc, method: "POST"}; - - if (!callback) { - if (params && typeof params === "function") { - callback = params; - params = null; - } else if (doc_name && typeof doc_name === "function") { - callback = doc_name; - doc_name = null; - } + + if(typeof params === "function") { + callback = params; + params = {}; } - + + if(typeof params === "string") { + params = {doc_name: params}; + } + if (params) { + if(params.doc_name) { + opts.doc = params.doc_name; + opts.method = "PUT"; + delete params.doc_name; + } opts.params = params; } - - if(doc_name) { - opts.doc = doc_name; - opts.method = "PUT"; - } + return relax(opts,callback); } diff --git a/tests/doc/insert.js b/tests/doc/insert.js index 3355165a..e014025d 100644 --- a/tests/doc/insert.js +++ b/tests/doc/insert.js @@ -3,6 +3,7 @@ var specify = require('specify') , timeout = helpers.timeout , nano = helpers.nano , nock = helpers.nock + , rev ; var mock = nock(helpers.couch, "doc/insert") @@ -16,7 +17,8 @@ specify("doc_insert:setup", timeout, function (assert) { }); specify("doc_insert:simple", timeout, function (assert) { - db.insert({"foo": "baz"}, "foobaz", function (error, foo) { + db.insert({"foo": "baz"}, "foobaz", function (error, foo) { + rev = foo.rev; assert.equal(error, undefined, "Should have stored foo"); assert.equal(foo.ok, true, "Response should be ok"); assert.ok(foo.rev, "Response should have rev"); @@ -24,7 +26,8 @@ specify("doc_insert:simple", timeout, function (assert) { }); specify("doc_insert:params", timeout, function (assert) { - db.insert({"foo": "baz"}, "foobaz", { new_edits: false }, function (error, foo) { + db.insert({"foo": "baz", _rev: rev}, {doc_name:"foobaz", new_edits:false}, + function (error, foo) { assert.equal(error, undefined, "Should have stored foo"); assert.equal(foo.ok, true, "Response should be ok"); assert.ok(foo.rev, "Response should have rev"); @@ -33,7 +36,7 @@ specify("doc_insert:params", timeout, function (assert) { specify("doc_insert:functions", timeout, function (assert) { db.insert({fn: function () { return true; }, - fn2: "function () { return true; }"}, function (error, fns) { + fn2: "function () { return true; }"}, function (error, fns) { assert.equal(error, undefined, "Should have stored foo"); assert.equal(fns.ok, true, "Response should be ok"); assert.ok(fns.rev, "Response should have rev"); diff --git a/tests/fixtures/doc/insert.json b/tests/fixtures/doc/insert.json index 3c8264bf..cb1fe250 100644 --- a/tests/fixtures/doc/insert.json +++ b/tests/fixtures/doc/insert.json @@ -13,8 +13,8 @@ , { "method" : "put" , "status" : 201 , "path" : "/doc_insert/foobaz?new_edits=false" - , "body" : "{\"foo\":\"baz\"}" - , "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"1-611488\"}" + , "body" : "{\"foo\":\"baz\",\"_rev\":\"1-611488\"}" + , "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"2-123456\"}" } , { "method" : "post" , "status" : 201