Package couchdb :: Module client :: Class Database

Class Database



object --+
         |
        Database

Representation of a database on a CouchDB server.

>>> server = Server('http://localhost:8888/')
>>> 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
<Row 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.
Row
__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.
Row
get(self, id, default=None)
Return the document with the specified ID.
generator
query(self, code, **options)
Execute an ad-hoc query against the database.
View
view(self, name, **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: Row
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)

 
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: Row
a Row object representing the requested document, or None if no document with the ID was found

query(self, code, **options)

 

Execute an ad-hoc query against the database.

>>> server = Server('http://localhost:8888/')
>>> 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')
...         return {'key': doc.name};
... }'''
>>> for row in db.query(code):
...     print row['key']
John Doe
Mary Jane
>>> for row in db.query(code, reverse=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
Returns: generator
an iterable over the resulting Row objects

view(self, name, **options)

 

Execute a predefined view.

>>> server = Server('http://localhost:8888/')
>>> 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: View
a View object

Property Details

name

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