Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 0160051
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -281,9 +281,9 @@ an object containing the nano configurations, possible keys are:


## document functions ## 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 ``` js
var alice = nano.use('alice'); var alice = nano.use('alice');
Expand Down
32 changes: 16 additions & 16 deletions nano.js
Expand Up @@ -565,27 +565,27 @@ module.exports = exports = nano = function database_module(cfg) {
* *
* @see relax * @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"}; var opts = {db: db_name, body: doc, method: "POST"};


if (!callback) { if(typeof params === "function") {
if (params && typeof params === "function") { callback = params;
callback = params; params = {};
params = null;
} else if (doc_name && typeof doc_name === "function") {
callback = doc_name;
doc_name = null;
}
} }


if(typeof params === "string") {
params = {doc_name: params};
}

if (params) { if (params) {
if(params.doc_name) {
opts.doc = params.doc_name;
opts.method = "PUT";
delete params.doc_name;
}
opts.params = params; opts.params = params;
} }


if(doc_name) {
opts.doc = doc_name;
opts.method = "PUT";
}
return relax(opts,callback); return relax(opts,callback);
} }


Expand Down
9 changes: 6 additions & 3 deletions tests/doc/insert.js
Expand Up @@ -3,6 +3,7 @@ var specify = require('specify')
, timeout = helpers.timeout , timeout = helpers.timeout
, nano = helpers.nano , nano = helpers.nano
, nock = helpers.nock , nock = helpers.nock
, rev
; ;


var mock = nock(helpers.couch, "doc/insert") var mock = nock(helpers.couch, "doc/insert")
Expand All @@ -16,15 +17,17 @@ specify("doc_insert:setup", timeout, function (assert) {
}); });


specify("doc_insert:simple", 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(error, undefined, "Should have stored foo");
assert.equal(foo.ok, true, "Response should be ok"); assert.equal(foo.ok, true, "Response should be ok");
assert.ok(foo.rev, "Response should have rev"); assert.ok(foo.rev, "Response should have rev");
}); });
}); });


specify("doc_insert:params", 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(error, undefined, "Should have stored foo");
assert.equal(foo.ok, true, "Response should be ok"); assert.equal(foo.ok, true, "Response should be ok");
assert.ok(foo.rev, "Response should have rev"); assert.ok(foo.rev, "Response should have rev");
Expand All @@ -33,7 +36,7 @@ specify("doc_insert:params", timeout, function (assert) {


specify("doc_insert:functions", timeout, function (assert) { specify("doc_insert:functions", timeout, function (assert) {
db.insert({fn: function () { return true; }, 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(error, undefined, "Should have stored foo");
assert.equal(fns.ok, true, "Response should be ok"); assert.equal(fns.ok, true, "Response should be ok");
assert.ok(fns.rev, "Response should have rev"); assert.ok(fns.rev, "Response should have rev");
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/doc/insert.json
Expand Up @@ -13,8 +13,8 @@
, { "method" : "put" , { "method" : "put"
, "status" : 201 , "status" : 201
, "path" : "/doc_insert/foobaz?new_edits=false" , "path" : "/doc_insert/foobaz?new_edits=false"
, "body" : "{\"foo\":\"baz\"}" , "body" : "{\"foo\":\"baz\",\"_rev\":\"1-611488\"}"
, "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"1-611488\"}" , "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"2-123456\"}"
} }
, { "method" : "post" , { "method" : "post"
, "status" : 201 , "status" : 201
Expand Down

0 comments on commit 0160051

Please sign in to comment.