Module to extend http://code.google.com/p/couchdb-python/
Usefull links:
CouchDB document abstraction class
Parameters: |
|
---|---|
Return type: | chula.nosql.couch.Document (or subclass of) |
Note
For server lookup logic, see chula.nosql.couch.connect()
Example use:
>>> from chula.nosql import couch
>>>
>>> # Create a couch model class that specifies it's db
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> # Create the new document with some stuff
>>> f = Foo('abc')
>>> f['color'] = 'red'
>>> f['options'] = [1, 2, 3]
>>> rev = f.persist()
>>>
>>> # The document has been persisted, so you can fetch it later
>>> f = Foo('abc')
>>> f['options'].pop()
3
Sharding:
>>> from chula.nosql import couch
>>>
>>> # Create a couch model class that specifies it's db
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> # Create the new document with some stuff
>>> f = Foo('Marry_Sue', shard='s')
>>> rev = f.persist()
>>>
>>> # The document has been persisted, so you can fetch it later
>>> f = Foo('Marry_Sue', shard='s')
What track_dirty does:
>>> import time
>>> from chula.nosql import couch
>>>
>>> # Create a couch model class that specifies it's db
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> doc_id = 'my-uniq-key-%s' % time.time()
>>>
>>> # Create the new document
>>> f = Foo(doc_id)
>>> rev1 = f.persist()
>>>
>>> # Fetch the document from scratch, revision is the same
>>> f = Foo(doc_id)
>>> rev1 == f['_rev']
True
>>>
>>> # Persisting the document unmodified doesn't actually talk to
>>> # the server, nothing has changed
>>> rev2 = f.persist()
>>> rev1 == rev2
True
>>>
>>> # Here we will get a new revision because the structure was mutated
>>> f['new_key'] = {'some-structure':[1, 2, 3, 4]}
>>> rev4 = f.persist()
>>> rev2 == rev4
False
Delete the specified document from the specified databse (and shard if specified)
Parameters: |
|
---|---|
Return type: | UTF-8 encoded str |
>>> from chula.nosql import couch
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> Foo.delete('abc')
Validate the given document id to make sure it’ll be supported by CouchDB, and encode as UTF-8. If the id is not compliant, a chula.nosql.couch.InvalidCouchdbDocumentIdError will be raised.
Parameters: | id (str) – Proposed CouchDB document id |
---|---|
Return type: | UTF-8 encoded str |
>>> from chula.nosql import couch
>>> couch.Document.sanitize_id('abc')
'abc'
CouchDB document abstraction class (plural)
Parameters: |
|
---|---|
Return type: | None |
>>> from chula.nosql import couch
>>>
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> # Now create the plural version of the class
>>> class Foos(couch.Documents):
... DB = Foo.DB
Fetch from CouchDB using an inline query. This is very expensive as the query is compiled on every request. Only use this for development or debugging. When the query is ready, create a view out of it for far improved performance.
Parameters: |
|
---|---|
Return type: | list of chula.nosql.couch.Document or subclass of it |
>>> from chula.nosql import couch
>>>
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> # Now create the plural version of the class
>>> class Foos(couch.Documents):
... DB = Foo.DB
>>>
>>> # Create our javascript query function
>>> query = "function(doc) { emit(null, doc);}"
>>>
>>> # Test our shiney adhoc query
>>> docs = Foos().query(query, cls=Foo)
>>> doc = docs.pop()
>>> doc.keys()
['new_key', '_rev', '_id', 'modified', 'created']
>>>
>>> # A few handy keyword args
>>> foos = Foos()
>>> docs = foos.query(query, cls=Foo, limit=3, descending=True)
>>> len(docs)
3
Fetch from a CouchDB view. Views are by default optimized for write performance, thus the first fetch can be slow if data has recently changed.
Parameters: |
|
---|---|
Return type: | list of chula.nosql.couch.Document or subclass of it |
>>> from chula.nosql import couch
>>>
>>> class Foo(couch.Document):
... DB = 'foo'
>>>
>>> # Now create the plural version of the class
>>> class Foos(couch.Documents):
... DB = Foo.DB
>>>
>>> # First let's fetch using a builtin couch view
>>> docs = Foos().view('_all_docs', cls=Foo)
>>> len(docs) > 1
True
>>> doc = docs.pop()
>>>
>>> # A few handy keyword args
>>> foos = Foos()
>>> docs = foos.view('_all_docs', cls=Foo, limit=4, descending=True)
>>> len(docs)
4
Obtain a connection to a CouchDB server.
Parameters: |
|
---|---|
Return type: |
Note
The server lookup is in the following order, first match wins:
Warning
CouchDB does not support upper case letters in database names. This might save you time :)