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

Commit

Permalink
add db.show
Browse files Browse the repository at this point in the history
- allows easy querying of show functions
  • Loading branch information
klaemo committed Jul 24, 2012
1 parent 6f681ac commit 1e0d114
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -450,6 +450,21 @@ alice.view('characters', 'crazy_ones', function(err, body) {
});
```

### db.show(designname, showname, docId, [params], [callback])

calls a show function of the specified design for the document specified by docId with
optional query string additions `params`.

``` js
alice.show('characters', 'formatDoc', '3621898430' function(err, doc) {
if (!err) {
console.log(doc);
}
});
```
take a look at the [couchdb wiki](http://wiki.apache.org/couchdb/Formatting_with_Show_and_List#Showing_Documents)
for possible query paramaters and more information on show functions.

### db.atomic(designname, updatename, docname, [body], [callback])

calls the design's update function with the specified doc in input.
Expand Down
21 changes: 21 additions & 0 deletions nano.js
Expand Up @@ -736,6 +736,26 @@ module.exports = exports = nano = function database_module(cfg) {
}
}

/*
* calls a show function
*
* @param {design_name:string} design document name
* @param {show_fn_name:string} show function to call
* @param {docId:string} id of the doc
* @param {params:object:optional} additions to the querystring
*
* @see relax
*/
function show_doc(design_name,show_fn_name,docId,params,callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
var show_fn_path = '_design/' + design_name + '/_show/' + show_fn_name + '/' + docId;
return relax({db: db_name, path: show_fn_path
, method: "GET", params: params},callback);
}

/*
* calls document update handler design document
*
Expand Down Expand Up @@ -876,6 +896,7 @@ module.exports = exports = nano = function database_module(cfg) {
, get : get_att
, destroy : destroy_att
}
, show : show_doc
, atomic : update_with_handler_doc
, updateWithHandler : update_with_handler_doc // alias
};
Expand Down
39 changes: 39 additions & 0 deletions tests/fixtures/show/query.json
@@ -0,0 +1,39 @@
[
{ "method" : "put"
, "path" : "/show_query"
, "status" : 201
, "response" : "{ \"ok\": true }"
}
, { "method" : "put"
, "path" : "/show_query/_design/people"
, "body" : "{\"shows\":{\"singleDoc\":\"function (doc, req) { return { body: { name: doc.name, city: doc.city, format: 'json' }, headers: { 'Content-Type': 'application/json' } }; }\"}}"
, "status" : 201
, "response" : "{\"ok\":true,\"id\":\"_design/people\",\"rev\":\"1-14e6bc\"}"
}
, { "method" : "put"
, "status" : 201
, "path" : "/show_query/p_clemens"
, "body" : "{\"name\":\"Clemens\",\"city\":\"Dresden\"}"
, "response" : "{\"ok\":true,\"id\":\"p_clemens\",\"rev\":\"1-4c6114\"}"
}
, { "method" : "put"
, "status" : 201
, "path" : "/show_query/p_randall"
, "body" : "{\"name\":\"Randall\",\"city\":\"San Francisco\"}"
, "response" : "{\"ok\":true,\"id\":\"p_randall\",\"rev\":\"1-4c6114\"}"
}
, { "method" : "put"
, "status" : 201
, "path" : "/show_query/p_nuno"
, "body" : "{\"name\":\"Nuno\",\"city\":\"New York\"}"
, "response" : "{\"ok\":true,\"id\":\"p_nuno\",\"rev\":\"1-4c6114\"}"
}
, { "path" : "/show_query/_design/people/_show/singleDoc/p_clemens"
, "response" : "{\"name\":\"Clemens\",\"city\":\"Dresden\",\"format\":\"json\"}\n"
}
, { "method" : "delete"
, "path" : "/show_query"
, "status" : 200
, "response" : "{ \"ok\": true }"
}
]
54 changes: 54 additions & 0 deletions tests/show/query.js
@@ -0,0 +1,54 @@
var specify = require('specify')
, async = require('async')
, helpers = require('../helpers')
, timeout = helpers.timeout
, nano = helpers.nano
, nock = helpers.nock
;

var mock = nock(helpers.couch, "show/query")
, db = nano.use("show_query")
;

specify("show_query:setup", timeout, function (assert) {
nano.db.create("show_query", function (err) {
assert.equal(err, undefined, "Failed to create database");
db.insert(
{ "shows": {
"singleDoc": function(doc, req) { return { body: { name: doc.name, city: doc.city, format: 'json' }, headers: { 'Content-Type': 'application/json' } }; }
}
}, '_design/people', function (error, response) {
assert.equal(error, undefined, "Failed to create show function");
assert.equal(response.ok, true, "Response should be ok");
async.parallel(
[ function(cb) { db.insert(
{ name: "Clemens", city: "Dresden" }, "p_clemens", cb); }
, function(cb) { db.insert(
{ name: "Randall", city: "San Francisco" }, "p_randall", cb); }
, function(cb) { db.insert(
{ name: "Nuno", city: "New York" }, "p_nuno", cb); }
]
, function(error, results) {
assert.equal(error, undefined, "Should have stored docs");
});
});
});
});

specify("show_query:test", timeout, function (assert) {
db.show('people','singleDoc', 'p_clemens', function (error, doc) {
assert.equal(error, undefined, "Show function didn't respond");
assert.equal(doc.name,'Clemens');
assert.equal(doc.city,'Dresden');
assert.equal(doc.format,'json');
});
});

specify("show_query:teardown", timeout, function (assert) {
nano.db.destroy("show_query", function (err) {
assert.equal(err, undefined, "Failed to destroy database");
assert.ok(mock.isDone(), "Some mocks didn't run");
});
});

specify.run(process.argv.slice(2));

0 comments on commit 1e0d114

Please sign in to comment.