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

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.core.View(db, view_path, wrapper=None, **params)

Properties

database
Database instance on which is the view is executed

Methods

__call__()
x.__call__(...) <==> x(...)
count()

TempView class

This object is used to retrieve result from a couchdb temp view. It is inherited from simplecouchdb.core.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.core.TempView(db, design, wrapper=None, **params)
..automethod:: simplecouchdb.core.TempView.get

AllDocumentsView class

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

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')

ViewCache class

Other

See also

Reference Database class