CouchDBClient (document_store/couchdb_client.py) wraps aiocouch and
exposes typed async methods. DocumentStore (document_store/__init__.py)
is a thin facade that routes calls to CouchDBClient.
DocumentStore
└─→ CouchDBClient (aiocouch)
└─→ CouchDB server
-
System database initialization inside
connect()— CouchDB requires_usersand_replicatorsystem databases to exist before application databases can be reliably created. Initializing them inconnect()removes the need for a separatecouchdb-initsidecar or init container, simplifying the deployment topology. -
Race-condition-safe creation via
412tolerance — Multiple service replicas may callconnect()simultaneously. UsingPUT /dband treating412 Precondition Failedas success is the standard CouchDB pattern for idempotent database creation; it is simpler and more reliable than aHEAD-then-PUTcheck-and-create pattern. -
Content hash for idempotency — Comparing a SHA-256 hash of the serialized ARC JSON avoids unnecessary CouchDB writes and downstream Celery tasks when a client re-submits an unchanged ARC. The hash is stored as a field on the document.
-
Concrete types for
_clientand_db— Annotating_client: CouchDB | Noneand_db: Database | None(instead ofAny) allows Mypy to catch incorrect attribute access at compile time. -
Lazy
aiohttp.ClientSessionfor raw calls — Some operations (e.g., index management) require direct HTTP calls not covered by theaiocouchAPI. A shared session is created lazily on first use and closed alongside the main client to avoid resource leaks.