couchdb.design.ViewDefinition

Package couchdb :: Module design :: Class ViewDefinition |

# Class ViewDefinition

    object --+
             |
            ViewDefinition

Definition of a view stored in a specific design document.

An instance of this class can be used to access the results of the view, as well as to keep the view definition in the design document up to date with the definition in the application code.

    >>> from couchdb import Server
    >>> server = Server('http://localhost:5984/')
    >>> db = server.create('python-tests')

    >>> view = ViewDefinition('tests', 'all', '''function(doc) {
    ...     emit(doc._id, null);
    ... }''')
    >>> view.get_doc(db)

The view is not yet stored in the database, in fact, design doc doesn't even exist yet. That can be fixed using the sync method:

    >>> view.sync(db)

    >>> design_doc = view.get_doc(db)
    >>> design_doc                                          #doctest: +ELLIPSIS
    <Document '_design/tests'@'...' {...}>
    >>> print design_doc['views']['all']['map']
    function(doc) {
        emit(doc._id, null);
    }

If you use a Python view server, you can also use Python functions instead of code embedded in strings:

    >>> def my_map(doc):
    ...     yield doc['somekey'], doc['somevalue']
    >>> view = ViewDefinition('test2', 'somename', my_map, language='python')
    >>> view.sync(db)
    >>> design_doc = view.get_doc(db)
    >>> design_doc                                          #doctest: +ELLIPSIS
    <Document '_design/test2'@'...' {...}>
    >>> print design_doc['views']['somename']['map']
    def my_map(doc):
        yield doc['somekey'], doc['somevalue']

Use the static sync_many() method to create or update a collection of views in the database in an atomic and efficient manner, even across different design documents.

    >>> del server['python-tests']

Instance Methods
__init__(self, design, name, map_fun, reduce_fun=None, language=`'``javascript``'`, wrapper=None, **defaults) Initialize the view definition. |
ViewResults |
__call__(self, db, **options) Execute the view in the given database. |
__repr__(self) repr(x) |
Document |
get_doc(self, db) Retrieve and return the design document corresponding to this view definition from the given database. |
sync(self, db) Ensure that the view stored in the database matches the view defined by this instance. |
Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Static Methods
sync_many(db, views, remove_missing=False, callback=None) Ensure that the views stored in the database that correspond to a given list of ViewDefinition instances match the code defined in those instances. |

Properties
Inherited from `object`: `__class__`

Method Details

### __init__(self, design, name, map_fun, reduce_fun=None, language=`'``javascript``'`, wrapper=None, **defaults) (Constructor) |
Initialize the view definition. Note that the code in `map_fun` and `reduce_fun` is automatically dedented, that is, any common leading whitespace is removed from each line. Parameters: `design` - the name of the design document `name` - the name of the view `map_fun` - the map function code `reduce_fun` - the reduce function code (optional) `language` - the name of the language used `wrapper` - an optional callable that should be used to wrap the result rows Overrides: object.__init__

### __call__(self, db, **options) (Call operator) |
Execute the view in the given database. Parameters: `db` - the Database instance `options` - optional query string parameters Returns: ViewResults the view results

### __repr__(self) (Representation operator) |
repr(x) Overrides: object.__repr__ (inherited documentation)

### get_doc(self, db) |
Retrieve and return the design document corresponding to this view definition from the given database. Parameters: `db` - the Database instance Returns: Document a client.Document instance, or `None` if the design document does not exist in the database

### sync(self, db) |
Ensure that the view stored in the database matches the view defined by this instance. Parameters: `db` - the Database instance

### sync_many(db, views, remove_missing=False, callback=None) Static Method |
Ensure that the views stored in the database that correspond to a given list of ViewDefinition instances match the code defined in those instances. This function might update more than one design document. This is done using the CouchDB bulk update feature to ensure atomicity of the operation. Parameters: `db` - the Database instance `views` - a sequence of ViewDefinition instances `remove_missing` - whether views found in a design document that are not found in the list of ViewDefinition instances should be removed `callback` - a callback function that is invoked when a design document gets updated; the callback gets passed the design document as only parameter, before that doc has actually been saved back to the database