Permalink
Browse files

[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
  • Loading branch information...
dscape committed Aug 7, 2012
1 parent ff63f0e commit 01600515cbd6ba1e1e1d839e9a2cedec9edadc01
Showing with 26 additions and 23 deletions.
  1. +2 −2 README.md
  2. +16 −16 nano.js
  3. +6 −3 tests/doc/insert.js
  4. +2 −2 tests/fixtures/doc/insert.json
View
@@ -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');
View
32 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);
}
View
@@ -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,15 +17,17 @@ 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");
});
});
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");
@@ -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

0 comments on commit 0160051

Please sign in to comment.