Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add db.show

- allows easy querying of show functions
  • Loading branch information...
commit 1e0d1145ef3aade7c96b5478f222c6340870adec 1 parent 6f681ac
@klaemo klaemo authored
View
15 README.md
@@ -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.
View
21 nano.js
@@ -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
*
@@ -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
};
View
39 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 }"
+ }
+]
View
54 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));
Please sign in to comment.
Something went wrong with that request. Please try again.