Wallaby backend for CouchDB
This package provides an asynchronous python interface to CouchDB (using twisted).
For more information on wallaby visit http://wlby.freshx.de
Installation
You can install the couchdb backend with pip
pip install wallaby-backend-couchdb
How to use
The library is based on twisted's asynchronous pattern. To use the library in an asynchronous fassion you
first need to create an reactor based application:
from twisted.internet import defer
@defer.inlineCallbacks
def run():
d = defer.Deferred()
reactor.callLater(1.0, d.callback)
yield d
reactor.stop()
from twisted.internet import reactor
reactor.callWhenRunning(run)
reactor.run()
Now we can connect to an existing CouchDB database:
@defer.inlineCallbacks
def run():
from wallaby.backends.couchdb import Database
db = Database(
"<name of database>",
username="<username>",
password="<password>",
url="http://localhost:5984"
)
info = yield db.info()
print info
reactor.stop()
Create and delete database
With the required permissions you could easily create and destroy databases
newdb = Database(
"<name of new database>",
username="<username>",
password="<password>",
url="http://localhost:5984"
)
res = yield newdb.create()
res = yield newdb.destroy()
Read and write document
doc = yield db.get('docid')
doc['test'] = 'Hello World'
res = yield db.save(doc)
Creating and deleting documents
doc = {'_id': 'newdocid'}
res = yield db.save(doc)
if not 'error' in res:
print doc['_rev']
res = yield db.delete(doc)
Attachment handling
content = open('test.png').read()
res = yield db.put_attachment(doc, 'newimage.png', content, content-type='image/png')
content = yield db.get_attachment(doc, 'newimage.png')
res = yield db.delete_attachment(doc, 'newimage.png')
Views
rows = yield db.view('_design/designname/_view/viewname')
rows = yield db.view('_design/designname/_view/viewname', count=100)
Changes
def callback(changes, viewID=None):
pass
db.changes(cb=callback, since=12345, filter="couchappdoc/all")
db.changes(cb=callback, since=12345, filter="_view", view="couchappdoc/viewname")
db.unchanges(since=12345, filter="couchappdoc/all")
db.changes(since=12345, filter="_view", view="couchappdoc/viewname")