Package couchdbkit :: Module client :: Class Database
[hide private]
[frames] | no frames]

Class Database

source code

object --+
         |
        Database

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

Instance Methods [hide private]
 
__init__(self, uri, create=False, server=None, **params)
Constructor for Database
source code
 
__repr__(self)
repr(x)
source code
 
info(self)
Get database information
source code
 
compact(self, dname=None)
compact database
source code
 
view_cleanup(self) source code
 
flush(self)
Remove all docs from a database except design docs.
source code
 
doc_exist(self, docid)
Test if document exists in a database
source code
 
open_doc(self, docid, **params)
Get document from database
source code
 
get(self, docid, **params)
Get document from database
source code
 
list(self, list_name, view_name, **params)
Execute a list function on the server and return the response.
source code
 
show(self, show_name, doc_id, **params)
Execute a show function on the server and return the response.
source code
 
all_docs(self, by_seq=False, **params)
Get all documents from a database
source code
 
get_rev(self, docid)
Get last revision from docid (the '_rev' member)
source code
 
save_doc(self, doc, encode_attachments=True, force_update=False, **params)
Save a document.
source code
 
save_docs(self, docs, use_uuids=True, all_or_nothing=False, **params)
bulk save.
source code
 
bulk_save(self, docs, use_uuids=True, all_or_nothing=False, **params)
bulk save.
source code
 
delete_docs(self, docs, all_or_nothing=False, empty_on_delete=False, **params)
bulk delete.
source code
 
bulk_delete(self, docs, all_or_nothing=False, empty_on_delete=False, **params)
bulk delete.
source code
 
delete_doc(self, doc, **params)
delete a document or a list of documents @param doc: str or dict, document id or full doc.
source code
 
copy_doc(self, doc, dest=None, headers=None)
copy an existing document to a new id.
source code
 
raw_view(self, view_path, params) source code
 
raw_temp_view(db, design, params) source code
 
view(self, view_name, schema=None, wrapper=None, **params)
get view results from database.
source code
 
temp_view(self, design, schema=None, wrapper=None, **params)
get adhoc view results.
source code
 
search(self, view_name, handler='_fti/_design', wrapper=None, schema=None, **params)
Search.
source code
 
documents(self, schema=None, wrapper=None, **params)
return a ViewResults objects containing all documents.
source code
 
iterdocuments(self, schema=None, wrapper=None, **params)
return a ViewResults objects containing all documents.
source code
 
put_attachment(self, doc, content, name=None, content_type=None, content_length=None, headers=None)
Add attachement to a document.
source code
 
delete_attachment(self, doc, name, headers=None)
delete attachement to the document
source code
 
fetch_attachment(self, id_or_doc, name, stream=False, headers=None)
get attachment in a document
source code
 
ensure_full_commit(self)
commit all docs in memory
source code
 
__len__(self) source code
 
__contains__(self, docid) source code
 
__getitem__(self, docid) source code
 
__setitem__(self, docid, doc) source code
 
__delitem__(self, docid) source code
 
__iter__(self) source code
 
__nonzero__(self) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, uri, create=False, server=None, **params)
(Constructor)

source code 

Constructor for Database

Parameters:
  • uri - str, Database uri
  • create - boolean, False by default, if True try to create the database.
  • server - Server instance
Overrides: object.__init__

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

info(self)

source code 

Get database information

Returns:
dict

compact(self, dname=None)

source code 

compact database

Parameters:
  • dname - string, name of design doc. Usefull to compact a view.

doc_exist(self, docid)

source code 

Test if document exists in a database

Parameters:
  • docid - str, document id
Returns:
boolean, True if document exist

open_doc(self, docid, **params)

source code 

Get document from database

Args:

Parameters:
  • docid - str, document id to retrieve
  • wrapper - callable. function that takes dict as a param. Used to wrap an object.
  • **params - See doc api for parameters to use: http://wiki.apache.org/couchdb/HTTP_Document_API
Returns:
dict, representation of CouchDB document as a dict.

get(self, docid, **params)

source code 

Get document from database

Args:

Parameters:
  • docid - str, document id to retrieve
  • wrapper - callable. function that takes dict as a param. Used to wrap an object.
  • **params - See doc api for parameters to use: http://wiki.apache.org/couchdb/HTTP_Document_API
Returns:
dict, representation of CouchDB document as a dict.

list(self, list_name, view_name, **params)

source code 

Execute a list function on the server and return the response. If the response is json it will be deserialized, otherwise the string will be returned.

Args:

Parameters:
  • list_name - should be 'designname/listname'
  • view_name - name of the view to run through the list document
  • params - params of the list

show(self, show_name, doc_id, **params)

source code 

Execute a show function on the server and return the response. If the response is json it will be deserialized, otherwise the string will be returned.

Args:

Parameters:
  • show_name - should be 'designname/showname'
  • doc_id - id of the document to pass into the show document
  • params - params of the show

all_docs(self, by_seq=False, **params)

source code 
Get all documents from a database

This method has the same behavior as a view.

`all_docs( **params )` is the same as `view('_all_docs', **params)`
 and `all_docs( by_seq=True, **params)` is the same as
`view('_all_docs_by_seq', **params)`

You can use all(), one(), first() just like views

Args:
@param by_seq: bool, if True the "_all_docs_by_seq" is passed to
couchdb. It will return an updated list of all documents.

@return: list, results of the view

get_rev(self, docid)

source code 

Get last revision from docid (the '_rev' member)

Parameters:
  • docid - str, undecoded document id.

