Package couchdb :: Module client :: Class Database

Class Database



object --+
         |
        Database

Representation of a database on a CouchDB server.

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

New documents can be added to the database using the create() method:

>>> doc_id = db.create({'type': 'Person', 'name': 'John Doe'})

This class provides a dictionary-like interface to databases: documents are retrieved by their ID using item access

>>> doc = db[doc_id]
>>> doc                 #doctest: +ELLIPSIS
<Document u'...'@... {...}>

Documents are represented as instances of the Row class, which is basically just a normal dictionary with the additional attributes id and rev:

>>> doc.id, doc.rev     #doctest: +ELLIPSIS
(u'...', ...)
>>> doc['type']
u'Person'
>>> doc['name']
u'John Doe'

To update an existing document, you use item access, too:

>>> doc['name'] = 'Mary Jane'
>>> db[doc.id] = doc

The create() method creates a document with an auto-generated ID. If you want to explicitly specify the ID, you'd use item access just as with updating:

>>> db['JohnDoe'] = {'type': 'person', 'name': 'John Doe'}
>>> 'JohnDoe' in db
True
>>> len(db)
2
>>> list(db)            #doctest: +ELLIPSIS
[u'...', u'JohnDoe']
>>> del server['python-tests']


Instance Methods
 
__init__(self, uri, name=None, http=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
 
__repr__(self)
repr(x)
 
__contains__(self, id)
Return whether the database contains a document with the specified ID.
 
__iter__(self)
Return the IDs of all documents in the database.
 
__len__(self)
Return the number of documents in the database.
 
__delitem__(self, id)
Remove the document with the specified ID from the database.
Document
__getitem__(self, id)
Return the document with the specified ID.
 
__setitem__(self, id, content)
Create or update a document with the specified ID.
unicode
create(self, data)
Create a new document in the database with a generated ID.
Document
get(self, id, default=None, **options)
Return the document with the specified ID.
ViewResults
query(self, code, content_type='text/javascript', wrapper=None, **options)
Execute an ad-hoc query (a "temp view") against the database.
generator
update(self, documents)
Perform a bulk update or insertion of the given documents using a single HTTP request.
ViewResults
view(self, name, wrapper=None, **options)
Execute a predefined view.

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

Properties
  name

Inherited from object: __class__

Method Details

__init__(self, uri, name=None, http=None)
(Constructor)

 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

__repr__(self)
(Representation operator)

 
repr(x)
Overrides: object.__repr__
(inherited documentation)

__contains__(self, id)
(In operator)

 
Return whether the database contains a document with the specified ID.
Parameters:
  • id - the document ID
Returns:
True if a document with the ID exists, False otherwise

__delitem__(self, id)
(Index deletion operator)

 
Remove the document with the specified ID from the database.
Parameters:
  • id - the document ID

__getitem__(self, id)
(Indexing operator)

 
Return the document with the specified ID.
Parameters:
  • id - the document ID
Returns: Document
a Row object representing the requested document

__setitem__(self, id, content)
(Index assignment operator)

 
Create or update a document with the specified ID.
Parameters:
  • id - the document ID
  • content - the document content; either a plain dictionary for new documents, or a Row object for existing documents

create(self, data)

 

Create a new document in the database with a generated ID.

Any keyword arguments are used to populate the fields of the new document.

Parameters:
  • data - the data to store in the document
Returns: unicode
the ID of the created document

get(self, id, default=None, **options)

 
Return the document with the specified ID.
Parameters:
  • id - the document ID
  • default - the default value to return when the document is not found
Returns: Document
a Row object representing the requested document, or None if no document with the ID was found

query(self, code, content_type='text/javascript', wrapper=None, **options)

 

Execute an ad-hoc query (a "temp view") against the database.

>>> server = Server('http://localhost:5984/')
>>> db = server.create('python-tests')
>>> db['johndoe'] = dict(type='Person', name='John Doe')
>>> db['maryjane'] = dict(type='Person', name='Mary Jane')
>>> db['gotham'] = dict(type='City', name='Gotham City')
>>> code = '''function(doc) {
...     if (doc.type=='Person')
...         map(doc.name, null);
... }'''
>>> for row in db.query(code):
...     print row.key
John Doe
Mary Jane
>>> for row in db.query(code, descending=True):
...     print row.key
Mary Jane
John Doe
>>> for row in db.query(code, key='John Doe'):
...     print row.key
John Doe
>>> del server['python-tests']
Parameters:
  • code - the code of the view function
  • content_type - the MIME type of the code, which determines the language the view function is written in
Returns: ViewResults
the view reults

update(self, documents)

 

Perform a bulk update or insertion of the given documents using a single HTTP request.

>>> server = Server('http://localhost:5984/')
>>> db = server.create('python-tests')
>>> for doc in db.update([
...     Document(type='Person', name='John Doe'),
...     Document(type='Person', name='Mary Jane'),
...     Document(type='City', name='Gotham City')
... ]):
...     print repr(doc) #doctest: +ELLIPSIS
<Document u'...'@u'...' {'type': 'Person', 'name': 'John Doe'}>
<Document u'...'@u'...' {'type': 'Person', 'name': 'Mary Jane'}>
<Document u'...'@u'...' {'type': 'City', 'name': 'Gotham City'}>
>>> del server['python-tests']
Parameters:
  • documents - a sequence of dictionaries or Document objects
Returns: generator
an iterable over the resulting documents

Since: version 0.2

view(self, name, wrapper=None, **options)

 

Execute a predefined view.

>>> server = Server('http://localhost:5984/')
>>> db = server.create('python-tests')
>>> db['gotham'] = dict(type='City', name='Gotham City')
>>> for row in db.view('_all_docs'):
...     print row.id
gotham
>>> del server['python-tests']
Returns: ViewResults
the view results

Property Details

name

Get Method:
couchdb.client.Database._get_name(self)