Crate Guide
RouchDB is structured as a Cargo workspace with 11 crates. This guide helps you figure out where to add new code.
Where Do I Add Code?
Use this decision tree to find the right crate:
Is it a new document type, revision type, or error variant?
--> rouchdb-core
Is it a new storage backend?
--> New crate implementing the Adapter trait (see "Adding an Adapter" below)
Is it a change to how queries work (Mango selectors, map/reduce)?
--> rouchdb-query
Is it a change to design documents or the persistent view engine?
--> rouchdb-views
Is it a change to the replication protocol or checkpointing?
--> rouchdb-replication
Is it a change to the changes feed or live streaming?
--> rouchdb-changes
Is it a change to the high-level Database API?
--> rouchdb (the umbrella crate)
Is it a change to how CouchDB HTTP communication works?
--> rouchdb-adapter-http
Is it a change to the in-memory storage implementation?
--> rouchdb-adapter-memory
Is it a change to the persistent redb storage implementation?
--> rouchdb-adapter-redb
Is it a change to the HTTP server or Fauxton integration?
--> rouchdb-server
Is it a change to the CLI tool (rouchdb binary)?
--> rouchdb-cli
Crate Descriptions
rouchdb-core
The foundation crate. Everything else depends on it.
Responsibility: Core types, traits, and algorithms shared across the entire project.
Key files:
src/adapter.rs– TheAdaptertrait that all storage backends implement. Defines methods likeget,bulk_docs,all_docs,changes,revs_diff,bulk_get, and local document operations.src/document.rs– Document types (Document,Revision,DocResult,DbInfo,AllDocsResponse,ChangesResponse,Seq, etc.).src/rev_tree.rs– Revision tree data structures (RevTree,RevPath,RevNode) and tree traversal utilities.src/merge.rs– Revision tree merging algorithm, winning revision selection (winning_rev), and conflict detection.src/collation.rs– CouchDB-compatible collation ordering for view keys.src/error.rs–RouchErrorenum andResulttype alias.
rouchdb-adapter-memory
Responsibility: In-memory adapter for fast testing and ephemeral databases. Stores documents in HashMap behind a RwLock.
Key files:
src/lib.rs– FullAdaptertrait implementation withStoredDocinternal structure, revision tree management, and sequence tracking.
rouchdb-adapter-redb
Responsibility: Persistent local storage using redb, a pure-Rust embedded key-value store.
Key files:
src/lib.rs–RedbAdapterstruct implementing theAdaptertrait with durable on-disk storage.
rouchdb-adapter-http
Responsibility: Remote CouchDB communication. Maps each Adapter method to the corresponding CouchDB REST API endpoint using reqwest.
Key files:
src/lib.rs–HttpAdapterstruct, CouchDB JSON response deserialization types, and the fullAdaptertrait implementation.
rouchdb-changes
Responsibility: Streaming changes feed with one-shot and live/continuous modes.
Key files:
src/lib.rs–ChangeSender,ChangeReceiver,ChangeNotification, andLiveChangesStream. Uses Tokio broadcast channels for real-time change notification.
rouchdb-replication
Responsibility: CouchDB replication protocol implementation. Handles checkpoint-based incremental replication between any two adapters.
Key files:
src/protocol.rs– The core replication loop: read changes from source, diff against target, fetch missing documents, write to target.src/checkpoint.rs– Checkpoint management using_localdocuments to track replication progress.src/lib.rs– Public API:replicate()function,ReplicationOptions,ReplicationResult,ReplicationEvent.
rouchdb-query
Responsibility: Mango query selectors and map/reduce view queries.
Key files:
src/mango.rs– Mango selector parsing and matching (matches_selector). Supports operators like$eq,$gt,$in,$regex, etc.src/mapreduce.rs– Map/reduce view execution (query_view). Takes a map function, optional reduce function, and query options.src/lib.rs– Re-exports:find,FindOptions,FindResponse,ViewQueryOptions,ViewResult,ReduceFn,SortField.
rouchdb-views
Responsibility: Design documents and the persistent view engine.
Key files:
src/lib.rs–DesignDocumentstruct (with views, filters, validate_doc_update) andViewEnginefor persistent map/reduce indexes.
rouchdb
Responsibility: The umbrella crate that end users depend on. Provides the high-level Database struct and re-exports types from all other crates.
Key files:
src/lib.rs–Databasestruct with user-friendly methods (put,get,update,remove,replicate_to,replicate_from,find,all_docs,changes). Wraps anyAdapterbehind anArc<dyn Adapter>.tests/*.rs– Integration tests against a real CouchDB instance (multiple test files:replication.rs,http_crud.rs,changes_feed.rs, etc.).
rouchdb-server
Responsibility: CouchDB-compatible HTTP server that wraps a Database instance behind an Axum web server. Serves the Fauxton web dashboard for browsing documents.
Key files:
src/lib.rs– Public API:build_router(),start_server(),ServerConfig.src/main.rs– Standalone binary entry point with clap CLI.src/state.rs–AppStateshared across all route handlers.src/error.rs– MapsRouchErrorto CouchDB-style JSON error responses.src/routes/*.rs– Route handlers for each CouchDB endpoint (root, session, all_dbs, database, document, all_docs, bulk_docs, find, index, explain, compact, design, fauxton).fauxton/– Embedded Fauxton static files (downloaded viascripts/download-fauxton.sh, gitignored).
rouchdb-cli
Responsibility: Command-line tool for inspecting and querying redb database files.
Key files:
src/main.rs– Clap-based CLI with subcommands:info,get,all-docs,find,changes,dump,replicate,compact.
Adding an Adapter
To add a new storage backend (e.g., SQLite, IndexedDB via wasm):
- Create a new crate:
crates/rouchdb-adapter-yourbackend/ - Add it to the workspace
memberslist in the rootCargo.toml - Depend on
rouchdb-corefor theAdaptertrait and document types - Implement the
Adaptertrait fromrouchdb_core::adapter::Adapter - Add the crate as a dependency in the umbrella
rouchdbcrate and re-export it
The Adapter trait requires implementing these async methods:
info()– Database metadataget()– Fetch a single documentbulk_docs()– Write multiple documents atomicallyall_docs()– List/query all documentschanges()– Get changes since a sequencerevs_diff()– Compare revision sets (for replication)bulk_get()– Fetch multiple documents by ID and revisionget_local()/put_local()/remove_local()– Local (non-replicated) document operationsget_attachment()/put_attachment()/remove_attachment()– Attachment storageclose()– Clean up resources
Look at rouchdb-adapter-memory as a reference implementation – it is the simplest complete adapter.
Dependency Graph
rouchdb (umbrella)
|-- rouchdb-core
|-- rouchdb-adapter-memory --> rouchdb-core
|-- rouchdb-adapter-redb --> rouchdb-core
|-- rouchdb-adapter-http --> rouchdb-core
|-- rouchdb-changes --> rouchdb-core
|-- rouchdb-query --> rouchdb-core
|-- rouchdb-views --> rouchdb-core
|-- rouchdb-replication --> rouchdb-core, rouchdb-query
rouchdb-server --> rouchdb, rouchdb-core (HTTP server + Fauxton)
rouchdb-cli --> rouchdb (CLI tool)
All library crates depend on rouchdb-core. The umbrella rouchdb crate depends on all of them and re-exports their public APIs. The rouchdb-server and rouchdb-cli crates are standalone binaries that depend on the umbrella crate.