Python couchbase lite c
Python bindings for couchbase lite c.
Documentation
github https://github.com/couchbaselabs/couchbase-lite-C swift docs https://docs.couchbase.com/couchbase-lite/current/swift/learn/swift-database.html N1QL docs https://www.couchbase.com/products/n1ql
Install
1. Build/Install couchbase lite c.
2. Build/Install bindings
# from repo root cd bindings/python pip install cffi ./build.sh test/test.sh # install to desired environment # cp -Ra CouchbaseLite /usr/lib/python3.9/site-packages/
Usage
The python bindings are modeled after the official swift API.
Database
A database is a container for documents
from CouchbaseLite import Database as database # normal config = database.DatabaseConfiguration('/var/tmp') db = database.Database('db_filename', config) # /var/tmp/db_filename.cblite2/ # with encryption # TODO!Documents
Documents are stored as JSON objects.
There are 2x Document Types:
Document
MutableDocument
Do not use key names that start with an underscore - those are reserved.
from CouchbaseLite import Document as document with db: doc = document.MutableDocument('foo') # id=='foo' doc.properties.update({'red': 1}) db.getDocument('foo').properties # {'red': 1} db.saveDocument(doc) mdoc = document.MutableDocument('foo') # id=='foo' mdoc.properties.update({'blue': 1}) mdoc['blue'] # 1 db.getDocument('foo').properties # {'blue': 1} db.saveMutableDocument(mdoc)Blobs
Blobs are binary blobs. They can be assigned as values on documents.
NOTE:
I believe blob data is automatically serialized/deserialized when document is saved/retrieved. Untested.
from CouchbaseLite import Blob as blob with open('out.png') as fd: png_contents = fd.read() b = blob.Blob(png_contents, contentType='image/png')Query
JSONQuery
TODO:
find spec for JSON query syntax
from CouchbaseLite import Query as query q = query.JSONQuery(db, {...}) print(q.explanation) for row in q.execute(): print(row.asArray()) print(row.asDictionary())N1QL
SQL-like queries that operate on json objects. See N1QL for syntax.
from CouchbaseLite import Query as query q = query.N1QLQuery(db, 'SELECT * FROM bucket') q.execute():Live Query
Use Listeners to monitor database for changes to particular documents.
Queries remain perpetually active, behave as a subscription.def my_listener(doc_ids): print('ids changed: {}'.format(doc_ids)) listener = db.addListener(my_listener) # ... work with db listener.remove()Replication
Replication is handled using a different the sync_gateway tool.
See couchbase for details.