CouchDB for Coldfusion

PDF HTML FlashPaper

Main | Tutorials | ViewCFC

Overview

View.cfc implements the CouchDB View API. It extends Document.cfc and overwrites the Load() function.

What is a View?

A Couch DB View is a static result set that has been dynamically built by a custom "map" function. A map function parses all of the documents in your database and "emits" a single key/value result for each document that should be added to the View it is building.

Once a view has been built, it remains static until a document is added, updated, or removed. Since the database is not queried for every View request, the database response speed is very fast.

Methods

View.cfc has all of the same properties and methods as Document.cfc, except for the following:

Load()

View.cfc requires an ID and View name. The ID provided should not include "_design/", since that is implied when working with views. The View name should not include "_view/", since that is also implied when working with views. Example:

view_obj = createObject('component','view').init('my_database'); view_results = view_obj.load('posts','byAuthor');

The above code returns view results located at: /my_database/_design/posts/_view/byAuthor

You may also pass the following optional parameters available in the CouchDB View API:

  • key - Specific key to retrieve.
  • keys - List of keys to retrieve.
  • startkey - Key to start at.
  • startkey_docid - Document ID to start with.
  • endkey - Key to end at.
  • endkey_docid - Last document ID. Included in result unless inclusive_end is explicitly set to false.
  • limit - Limit the number of documents in the output. Numeric or blank.
  • stale - If stale=ok CouchDB will not refresh the view even if it is stalled. 'ok' or blank.
  • descending - Return the results in reverse order.
  • skip - Number of documents to skip.
  • group - Controls whether the reduce function reduces to a set of distinct keys or to a single result row. True/false/blank.
  • group_level - The array level at which to perform reduce on. Numeric or -.
  • reduce - Whether to use the reduce function of the view. CouchDB defaults to true if a reduce function is defined, false otherwise. True/false/blank.
  • include_docs - Include the document which emitted each view entry.
  • inclusive_end - Whether the endkey is included in the result.

Temporary()

Returns a temporary view result. Requires a JSON-encoded map function.

Cleanup()

Removes all unused view results from the database.

Compact()

Compacts individual design view results.