This document describe the detail of View API.
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)
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
ViewResults object. It act as a list. you could use iter, list and empty function
Properties
Methods
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')
View object. Object to access to a view. All other Views object (except ViewDefintion) inherit this.
Properties
Methods
get results of a view
get max number of resuls of a view
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')
Temp view representation
..automethod:: simplecouchdb.client.TempView.get
This view object nherited from simplecouchdb.client.View, is only used by simplecouchdb.client.Database.documents()
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 to maintain/ register a ‘_design’ document. Idee borrowed to couchdb-python
Properties
Methods
register view in db
A cache that store view installed in db
Methods
Add a view definition to the cache. View are only created When you sync a database (see simplecouchdb.client.Database.sync() method)
Parameters: |
|
---|
get all views of a database registered in the cache
Parameters: |
|
---|
Get view registered in the cache for a database.
Parameters: |
|
---|---|
Returns: | simplecouchdb.client.ViewDefinition instance |
registered in the cache.
register a ViewDefintion instance for db_name
Parameters: |
|
---|
See also
Reference Database class