Metadata-Version: 2.4
Name: couchdb-sdk
Version: 0.7.0
Summary: Apache CouchDB API
Home-page: https://github.com/laveresteban/couchdb-python
Author: Esteban Laver
Author-email: laveresteban@gmail.com
License: Apache-2.0
Project-URL: Source, https://github.com/laveresteban/couchdb-python
Project-URL: Changelog, https://github.com/laveresteban/couchdb-python/blob/main/CHANGELOG.md
Keywords: OpenAPI,OpenAPI-Generator,Apache CouchDB API,CouchDB
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Typing :: Typed
Requires-Python: >= 3.12
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: urllib3<3.0.0,>=1.25.3
Requires-Dist: python-dateutil>=2.8.2
Requires-Dist: pydantic>=2
Requires-Dist: typing-extensions>=4.7.1
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: license-file
Dynamic: project-url
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# couchdb-python

Python SDK for Apache CouchDB 3.x. It comes as two packages, both published together as `couchdb-sdk` on PyPI:

| Package | What it is | Edited by |
|---------|-----------|-----------|
| `couchdb_sdk` | Idiomatic client: dict-like documents, automatic `_rev` tracking, typed errors, conflict retries | hand-written |
| `couchdb_client` | Low-level client generated from [couchdb-openapi](https://github.com/laveresteban/couchdb-openapi) ([API docs](docs/)) | **generated, don't edit** |

## Quick start

```python
from couchdb_sdk import CouchDB, Conflict

with CouchDB("http://localhost:5984", "admin", "password") as couch:
    db = couch.create_database("people")

    db["alice"] = {"age": 30}           # create
    alice = db["alice"]                 # read -> dict with _id/_rev
    alice["age"] = 31
    db.save(alice)                      # update; alice["_rev"] is refreshed

    db.update("alice", lambda d: {**d, "age": d["age"] + 1})  # retries on 409

    db.bulk_save([{"age": a} for a in (20, 40, 50)])
    db.create_index(["age"], name="age-idx")
    older = db.find({"age": {"$gt": 30}}, sort=[{"age": "desc"}]).docs

    db.save_design("stats", {"by_age": {"map": "function(d){ emit(d.age, 1); }", "reduce": "_count"}})
    db.view("stats", "by_age")

    db.put_attachment("alice", "avatar.png", open("avatar.png", "rb").read(), "image/png")
    page = db.changes(since="0", doc_ids=["alice"])
    for change in db.follow(checkpoint="indexer", include_docs=True):
        ...  # retries transient errors; resumes from _local/indexer after restart
    couch.replicate("people", "people_backup", create_target=True)

    # Replication building blocks, for writing your own sync
    db.revs_diff({"alice": [alice["_rev"]]})            # revisions the db is missing
    db.bulk_get([{"id": "alice"}], revs=True)           # docs with _revisions
    db.bulk_save(docs_with_revisions, new_edits=False)  # keep the given _rev ids
```

`login()` keeps the cookie session alive: it takes the renewed cookie CouchDB
sends and logs in again once if the session expires.

Housekeeping and cheap checks:

```python
db.head("alice")                      # current rev without the body, or None
db.get_if_changed("alice", rev)       # None if unchanged (ETag / 304)
db.explain({"age": {"$gt": 30}})      # which index a query uses
db.purge({"alice": [rev]})            # remove revisions for good
db.compact(); db.view_cleanup()
couch.dbs_info(["people", "other"]); couch.active_tasks(); couch.db_updates(feed="longpoll")
```

Errors are typed: `NotFound` (also a `KeyError`), `Conflict`, `Unauthorized`,
`Forbidden`, `PreconditionFailed`, all subclasses of `CouchDBError` with
`.status`, `.error` and `.reason`.

Need an endpoint the wrapper doesn't cover? Use the generated client directly:
`couchdb_client.DocumentsApi(couch.client)`.

## Repository layout

```
couchdb_client/        generated; regenerated by couchdb-sdk-generator PRs
couchdb_sdk/           hand-written idiomatic layer
tests/                 pytest: unit + integration (real CouchDB)
conformance/           Gauge step implementations for the shared cross-SDK suite
scripts/conformance.sh runs the shared suite from couchdb-sdk-generator
```

Files the generator must not touch are listed in `.openapi-generator-ignore`.

## Testing

Both suites need a CouchDB server:

```sh
docker run -d --name couchdb -p 5984:5984 -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password couchdb:3.4
for db in _users _replicator _global_changes; do curl -X PUT http://admin:password@localhost:5984/$db; done

python -m venv .venv && .venv/Scripts/activate      # or: source .venv/bin/activate
pip install -e . -r test-requirements.txt

pytest tests                       # unit + integration
bash scripts/conformance.sh        # Gauge conformance (needs Node; specs from ../couchdb-sdk-generator)
bash scripts/conformance.sh --tags documents        # one area
```

Override the server with `COUCHDB_URL`, `COUCHDB_USER` and `COUCHDB_PASSWORD`.

## Versioning

The package version is the spec's `info.version`, stamped at generation time.
`.generated-from` records the exact spec and generator commits.

## Roadmap

Planned fixes and features are in [ROADMAP.md](ROADMAP.md).

## License

Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
