couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "Formatting_with_Show_and_List" by JanLehnardt
Date Sun, 04 Jul 2010 12:25:53 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "Formatting_with_Show_and_List" page has been changed by JanLehnardt.
The comment on this change is: make 0.10 the default api for lists, keep 0.9 for bc's sake,
but make it less prominent.
http://wiki.apache.org/couchdb/Formatting_with_Show_and_List?action=diff&rev1=22&rev2=23

--------------------------------------------------

  }}}
  The request and response objects are of the same format used by `_external` functions, as
documented in ExternalProcesses.
  
- Since CouchDB 0.11.0 you can use the `send()` function as explained below in show functions.
+ Since CouchDB 0.11.0 you can use the `send()` function as explained below in show functions
as well.
  
- == Listing Views with CouchDB 0.9 ==
+ == Listing Views with CouchDB 0.10 and later ==
+ 
  List functions are stored under the `lists` key of a design document. Here's an example
design doc with list functions, in addition to views:
  
  {{{#!highlight javascript
@@ -76, +77 @@

          "people-by-name": "function(doc) { /*...*/ }"
      },
      "lists": {
-         "index-posts": "function(head, row, req, row_info) { /*...*/ }",
+         "index-posts": "function(head, req) { /*...*/ }",
-         "browse-people": "function(head, row, req, row_info) { /*...*/ }"
+         "browse-people": "function(head, req) { /*...*/ }"
      }
  }
  }}}
@@ -92, +93 @@

  }}}
  As above, we assume the database is named "db" and the design doc "examples".
  
- Couchdb 0.10 supports an alternate form of URL which allows you to use a list function and
a view from different design documents.  This is particularly useful when you want to use
a different language for the list and for the view.  These URLs are very similar to the above
examples, but instead of the tail portion being the name of the view, the tail portion can
consist of two parts - a design doc name and the name of the view in that second document.
 For example:
+ Couchdb 0.10 supports an alternate form of URL which allows you to use a list function and
a view from different design documents.  This is particularly useful when you want to use
a different language for the list and for the view. These URLs are very similar to the above
examples, but instead of the tail portion being the name of the view, the tail portion can
consist of two parts - a design doc name and the name of the view in that second document.
 For example:
  
  {{{
  GET /db/_design/examples/_list/index-posts/other_ddoc/posts-by-tag?key="howto"
  }}}
  [As above, we assume the database is named "db" and the design doc with the list is named
"examples", while the design doc with the view is "other_ddoc".]
  
- A list function has a more interesting signature, as it is passed the head of the view on
first invocation, then each row in turn, then called one more time for the tail of the view.
The function should check the `head` and `row` parameters to identify which state it's being
called in; the sequence of calls to `listfn`, for a view with three rows, would look like:
  
- {{{#!highlight javascript
- listfn(head, null,    req, null    );  // Before the first row: head is non-null
- listfn(null, rows[0], req, row_info);  // First row
- listfn(null, rows[1], req, row_info);  // Subsequent rows...
- listfn(null, rows[2], req, row_info);
- listfn(null, null,    req, row_info);  // After last row: row=null
- }}}
- The `head` parameter -- which is only passed into the first call -- contains an object with
information about the view that is to be iterated over. It's much like the response object
returned from a view query in the CouchDB JavaScript API; useful properties include `total_rows`
and `offset`.
- 
- The `row_info` parameter contains an object with information about the iteration state.
Its properties include:
- 
-  * `row_number` (the current row number)
-  * `first_key` (the first key of the view to be listed)
-  * `prev_key` (the key of the row in the previous iteration)
- 
- Example list function:
- 
- {{{#!highlight javascript
- function(head, row, req, row_info) {
-   if (head) {
-     return "<p>head: "+JSON.stringify(head)+"</p><ul>";
-   } else if (row) {
-     return "<li>"+JSON.stringify(row)+"</li>";
-   } else {
-     return "</ul><h4>the tail</h4>"
-   }
- }
- }}}
- 
- == Listing Views with CouchDB 0.10 and later ==
- The list API simplified from 0.9 to 0.10
- 
- Example `list` function
+ An Example `list` function
  
  {{{#!highlight javascript
  function(head, req) {
@@ -145, +113 @@

    });
    while(row = getRow()) {
      send(row.value);
+   }
+ }
+ }}}
+ 
+ == Listing Views with CouchDB 0.9 ==
+ 
+ List functions were introduced in CouchDB 0.9 and had different, more complex API:
+ 
+ A list function has a more interesting signature, as it is passed the head of the view on
first invocation, then each row in turn, then called one more time for the tail of the view.
The function should check the `head` and `row` parameters to identify which state it's being
called in; the sequence of calls to `listfn`, for a view with three rows, would look like:
+ 
+ {{{#!highlight javascript
+ listfn(head, null,    req, null    );  // Before the first row: head is non-null
+ listfn(null, rows[0], req, row_info);  // First row
+ listfn(null, rows[1], req, row_info);  // Subsequent rows...
+ listfn(null, rows[2], req, row_info);
+ listfn(null, null,    req, row_info);  // After last row: row=null
+ }}}
+ The `head` parameter -- which is only passed into the first call -- contains an object with
information about the view that is to be iterated over. It's much like the response object
returned from a view query in the CouchDB JavaScript API; useful properties include `total_rows`
and `offset`.
+ 
+ The `row_info` parameter contains an object with information about the iteration state.
Its properties include:
+ 
+  * `row_number` (the current row number)
+  * `first_key` (the first key of the view to be listed)
+  * `prev_key` (the key of the row in the previous iteration)
+ 
+ Example list function:
+ 
+ {{{#!highlight javascript
+ function(head, row, req, row_info) {
+   if (head) {
+     return "<p>head: "+JSON.stringify(head)+"</p><ul>";
+   } else if (row) {
+     return "<li>"+JSON.stringify(row)+"</li>";
+   } else {
+     return "</ul><h4>the tail</h4>"
    }
  }
  }}}

Mime
View raw message