From 1e96e6d2a2c8457d3dae8a1db5386fa8a68ffbe1 Mon Sep 17 00:00:00 2001 From: Scott Smith Date: Sun, 26 Feb 2012 14:26:50 -0800 Subject: [PATCH] Added a very simple example, and re-titled to match the goal, not the means. The developer's perspective is "I know what page I want the user to see. How do I make that happen with couchdb?". The answer is to organize the data in a view function, and then write a list function which will do the content-type specific parts, such as generate HTML. This page presumed the user already knew that they needed to transform a view to get content, and was already thinking from that perspective. I hope my addition at the top brings them across, before delving into details. --- draft/transforming.html | 45 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/draft/transforming.html b/draft/transforming.html index d07415b..b72eefc 100644 --- a/draft/transforming.html +++ b/draft/transforming.html @@ -1,4 +1,4 @@ -Transforming Views with List Functions +Rendering Content Based-On Multiple Documents with List Functions @@ -10,16 +10,53 @@ -

Transforming Views with List Functions

+

Rendering Content Based-On Multiple Documents with List Functions

-

Just as show functions convert documents to arbitrary output formats, CouchDB list functions allow you to render the output of view queries in any format. The powerful iterator API allows for flexibility to filter and aggregate rows on the fly, as well as output raw transformations for an easy way to make Atom feeds, HTML lists, CSV files, config files, or even just modified JSON. +

Just as show functions convert an individual document into an arbitrary output format, CouchDB list functions are used to render documents as a group. + +

A list function is invoked with a URL specifying both the list function name and also the underlying view which will provide and organize the data. + +

For example, given this simple view, which indexes documents by "user_id": +

+http://mysite/mydb/_design/myapp/_view/mydocs-by-user
+
+
+function(doc) {
+  if (doc.user_id) {
+    emit(doc.user_id, null);
+  }
+};
+
+ + +

+This list function renders the list of users in a simple HTML page: +

+http://mysite/mydb/_design/myapp/_list/mylist/mydocs-by-user
+
+
+function(doc, req) {   
+    provides("html", function() {
+        html = "<html><body><ol>\n";
+        while (row = getRow()) {
+            html += "<li>" + row.key + ":" + row.value + "</li>\n";
+        }   
+        html += "</ol></body></head>";
+        return html;
+    }); 
+}
+
+ +

Note that a list function can be used with a variety of views, or might be tailored to produce an elaborate page from a view designed specifically to organize data for it. + +

The powerful iterator API allows for flexibility to filter and aggregate rows on the fly, as well as output raw transformations for an easy way to make Atom feeds, HTML lists, CSV files, config files, or even just modified JSON.

List functions are stored under the lists field of a design document. Here’s an example design document that contains two list functions:

 {
   "_id" : "_design/foo",
-  "_rev" : "1-67at7bg",
+  "_rev" : "1-67aGt7bg",
   "lists" : {
     "bar" : "function(head, req) { var row; while (row = getRow()) { ... } }",
     "zoom" : "function() { return 'zoom!' }",