Chapter 9. Transforming Views 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.
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","lists":{"bar":"function(head, req) { var row; while (row = getRow()) { ... } }","zoom":"function() { return 'zoom!' }",}}
Arguments to the List Function
The function is called with two arguments, which can sometimes be
ignored, as the row data itself is loaded during function execution. The
first argument, head, contains information about the
view. Here’s what you might see looking at a JSON representation of
head:
{total_rows:10,offset:0}
The request itself is a much richer data structure. This is the same
request object that is available to show, update, and filter functions.
We’ll go through it in detail here as a reference. Here’s the example
req object:
{"info":{"db_name":"test_suite_db","doc_count":11,"doc_del_count":0,"update_seq":11,"purge_seq":0,"compact_running":false,"disk_size":4930,"instance_start_time":"1250046852578425","disk_format_version" ...