save_doc(self, doc, encode_attachments=True, force_update=False, **params)

source code 

Save a document. It will use the `_id` member of the document or request a new uuid from CouchDB. IDs are attached to documents on the client side because POST has the curious property of being automatically retried by proxies in the event of network segmentation and lost responses. (Idee from `Couchrest <http://github.com/jchris/couchrest/>`)

Parameters:
  • doc - dict. doc is updated with doc '_id' and '_rev' properties returned by CouchDB server when you save.
  • force_update - boolean, if there is conlict, try to update with latest revision @param params, list of optionnal params, like batch="ok"

save_docs(self, docs, use_uuids=True, all_or_nothing=False, **params)

source code 

bulk save. Modify Multiple Documents With a Single Request

Parameters:
  • docs - list of docs
  • use_uuids - add _id in doc who don't have it already set.
  • all_or_nothing - In the case of a power failure, when the database restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will

    .. seealso:: `HTTP Bulk Document API <http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API>`

bulk_save(self, docs, use_uuids=True, all_or_nothing=False, **params)

source code 

bulk save. Modify Multiple Documents With a Single Request

Parameters:
  • docs - list of docs
  • use_uuids - add _id in doc who don't have it already set.
  • all_or_nothing - In the case of a power failure, when the database restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will

    .. seealso:: `HTTP Bulk Document API <http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API>`

delete_docs(self, docs, all_or_nothing=False, empty_on_delete=False, **params)

source code 

bulk delete. It adds '_deleted' member to doc then uses bulk_save to save them.

Parameters:
  • empty_on_delete - default is False if you want to make sure the doc is emptied and will not be stored as is in Apache CouchDB.
  • all_or_nothing - In the case of a power failure, when the database restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will

    .. seealso:: `HTTP Bulk Document API <http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API>`

bulk_delete(self, docs, all_or_nothing=False, empty_on_delete=False, **params)

source code 

bulk delete. It adds '_deleted' member to doc then uses bulk_save to save them.

Parameters:
  • empty_on_delete - default is False if you want to make sure the doc is emptied and will not be stored as is in Apache CouchDB.
  • all_or_nothing - In the case of a power failure, when the database restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will

    .. seealso:: `HTTP Bulk Document API <http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API>`

delete_doc(self, doc, **params)

source code 
delete a document or a list of documents
@param doc: str or dict,  document id or full doc.
@return: dict like:

.. code-block:: python

    {"ok":true,"rev":"2839830636"}

copy_doc(self, doc, dest=None, headers=None)

source code 

copy an existing document to a new id. If dest is None, a new uuid will be requested

Parameters:
  • doc - dict or string, document or document id
  • dest - basestring or dict. if _rev is specified in dict it will override the doc

view(self, view_name, schema=None, wrapper=None, **params)

source code 

get view results from database. viewname is generally a string like `designname/viewname". It return an ViewResults object on which you could iterate, list, ... . You could wrap results in wrapper function, a wrapper function take a row as argument. Wrapping could be also done by passing an Object in obj arguments. This Object should have a `wrap` method that work like a simple wrapper function.

@param view_name, string could be '_all_docs', '_all_docs_by_seq', 'designname/viewname' if view_name start with a "/" it won't be parsed and beginning slash will be removed. Usefull with c-l for example. @param schema, Object with a wrapper function

Parameters:
  • wrapper - function used to wrap results
  • params - params of the view

temp_view(self, design, schema=None, wrapper=None, **params)

source code 

get adhoc view results. Like view it reeturn a ViewResult object.

search(self, view_name, handler='_fti/_design', wrapper=None, schema=None, **params)

source code 

Search. Return results from search. Use couchdb-lucene with its default settings by default.

documents(self, schema=None, wrapper=None, **params)

source code 

return a ViewResults objects containing all documents. This is a shorthand to view function.

iterdocuments(self, schema=None, wrapper=None, **params)

source code 

return a ViewResults objects containing all documents. This is a shorthand to view function.

put_attachment(self, doc, content, name=None, content_type=None, content_length=None, headers=None)

source code 

Add attachement to a document. All attachments are streamed.

Parameters:
  • doc - dict, document object
  • content - string or :obj:`File` object.
  • name - name or attachment (file name).
  • content_type - string, mimetype of attachment. If you don't set it, it will be autodetected.
  • content_lenght - int, size of attachment.
Returns:
bool, True if everything was ok.

Example:

>>> from simplecouchdb import server
>>> server = server()
>>> db = server.create_db('couchdbkit_test')
>>> doc = { 'string': 'test', 'number': 4 }
>>> db.save(doc)
>>> text_attachment = u'un texte attaché'
>>> db.put_attachment(doc, text_attachment, "test", "text/plain")
True
>>> file = db.fetch_attachment(doc, 'test')
>>> result = db.delete_attachment(doc, 'test')
>>> result['ok']
True
>>> db.fetch_attachment(doc, 'test')
>>> del server['couchdbkit_test']
{u'ok': True}

delete_attachment(self, doc, name, headers=None)

source code 

delete attachement to the document

Parameters:
  • doc - dict, document object in python
  • name - name of attachement
Returns:
dict, with member ok set to True if delete was ok.

fetch_attachment(self, id_or_doc, name, stream=False, headers=None)

source code 

get attachment in a document

Parameters:
  • id_or_doc - str or dict, doc id or document dict
  • name - name of attachment default: default result
  • stream - boolean, if True return a file object
Returns:
`restkit.httpc.Response` object