Views classes

This document describe the detail of View API.

  • ViewResults class: A ViewResults instance is constructed by View, SLowView, AllDocumentView and Viewdefinition objects. Results are only retrieved when you use one method of ViewResults instance.
  • View class: Object that allow you to retrieve resulys of a view in db.
  • TempView class: same as view but for temporary view
  • AllDocumentsView class: use by Database.documents method
  • ViewDefinition class: A ViewDefiniton object allow you to easyly create a view and add it to a database. You could sync a database to add allo cached ViewDefintion’s object.

Sample usage of a view :

>>> from simplecouchdb import Server, Database, Views
>>> server = Server()
>>> db = Database(name="test", server=server)
>>> results = db.view.get("test/all")

at this step, ou have only set an instance of ViewResults.

On only retrieved results when you for example iter results :

>>> list_of_results = list(results)

ViewResults class

This class is returned by Views object. ViewResults object can be constructed and passed around without hitting the CouchDB server. No database activity actually occurs until you do something to evaluate the viewresult.

You can evaluate a ViewResult in the following ways:

  • Iteration: A ViewResult is iterable and it execute the resquest to CouchDB the first time you execute it.

  • len(). A ViewResults is evaluated when you call len() on it. This, as you might expect, returns the length of the result list.

  • list(). Force evaluation by calling list() on it

    list = list(db.view("test/all"))
    

Description of the class

class simplecouchdb.client.ViewResults(view, wrapper=None, **params)

ViewResults object. It act as a list. you could use iter, list and empty function

Properties

view
Instance of View used by ViewResults.
wrapper
callable to wrap result in an object
params
view parameters

Methods

__len__()
__iter__()
__nonzero__()
fetch()

View class

Usage example:

>>> db = self.server.create_db('simplecouchdb_test')
>>> doc1 = { '_id': 'test', 'lastname': 'doo', 'firtsname': "john", 'docType': 'identity' }
>>> db.save(doc1)
>>> doc2 = { '_id': 'test2', 'lastname': 'dupont', 'firstname': "jean", 'docType': 'identity'}
>>> db.save(doc2)
>>> design_doc = { '_id': '_design/test', 'language': 'javascript',
    'views': {  'all': { "map": """function(doc) {
    if (doc.docType == "identity") { emit(doc._id, doc); }}""" } }}
>>> db.save(design_doc)
>>> results = db.view.get('test/all')
>>> len(results)
2
>>> self.server.delete_db('simplecouchdb_test')
class simplecouchdb.client.View(database)

View object. Object to access to a view. All other Views object (except ViewDefintion) inherit this.

Properties

database
Database instance on which is the view is executed

Methods

__call__(*args, **kwargs)
get(view_name, wrapper=None, **params)

get results of a view

Args:
view_name: name of view wrapper: callable to wrap results params: params of a view
Return:
ViewResults instance
count(view_name)

get max number of resuls of a view

Args:
view_name: name of view
Return:
int

TempView class

This object is used to retrieve result from a couchdb temp view. It is inherited from simplecouchdb.client.View object.

Usage example:

>>> db = self.server.create_db('simplecouchdb_test')
>>> db = self.server.create_db('simplecouchdb_test')
>>> doc1 = { '_id': 'test', 'lastname': 'doo', 'firtsname': "john", 'docType': 'identity' }
>>> db.save(doc1)
>>> doc2 = { '_id': 'test2', 'lastname': 'dupont', 'firstname': "jean", 'docType': 'identity'}
>>> db.save(doc2)
>>> design_doc = { "map": """function(doc) { if (doc.docType == "identity") { emit(doc._id, doc); }}""" }
>>> results = db.slow_view.get(design_doc)
>>> len(results)
2
>>> self.server.delete_db('simplecouchdb_test')
class simplecouchdb.client.TempView(database)

Temp view representation

..automethod:: simplecouchdb.client.TempView.get

AllDocumentsView class

This view object nherited from simplecouchdb.client.View, is only used by simplecouchdb.client.Database.documents()

class simplecouchdb.client.AllDocumentsView(database)

ViewDefinition class

This object is used o create and register views for a database.

Example usage:

>>> view = ViewDefinition('test', 'all', map_fun= """function(doc)
    { if (doc.docType == "identity") { emit(doc._id, doc);}}""")
>>> db = self.server.create_db('simplecouchdb_test')
    >>> doc1 = { '_id': 'test', 'string': 'test', 'number': 4, 'docType': 'test' }
>>> db.save(doc1)
>>> doc2 = { '_id': 'test2', 'string': 'test', 'number': 2, 'docType': 'test'}
>>> db.save(doc2)
>>> view.sync(db)
>>> results = db.view.get('test/all')
>>> len(results)
2
>>> self.server.delete_db('simplecouchdb_test')
class simplecouchdb.client.ViewDefinition(design_name, view_name, map_fun=None, reduce_fun=None, language='javascript', wrapper=None, **params)

class to maintain/ register a ‘_design’ document. Idee borrowed to couchdb-python

Properties

design_name
name of design document
view_nane
name of view
map_fun
str of file object, string for map function
reduce_fun
str of file object, string for reduce function
language
language of view, default is javascript
wrapper
default wrapper of view
params
default parameters of view

Methods

register(db_name)

register view in db

Args:
db_name: name of db
sync(db, verbose_level=None)
sync view in db. If it don’t exist, create it.
get(db, wrapper=None, sync=True, **params)
get result of view like View object. If sync is true try ro create or update the view.

ViewCache class

class simplecouchdb.client.ViewCache

A cache that store view installed in db

Methods

add_view(dbname, view_def)

Add a view definition to the cache. View are only created When you sync a database (see simplecouchdb.client.Database.sync() method)

Parameters:
get_views(dbname)

get all views of a database registered in the cache

Parameters:
  • dbname – name of database
get_view(dbname, design_name, view_name)

Get view registered in the cache for a database.

Parameters:
  • dbname – name of database
  • design_name – name of design document
  • view_name – name of view
Returns:

simplecouchdb.client.ViewDefinition instance

registered in the cache.

Other

simplecouchdb.client.register_view(dbname, view_def)

register a ViewDefintion instance for db_name

Parameters:
  • dbname – name of database
  • view_def – ViewDefinition object

See also

Reference Database class