Database class

Module to manage access to databases in couchdb.

class simplecouchdb.client.Database(server, dbname)

Object that abstract access to a CouchDB database A Database object could act as a Dict object.

Properties

server
Server instance. See Server class.
dbname
Str, name of database.

Methods

info()

Get infos of database

Returns:dict
doc_exist(docid)

Test if document exist in database

Parameters:
  • docid – str, document id
Returns:

boolean, True if document exist

get(docid, default=None, rev=None)

Get document from database

Args: :param docid: str, document id to retrieve default: default result :param rev: if specified, allow you to retrieve a specifiec revision of document

Returns:dict, representation of CouchDB document as a dict.
revisions(docid, with_doc=True)

retrieve revisions of a doc

Parameters:
  • docid – str, id of document
  • with_doc – bool, if True return document

dict with revisions as member, if false return only revisions

Returns:dict: ‘_rev_infos’ member if you have set with_doc

to True :

{
    "_revs_info": [
        {"rev": "123456", "status": "disk"},
            {"rev": "234567", "status": "missing"},
            {"rev": "345678", "status": "deleted"},
    ]
}

If False, return current revision of the document, but with an additional field, _revs, the value being a list of the available revision IDs.

save(doc)

Save one documents or multiple documents.

Parameters:
  • doc – dict or list/tuple of dict.
Returns:

dict or list of dict: dict or list are updated

with doc ‘_id’ and ‘_rev’ properties returned by CouchDB server.

delete(doc_or_docs)

delete a document or a list of document

Parameters:
  • doc_or_docs – list or str: doment id or list

of documents or list with _id and _rev, optionnaly _deleted member set to true. See _bulk_docs document on couchdb wiki.

Returns:list of doc or dict like:
{"ok":true,"rev":"2839830636"}
iterdocuments(**params)

iter documents of a db

Parameters:
  • params – dict or strings,
add_attachment(doc, content, name=None, content_type=None)

Add attachement to a document.

Parameters:
  • doc – dict, document object
  • content – string or File object.
  • name – name or attachment (file name).
  • content_type – string, mimetype of attachment.

If you don’t set it, it will be autodetected.

Returns:bool, True if everything was ok.

Example:

>>> db = self.server.create_db('simplecouchdb_test')
>>> doc = { 'string': 'test', 'number': 4 }
>>> db.save(doc)
>>> text_attachment = "un texte attaché"
>>> db.add_attachment(doc, text_attachment, "test", "text/plain")
True
>>> db.get_attachment(doc, "test")
u'un texte attaché'
>>> db.delete_attachment(doc, 'test')
>>> db.get_attachment(doc, 'test')
None
>>> self.server.delete_db('simplecouchdb_test')
get_attachment(id_or_doc, name, default=None)

get attachment in document

Parameters:
  • id_or_doc – str or dict, doc id or document dict
  • name – name of attachment default: default result
Returns:

str, attachment

delete_attachment(doc, name)

delete attachement of documen

Parameters:
  • doc – dict, document object in python
  • name – name of attachement
Returns:

dict, withm member ok setto True if delete was ok.

sync(verbose_level=None)

sync all views definition stored in cache.

see Views classes for more info.

add_view(design_name, view_name, map_fun, reduce_fun=None, language='javascript', **params)
create a view definition and add it to db
add_viewdef(view_def)
add a view defintion to db
__len__()
__contains__(docid)
__getitem__(id)
__setitem__(docid, doc)
__delitem__(docid)
__iter__()
__nonzero__()
documents

Get all documents in a database. It correspond to somedatabase/all_docs call

Returns:AllDocumentsView instance.
view

See Views classes documentation

Returns:View instance.
temp_view

See Views classes documentation

Returns:TemporaryView instance

See also

References Server class, Views classes

Previous topic

Server class

Next topic

Views classes

This Page

Quick search