Skip to main content

A wrapper around the CouchDB API.

Project description

CouchDB3

CouchDB3 is a wrapper around the CouchDB API. For more detailed information, please refer to the documentation.

Disclaimer

Big parts of the documentation (and thus docstrings) have been copied from CouchDB's API's great official documentation.

Requirements

  • Python version >= 3.7
  • CouchDB version 3.x

Installation

Installing via PyPi

pip install couchdb3

Installing via Github

python -m pip install git+https://github.com/n-Vlahovic/couchdb3.git

Installing from source

git clone https://github.com/n-Vlahovic/couchdb3
python -m pip install -e couchdb3

Quickstart

Connecting to a database server

import couchdb3

client = couchdb3.Server(
    "http://user:password@127.0.0.1:5984"
)

# Checking if the server is up
print(client.up())
# True

user and password can also be passed into the Server constructor as keyword parameters, e.g.

client = couchdb3.Server(
    "127.0.0.1:5984:",  # Scheme omitted - will assume http protocol
    user="user",
    password="password"
)

Both approaches are equivalent, i.e. in both cases the instance's scheme,host,port,user,password will be identical.

Further, clients can be used with context managers:

with couchdb3.Server("http://user:password@127.0.0.1:5984") as client:
    # Do stuff
    ...

Getting or creating a database

dbname = "mydb"
db = client.get(dbname) if dbname in client else client.create(dbname)
print(db)
# Database: mydb

Creating a document

mydoc = {
    "_id": "mydoc-id",
    "name": "Hello",
    "type": "World"
}
print(db.save(mydoc))
# ('mydoc-id', True, '1-24fa3b3fd2691da9649dd6abe3cafc7e')

Note: Database.save requires the document to have an id (i.e. a key _id), Database.create does not.

Updating a document

To update an existing document, retrieving the revision is paramount. In the example below, dbdoc contains the key _rev and the builtin dict.update function is used to update the document before saving it.

mydoc = {
    "_id": "mydoc-id",
    "name": "Hello World",
    "type": "Hello World"
}
dbdoc = db.get(mydoc["_id"])
dbdoc.update(mydoc)
print(db.save(dbdoc))
# ('mydoc-id', True, '2-374aa8f0236b9120242ca64935e2e8f1')

Alternatively, one can use Database.rev to fetch the latest revision and overwrite the document

mydoc = {
    "_id": "mydoc-id",
    "_rev": db.rev("mydoc-id"),
    "name": "Hello World",
    "type": "Hello World"
}
print(db.save(mydoc))
# ('mydoc-id', True, '3-d56b14b7ffb87960b51d03269990a30d')

Deleting a document

To delete a document, the docid and rev are needed

docid = "mydoc-id"
print(db.delete(docid=docid, rev=db.rev(docid)))  # Fetch the revision on the go
# True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

CouchDB3-1.1.0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

CouchDB3-1.1.0-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file CouchDB3-1.1.0.tar.gz.

File metadata

  • Download URL: CouchDB3-1.1.0.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for CouchDB3-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7e700c5646761a38b3f66cc8d1cb4cdfcf5700bd42a96d0781ce6aa400697035
MD5 a75dfc595daf794e3d4e1676b36ccea3
BLAKE2b-256 3b2b7ae54001cd765a53514685b643c8d206368465164ef4721f095cab42fed0

See more details on using hashes here.

File details

Details for the file CouchDB3-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: CouchDB3-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for CouchDB3-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eafd849f6ae3a67053fd2bd6d05afdc7b1fd6bcd54978ea97824e1fd232d6c29
MD5 36db0adcd9d45e300a6b12c9a6efcbde
BLAKE2b-256 d6644a17c51b8ea2a395e8ad2de95a4b08540b4483d423e1bd43a041bc04cf4c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page