Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

RouchDB

A local-first document database for Rust with CouchDB replication protocol support.

RouchDB is the Rust equivalent of PouchDB — it gives you a local JSON document store that syncs bidirectionally with Apache CouchDB and compatible servers.

Why RouchDB?

  • No equivalent exists in Rust. Crates like couch_rs provide CouchDB HTTP clients, but none offer local storage with replication. RouchDB fills this gap.
  • Offline-first by design. Your app works without a network connection. When connectivity returns, RouchDB syncs changes automatically.
  • Full CouchDB compatibility. Implements the CouchDB replication protocol, revision tree model, and Mango query language.

Features

  • CRUD operations — put, get, update, remove, bulk_docs, all_docs
  • Mango queries — $eq, $gt, $in, $regex, $or, and 15+ operators
  • Map/reduce views — Rust closures with built-in Sum, Count, Stats reduces
  • Changes feed — one-shot and live streaming of document mutations
  • Replication — push, pull, and bidirectional sync with CouchDB
  • Conflict resolution — deterministic winner algorithm, conflict detection utilities
  • CouchDB-compatible HTTP server — browse databases with Fauxton, connect any CouchDB client
  • Pluggable storage — in-memory, persistent (redb), or remote (HTTP)
  • No system libraries — pure-Rust storage (redb), and HTTPS through rustls by default instead of OpenSSL

Target Use Cases

  • Desktop apps with Tauri that need offline sync
  • CLI tools that store data locally and sync to a server
  • Backend services that replicate between CouchDB instances
  • Any Rust application that needs a local document database

Quick Example

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    // Create a document
    let result = db.put("user:alice", serde_json::json!({
        "name": "Alice",
        "age": 30
    })).await?;

    // Read it back
    let doc = db.get("user:alice").await?;
    println!("{}: {}", doc.id, doc.data);

    Ok(())
}

Ready to start? Head to Installation.

Installation

Upgrading from 0.4? 0.5 does not open .redb files written by 0.4 until you upgrade them once, with rouchdb migrate app.redb (which writes a verified backup first; --dry-run only reads the file) or Database::open_with + UpgradePolicy::WithBackup, with about three times the file size of free disk space; afterwards 0.4 cannot open them, and the first compact() deletes old revision bodies. See Migrating from 0.4 to 0.5.

Full Package

Add RouchDB to your project with all features:

[dependencies]
rouchdb = "0.5"
tokio = { version = "1", features = ["full"] }
serde_json = "1"

This gives you local storage (redb), HTTP client, replication, queries, and the high-level Database API.

RouchDB requires Rust 1.88 or newer.

TLS Backend

HTTPS connections to CouchDB (Database::http("https://...")) use rustls with the bundled Mozilla root certificates by default, so no OpenSSL or other system library is needed. Choose another backend with Cargo features:

FeatureTLS stackTrusted roots
rustls-tls (default)rustlsBundled Mozilla roots
rustls-tls-native-rootsrustlsThe operating system’s certificate store (e.g. a private CA)
native-tlsPlatform stack (OpenSSL on Linux)The operating system’s certificate store
[dependencies]
rouchdb = { version = "0.5", default-features = false, features = ["native-tls"] }

With default-features = false and no TLS feature, only plain http:// URLs work, and the build has no C code at all (rustls’ default crypto provider, ring, compiles a small amount of bundled C and assembly). The same features exist on rouchdb-adapter-http. These features are new in 0.5.0 (see the changelog); 0.4.x always uses native-tls.

Exact numbers

By default JSON numbers are serde_json numbers: integers that fit i64/u64 are exact, anything else goes through f64 (so 18446744073709551616 is stored as 1.8446744073709552e19 and 1.50 as 1.5). The opt-in arbitrary-precision feature keeps every number exactly as written, through the memory and redb adapters, replication and the HTTP adapter (CouchDB itself keeps integers of any size exact and rounds decimals to doubles):

cargo add rouchdb --features arbitrary-precision

It enables serde_json’s arbitrary_precision for the whole build, which also changes serde_json::Number for your own code (comparisons are textual: 1.0 and 1.00 are different numbers), and revision ids of documents with such numbers differ from those computed without the feature. See Differences from CouchDB.

Minimal Setup

If you only need local storage without replication or HTTP:

[dependencies]
rouchdb-core = "0.5"
rouchdb-adapter-redb = "0.5"
tokio = { version = "1", features = ["full"] }
serde_json = "1"

Individual Crates

Pick exactly what you need:

CrateWhat it adds
rouchdb-coreTypes, traits, revision tree, collation, errors
rouchdb-adapter-memoryIn-memory adapter (testing, ephemeral data)
rouchdb-adapter-redbPersistent local storage via redb
rouchdb-adapter-httpCouchDB HTTP client adapter
rouchdb-changesChanges feed (one-shot and live streaming)
rouchdb-replicationCouchDB replication protocol
rouchdb-queryMango selectors and map/reduce views
rouchdb-viewsDesign documents and persistent view engine
rouchdb-serverCouchDB-compatible HTTP server with Fauxton
rouchdb-cliCLI tool for inspecting databases
rouchdbUmbrella crate — re-exports everything above

CLI Tool

RouchDB includes a command-line tool for inspecting and querying redb database files, published on crates.io as rouchdb-cli:

cargo install rouchdb-cli

From a checkout of the repository, cargo install --path crates/rouchdb-cli installs the same tool.

This installs the rouchdb binary. Usage examples:

# Show database info
rouchdb info mydb.redb

# Upgrade a file written by rouchdb 0.4 (writes a verified backup first)
rouchdb migrate mydb.redb

# Get a document by ID
rouchdb get mydb.redb user:alice

# List all documents with their bodies
rouchdb all-docs mydb.redb --include-docs

# Query with a Mango selector
rouchdb find mydb.redb --selector '{"age": {"$gte": 30}}'

# View the changes feed
rouchdb changes mydb.redb --include-docs

# Export all documents as JSON
rouchdb dump mydb.redb --pretty

# Create or update a document
rouchdb put mydb.redb user:alice '{"name":"Alice","age":30}'
rouchdb put mydb.redb user:alice '{"name":"Alice","age":31}' --rev 1-abc

# Upsert — auto-fetches the current rev, creates if missing
rouchdb put mydb.redb user:alice '{"name":"Alice","age":32}' --force

# Create a document with auto-generated ID
rouchdb post mydb.redb '{"name":"Bob","age":25}'

# Delete a document
rouchdb delete mydb.redb user:alice --rev 2-def

# Import documents from a JSON file
rouchdb import mydb.redb docs.json

# Replicate to CouchDB
rouchdb replicate mydb.redb http://admin:password@localhost:5984/mydb

# Compact the database
rouchdb compact mydb.redb

Add --pretty (or -p) to any command for formatted JSON output.

HTTP Server

RouchDB includes a CouchDB-compatible HTTP server that lets you browse databases with the Fauxton web UI or connect any CouchDB client. Install it from source:

cargo install --path crates/rouchdb-server

Download Fauxton (optional, for the web dashboard):

bash scripts/download-fauxton.sh

Start the server:

# Serve a redb database file
rouchdb-server mydb.redb --port 5984

# Open Fauxton in your browser
open http://localhost:5984/_utils/

The server exposes CouchDB-compatible REST endpoints — documents, queries, changes feed, attachments, security, design docs, and more — so tools like PouchDB, curl, or any CouchDB client library can connect to it.

Options:

rouchdb-server <path.redb> [OPTIONS]

Options:
  -p, --port <PORT>                Port to listen on [default: 5984]
      --host <HOST>                Host to bind to [default: 127.0.0.1]
      --db-name <NAME>             Database name [default: filename without extension]
      --admin <USER:PASSWORD>      Require admin credentials [env: ROUCHDB_ADMIN]
      --cors-origin <ORIGIN>       Allow CORS from this origin (repeatable) [env: ROUCHDB_CORS_ORIGINS]
      --allowed-host <HOST>        Also accept this name in the Host header (repeatable) [env: ROUCHDB_ALLOWED_HOSTS]
      --allow-unauthenticated      Serve on a non-loopback --host without --admin [env: ROUCHDB_ALLOW_UNAUTHENTICATED]
      --trust-proxy                Trust X-Forwarded-Proto from a reverse proxy [env: ROUCHDB_TRUST_PROXY]
      --max-request-size <BYTES>   Largest accepted request body [default: 67108864]
      --session-timeout <SECONDS>  Idle lifetime of a _session cookie [default: 600]
      --upgrade                    Upgrade a file written by rouchdb 0.4 (after a verified backup)

rouchdb-server --help describes every option. Switches set through the environment take 1, true, yes, on (or 0, false, no, off).

The server serves a single database. DELETE /{db} destroys its data and the database answers 404 until PUT /{db} creates it again (a restart also brings it back, empty).

GET / and GET /{db} report a uuid (32 hex digits, like CouchDB’s) derived from the served database’s identity (Adapter::id()): it is stable across restarts on the same file, different for every file, and renewed when DELETE /{db} destroys the database. HTTP clients (Database::http, PouchDB) identify a database by this uuid plus its name, so two servers that serve same-named databases are two databases to them, and replication between them resumes from its checkpoints.

Server security

The defaults are meant for local development: the server listens on 127.0.0.1, CORS is disabled and authentication is off.

  • Authentication. Set ROUCHDB_ADMIN=user:password (or --admin) to require credentials: HTTP Basic auth or a _session cookie (HttpOnly, SameSite=Strict), which expires after --session-timeout seconds without use. Only /, /_session, /_uuids and /_utils stay public. Prefer the environment variable, so the password does not show up in the shell history or ps.

  • Listening on the network. On an address other than loopback (--host 0.0.0.0, ::, a LAN address or a host name other than localhost) the server refuses to start without --admin, since anyone who can reach the address could read, write and delete the database:

    ROUCHDB_ADMIN=admin:secret rouchdb-server mydb.redb --host 0.0.0.0
    

    If every client that can reach the address is trusted (an isolated container network, for example), serve without authentication explicitly with --allow-unauthenticated (or ROUCHDB_ALLOW_UNAUTHENTICATED=1); the server then prints a warning at startup.

  • CORS. Allow browser apps on other origins explicitly with --cors-origin http://localhost:3000 (repeatable). Credentials are allowed for listed origins; * allows any origin without credentials.

  • Host header (DNS rebinding). A web page can point its own domain name at 127.0.0.1 after it has loaded, and then talk to a local server with same-origin requests, which CORS does not stop; the browser still sends the page’s domain in the Host header. On a loopback --host the server therefore only answers requests addressed to localhost, 127.0.0.1, [::1] or the --host address (with any port, in any case), and answers any other Host with a 400 bad_request, before authentication, CORS and routing (/_utils included). A request without Host (HTTP/1.0) is served: browsers always send it. --allowed-host (repeatable, or comma-separated in ROUCHDB_ALLOWED_HOSTS) adds names or IP addresses, without port. On any other --host the header is checked only when --allowed-host is given, and the loopback names and the --host address are then accepted too.

  • Response headers. Every response carries X-Content-Type-Options: nosniff, and attachments are served with Content-Security-Policy: sandbox, so an uploaded HTML page or script cannot run on the server’s origin.

  • Not covered. Per-database _security members and admins are stored but not enforced: with --admin, the admin is the only user.

Behind a reverse proxy

A reverse proxy (nginx, Caddy, Traefik, …) in front of a server on loopback usually forwards the client’s Host, its public name. Declare that name, or the server answers 400:

ROUCHDB_ADMIN=admin:secret rouchdb-server mydb.redb \
  --allowed-host db.example.com --trust-proxy
  • --trust-proxy (ROUCHDB_TRUST_PROXY=1) tells the server that X-Forwarded-Proto comes from the proxy: when it says https, the _session cookie gets the Secure attribute. Without the flag the header does not change the cookie (a Secure cookie never comes back over plain HTTP, which would break logging in on http://localhost). Only use it when the proxy sets that header, since a client that reaches the server directly could send any value.
  • As in CouchDB, the Location header of 201 Created responses is an absolute URL built from X-Forwarded-Host and X-Forwarded-Proto when the request has them (with or without --trust-proxy), else from Host. Only Host is checked against the allowed names, so if your clients follow Location, have the proxy set X-Forwarded-Host (or drop the client’s).

Async Runtime

RouchDB is built on Tokio. All database operations are async and require a Tokio runtime:

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = rouchdb::Database::memory("mydb");
    // ... your code here
    Ok(())
}

Verifying the Installation

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("test");

    let result = db.put("hello", serde_json::json!({"msg": "it works!"})).await?;
    assert!(result.ok);

    let doc = db.get("hello").await?;
    println!("{}", doc.data["msg"]); // "it works!"

    Ok(())
}

If this compiles and prints "it works!", you’re all set. Head to the Quickstart.

Quickstart

This guide walks you through the core features of RouchDB in 5 minutes.

Create a Database

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    // In-memory (data lost when dropped — great for testing)
    let db = Database::memory("mydb");

    // Persistent (stored on disk via redb)
    // let db = Database::open("mydb.redb", "mydb")?;

    // Remote CouchDB
    // let db = Database::http("http://admin:password@localhost:5984/mydb");

    Ok(())
}

Put and Get Documents

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    // Create a document
    let result = db.put("user:alice", serde_json::json!({
        "name": "Alice",
        "email": "alice@example.com",
        "age": 30
    })).await?;

    println!("Created with rev: {}", result.rev.unwrap());

    // Read it back
    let doc = db.get("user:alice").await?;
    println!("Name: {}", doc.data["name"]); // "Alice"

    Ok(())
}

Update and Delete

Every update requires the current revision to prevent conflicts:

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    // Create
    let r1 = db.put("user:alice", serde_json::json!({"name": "Alice", "age": 30})).await?;
    let rev = r1.rev.unwrap();

    // Update (must provide current rev)
    let r2 = db.update("user:alice", &rev, serde_json::json!({
        "name": "Alice",
        "age": 31
    })).await?;

    // Delete (must provide current rev)
    let rev2 = r2.rev.unwrap();
    db.remove("user:alice", &rev2).await?;

    Ok(())
}

Query with Mango

Find documents matching a selector:

use rouchdb::{Database, FindOptions};

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    db.put("alice", serde_json::json!({"name": "Alice", "age": 30})).await?;
    db.put("bob", serde_json::json!({"name": "Bob", "age": 25})).await?;
    db.put("carol", serde_json::json!({"name": "Carol", "age": 35})).await?;

    // Find users older than 28
    let result = db.find(FindOptions {
        selector: serde_json::json!({"age": {"$gte": 28}}),
        ..Default::default()
    }).await?;

    for doc in &result.docs {
        println!("{}: age {}", doc["name"], doc["age"]);
    }
    // Alice: age 30
    // Carol: age 35

    Ok(())
}

Sync Two Databases

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let local = Database::memory("local");
    let remote = Database::memory("remote");

    // Add data to each side
    local.put("doc1", serde_json::json!({"from": "local"})).await?;
    remote.put("doc2", serde_json::json!({"from": "remote"})).await?;

    // Bidirectional sync
    let (push, pull) = local.sync(&remote).await?;
    println!("Push: {} docs written", push.docs_written);
    println!("Pull: {} docs written", pull.docs_written);

    // Both databases now have both documents
    let info = local.info().await?;
    println!("Local has {} docs", info.doc_count); // 2

    Ok(())
}

Next Steps

Core Concepts

Understanding these five concepts will make everything else in RouchDB click.

Documents

A document is a JSON object identified by a unique _id. This is the fundamental unit of data in RouchDB.

{
  "_id": "user:alice",
  "_rev": "1-abc123...",
  "name": "Alice",
  "email": "alice@example.com",
  "age": 30
}
  • _id — unique identifier you choose (or RouchDB generates a UUID v4, written like CouchDB’s ids as 32 hex digits)
  • _rev — revision string managed by RouchDB (never set this manually)
  • Everything else is your data, stored as serde_json::Value

In Rust, you work with the Document struct:

#![allow(unused)]
fn main() {
pub struct Document {
    pub id: String,
    pub rev: Option<Revision>,
    pub deleted: bool,
    pub data: serde_json::Value,
    pub attachments: HashMap<String, AttachmentMeta>,
}
}

Revisions

Every time a document is created or updated, RouchDB assigns it a new revision. Revisions look like {generation}-{hash}:

1-9a2c3b4d5e6f...    (first version)
2-7f8e9d0c1b2a...    (after first update)
3-3a4b5c6d7e8f...    (after second update)
  • The generation (1, 2, 3…) counts how many times the document has been modified.
  • The hash is an MD5 digest of the document’s content, making it deterministic.

Why revisions matter:

  1. Conflict detection — when you update a document, you must provide the current _rev. If someone else updated it first, you get a Conflict error.
  2. Replication — revisions let two databases figure out which changes they’re missing.
  3. History — RouchDB keeps a tree of revisions (not the full data — old revisions are compacted away).

Conflicts

Conflicts happen when the same document is modified on two different replicas before they sync.

         Replica A              Replica B
            |                      |
        doc rev 1-abc          doc rev 1-abc
            |                      |
        update to 2-def        update to 2-ghi
            |                      |
            +-------  sync  -------+
            |                      |
        conflict! two rev-2 branches

RouchDB handles this the same way CouchDB does:

  1. Deterministic winner — one revision is picked as the “winner” using a consistent algorithm (non-deleted beats deleted, higher generation wins, ties broken by lexicographic hash comparison).
  2. No data loss — the “losing” revision is kept as a conflict. You can read it and resolve it.
  3. Application-level resolution — your code decides how to merge conflicting changes.

Read more in the Conflict Resolution guide.

Adapters

An adapter is a storage backend. RouchDB provides three built-in adapters behind a single Adapter trait:

AdapterConstructorUse case
MemoryDatabase::memory("name")Tests, ephemeral caches
RedbDatabase::open("path.redb", "name")Persistent local storage
HTTPDatabase::http("http://...")Remote CouchDB server

All three implement the same trait, so your code works identically regardless of the backend:

#![allow(unused)]
fn main() {
// This function works with any adapter
async fn count_docs(db: &Database) -> rouchdb::Result<u64> {
    let info = db.info().await?;
    Ok(info.doc_count)
}
}

You can also implement the Adapter trait yourself for custom backends (SQLite, S3, etc.).

Changes Feed

Every mutation (create, update, delete) gets a sequence number. The changes feed lets you query “what changed since sequence X?”:

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesOptions, Seq};

async fn example(db: &Database) -> rouchdb::Result<()> {
    let changes = db.changes(ChangesOptions {
        since: Seq::Num(0), // from the beginning
        include_docs: true,
        ..Default::default()
    }).await?;

    for change in &changes.results {
        println!("seq {}: {} {}",
            change.seq,
            change.id,
            if change.deleted { "(deleted)" } else { "" }
        );
    }

    Ok(())
}
}

The changes feed is also the backbone of replication — it’s how two databases discover what they need to sync.

For live updates (streaming changes as they happen), see the Changes Feed guide.

Migrating from 0.4 to 0.5

RouchDB 0.5 is a CouchDB-fidelity release: behavior was checked against CouchDB 3.5.1 and changed wherever RouchDB disagreed. Most programs only need the first few sections below; the rest lists behavior changes you may rely on without knowing it. The full list is in the changelog.

Using redb files (Database::open, rouchdb-server, the CLI)? Read redb files first. 0.5 refuses a file written by 0.4 until you upgrade it once (rouchdb migrate app.redb, which keeps a verified backup and needs about three times the file size of free disk space; --dry-run only reads the file), and afterwards 0.4 cannot open it. The first compact() after the upgrade permanently deletes old revision bodies.


Update the dependency

[dependencies]
rouchdb = "0.5"

HTTPS now uses rustls with bundled Mozilla roots by default. If your CouchDB certificate is signed by a private CA installed in the OS trust store, pick a TLS feature that reads the OS store:

[dependencies]
rouchdb = { version = "0.5", default-features = false, features = ["rustls-tls-native-roots"] }
# or features = ["native-tls"] for the platform TLS stack (the only option in 0.4)

The minimum supported Rust version is 1.88.


redb files: upgrade once, explicitly

The redb file format changed, and a file cannot be used by both versions: a 0.4 build writing to a file 0.5 wrote would treat its documents as missing and silently replace their history. So:

  • 0.5 does not open a file written by 0.4. Database::open / RedbAdapter::open return RouchError::UpgradeRequired, and the file is not modified. This includes read-only uses (rouchdb info, rouchdb get) and rouchdb-server.
  • You upgrade each file once, keeping a verified backup, in one of three ways:
rouchdb migrate app.redb --dry-run  # report only: the file is only read
rouchdb migrate app.redb            # backup to app.redb.rouchdb-0.4.bak, then upgrade
rouchdb migrate app.redb --backup /elsewhere/app.bak
rouchdb migrate app.redb --no-backup
use rouchdb::{Database, OpenOptions, RedbAdapter, UpgradePolicy};

// Explicitly, before opening:
let report = RedbAdapter::upgrade("app.redb", UpgradePolicy::WithBackup(None))?;
println!("{report}");

// Or when opening (a 0.4 file is upgraded; others open as usual):
let db = Database::open_with(
    "app.redb",
    "app",
    OpenOptions::new().upgrade(UpgradePolicy::WithBackup(None)),
)?;

rouchdb-server --upgrade app.redb does the same before serving. RedbAdapter::inspect_upgrade(path) is the dry run from Rust. The CLI is installed from the repository (cargo install --git https://github.com/rubylab-app/rouchdb --tag v0.5.0 rouchdb-cli, or cargo install --path crates/rouchdb-cli from a checkout).

The dry run (--dry-run, RedbAdapter::inspect_upgrade) only reads the file, in read transactions: not a byte of it changes and it needs no free space. It runs the upgrade’s analysis: it validates every record and computes the plan, so a file the upgrade would refuse for its content (see below) is refused with the same error, and its report has the same counts the upgrade will report. It does not exercise the rest of the upgrade: it does not check the backup path, write or verify the backup, check the free disk space, or write and commit the upgrade, so the upgrade can still fail at one of those steps (an existing backup path, a full disk).

The backup (default <file>.rouchdb-0.4.bak) is a complete copy that 0.4 opens exactly like the original: every table is copied from one read transaction, reopened and compared entry by entry, synced, then renamed into place. The upgrade is refused if the backup path exists. If a previous attempt was interrupted:

  • a <backup>.partial file is what a killed process left while writing the backup: it is not a backup, the database file was not changed by that attempt, and the error says to delete it and retry;
  • a <backup> file left by an attempt interrupted after the backup is a complete backup (it gets its final name only after it has been verified): move it elsewhere to keep it, or pass another --backup path. The same messages appear when rouchdb-server --upgrade is restarted.

Disk space and memory. The upgrade needs about twice the file size of free disk space (redb copies every page it changes and commits in two phases), and the backup about the file size again: plan for about three times the file size free when backing up (about twice with --no-backup). There is no space check beforehand: running out of space fails cleanly, leaving the file as it was. Memory stays small whatever the file size (records and attachments are processed one at a time with a 32 MiB page cache): a 2.2 GB file holding 1 GB of attachments upgrades with a backup in about 110 MB of memory.

The upgrade is one atomic transaction with two-phase commit: the file is upgraded completely or not at all. What an error leaves behind depends on when it happens:

  • Before the commit (writing the backup, or applying the plan): nothing was written to the file, which is exactly as it was, and the backup made for that attempt is removed (it is redundant, and would block a retry).
  • During the commit (the final write fails): the backup is kept, and the error says so. If rouchdb still reports that the file must be upgraded, the file was not changed: move the backup elsewhere (or choose another backup path) before retrying. Otherwise the upgrade was committed, and the backup is your copy of the old file.

The upgrade:

  1. re-keys attachment bytes by digest (0.4 kept one copy per document and attachment name) and counts the documents;
  2. moves _local/… documents that 0.4 stored as ordinary documents (written with put("_local/…") or bulk_docs) to the local document store, where 0.5 reads them: the latest body is kept with revision 0-N (N = its generation), deleted ones and conflicting branches are dropped (see _local/ ids);
  3. rewrites revision ids stored in upper-case hex (accepted by 0.4 through new_edits: false) in lower case, so 0.5 can address them (see Revision ids that differ only in case);
  4. installs the format guard: from then on, and for every file 0.5 creates, rouchdb 0.1–0.4 fail to open the file with metadata is of type Table<&str, rouchdb-format-2 (this file requires rouchdb >= 0.5)> and write nothing. Downgrading means restoring the backup.

It reports what it found: documents, _local/ documents moved, revisions normalized, case-duplicate revisions merged and bodies discarded, documents whose winning revision changes, attachment references whose bytes 0.4 had already lost (re-attaching a name in 0.4 overwrote the bytes of older revisions), old revision bodies, and documents whose attachment bytes only old revisions reference. The report ends with advice that matches what was done (dry run or not, backup or not). If a record cannot be decoded, the upgrade stops with an error naming it, and changes nothing: repair or remove that record with 0.4 and retry. The same applies to a _local/x document that collides with a local document x written with put_local, and to a live document whose id is exactly _local/ (a local document with an empty name, which 0.5 cannot address): copy it if needed and delete it with 0.4. These checks run before the backup is written.

Revision ids that differ only in case

0.4 accepted replicated revision ids in upper-case hex and compared ids as written, so a file can hold the same revision under two spellings (2-ABC… and 2-abc…, which 0.4 saw as two revisions), and 0.4 may have picked a different winner among conflicting revisions than 0.5 does (0.4 ranked B… below a…; 0.5 compares b… with a…). The upgrade:

  • merges the spellings of a revision into one lower-case revision, which keeps the children of both;
  • keeps one copy of their bodies when both are stored and identical;
  • when the bodies differ, keeps the body of the spelling 0.4 ranked first — a leaf before an inner revision, a live leaf before a deleted one, then the greater id in byte order (lower case sorts after upper case). This is the order in which 0.4 picked the winning revision, so if one of the spellings was the document’s winner in 0.4, its body is kept. The other body is listed in UpgradeReport::case_duplicate_bodies_discarded (document, discarded spelling, kept spelling) and printed by rouchdb migrate; it stays in the backup;
  • reports the documents whose winning revision changes (docs_with_changed_winner and its count).

Files of 0.5 development builds

Files written by unreleased 0.5 development builds (which 0.4 cannot open either) are upgraded on any open, after a verified backup to <file>.rouchdb-0.5-pre.bak, a copy of the file as the development build left it (0.4 cannot open that copy; this version upgrades it again). UpgradePolicy::InPlaceNoBackup skips the backup.

The first compact() after the upgrade deletes data 0.4 kept. 0.4’s compact did nothing; 0.5 keeps only the bodies of leaf revisions and the attachment bytes they reference. The first compaction therefore permanently deletes every old revision body and every attachment only old revisions reference (0.4 dropped the attachments of a document whose body was updated without them, so this is common). Read the report, and keep the backup until you are sure.

Revision trees stay in 0.4’s nested format until each document is next written (both are readable). Attachments stored by 0.4 read back with revpos 0. Files written by a newer RouchDB, and redb files RouchDB did not create, are refused and left untouched.


Single-document writes return errors

put, update, remove, post and put_design used to return Ok with ok: false when the write failed. They now return Err, like every other failing call. bulk_docs still returns one DocResult per document.

#![allow(unused)]
fn main() {
// 0.4
let res = db.update("doc1", &rev, json!({"n": 2})).await?;
if !res.ok {
    // stale revision
}

// 0.5
match db.update("doc1", &rev, json!({"n": 2})).await {
    Ok(res) => println!("new rev {}", res.rev.unwrap_or_default()),
    Err(RouchError::Conflict) => { /* stale revision: re-read and retry */ }
    Err(e) => return Err(e),
}
}

Related changes, identical on the memory, redb and HTTP adapters:

Call0.40.5
update of a document that does not existok: false (not_found)Err(Conflict)
remove of a missing or already deleted documentok: false (over HTTP it could write a tombstone)Err(NotFound)
get with a malformed rev ("garbage")NotFound (local), BadRequest (HTTP)Err(InvalidRev)
get of a stemmed or compacted revisionOk with a stale or empty bodyErr(NotFound)

Option defaults equal new()

BulkDocsOptions::default() used to mean replication mode (new_edits: false), AllDocsOptions::default() an exclusive end_key, and ViewQueryOptions::default() no reduce and an exclusive end_key. All three now equal new().

#![allow(unused)]
fn main() {
// 0.4: silently wrote in replication mode
db.bulk_docs(docs, BulkDocsOptions::default()).await?;
// 0.5: Default is a normal write; ask for replication explicitly
db.bulk_docs(docs, BulkDocsOptions::replication()).await?;

// 0.4: end_key excluded unless you used new()
AllDocsOptions { end_key: Some("m".into()), ..Default::default() }
// 0.5: end_key is inclusive, like CouchDB; opt out explicitly
AllDocsOptions { end_key: Some("m".into()), inclusive_end: false, ..Default::default() }

// 0.4: map rows and an exclusive end_key, even with a reduce function
ViewQueryOptions { end_key: Some(json!("m")), ..Default::default() }
// 0.5: reduces (when given a reduce function) and includes end_key, like
// CouchDB and `new()`; opt out explicitly
ViewQueryOptions { end_key: Some(json!("m")), reduce: false, inclusive_end: false, ..Default::default() }
}

all_docs rows

AllDocsRow has an optional id and value and a new error:

#![allow(unused)]
fn main() {
// 0.4
pub struct AllDocsRow { pub id: String, pub key: String, pub value: AllDocsRowValue, pub doc: Option<Value> }
// 0.5
pub struct AllDocsRow {
    pub id: Option<String>,             // None for an error row
    pub key: String,                    // always set; the id for document rows
    pub value: Option<AllDocsRowValue>, // None for an error row
    pub doc: Option<Value>,
    pub error: Option<String>,          // Some("not_found") for an unknown key
}
}
#![allow(unused)]
fn main() {
// 0.4
for row in &resp.rows {
    println!("{} {}", row.id, row.value.rev);
}
// 0.5: `key` is the document id of a document row; `rev()` its revision
for row in &resp.rows {
    println!("{} {}", row.key, row.rev().unwrap_or_default());
}
}

With keys, rows match the request one to one, as in CouchDB and PouchDB. Unknown ids are no longer skipped, and deleted documents get a row too:

#![allow(unused)]
fn main() {
let resp = db.all_docs(AllDocsOptions {
    keys: Some(vec!["a".into(), "nope".into()]),
    ..AllDocsOptions::new()
}).await?;
// 0.4: rows == [a]     (memory/redb skipped "nope", HTTP dropped CouchDB's row)
// 0.5: rows == [a, not_found]    rows[1] == AllDocsRow::not_found("nope")
for row in &resp.rows {
    if row.is_error() { /* no such document */ }
    else if row.is_deleted() { /* value.deleted == Some(true), no doc */ }
    else if let Some(doc) = &row.doc { /* include_docs */ }
}
}

skip and limit count error rows too.


Design documents

DesignDocument is lossless: any design document CouchDB accepts reads and writes back unchanged.

#![allow(unused)]
fn main() {
// 0.4
pub struct DesignDocument {
    pub id: String, pub rev: Option<String>,
    pub views: HashMap<String, ViewDef>,
    pub filters: HashMap<String, String>, pub validate_doc_update: Option<String>,
    pub shows: HashMap<String, String>, pub lists: HashMap<String, String>,
    pub updates: HashMap<String, String>, pub language: Option<String>,
}
pub struct ViewDef { pub map: String, pub reduce: Option<String> }

// 0.5
pub struct DesignDocument {
    pub id: String, pub rev: Option<String>,
    pub views: HashMap<String, ViewDef>,     // JavaScript views (string `map`)
    pub other_views: Map<String, Value>,     // new: `lib`, Mango (`language: "query"`) views
    pub filters: HashMap<String, Value>,     // String -> Value (CouchDB accepts objects)
    pub validate_doc_update: Option<String>,
    pub shows: HashMap<String, Value>,
    pub lists: HashMap<String, Value>,
    pub updates: HashMap<String, Value>,
    pub language: Option<String>,
    pub extra: Map<String, Value>,           // new: options, autoupdate, rewrites, custom fields
}
pub struct ViewDef { pub map: String, pub reduce: Option<String>, pub extra: Map<String, Value> }
}

Both types implement Default and have builders:

#![allow(unused)]
fn main() {
// 0.4
let ddoc = DesignDocument {
    id: "_design/app".into(), rev: None,
    views: HashMap::from([("by_type".into(), ViewDef { map: MAP.into(), reduce: Some("_count".into()) })]),
    filters: HashMap::new(), validate_doc_update: None, shows: HashMap::new(),
    lists: HashMap::new(), updates: HashMap::new(), language: None,
};
// 0.5 (or a struct literal with ..Default::default())
let ddoc = DesignDocument::new("app")
    .with_view("by_type", ViewDef::new(MAP).with_reduce("_count"))
    .with_filter("users", "function(doc) { return doc.type === 'user'; }");
}

Filter, show, list and update functions are serde_json::Values:

#![allow(unused)]
fn main() {
// 0.4
ddoc.filters.insert("f".into(), "function(doc) { ... }".to_string());
let src: &String = &ddoc.filters["f"];
// 0.5
ddoc.filters.insert("f".into(), "function(doc) { ... }".into());
let src: Option<&str> = ddoc.filters["f"].as_str();
}

put_design writes exactly the struct it is given, like a PUT. In 0.4 it copied members the struct could not hold (views.lib, Mango views, custom fields) from the revision it replaced. If you build a fresh DesignDocument with a rev, start from the stored one instead, or those members are removed:

#![allow(unused)]
fn main() {
// 0.4: members not in the struct were copied from the stored revision
db.put_design(DesignDocument { rev: Some(rev), views: my_views, ..my_other_fields }).await?;
// 0.5: start from the stored document
let mut ddoc = db.get_design("app").await?;
ddoc.views = my_views;
db.put_design(ddoc).await?;
}

Writes are validated like CouchDB

Documents written with put, post, update or bulk_docs (normal mode) are rejected with BadRequest when they:

  • are not JSON objects;
  • contain _-prefixed members CouchDB does not know (_foo), or reserved _ ids;
  • have a wrongly typed _id, _rev, _deleted or _attachments, or an empty _id;
  • are nested deeper than MAX_NESTING_DEPTH (1000) levels.

Read-only metadata that get adds (_conflicts, _revs_info, _revisions) is dropped on write, so writing back a document you read is safe.

In bulk_docs results, the local adapters now give an invalid document’s bare reason ("Bad special document member: _foo"), like the HTTP adapter and CouchDB; 0.4 prefixed it with bad request: .

Generated ids

Ids generated by post, by bulk_docs for documents without _id and by the server’s POST /{db} are 32 lower-case hex digits, as in CouchDB (still random UUID v4 values):

#![allow(unused)]
fn main() {
// 0.4: "0b9e4c52-5d7a-4f7e-9c1b-3a2f6e8d9c10"
// 0.5: "0b9e4c525d7a4f7e9c1b3a2f6e8d9c10"
let id = db.post(json!({"n": 1})).await?.id;
}

Over HTTP a document without an id is sent without _id, so CouchDB generates it.

Nesting

Documents nested up to MAX_NESTING_DEPTH (1000) levels are accepted on every path, including the server and the CLI (0.4 failed above serde_json’s 128 levels). HttpAdapter::bulk_docs rejects a deeper document with a per-document BadRequest instead of sending it.

#![allow(unused)]
fn main() {
// 0.5: Err(BadRequest), as in CouchDB: `_meta` is not a known special member
db.put("doc1", json!({"_meta": {"source": "import"}})).await?;

// 0.5: keep application metadata in a normal member
db.put("doc1", json!({"meta": {"source": "import"}})).await?;
}

_local/ ids

A _local/… id now means a local document on every adapter, as in CouchDB: revisions are 0-1, 0-2, …, there is no conflict check, and the document is not listed by all_docs, not in the changes feed and not replicated. In 0.4 the memory and redb adapters stored it as a normal, replicated document.

#![allow(unused)]
fn main() {
db.put("_local/settings", json!({"theme": "dark"})).await?; // rev "0-1"
db.get("_local/settings").await?;                           // still readable
// but it no longer appears in all_docs() or changes(), and does not replicate
}

If you relied on _local/ documents replicating, rename them. The redb upgrade moves the _local/… documents 0.4 stored as ordinary documents to the local store (see redb files).

Attachments

  • Deleting a document drops its attachments.
  • An explicit _attachments map is exact: entries you omit are removed (stubs keep an attachment). An empty map still keeps the parent’s attachments.
  • Stubs are matched by name; the stored metadata wins over the stub’s. A stub for a name that has no stored attachment fails with missing_stub.

AttachmentMeta gains revpos, encoding and encoded_length:

#![allow(unused)]
fn main() {
// 0.4
let meta = AttachmentMeta { content_type: "image/png".into(), digest: String::new(), length: bytes.len() as u64, stub: false, data: Some(bytes) };
// 0.5: inline attachment, digest and length computed
let meta = AttachmentMeta::new("image/png", bytes);
// or add ..Default::default() to existing struct literals
}

revpos is the generation of the revision that uploaded the data, set by the write as in CouchDB, and it is now included in stubs (Document::to_json, get, bulk_get) and replicated, so attachments pushed to CouchDB no longer arrive with revpos: 0. Attachments stored by 0.4 in redb read back with revpos 0. AttachmentMeta::to_json(data) builds the CouchDB _attachments member.


Revision history

  • Stemming (revs_limit, default 1000) now prunes every branch, and the bodies of stemmed revisions are gone. Configure it with MemoryAdapter::with_rev_limit or RedbAdapter::with_rev_limit (0 = unlimited).
  • Re-creating a deleted document continues its history (3-… after 2-… was deleted) instead of starting at 1-….
  • Revision ids are normalized to lower-case hex (the redb upgrade rewrites upper-case ids 0.4 stored).
  • rouchdb_core::merge::latest_rev was removed. Use latest_leaf, which the adapters already used:
#![allow(unused)]
fn main() {
// 0.4
let rev = rouchdb_core::merge::latest_rev(&tree, pos, &hash);
// 0.5
let rev = rouchdb_core::merge::latest_leaf(&tree, pos, &hash);
}

Mango queries

Mango now follows CouchDB, which differs from PouchDB and from 0.4 in a few places. This applies to find, changes with a selector, and selector-filtered replication.

Missing fields. A document without the field only matches {"$exists": false}. $ne, $nin, $not and $nor no longer match it:

#![allow(unused)]
fn main() {
// docs: {"_id": "a", "status": "archived"}, {"_id": "b"}  (no status)
let opts = FindOptions { selector: json!({"status": {"$ne": "archived"}}), ..Default::default() };
// 0.4: ["b"]   0.5: []

// 0.5: say it explicitly to keep the 0.4 result
let opts = FindOptions {
    selector: json!({"$or": [
        {"status": {"$ne": "archived"}},
        {"status": {"$exists": false}}
    ]}),
    ..Default::default()
};
}

Projection. fields returns only the fields you ask for; _id is no longer added. Nested paths keep their structure:

#![allow(unused)]
fn main() {
let opts = FindOptions {
    selector: json!({"type": "user"}),
    fields: Some(vec!["_id".into(), "address.city".into()]), // add "_id" if you need it
    ..Default::default()
};
// 0.5 result: {"_id": "u1", "address": {"city": "Lima"}}
}

Errors instead of empty results. Invalid selectors, unknown operators, bad operator arguments ({"$not": 5}, {"$elemMatch": 1}), invalid regexes and invalid sort fields are Err(BadRequest) with CouchDB’s messages. A null selector is an error too; use json!({}) to match everything. A replication with an invalid ReplicationFilter::Selector fails with BadRequest before it reads or writes anything (replicate_live sends an Error event and stops, even with retry).

Other changes:

  • Nested objects are paths: {"a": {"b": 1}} means {"a.b": 1}.
  • $in / $nin compare against array elements; $all uses exact equality.
  • find never returns design documents.
  • A selector that is only an empty combinator ({"$and": []}) returns nothing.
  • {} below the top level is an equality test with {}: {"$and": [{}]} matches nothing, {"o": {"$and": [{}]}} matches o == {}. Operators without a field inside $and/$or/$nor ({"$and": [{"$gt": 1}]}) apply to the whole document instead of being rejected.
  • Descending sort ties come in reverse index order.
  • A sorted find skips documents that lack a sort field.
  • $regex accepts PCRE-style lookaround and backreferences.
  • SortField only deserializes from "field" or {"field": "asc" | "desc"}.

On Database::http, find, create_index, get_indexes, delete_index and explain now run on the CouchDB server, so results (including string order) are CouchDB’s own.


Views

ViewQueryOptions::new() now enables reduce, like CouchDB. If you pass a reduce function but want map rows, say so:

#![allow(unused)]
fn main() {
// 0.4: map rows, because new() left reduce off
let opts = ViewQueryOptions::new();
// 0.5: new() reduces; ask for map rows explicitly
let opts = ViewQueryOptions { reduce: false, ..ViewQueryOptions::new() };
}

Also:

  • _sum and _stats keep integers (90, not 90.0); arrays are summed element-wise and objects per field.

  • _sum over values it cannot add (strings, mixed types) no longer fails the query: the row’s value is CouchDB’s error object, so check for it if your map functions can emit such values. _stats over such values is still Err(BadRequest).

    #![allow(unused)]
    fn main() {
    // 0.5: emit(doc.type, doc.name) with _sum
    // rows[0].value == {"error": "builtin_reduce_error", "reason": "The _sum function requires ...", "caused_by": "alice"}
    if row.value.get("error").is_some() { /* not summable */ }
    }
  • These queries are now Err(BadRequest), as in CouchDB: a key range that cannot match (start_key after end_key without descending, or the reverse with it), several keys together with key/start_key/end_key, and group/group_level on a map-only view or with reduce: false.

    #![allow(unused)]
    fn main() {
    // 0.4: Ok with no rows.  0.5: Err(BadRequest("No rows can match your key range, ..."))
    let opts = ViewQueryOptions { start_key: Some(json!("z")), end_key: Some(json!("a")), ..ViewQueryOptions::new() };
    // 0.5: say descending if you meant it
    let opts = ViewQueryOptions { start_key: Some(json!("z")), end_key: Some(json!("a")), descending: true, ..ViewQueryOptions::new() };
    }
  • start_key/end_key are honored next to key (an explicit bound replaces the one key implies), and group + keys keeps the keys’ order when descending.

  • Custom reduce functions receive [key, doc_id] pairs as keys, and rereduce is always false.

  • Several keys with a reduce require group: true (and no group_level); include_docs with a reduce is an error.

  • total_rows counts the whole view and offset is the position of the first returned row.

  • Design documents are not passed to map functions.

Collation

-0.0 equals 0, large integers and floats compare exactly, and strings compare by UTF-16 code units (PouchDB’s order; only characters above U+FFFF move). to_indexable_string has a new format: rebuild anything you stored with it.


Partitions

Partition::all_docs lists every id of the partition (0.4 missed ids sorting after p:\u{10FFFF}) and applies inclusive_end only to the end key you pass, never to the partition’s own bound. Partition::find validates your selector as a whole selector first, and {} means every document of the partition.


Plugins

before_write now also runs for put_attachment and remove_attachment, with the revision the attachment write creates. As for replicated documents it can only accept or reject: a rejection is returned and nothing is written, modifications are ignored, and dropping the document is Forbidden. If a plugin rejects documents by shape, check that it accepts attachment-only revisions of documents it accepted before.


Replication and the HTTP adapter

The remote database is created on first use, as PouchDB does. To keep failing with NotFound when it is missing:

#![allow(unused)]
fn main() {
use std::sync::Arc;
use rouchdb::{Database, HttpAdapter, HttpAdapterOptions};

// 0.4
let db = Database::http("http://admin:password@localhost:5984/mydb");

// 0.5: same call creates `mydb` if needed; opt out with skip_setup
let adapter = HttpAdapter::with_options(
    "http://admin:password@localhost:5984/mydb",
    HttpAdapterOptions { skip_setup: true, ..Default::default() },
);
let db = Database::from_adapter(Arc::new(adapter));
}

HttpAdapterOptions also sets the timeouts (30 s to connect, 60 s of inactivity by default).

One rescan after upgrading. Every replication gets a new replication id, so its first run re-reads the changes feed from the start; revs_diff means no document is transferred again, and the old _local checkpoints are left unused:

  • an HTTP peer is identified by the server uuid plus the database name;
  • a memory or redb database is identified by a uuid of its own, not by its name (two local databases with the same name used to share checkpoints, so sync between them skipped documents). destroy() gives the database a new one. redb stores the uuid in the file and combines it with the file’s canonical path: a file reopened at the same path (directly or through a symlink) keeps its checkpoints whatever name it is opened with, while a copy of the file (which holds the same uuid) is a different database, so syncing a file with its copy works both ways. Moving or copying a file makes its next replication with each peer rescan the changes feed once (no document is transferred again). Never destroy() a copy to give it a new identity: that deletes its documents;
  • ReplicationFilter::DocIds selections are fingerprinted as a JSON array of ids (ids joined with NUL could collide).

A custom Adapter should override Adapter::id() (the default is the database name) with an identity that tells apart same-named databases and changes on destroy(). When both peers of a replication report the same id, their checkpoints cannot be told apart, so the replication runs without them (scanning the whole changes feed each time) and reports it in ReplicationResult::warnings and a ReplicationEvent::Warning.

Other changes:

  • Documents rejected by a validation function (forbidden / unauthorized) are listed in errors and ok is false, but the replication moves past them instead of stopping forever.
  • ReplicationFilter::Custom closures no longer use checkpoints: every run starts from since.
  • For replicated documents, before_write plugins only accept or reject (their modifications are ignored), like CouchDB’s validate_doc_update.
  • HTTP errors are mapped from the status and CouchDB’s {error, reason}: 400/413/415 → BadRequest, 412 file_exists → DatabaseExists, 401 → Unauthorized.

Live feeds react immediately

Live changes feeds (live_changes, live_changes_events, LiveChangesStream) and replicate_live from a memory or redb database are woken by the adapter on each commit instead of polling. poll_interval now only applies to HTTP sources, to identity checks of HTTP targets and to retry delays, and live replication emits Paused as soon as it is caught up (0.4 needed one more empty poll). Tests that counted polls or slept for poll_interval may need adjusting. rouchdb_changes::ChangeNotification is now an alias of rouchdb_core::adapter::ChangeNotice (same fields).

destroy

After destroy() the handle stays usable and behaves as a new, empty database on every adapter (documents, local documents, attachments, security and Mango indexes are gone). The HTTP adapter re-creates the remote database on its next use, and destroying a database that no longer exists is Ok.

A destroyed memory or redb database gets a new identity (Adapter::id()), so replications from or to it start over instead of resuming from a checkpoint of the old database. It also announces the reset to its subscribers (ChangeNotice::reset(), with reset: true). A running replicate_live notices it right away, whether the destroyed database is the source or the target (even while the source is idle), and starts over from the start of the source’s changes feed, so a destroyed and reused target gets every document again. A target that cannot announce resets (HTTP) has its id compared every poll_interval; the HTTP adapter identifies a remote database by the server’s uuid and the database name, so a remote database that is deleted and re-created is not detected: cancel the replications that use it and copy everything again with since: Some(Seq::zero()) and checkpoint: false (see Re-creating a remote database); retrying with the existing checkpoints does not recover the lost documents.

Live changes streams (live_changes, live_changes_events, LiveChangesStream) do not start over after destroy(): they keep their last_seq and miss the new database’s changes. Cancel them and create new ones from Seq::zero() after destroying the database.


Non-exhaustive enums and result types

So that later releases can add fields and variants without breaking your code, 0.5 marks the types that may grow #[non_exhaustive] (see API Stability):

  • A match on RouchError, ChangesEvent, ReplicationEvent, ReplicationFilter, ReduceFn, StaleOption or SortField needs a catch-all arm, and the struct-like variants ChangesEvent::Complete and ReplicationEvent::Change need ..:
#![allow(unused)]
fn main() {
match event {
    ReplicationEvent::Change { docs_read, .. } => println!("{docs_read} read"),
    ReplicationEvent::Complete(result) => println!("{} written", result.docs_written),
    ReplicationEvent::Error(msg) => eprintln!("{msg}"),
    _ => {} // Active, Paused, and events added later
}
}
  • Result types can no longer be built with struct literals outside their crate (reading their public fields is unchanged): DocResult, DbInfo, AllDocsResponse, AllDocsRow, AllDocsRowValue, ChangesResponse, ChangeEvent, ChangeRev, RevsDiffResponse, RevsDiffResult, BulkGetResponse, BulkGetResult, BulkGetDoc, BulkGetError, PurgeResponse, ChangeNotice, FindResponse, IndexInfo, IndexFields, CreateIndexResponse, ExplainResponse, ExplainIndex, BuiltIndex, EmittedRow, and (library-built only) PutResponse, RevInfo, DocMetadata, ViewResult, ViewRow, ReplicationResult, PersistentViewIndex, Session, UserContext, PlannedWrite, LeafInfo. Use the constructors instead, typically in custom adapters, plugins and tests:
#![allow(unused)]
fn main() {
// 0.4
let r = DocResult { ok: true, id: "a".into(), rev: Some(rev), error: None, reason: None };
// 0.5
let r = DocResult::ok("a", rev);
let e = DocResult::error("b", "forbidden", "not allowed");
let info = DbInfo::new("mydb", doc_count, doc_del_count, update_seq);
}
  • Option and data structs are unchanged (GetOptions, AllDocsOptions, ChangesOptions, FindOptions, ViewQueryOptions, ReplicationOptions, ChangesStreamOptions, HttpAdapterOptions, IndexDefinition, BulkGetItem, Document, AttachmentMeta, DesignDocument, ViewDef, SecurityDocument, …), but write their literals with ..Default::default() (or ..X::new()) so the next field added is not a breaking change for you. Document::new(id, data), BulkGetItem::new(id).with_rev(rev) and Default for Document, IndexDefinition and BulkGetItem are new.

Custom adapters

  • Build the results you return with their constructors: see Non-exhaustive enums and result types and Building Results.
  • Adapter::put_security’s default now returns Err(BadRequest) instead of silently succeeding. Implement it if your adapter stores security documents.
  • New default method Adapter::id() (the database name). Override it if two instances of your adapter can share a name but not their data, so replications do not share checkpoints.
  • destroy must leave the adapter usable as an empty database.
  • New default method Adapter::subscribe() returning None: existing adapters compile unchanged and keep being polled. To wake live feeds and live replication immediately, return a receiver and send a notice after each commit:
#![allow(unused)]
fn main() {
// 0.5, in a custom adapter
fn subscribe(&self) -> Option<tokio::sync::broadcast::Receiver<ChangeNotice>> {
    Some(self.notices.subscribe()) // send ChangeNotice::new(seq, doc_id) after each commit
}
}

Exact numbers (opt-in)

Nothing changes by default. The new arbitrary-precision feature keeps JSON numbers exactly as written (integers beyond i64/u64, long decimals):

rouchdb = { version = "0.5", features = ["arbitrary-precision"] }

It enables serde_json’s arbitrary_precision for the whole build: serde_json::Number then compares textually (1.0 != 1.00), and revision ids of documents containing such numbers differ from those computed without the feature. See also “Differences from CouchDB” in the reference.


Server (rouchdb-server)

ServerConfig has new fields, so add ..Default::default() to struct literals:

#![allow(unused)]
fn main() {
use rouchdb_server::{AdminCredentials, ServerConfig};

// 0.4
let config = ServerConfig { port: 5984, host: "0.0.0.0".into(), db_name: "mydb".into() };

// 0.5
let config = ServerConfig {
    port: 5984,
    host: "0.0.0.0".into(),
    db_name: "mydb".into(),
    admin: Some(AdminCredentials::parse("admin:secret").expect("user:password")),
    cors_origins: vec!["http://localhost:3000".into()],
    ..Default::default() // max_request_size: 64 MiB, session_timeout: 10 min
};
}

CORS is off by default. A browser app served from another origin must be allowed explicitly; in 0.4 every origin was mirrored with credentials:

rouchdb-server mydb.redb --cors-origin http://localhost:3000   # or ROUCHDB_CORS_ORIGINS

Authentication is opt-in and recommended whenever other software can reach the port (the server warns when it listens on a non-loopback address without it):

ROUCHDB_ADMIN=admin:secret rouchdb-server mydb.redb --host 0.0.0.0

Everything except /, /_session, /_uuids and /_utils then needs Basic auth or a _session cookie. Wrong credentials are a 401 on every route, and session cookies expire after 10 minutes without use (--session-timeout).

HTTP clients of the server may notice:

Request0.40.5 (CouchDB)
DELETE /{db}database still served404 on every route until PUT /{db}
wrong-shape JSON body422400
DELETE /{db}/{docid} without a rev400409
design-document conflict201 "ok": false409
_bulk_docs with an invalid document201 (written, or a per-document error)400, nothing written
_bulk_docs with new_edits: falseone result per documentfailures only ([])
_find without limitevery matchat most 25, with a bookmark
POST /{db}/_index response idindex name_design/<ddoc> (also listed in _all_docs / _changes)
_all_docs?key=baccepted400; JSON-encode keys (?key="b")
_changes with an unknown feed or filterignored400 / 404
POST /{db}/_compact without Content-Type: application/json202415
/_uuids, POST /{db} generated idshyphenated UUIDs32 hex digits
POST /{db}/_all_docs with keysunknown keys droppedone row per key: {"key": …, "error": "not_found"}, "offset": null, "doc": null for deleted docs
GET /{db}/_design/{ddoc}typed members onlythe stored document, with the usual document options and headers
PUT/DELETE /{db}/{doc}/{att} with a malformed ?rev404/409 when the document is missing400 Invalid rev format
anonymous POST/PUT/DELETE / or /_active_tasksaccepted405 Only GET,HEAD allowed
several AuthSession cookiesany valid oneonly the first counts
malformed AuthSession cookieignored400 on every route, even with valid Basic credentials
malformed JSON bodyaxum’s message400 bad_request “invalid UTF-8 JSON”
request body over 2 MB413accepted up to 64 MiB (--max-request-size)
error bodiessometimes plain text; reasons like the doc idalways JSON, CouchDB names and reasons (missing, deleted, Document update conflict.)

CLI (rouchdb)

  • rouchdb migrate <path> upgrades a file written by 0.4 (see redb files); every other command refuses such a file and says so.
  • Read-only commands fail with exit 1 on a missing file instead of creating an empty one.
  • replicate exits 1 when ok is false; its output gains errors and last_seq.
  • dump inlines attachments (base64) and import restores them. dump still exports only winning revisions; use replicate to copy conflicts and history.
  • ROUCHDB_USER / ROUCHDB_PASSWORD supply credentials for http(s) URLs that carry none, and errors never print user:password@.

CRUD Operations

RouchDB provides a familiar document database API for creating, reading, updating, and deleting JSON documents. Every document has an _id (string identifier) and a _rev (revision string) that tracks its edit history.

Creating a Database

#![allow(unused)]
fn main() {
use rouchdb::Database;

// In-memory (data lost when dropped; great for tests)
let db = Database::memory("mydb");

// Persistent storage backed by redb
let db = Database::open("path/to/mydb.redb", "mydb")?;

// Remote CouchDB instance
let db = Database::http("http://localhost:5984/mydb");
}

Put (Create)

Use put() to create a new document with a given ID. The data is any serde_json::Value.

#![allow(unused)]
fn main() {
use serde_json::json;

let result = db.put("user:alice", json!({
    "name": "Alice",
    "email": "alice@example.com",
    "age": 30
})).await?;

assert!(result.ok);
assert_eq!(result.id, "user:alice");

// The new revision string (e.g. "1-a3f2b...")
let rev = result.rev.unwrap();
}

The returned DocResult contains:

  • ok – whether the write succeeded.
  • id – the document ID.
  • rev – the new revision string (if successful).
  • error / reason – error details (if failed).

Get (Read)

Retrieve a document by its ID.

#![allow(unused)]
fn main() {
let doc = db.get("user:alice").await?;

println!("Name: {}", doc.data["name"]);
println!("Rev:  {}", doc.rev.as_ref().unwrap());
}

The Document struct contains:

  • id – the document ID.
  • rev – the current Revision (pos + hash).
  • deleted – whether this document is deleted.
  • data – the JSON body as serde_json::Value.
  • attachments – a map of AttachmentMeta entries.

Get with Options

Use get_with_opts() when you need a specific revision or conflict information.

#![allow(unused)]
fn main() {
use rouchdb::GetOptions;

let doc = db.get_with_opts("user:alice", GetOptions {
    rev: Some("1-a3f2b...".to_string()),
    conflicts: true,
    ..Default::default()
}).await?;
}

GetOptions fields:

  • rev – fetch a specific revision instead of the winner.
  • conflicts – include conflicting revision IDs.
  • open_revs – return all open (leaf) revisions.
  • revs – include the full revision history chain.

Update

To update a document you must supply the current revision. This prevents lost updates when multiple writers are active.

#![allow(unused)]
fn main() {
let r1 = db.put("user:alice", json!({"name": "Alice", "age": 30})).await?;
let rev = r1.rev.unwrap();

let r2 = db.update("user:alice", &rev, json!({
    "name": "Alice",
    "age": 31,
    "email": "alice@newdomain.com"
})).await?;

assert!(r2.ok);
// r2.rev is now "2-..."
}

If you pass a stale revision, RouchDB returns RouchError::Conflict.

Remove (Delete)

Deletion in CouchDB-compatible databases is a soft delete: the document is marked with _deleted: true and a new revision is created.

#![allow(unused)]
fn main() {
let doc = db.get("user:alice").await?;
let rev = doc.rev.unwrap().to_string();

let result = db.remove("user:alice", &rev).await?;
assert!(result.ok);
}

After deletion, db.get("user:alice") returns RouchError::NotFound. The document still participates in replication so that deletions propagate to other replicas.

Bulk Docs

Write multiple documents in a single atomic operation. This is more efficient than individual puts, and it is the only way to write documents with explicit revision control.

#![allow(unused)]
fn main() {
use rouchdb::{Document, BulkDocsOptions};

let docs = vec![
    Document::new("user:bob", json!({"name": "Bob"})),
    Document::new("user:carol", json!({"name": "Carol"})),
];

let results = db.bulk_docs(docs, BulkDocsOptions::new()).await?;

for r in &results {
    println!("{}: ok={}", r.id, r.ok);
}
}

BulkDocsOptions has one key field:

  • new_edits (default true) – when true, the adapter generates new revision IDs and checks for conflicts. Set to false via BulkDocsOptions::replication() during replication, where revisions are accepted as-is.

All Docs

Query all documents in the database, optionally filtered by key range.

#![allow(unused)]
fn main() {
use rouchdb::AllDocsOptions;

let response = db.all_docs(AllDocsOptions {
    include_docs: true,
    start_key: Some("user:a".into()),
    end_key: Some("user:d".into()),
    limit: Some(10),
    ..AllDocsOptions::new()
}).await?;

for row in &response.rows {
    // `key` is the document ID; `rev()` is its winning revision.
    println!("{} rev={}", row.key, row.rev().unwrap_or_default());
    if let Some(ref doc) = row.doc {
        println!("  data: {}", doc);
    }
}
}

AllDocsOptions fields:

  • start_key / end_key – define the key range (string-sorted).
  • key – fetch a single key.
  • keys – fetch a specific set of keys.
  • include_docs – embed full document bodies in results.
  • conflicts – include conflict information for each document.
  • update_seq – include the database update sequence in the response.
  • descending – reverse the sort order.
  • skip – number of rows to skip.
  • limit – maximum number of rows.
  • inclusive_end – whether to include end_key (default true).

The response is an AllDocsResponse with total_rows, offset, and a rows vector of AllDocsRow.

With keys, there is one row per requested key, in order, like CouchDB: a key that names no document gets an error row (row.is_error(), row.error == Some("not_found"), no id or value), and a deleted document a row with row.is_deleted() and no doc:

#![allow(unused)]
fn main() {
let response = db.all_docs(AllDocsOptions {
    keys: Some(vec!["user:1".into(), "user:404".into()]),
    include_docs: true,
    ..AllDocsOptions::new()
}).await?;
for (key, row) in ["user:1", "user:404"].iter().zip(&response.rows) {
    assert_eq!(&row.key, key);
    match (&row.doc, row.is_error()) {
        (Some(doc), _) => println!("{key}: {doc}"),
        (None, true) => println!("{key}: not found"),
        (None, false) => println!("{key}: deleted"),
    }
}
}

Error Handling

RouchDB uses the RouchError enum for all errors. The most common ones in CRUD operations:

#![allow(unused)]
fn main() {
use rouchdb::RouchError;

match db.get("nonexistent").await {
    Ok(doc) => println!("Found: {}", doc.id),
    Err(RouchError::NotFound(msg)) => {
        println!("Document not found: {}", msg);
    }
    Err(e) => println!("Other error: {}", e),
}

// Conflict when updating with a stale revision
match db.update("user:alice", "1-stale", json!({})).await {
    Err(RouchError::Conflict) => {
        println!("Conflict! Re-read and retry.");
    }
    _ => {}
}
}

Key error variants:

  • RouchError::NotFound(String) – document or resource does not exist.
  • RouchError::Conflict – document update conflict (stale revision).
  • RouchError::BadRequest(String) – malformed input.
  • RouchError::InvalidRev(String) – unparseable revision string.
  • RouchError::MissingId – document ID was not provided.
  • RouchError::Unauthorized / RouchError::Forbidden(String) – access denied.
  • RouchError::DatabaseError(String) – storage-level errors.

Idiomatic Update-Retry Pattern

Because conflicts are expected in a multi-replica system, a common pattern is to re-read and retry:

#![allow(unused)]
fn main() {
loop {
    let doc = db.get("counter").await?;
    let rev = doc.rev.unwrap().to_string();
    let mut count = doc.data["count"].as_u64().unwrap_or(0);
    count += 1;

    match db.update("counter", &rev, json!({"count": count})).await {
        Ok(_) => break,
        Err(RouchError::Conflict) => continue, // retry
        Err(e) => return Err(e),
    }
}
}

Querying

RouchDB provides two query mechanisms, both compatible with CouchDB semantics:

  1. Mango queries – declarative, JSON-based selectors (like MongoDB’s query language).
  2. Map/reduce views – programmatic queries using Rust closures with optional aggregation.

Mango Queries

Mango is the simplest way to find documents. You provide a selector (a JSON object describing the match criteria) and the engine scans all documents, returning those that match.

Basic Find

#![allow(unused)]
fn main() {
use rouchdb::{Database, FindOptions};
use serde_json::json;

let db = Database::memory("mydb");
db.put("alice", json!({"name": "Alice", "age": 30, "city": "NYC"})).await?;
db.put("bob", json!({"name": "Bob", "age": 25, "city": "LA"})).await?;
db.put("carol", json!({"name": "Carol", "age": 35, "city": "NYC"})).await?;

let result = db.find(FindOptions {
    selector: json!({"age": {"$gte": 28}}),
    ..Default::default()
}).await?;

// Returns Alice (30) and Carol (35)
for doc in &result.docs {
    println!("{}", doc["name"]);
}
}

The FindResponse contains a single field: docs, a Vec<serde_json::Value> of matching documents with _id and _rev included.

FindOptions

#![allow(unused)]
fn main() {
use rouchdb::{FindOptions, SortField};
use std::collections::HashMap;

let opts = FindOptions {
    selector: json!({"city": "NYC"}),
    fields: Some(vec!["name".into(), "age".into()]),
    sort: Some(vec![
        SortField::Simple("age".into()),
        // Or with explicit direction:
        SortField::WithDirection(HashMap::from([
            ("name".into(), "desc".into())
        ])),
    ]),
    limit: Some(10),
    skip: Some(0),
};
}
  • selector – the query (see operators below).
  • fields – field projection; only these fields are returned (list _id explicitly if you need it). Nested paths such as address.city keep their structure.
  • sort – sort by one or more fields, ascending ("asc") or descending ("desc"). As in CouchDB (which sorts through an index on those fields), documents that lack a sort field are left out of sorted results; unlike CouchDB, no index is required.
  • limit – maximum number of results.
  • skip – number of results to skip (for pagination).

Comparison Operators

OperatorDescriptionExample
$eqEqual (also the implicit default){"age": {"$eq": 30}} or {"age": 30}
$neNot equal{"status": {"$ne": "archived"}}
$gtGreater than{"age": {"$gt": 20}}
$gteGreater than or equal{"age": {"$gte": 21}}
$ltLess than{"price": {"$lt": 100}}
$lteLess than or equal{"price": {"$lte": 99.99}}
$inValue is in array{"color": {"$in": ["red", "blue"]}}
$ninValue is not in array{"color": {"$nin": ["green"]}}

You can combine multiple operators on the same field to express ranges:

#![allow(unused)]
fn main() {
// Documents where 20 < age < 40
let selector = json!({"age": {"$gt": 20, "$lt": 40}});
}

Existence and Type Operators

OperatorDescriptionExample
$existsField exists (or not){"email": {"$exists": true}}
$typeField is a specific JSON type{"age": {"$type": "number"}}

Supported type names: "null", "boolean", "number", "string", "array", "object".

String Operators

OperatorDescriptionExample
$regexMatches a regular expression{"name": {"$regex": "^Ali"}}

Patterns use PCRE-like syntax (via fancy-regex), including lookaround and backreferences, as CouchDB does.

Array Operators

OperatorDescriptionExample
$allArray contains all listed elements (exact equality: 50.0 is not 50){"tags": {"$all": ["rust", "db"]}}
$sizeArray has exactly N elements{"tags": {"$size": 3}}
$elemMatchAt least one element matches sub-selectorSee below

$elemMatch example with an array of objects:

#![allow(unused)]
fn main() {
let selector = json!({
    "scores": {
        "$elemMatch": {
            "subject": "math",
            "grade": {"$gt": 80}
        }
    }
});
}

Arithmetic Operators

OperatorDescriptionExample
$modModulo: [divisor, remainder]{"n": {"$mod": [3, 1]}}

Logical Operators

OperatorDescriptionExample
$andAll sub-selectors must match{"$and": [{"age": {"$gte": 18}}, {"active": true}]}
$orAt least one sub-selector must match{"$or": [{"city": "NYC"}, {"city": "LA"}]}
$notNegate a selector (the argument must be an object){"$not": {"status": "archived"}}
$norNone of the sub-selectors match{"$nor": [{"status": "banned"}, {"age": {"$lt": 13}}]}

An empty $and, $or or $nor is always true, so {"age": 22, "$or": []} is {"age": 22}; but, as in CouchDB, a query whose selector is nothing but an empty combinator ({"$and": []}, {"$nor": []}, {"f": {"$or": []}}) returns no documents.

Note: multiple fields in the same selector object are an implicit $and:

#![allow(unused)]
fn main() {
// Equivalent to $and
let selector = json!({"name": "Alice", "age": {"$gte": 25}});
}

$not can also be used at the field level:

#![allow(unused)]
fn main() {
// Field-level negation: age is NOT greater than 30
let selector = json!({"age": {"$not": {"$gt": 30}}});
}

Nested Fields

Use dot notation to query nested objects:

#![allow(unused)]
fn main() {
let selector = json!({"address.city": "NYC"});
}

Deeply Nested Selectors

Compiling and matching a selector recurse once per nested object, and find does not limit how deep a selector is. On a 2 MiB thread (the default for Tokio workers), a debug build overflows the stack, aborting the process, at around 700 levels of nested objects; a release build handles a few thousand. Selectors parsed with serde_json::from_str are safe, because serde_json stops at 127 levels. If your application accepts selectors that may be deeper (from rouchdb_core::json::from_slice, another parser, or built in code), check their depth first with rouchdb_core::json::value_depth, or run queries on threads with a larger stack (tokio::runtime::Builder::thread_stack_size). rouchdb-server runs on 16 MiB threads for this reason.

Map/Reduce Views

Map/reduce gives you full programmatic control. You provide a map function (a Rust closure) that receives each document and emits key-value pairs. An optional reduce function aggregates the emitted values.

Map-Only Query

#![allow(unused)]
fn main() {
use rouchdb::{query_view, ViewQueryOptions};

let result = query_view(
    db.adapter(),
    &|doc| {
        // Emit the city as the key, 1 as the value
        let city = doc.get("city").cloned().unwrap_or(json!(null));
        vec![(city, json!(1))]
    },
    None, // no reduce
    ViewQueryOptions::new(),
).await?;

for row in &result.rows {
    println!("key={}, id={}", row.key, row.id.as_deref().unwrap_or(""));
}
}

The map closure receives a &serde_json::Value (the full document including _id and _rev) and returns a Vec<(serde_json::Value, serde_json::Value)> of emitted key-value pairs. You can emit zero, one, or multiple pairs per document.

Results are sorted by key using CouchDB’s collation order.

Key Filtering

#![allow(unused)]
fn main() {
let result = query_view(
    db.adapter(),
    &|doc| {
        let name = doc.get("name").cloned().unwrap_or(json!(null));
        vec![(name, json!(1))]
    },
    None,
    ViewQueryOptions {
        key: Some(json!("Bob")),       // exact key match
        ..ViewQueryOptions::new()
    },
).await?;
}

ViewQueryOptions fields:

  • key – return only rows with this exact key. With start_key or end_key it is the other bound of the range (as when startkey follows key in a CouchDB query string).
  • start_key / end_key – define a key range (inclusive by default). A range no row can be in (a start after the end, or before it when descending) is a BadRequest, as in CouchDB.
  • inclusive_end – whether to include the end key.
  • descending – reverse the sort order.
  • skip / limit – pagination.
  • include_docs – embed full documents (a value {"_id": ...} embeds that document instead).
  • reduce – whether to run the reduce function (on by default with new(), as in CouchDB).
  • group – group reduced results by key. Grouping (group, or a group_level above 0) without a reduce is a BadRequest.
  • group_level – for array keys, group by the first N elements.
  • keys – return the rows of these keys, in this order (reversed when descending, except for grouped reduce rows, which keep it). With a reduce, more than one key requires group: true without group_level (CouchDB’s rule); a single key behaves like key. Several keys cannot be combined with key, start_key or end_key.

Built-In Reduce Functions

RouchDB provides three built-in reduce functions matching CouchDB:

#![allow(unused)]
fn main() {
use rouchdb::ReduceFn;

// Sum all emitted numeric values
let sum_result = query_view(
    db.adapter(),
    &|doc| {
        let age = doc.get("age").cloned().unwrap_or(json!(0));
        vec![(json!(null), age)]
    },
    Some(&ReduceFn::Sum),
    ViewQueryOptions {
        reduce: true,
        ..ViewQueryOptions::new()
    },
).await?;
// sum_result.rows[0].value == 90 (30 + 25 + 35; integers stay integers)
}
  • ReduceFn::Sum – sums numbers (arrays element-wise, objects field by field). Like CouchDB, values it cannot add make the reduced value an error object ({"error": "builtin_reduce_error", "reason": ..., "caused_by": <value>}) instead of failing the query.
  • ReduceFn::Count – counts the number of rows.
  • ReduceFn::Stats – computes {"sum", "count", "min", "max", "sumsqr"}.

Group By

Group reduce results by key:

#![allow(unused)]
fn main() {
let result = query_view(
    db.adapter(),
    &|doc| {
        let city = doc.get("city").cloned().unwrap_or(json!(null));
        vec![(city, json!(1))]
    },
    Some(&ReduceFn::Count),
    ViewQueryOptions {
        reduce: true,
        group: true,
        ..ViewQueryOptions::new()
    },
).await?;

// Returns: [{"key": "LA", "value": 1}, {"key": "NYC", "value": 2}]
}

For compound (array) keys, use group_level to control grouping granularity:

#![allow(unused)]
fn main() {
let result = query_view(
    db.adapter(),
    &|doc| {
        let year = doc.get("year").cloned().unwrap_or(json!(null));
        let month = doc.get("month").cloned().unwrap_or(json!(null));
        vec![(json!([year, month]), json!(1))]
    },
    Some(&ReduceFn::Count),
    ViewQueryOptions {
        reduce: true,
        group: true,
        group_level: Some(1), // group by year only
        ..ViewQueryOptions::new()
    },
).await?;
}

Custom Reduce

For aggregations not covered by the built-ins, use ReduceFn::Custom:

#![allow(unused)]
fn main() {
let max_reduce = ReduceFn::Custom(Box::new(|_keys, values, _rereduce| {
    let max = values
        .iter()
        .filter_map(|v| v.as_f64())
        .fold(f64::NEG_INFINITY, f64::max);
    json!(max)
}));

let result = query_view(
    db.adapter(),
    &|doc| {
        let age = doc.get("age").cloned().unwrap_or(json!(0));
        vec![(json!(null), age)]
    },
    Some(&max_reduce),
    ViewQueryOptions {
        reduce: true,
        ..ViewQueryOptions::new()
    },
).await?;
}

The custom function receives (keys, values, rereduce). When rereduce is true, the function is being called to combine previously-reduced results.

Mango vs Map/Reduce: When to Use Each

Use CaseRecommendation
Simple field equality/range filtersMango
Field projection (return only some fields)Mango
Aggregation (sums, counts, statistics)Map/reduce
Complex key transformationsMap/reduce
Grouping by compound keysMap/reduce
Quick ad-hoc queriesMango
Custom sort by computed valueMap/reduce

Both approaches scan all documents (no persistent indexes yet), so performance is similar for small to medium databases. Map/reduce is more powerful but requires writing Rust closures, while Mango selectors can be built from JSON configuration at runtime.

Changes Feed

The changes feed is a core CouchDB concept: it provides an ordered log of every document modification in the database. Each change has a sequence number (Seq) that increases monotonically. You can ask for all changes since a given sequence, making the changes feed the foundation for replication, live queries, and reactive UIs.

One-Shot Changes

The simplest usage fetches all changes that have occurred since a given sequence, then returns.

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesOptions, Seq};
use serde_json::json;

let db = Database::memory("mydb");
db.put("a", json!({"v": 1})).await?;
db.put("b", json!({"v": 2})).await?;
db.put("c", json!({"v": 3})).await?;

let response = db.changes(ChangesOptions {
    since: Seq::default(), // start from the beginning
    include_docs: true,
    ..Default::default()
}).await?;

for event in &response.results {
    println!("seq={} id={} deleted={}", event.seq, event.id, event.deleted);
    if let Some(ref doc) = event.doc {
        println!("  doc: {}", doc);
    }
    for change_rev in &event.changes {
        println!("  rev: {}", change_rev.rev);
    }
}

println!("Last seq: {}", response.last_seq);
}

ChangesOptions

#![allow(unused)]
fn main() {
use rouchdb::{ChangesOptions, Seq};

let opts = ChangesOptions {
    since: Seq::Num(5),                          // start after sequence 5
    limit: Some(100),                             // return at most 100 changes
    descending: false,                            // chronological order
    include_docs: true,                           // embed full document bodies
    live: false,                                  // one-shot mode
    doc_ids: Some(vec!["user:alice".into()]),     // only these document IDs
    selector: None,                               // Mango selector filter
};
}
FieldTypeDescription
sinceSeqReturn changes after this sequence. Seq::Num(0) or Seq::default() means from the beginning.
limitOption<u64>Maximum number of change events to return.
descendingboolReverse the order (newest first).
include_docsboolInclude the full document body in each event.
liveboolUsed internally by the adapter; for live streaming, use LiveChangesStream.
doc_idsOption<Vec<String>>Filter changes to only these document IDs.
selectorOption<serde_json::Value>Mango selector — only changes matching this selector are returned.

ChangesResponse and ChangeEvent

The response has two fields:

  • results – a Vec<ChangeEvent>, each containing:
    • seq – the sequence identifier for this change.
    • id – the document ID.
    • changes – a list of ChangeRev structs (each has a rev string).
    • deleted – true if this change was a deletion.
    • doc – the document body (when include_docs is true).
  • last_seq – the sequence of the last event in the batch. Pass this as since in subsequent calls.

Incremental Polling

Fetch changes in pages by saving last_seq:

#![allow(unused)]
fn main() {
let mut since = Seq::default();

loop {
    let response = db.changes(ChangesOptions {
        since: since.clone(),
        limit: Some(50),
        ..Default::default()
    }).await?;

    if response.results.is_empty() {
        break; // caught up
    }

    for event in &response.results {
        process_change(event);
    }

    since = response.last_seq;
}
}

The Seq Type

Sequence identifiers differ between adapters:

  • Local adapters (memory, redb) use numeric sequences: Seq::Num(1), Seq::Num(2), etc.
  • CouchDB 3.x uses opaque string sequences: Seq::Str("13-g1AAAA...").

The Seq enum handles both transparently:

#![allow(unused)]
fn main() {
use rouchdb::Seq;

let local_seq = Seq::Num(42);
let couch_seq = Seq::Str("13-g1AAAABXeJzLYWBg...".into());

// Extract the numeric value (parses the prefix for CouchDB strings)
let n: u64 = couch_seq.as_num(); // 13

// Format for HTTP query parameters
let qs: String = couch_seq.to_query_string();

// The default is Seq::Num(0) (the beginning)
let start = Seq::default();
}

Always pass last_seq back as-is to since rather than trying to parse or increment it. This ensures correct behavior with CouchDB’s opaque sequences.

Live Changes Stream

For real-time reactivity, LiveChangesStream yields change events continuously, blocking when there are no new changes.

#![allow(unused)]
fn main() {
use std::sync::Arc;
use rouchdb_changes::{LiveChangesStream, ChangesStreamOptions};
use rouchdb::Seq;

let db = Arc::new(rouchdb_adapter_memory::MemoryAdapter::new("mydb"));

// `None`: the stream subscribes to the adapter's own change notifications
// (`Adapter::subscribe`), so a write wakes it up immediately.
let mut stream = LiveChangesStream::new(
    db.clone(),
    None,
    ChangesStreamOptions {
        since: Seq::default(),
        live: true,
        include_docs: true,
        limit: None,          // no limit; run forever
        ..Default::default()
    },
);

// In a loop, await the next change
loop {
    match stream.next_change().await {
        Some(event) => {
            println!("Change: {} seq={}", event.id, event.seq);
            if event.deleted {
                println!("  (deleted)");
            }
        }
        None => {
            println!("Stream ended");
            break;
        }
    }
}
}

ChangesStreamOptions

ChangesStreamOptions extends the one-shot options for live mode:

#![allow(unused)]
fn main() {
use std::time::Duration;

let opts = ChangesStreamOptions {
    since: Seq::default(),
    live: true,                                // keep listening
    include_docs: false,
    doc_ids: None,
    limit: Some(1000),                         // stop after 1000 total events
    poll_interval: Duration::from_millis(500), // fallback polling interval
};
}

The memory and redb adapters announce every committed change (Adapter::subscribe), and a live stream over them (live_changes, live_changes_events, Database::live_changes, or LiveChangesStream::new(adapter, None, ..)) waits on those notifications: a write reaches the stream at once, with no polling while idle. poll_interval is only used for adapters that cannot announce changes (the HTTP adapter: subscribe() returns None), when a ChangeReceiver you pass is not given, and as the base delay of retries after a failed fetch.

How It Works

The LiveChangesStream operates through a simple state machine:

  1. FetchingInitial – on first call, fetches all changes since the given sequence.
  2. Yielding – returns buffered change events one at a time.
  3. Waiting – when the buffer is exhausted, waits for a change notification (the adapter’s, or the ChangeReceiver you passed) or, without one, polls on a timer. The stream subscribes before its first read, so no change committed in between is missed; a notification is only a wake-up, the changes themselves are always read from the feed (a subscriber that lags behind re-reads it).
  4. Done – when the limit is reached or the channel closes.

After destroying the database

A live changes stream keeps its position (last_seq) for its whole life. When the database it follows is destroyed and reused, the new database numbers its changes from 1 again, so a stream still positioned after the old database’s changes misses them: it does not start over on its own (live replication does, see Replication). Cancel the stream (drop it or call ChangesHandle::cancel) and create a new one from Seq::zero() after destroy(); never pass a last_seq from before the destroy() to a new stream.

ChangeSender / ChangeReceiver

The ChangeSender and ChangeReceiver pair provides a Tokio broadcast channel for notifying live streams when documents change.

#![allow(unused)]
fn main() {
use rouchdb_changes::ChangeSender;
use rouchdb::Seq;

// Create the channel pair (capacity = max buffered notifications)
let (sender, initial_receiver) = ChangeSender::new(64);

// Create additional subscribers
let another_receiver = sender.subscribe();

// Notify all subscribers that a document changed
sender.notify(Seq::Num(42), "user:alice".into());
}

A custom adapter should rather implement Adapter::subscribe (a default method returning None): return a tokio::sync::broadcast::Receiver<ChangeNotice> and send a ChangeNotice::new(seq, doc_id) for every document change once it is committed. Live changes streams and live replication then use it automatically. ChangeSender remains for feeding a stream notifications by hand; ChangeNotification is the same type as ChangeNotice.

Custom Filter Closures

For flexible client-side filtering, pass a ChangesFilter closure that receives each ChangeEvent and returns true to include or false to skip:

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesStreamOptions, ChangesFilter};
use std::sync::Arc;
use std::time::Duration;

let db = Database::memory("mydb");

let filter: ChangesFilter = Arc::new(|event| {
    event.id.starts_with("user:")
});

let (mut rx, handle) = db.live_changes(ChangesStreamOptions {
    filter: Some(filter),
    poll_interval: Duration::from_millis(200),
    ..Default::default()
});

while let Some(event) = rx.recv().await {
    // Only user: docs arrive here
    println!("User changed: {}", event.id);
}

handle.cancel();
}

Database::live_changes()

The Database struct provides a high-level live_changes() method that returns an mpsc::Receiver<ChangeEvent> and a ChangesHandle. This is the recommended way to consume live changes:

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesStreamOptions};
use std::time::Duration;

let db = Database::memory("mydb");

let (mut rx, handle) = db.live_changes(ChangesStreamOptions {
    poll_interval: Duration::from_millis(200),
    ..Default::default()
});

// Receive events from the channel
while let Some(event) = rx.recv().await {
    println!("Change: {} seq={}", event.id, event.seq);
}

// Cancel the stream when done
handle.cancel();
}

Dropping the ChangesHandle also cancels the stream automatically.

Database::live_changes_events()

For applications that need lifecycle events (active, paused, errors) in addition to document changes, use live_changes_events(). It returns ChangesEvent enum variants instead of raw ChangeEvent structs:

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesStreamOptions, ChangesEvent};
use std::time::Duration;

let db = Database::memory("mydb");

let (mut rx, handle) = db.live_changes_events(ChangesStreamOptions {
    include_docs: true,
    poll_interval: Duration::from_millis(200),
    ..Default::default()
});

while let Some(event) = rx.recv().await {
    match event {
        ChangesEvent::Change(ce) => {
            println!("Doc changed: {} seq={}", ce.id, ce.seq);
        }
        ChangesEvent::Complete { last_seq, .. } => {
            println!("Caught up at seq {}", last_seq);
        }
        ChangesEvent::Error(msg) => {
            eprintln!("Error: {}", msg);
        }
        ChangesEvent::Paused => {
            println!("Waiting for new changes...");
        }
        ChangesEvent::Active => {
            println!("Processing changes...");
        }
        // `ChangesEvent` is `#[non_exhaustive]` (this also covers
        // `Heartbeat` and events added later).
        _ => {}
    }
}

handle.cancel();
}

ChangesEvent Variants

VariantDescription
Change(ChangeEvent)A document was created, updated, or deleted.
Complete { last_seq: Seq }All current changes have been processed.
Error(String)An error occurred while fetching changes.
PausedWaiting for new changes (no pending changes).
ActiveResumed processing after a pause.

Filtering by Document IDs

Both one-shot and live changes support filtering:

#![allow(unused)]
fn main() {
let response = db.changes(ChangesOptions {
    doc_ids: Some(vec!["user:alice".into(), "user:bob".into()]),
    include_docs: true,
    ..Default::default()
}).await?;

// Only changes to user:alice and user:bob are returned
}

This is useful for building reactive views that only care about a subset of documents.

Filtering by Mango Selector

You can filter changes using a Mango selector — only changes to documents matching the selector are returned:

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesOptions};

let db = Database::memory("mydb");

// One-shot: only changes for documents where type == "user"
let changes = db.changes(ChangesOptions {
    selector: Some(serde_json::json!({"type": "user"})),
    include_docs: true,
    ..Default::default()
}).await?;

for event in &changes.results {
    println!("{}: {:?}", event.id, event.doc);
}
}

For live changes with selector filtering:

#![allow(unused)]
fn main() {
use rouchdb::{Database, ChangesStreamOptions};
use std::time::Duration;

let db = Database::memory("mydb");

let (mut rx, handle) = db.live_changes(ChangesStreamOptions {
    selector: Some(serde_json::json!({"type": "user"})),
    include_docs: true,
    poll_interval: Duration::from_millis(200),
    ..Default::default()
});

while let Some(event) = rx.recv().await {
    // Only user documents arrive here
    println!("User changed: {}", event.id);
}

handle.cancel();
}

When using the HTTP adapter (CouchDB), the selector is passed natively via filter=_selector for server-side filtering. For local adapters (memory, redb), documents are fetched with include_docs: true internally and filtered in Rust.

Replication

Replication is what makes RouchDB a local-first database. It implements the CouchDB replication protocol, allowing bidirectional sync between any two databases – local to local, local to remote CouchDB, or even remote to remote.

Quick Start

#![allow(unused)]
fn main() {
use rouchdb::Database;
use serde_json::json;

let local = Database::open("data/myapp.redb", "myapp")?;
let remote = Database::http("http://localhost:5984/myapp");

// Push local changes to CouchDB
local.replicate_to(&remote).await?;

// Pull remote changes to local
local.replicate_from(&remote).await?;

// Or do both directions at once
let (push_result, pull_result) = local.sync(&remote).await?;
}

Setting Up CouchDB with Docker

A minimal docker-compose.yml for local development:

version: "3"
services:
  couchdb:
    image: couchdb:3
    ports:
      - "5984:5984"
    environment:
      COUCHDB_USER: admin
      COUCHDB_PASSWORD: password
    volumes:
      - couchdata:/opt/couchdb/data

volumes:
  couchdata:

Start it and create a database:

docker compose up -d

# Create the database
curl -X PUT http://admin:password@localhost:5984/myapp

Then connect from RouchDB:

#![allow(unused)]
fn main() {
let remote = Database::http("http://admin:password@localhost:5984/myapp");
}

Replication Methods

replicate_to

Push documents from this database to a target.

#![allow(unused)]
fn main() {
let result = local.replicate_to(&remote).await?;
println!("Pushed {} docs", result.docs_written);
}

replicate_from

Pull documents from a source into this database.

#![allow(unused)]
fn main() {
let result = local.replicate_from(&remote).await?;
println!("Pulled {} docs", result.docs_written);
}

sync

Bidirectional sync: pushes first, then pulls. Returns both results as a tuple.

#![allow(unused)]
fn main() {
let (push, pull) = local.sync(&remote).await?;

println!("Push: {} written, Pull: {} written",
    push.docs_written, pull.docs_written);
}

replicate_to_with_opts

Push with custom replication options.

#![allow(unused)]
fn main() {
use rouchdb::ReplicationOptions;

let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    batch_size: 50,
    ..Default::default()
}).await?;
}

ReplicationOptions

#![allow(unused)]
fn main() {
use rouchdb::ReplicationOptions;

let opts = ReplicationOptions {
    batch_size: 100,                                   // documents per batch (default: 100)
    batches_limit: 10,                                 // max batches to buffer (default: 10)
    filter: None,                                      // optional filter (default: None)
    live: false,                                       // continuous mode (default: false)
    retry: false,                                      // auto-retry on failure (default: false)
    poll_interval: std::time::Duration::from_millis(500), // live mode poll interval
    back_off_function: None,                           // custom backoff for retries
    ..Default::default()
};
}
FieldDefaultDescription
batch_size100Number of documents to process in each replication batch. Smaller values mean more frequent checkpoints.
batches_limit10Maximum number of batches to buffer. Controls memory usage for large replications.
filterNoneOptional ReplicationFilter for selective replication. See Filtered Replication.
sinceNoneOverride the starting sequence instead of reading from checkpoint. Useful for replaying changes from a known point.
checkpointtrueSet to false to disable checkpoint saving. Each replication will start from the beginning (or since).
livefalseEnable continuous replication that keeps running and picks up new changes.
retryfalseAutomatically retry on network or transient errors (live mode).
poll_interval500msHow frequently to poll for new changes in live mode, for a source that cannot announce them (a remote CouchDB), and to check the identity of such a target. A local source (memory, redb) wakes the replication up on each change instead.
back_off_functionNoneCustom backoff function for retries. Receives retry count, returns delay.

Filtered Replication

You can replicate a subset of documents using ReplicationFilter. Three filter types are available:

Filter by Document IDs

Replicate only specific documents by their IDs. This is the most efficient filter – it pushes the filtering down to the changes feed.

#![allow(unused)]
fn main() {
use rouchdb::{ReplicationOptions, ReplicationFilter};

let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    filter: Some(ReplicationFilter::DocIds(vec![
        "user:alice".into(),
        "user:bob".into(),
    ])),
    ..Default::default()
}).await?;
}

Filter by Mango Selector

Replicate documents matching a Mango query selector. The selector is evaluated against each document’s data after fetching.

#![allow(unused)]
fn main() {
use rouchdb::{ReplicationOptions, ReplicationFilter};

let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    filter: Some(ReplicationFilter::Selector(serde_json::json!({
        "type": "invoice",
        "status": "pending"
    }))),
    ..Default::default()
}).await?;
}

Filter by Custom Closure

Pass a Rust closure that receives each ChangeEvent and returns true to replicate or false to skip.

#![allow(unused)]
fn main() {
use rouchdb::{ReplicationOptions, ReplicationFilter};

let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    filter: Some(ReplicationFilter::Custom(std::sync::Arc::new(|change| {
        change.id.starts_with("public:")
    }))),
    ..Default::default()
}).await?;
}

Note: Checkpoints advance past all processed changes regardless of filtering. This means re-running filtered replication won’t re-evaluate previously seen changes.

How the Replication Protocol Works

RouchDB implements the standard CouchDB replication protocol. Each replication run follows these steps:

  1. Read checkpoint – Load the last successfully replicated sequence from the local document store. This allows replication to resume where it left off.

  2. Fetch changes – Query the source’s changes feed starting from the checkpoint sequence, limited to batch_size changes per request.

  3. Compute revs_diff – Send the changed document IDs and their revisions to the target. The target responds with which revisions it is missing, avoiding redundant transfers.

  4. Fetch missing documents – Use bulk_get to retrieve only the documents and revisions the target does not have.

  5. Write to target – Write the missing documents to the target using bulk_docs with new_edits: false (replication mode), which preserves the original revision IDs and merges them into the target’s revision trees.

  6. Save checkpoint – Persist the last replicated sequence so the next run can start from where this one ended.

Steps 2-6 repeat in a loop until no more changes remain.

ReplicationResult

Every replication call returns a ReplicationResult:

#![allow(unused)]
fn main() {
use rouchdb::ReplicationResult;

let result = local.replicate_to(&remote).await?;

if result.ok {
    println!("Replication succeeded");
} else {
    println!("Replication had errors:");
    for err in &result.errors {
        println!("  - {}", err);
    }
}

println!("Documents read:    {}", result.docs_read);
println!("Documents written: {}", result.docs_written);
println!("Last sequence:     {}", result.last_seq);
}
FieldTypeDescription
okbooltrue if no errors occurred.
docs_readu64Number of change events read from the source.
docs_writtenu64Number of documents written to the target.
errorsVec<String>Descriptions of any errors during replication.
last_seqSeqThe source sequence up to which replication completed.
warningsVec<String>Conditions that did not stop the replication (such as both databases reporting the same identity, which disables checkpoints). They do not affect ok.

Note that docs_read may be greater than docs_written when the target already has some of the documents (incremental replication).

Incremental Replication

Replication is incremental by default. Checkpoints are stored as local documents (prefixed with _local/) that are not themselves replicated. After an initial full sync, subsequent calls only transfer new changes:

#![allow(unused)]
fn main() {
// First run: syncs everything
let r1 = local.replicate_to(&remote).await?;
println!("Initial: {} docs written", r1.docs_written); // e.g. 500

// Add some new documents
local.put("new_doc", json!({"data": "hello"})).await?;

// Second run: only syncs the delta
let r2 = local.replicate_to(&remote).await?;
println!("Incremental: {} docs written", r2.docs_written); // 1
}

Checkpoints are keyed on both databases’ identities (Adapter::id()), not their names. A redb file’s identity includes its path: a copy of the file is a different database (syncing a file with its copy works both ways), and a file that is moved or copied is rescanned once with each peer. Two databases that report the same identity (a custom adapter that keeps the default id(), the database name) are replicated without checkpoints, and ReplicationResult::warnings says so.

Replication Events

Use replicate_to_with_events() to receive progress events during replication:

#![allow(unused)]
fn main() {
use rouchdb::ReplicationEvent;

let (result, mut rx) = local.replicate_to_with_events(
    &remote,
    ReplicationOptions::default(),
).await?;

// Drain events after replication completes
while let Ok(event) = rx.try_recv() {
    match event {
        ReplicationEvent::Active => println!("Replication started"),
        ReplicationEvent::Change { docs_read, .. } => {
            println!("Progress: {} docs read", docs_read);
        }
        ReplicationEvent::Complete(result) => {
            println!("Done: {} written", result.docs_written);
        }
        ReplicationEvent::Error(msg) => println!("Error: {}", msg),
        ReplicationEvent::Paused => println!("Waiting for changes..."),
        // `ReplicationEvent` is `#[non_exhaustive]`: new events may be added.
        _ => {}
    }
}
}

Event Variants

VariantDescription
ActiveReplication has started or resumed processing.
Change { docs_read }A batch of changes was processed.
PausedWaiting for more changes (live mode).
Complete(ReplicationResult)Replication finished (one-shot or one cycle in live mode).
Error(String)An error occurred during replication.
Warning(String)A condition that did not stop the replication, also listed in ReplicationResult::warnings (sent once per live session).

ReplicationEvent is #[non_exhaustive] (a match needs a _ arm) and so is its Change variant (match it as Change { docs_read, .. }), so that events and fields can be added without a breaking release. See API Stability.

Live (Continuous) Replication

Live replication keeps running in the background, replicating new changes as they happen: a local source (memory, redb) announces each committed change, a remote one is polled every poll_interval. This is the equivalent of PouchDB’s { live: true } option.

If a local database (memory, redb) at either end is destroyed (and maybe reused) while the replication runs, the replication notices it and starts over, copying every document of the source again, even when the source is idle. This automatic recovery does not cover a remote database: see Re-creating a remote database.

#![allow(unused)]
fn main() {
use rouchdb::{ReplicationOptions, ReplicationEvent};

let (mut rx, handle) = local.replicate_to_live(&remote, ReplicationOptions {
    live: true,
    poll_interval: std::time::Duration::from_millis(500),
    retry: true,
    ..Default::default()
});

// Process events in a loop
tokio::spawn(async move {
    while let Some(event) = rx.recv().await {
        match event {
            ReplicationEvent::Complete(r) => {
                println!("Batch done: {} docs written", r.docs_written);
            }
            ReplicationEvent::Paused => {
                println!("Up to date, waiting for new changes...");
            }
            ReplicationEvent::Error(msg) => {
                eprintln!("Replication error: {}", msg);
            }
            _ => {}
        }
    }
});

// ... later, when you want to stop:
handle.cancel();
}

ReplicationHandle

The ReplicationHandle returned by replicate_to_live() controls the background replication:

  • handle.cancel() – Stops the replication gracefully.
  • Dropping the handle also cancels the replication (via Drop implementation).

Retry and Backoff

When retry: true is set, live replication will automatically retry after transient errors. You can customize the backoff strategy:

#![allow(unused)]
fn main() {
let (rx, handle) = local.replicate_to_live(&remote, ReplicationOptions {
    live: true,
    retry: true,
    back_off_function: Some(Box::new(|retry_count| {
        // Exponential backoff: 1s, 2s, 4s, 8s, max 30s
        let delay = std::cmp::min(1000 * 2u64.pow(retry_count), 30_000);
        std::time::Duration::from_millis(delay)
    })),
    ..Default::default()
});
}

Re-creating a remote database

The HTTP adapter identifies a remote database by the server’s uuid and the database name. A remote database that is deleted and re-created under the same name therefore looks like the same database: a running live replication keeps its position, and a new replication resumes from the existing checkpoints. Neither copies the documents the re-created database lost, and simply retrying does not help.

(rouchdb-server derives its uuid from the served database’s identity, which DELETE /{db} renews, so a replication set up with a new Database::http after the database is re-created there starts over by itself. A running replication, or a Database::http that already replicated before the re-creation (it keeps the uuid it read), still needs the steps below.)

When you delete and re-create a remote database that takes part in a replication:

  1. Cancel every live replication to or from it (handle.cancel()).

  2. Re-create the database.

  3. Copy everything again with a one-shot replication that ignores the checkpoints:

    #![allow(unused)]
    fn main() {
    use rouchdb::{ReplicationOptions, Seq};
    
    local.replicate_to_with_opts(&remote, ReplicationOptions {
        since: Some(Seq::zero()),
        checkpoint: false,
        ..Default::default()
    }).await?;
    }
  4. Start the live replication again.

Destroying a local database (memory, redb) needs none of this: it gets a new identity, and replications from or to it start over on their own.

Complete Example: Local-to-CouchDB Sync

use rouchdb::{Database, ReplicationOptions};
use serde_json::json;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    // Open persistent local database
    let local = Database::open("data/todos.redb", "todos")?;

    // Connect to CouchDB
    let remote = Database::http("http://admin:password@localhost:5984/todos");

    // Create some local documents
    local.put("todo:1", json!({
        "title": "Buy groceries",
        "done": false
    })).await?;

    local.put("todo:2", json!({
        "title": "Write documentation",
        "done": true
    })).await?;

    // Push to CouchDB with custom batch size
    let push = local.replicate_to_with_opts(&remote, ReplicationOptions {
        batch_size: 50,
        ..Default::default()
    }).await?;

    println!("Push complete: {} docs written", push.docs_written);

    // Pull any changes others made on CouchDB
    let pull = local.replicate_from(&remote).await?;
    println!("Pull complete: {} docs written", pull.docs_written);

    // Check local state
    let info = local.info().await?;
    println!("Local database: {} docs, seq {}",
        info.doc_count, info.update_seq);

    Ok(())
}

Local-to-Local Replication

Replication works between any two adapters, not just local and remote. This is useful for backup, migration, or testing:

#![allow(unused)]
fn main() {
let db_a = Database::memory("a");
let db_b = Database::memory("b");

db_a.put("doc1", json!({"from": "a"})).await?;
db_b.put("doc2", json!({"from": "b"})).await?;

// Sync both directions
let (push, pull) = db_a.sync(&db_b).await?;

// Both databases now have both documents
assert_eq!(db_a.info().await?.doc_count, 2);
assert_eq!(db_b.info().await?.doc_count, 2);
}

Conflict Resolution

Conflicts are a natural part of distributed databases. When the same document is edited on two replicas before they sync, both replicas create new revisions that branch from the same parent. CouchDB (and RouchDB) handle this gracefully: rather than rejecting one edit, both revisions are preserved. The system deterministically picks a winner so every replica agrees on what db.get() returns, while the losing revisions remain accessible for manual resolution.

Why Conflicts Happen

Consider two replicas, A and B, that have both synced document todo:1 at revision 1-abc:

Replica A: todo:1 @ 1-abc  -->  update  -->  2-def
Replica B: todo:1 @ 1-abc  -->  update  -->  2-ghi

When A and B sync, the revision tree for todo:1 becomes:

1-abc --> 2-def  (branch from replica A)
      --> 2-ghi  (branch from replica B)

Both 2-def and 2-ghi are valid. The system picks one as the winner; the other becomes a conflict.

The Deterministic Winner Algorithm

CouchDB uses a deterministic algorithm so that every replica independently arrives at the same winner without any coordination:

  1. Non-deleted leaves beat deleted leaves. A live document always wins over a tombstone at the same generation.
  2. Higher position (generation) wins. If one branch has more edits, it wins.
  3. Lexicographically greater hash breaks ties. When two leaves have the same generation and deletion status, the one with the larger hash string wins.

This means if 2-ghi and 2-def are both non-deleted at generation 2, then 2-ghi wins because "ghi" > "def" lexicographically.

RouchDB exposes this algorithm through the winning_rev() function:

#![allow(unused)]
fn main() {
use rouchdb::winning_rev;

// Given a RevTree (the full revision tree for a document)
let winner = winning_rev(&rev_tree);
// Returns Option<Revision> -- the winning leaf revision
}

Detecting Conflicts

Reading Conflicts with get_with_opts

To see whether a document has conflicts, use get_with_opts with conflicts: true:

#![allow(unused)]
fn main() {
use rouchdb::{Database, GetOptions};

let db = Database::memory("mydb");

// ... after replication creates a conflict ...

let doc = db.get_with_opts("todo:1", GetOptions {
    conflicts: true,
    ..Default::default()
}).await?;

// The document body is the winning revision
println!("Winner: {} rev={}", doc.id, doc.rev.as_ref().unwrap());

// Check the _conflicts field in the returned JSON
let json = doc.to_json();
if let Some(conflicts) = json.get("_conflicts") {
    println!("Conflicting revisions: {}", conflicts);
}
}

Using collect_conflicts

If you have access to the document’s revision tree (from the adapter’s internal metadata), you can use the collect_conflicts utility:

#![allow(unused)]
fn main() {
use rouchdb_core::merge::collect_conflicts;

// rev_tree: RevTree -- the document's full revision tree
let conflicts = collect_conflicts(&rev_tree);

for conflict_rev in &conflicts {
    println!("Conflict: {}", conflict_rev);
    // conflict_rev is a Revision { pos, hash }
}
}

collect_conflicts returns all non-winning, non-deleted leaf revisions. Deleted leaves are excluded because a delete inherently resolves that branch.

Using is_deleted

Check whether the winning revision of a document is a deletion:

#![allow(unused)]
fn main() {
use rouchdb_core::merge::is_deleted;

if is_deleted(&rev_tree) {
    println!("The document's winning revision is deleted");
}
}

Resolving Conflicts

The standard resolution strategy is:

  1. Read the winning revision and all conflicting revisions.
  2. Merge the data as your application sees fit.
  3. Update the winner with the merged data.
  4. Delete each losing revision.

After this, the document has a single non-deleted leaf and no more conflicts.

Complete Example

#![allow(unused)]
fn main() {
use rouchdb::{Database, GetOptions, RouchError};
use serde_json::json;

async fn resolve_conflicts(db: &Database, doc_id: &str) -> rouchdb::Result<()> {
    // Step 1: Read the winner with conflicts
    let doc = db.get_with_opts(doc_id, GetOptions {
        conflicts: true,
        ..Default::default()
    }).await?;

    let winner_rev = doc.rev.as_ref().unwrap().to_string();
    let winner_data = doc.data.clone();

    // Extract conflict revisions from the JSON representation
    let doc_json = doc.to_json();
    let conflict_revs: Vec<String> = doc_json
        .get("_conflicts")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();

    if conflict_revs.is_empty() {
        println!("No conflicts to resolve");
        return Ok(());
    }

    // Step 2: Read each conflicting revision
    let mut all_versions = vec![winner_data.clone()];
    for rev in &conflict_revs {
        let conflict_doc = db.get_with_opts(doc_id, GetOptions {
            rev: Some(rev.clone()),
            ..Default::default()
        }).await?;
        all_versions.push(conflict_doc.data);
    }

    // Step 3: Merge the data (application-specific logic)
    // This example takes the winner and appends notes from losers
    let merged = merge_application_data(&all_versions);

    // Step 4: Update the winner with merged data
    let update_result = db.update(doc_id, &winner_rev, merged).await?;
    let new_rev = update_result.rev.unwrap();
    println!("Updated winner to rev {}", new_rev);

    // Step 5: Delete each losing revision
    for rev in &conflict_revs {
        db.remove(doc_id, rev).await?;
        println!("Deleted conflict rev {}", rev);
    }

    println!("All conflicts resolved for {}", doc_id);
    Ok(())
}

fn merge_application_data(versions: &[serde_json::Value]) -> serde_json::Value {
    // Your merge logic here. Common strategies:
    // - Last-write-wins (pick the one with the latest timestamp field)
    // - Field-level merge (combine non-overlapping fields)
    // - Domain-specific (e.g., union of tags, max of counters)

    // Simple example: take the first version's data
    // and merge "tags" arrays from all versions
    let mut result = versions[0].clone();
    let mut all_tags: Vec<serde_json::Value> = Vec::new();

    for version in versions {
        if let Some(tags) = version.get("tags").and_then(|t| t.as_array()) {
            for tag in tags {
                if !all_tags.contains(tag) {
                    all_tags.push(tag.clone());
                }
            }
        }
    }

    if !all_tags.is_empty() {
        result["tags"] = serde_json::Value::Array(all_tags);
    }

    result
}
}

Common Merge Strategies

Last-Write-Wins (LWW)

If your documents include a modified_at timestamp, pick the most recent version:

#![allow(unused)]
fn main() {
fn lww_merge(versions: &[serde_json::Value]) -> serde_json::Value {
    versions.iter()
        .max_by_key(|v| {
            v.get("modified_at")
                .and_then(|t| t.as_str())
                .unwrap_or("")
                .to_string()
        })
        .cloned()
        .unwrap_or(json!({}))
}
}

Field-Level Merge

Combine non-overlapping changes from different replicas:

#![allow(unused)]
fn main() {
fn field_merge(
    base: &serde_json::Value,
    a: &serde_json::Value,
    b: &serde_json::Value,
) -> serde_json::Value {
    let mut result = base.clone();
    if let Some(obj) = result.as_object_mut() {
        // For each field, if only one side changed it, take that change
        for (key, b_val) in b.as_object().unwrap_or(&serde_json::Map::new()) {
            let base_val = base.get(key);
            let a_val = a.get(key);
            if a_val == base_val && Some(b_val) != base_val {
                obj.insert(key.clone(), b_val.clone());
            }
        }
        for (key, a_val) in a.as_object().unwrap_or(&serde_json::Map::new()) {
            let base_val = base.get(key);
            if Some(a_val) != base_val {
                obj.insert(key.clone(), a_val.clone());
            }
        }
    }
    result
}
}

Prevention: Reducing Conflicts

While conflicts are handled gracefully, you can reduce their frequency:

  • Sync frequently. Shorter intervals between replications mean less opportunity for divergent edits.
  • Use fine-grained documents. Instead of one big document, split data into smaller documents that are less likely to be edited concurrently.
  • Design for commutative operations. CRDTs and append-only patterns naturally avoid conflicts.

Adapters

RouchDB uses an adapter pattern to separate the database API from the underlying storage engine. The Database struct wraps any implementation of the Adapter trait, so the same high-level API works identically whether data lives in memory, on disk, or on a remote CouchDB server.

Built-In Adapters

RouchDB ships with three adapters. Each has a convenience constructor on the Database type.

MemoryAdapter

Stores everything in memory. Data is lost when the Database is dropped.

#![allow(unused)]
fn main() {
use rouchdb::Database;

let db = Database::memory("mydb");
}

When to use:

  • Unit and integration tests.
  • Temporary scratch databases.
  • Prototyping without setting up storage.
  • As a replication target for in-process data transformation.

RedbAdapter

Persistent storage backed by redb, a pure-Rust embedded key-value store. No C dependencies, no FFI, no runtime configuration.

#![allow(unused)]
fn main() {
use rouchdb::Database;

let db = Database::open("path/to/mydb.redb", "mydb")?;
}

The first argument is the filesystem path for the redb file. The second is the logical database name (used in replication checkpoints and db.info()).

A file written by rouchdb 0.4 or earlier is refused with RouchError::UpgradeRequired and left untouched; upgrade it once with rouchdb migrate <path> (which writes a verified backup first) or open it with Database::open_with(path, name, OpenOptions::new().upgrade(UpgradePolicy::WithBackup(None))). Files written by 0.5 cannot be opened by 0.4. See Migrating from 0.4 to 0.5.

When to use:

  • Production local-first applications.
  • Any scenario where data must survive process restarts.
  • Offline-capable applications that sync when connectivity returns.

HttpAdapter

Connects to a remote CouchDB (or compatible) server over HTTP. All operations are translated to CouchDB REST API calls.

#![allow(unused)]
fn main() {
use rouchdb::Database;

// Without authentication
let db = Database::http("http://localhost:5984/mydb");

// With basic auth embedded in the URL
let db = Database::http("http://admin:password@localhost:5984/mydb");
}

When to use:

  • Connecting to a CouchDB cluster.
  • Using CouchDB as the “source of truth” server.
  • As a replication source or target.
  • When you need CouchDB’s built-in features (Mango indexes, design documents, etc.) directly.

Choosing an Adapter

ScenarioAdapter
TestsDatabase::memory()
Desktop / mobile / CLI appDatabase::open() (redb)
Server talking to CouchDBDatabase::http()
Local-first with syncDatabase::open() locally, Database::http() for the remote, then sync()

Using Database::from_adapter

If you have an adapter instance created outside the convenience constructors (for example, a custom adapter or one configured with special options), use from_adapter:

#![allow(unused)]
fn main() {
use std::sync::Arc;
use rouchdb::{Database, MemoryAdapter};

let adapter = Arc::new(MemoryAdapter::new("custom"));
let db = Database::from_adapter(adapter);

// Works exactly like Database::memory("custom")
db.put("doc1", serde_json::json!({"hello": "world"})).await?;
}

Accessing the Underlying Adapter

You can get a reference to the underlying adapter for operations that go through the Adapter trait directly:

#![allow(unused)]
fn main() {
let db = Database::memory("mydb");

// Returns &dyn Adapter
let adapter = db.adapter();

// Use adapter methods directly (e.g., for map/reduce)
use rouchdb::{query_view, ViewQueryOptions};

let result = query_view(
    adapter,
    &|doc| {
        let name = doc.get("name").cloned().unwrap_or(serde_json::json!(null));
        vec![(name, serde_json::json!(1))]
    },
    None,
    ViewQueryOptions::new(),
).await?;
}

This is particularly useful for query_view(), attachment operations, and local document storage, which take an &dyn Adapter parameter.

Implementing a Custom Adapter

To create your own storage backend, implement the Adapter trait from rouchdb_core. Here is the full trait signature:

#![allow(unused)]
fn main() {
use async_trait::async_trait;
use rouchdb_core::adapter::Adapter;
use rouchdb_core::document::*;
use rouchdb_core::error::Result;
use std::collections::HashMap;

pub struct MyAdapter {
    // your storage state
}

#[async_trait]
impl Adapter for MyAdapter {
    async fn info(&self) -> Result<DbInfo> {
        // db_name, doc_count, doc_del_count, update_seq
        Ok(DbInfo::new("mydb", 0, 0, 0))
    }

    async fn get(&self, id: &str, opts: GetOptions) -> Result<Document> { todo!() }

    async fn bulk_docs(
        &self,
        docs: Vec<Document>,
        opts: BulkDocsOptions,
    ) -> Result<Vec<DocResult>> { todo!() }

    async fn all_docs(&self, opts: AllDocsOptions) -> Result<AllDocsResponse> { todo!() }

    async fn changes(&self, opts: ChangesOptions) -> Result<ChangesResponse> { todo!() }

    async fn revs_diff(
        &self,
        revs: HashMap<String, Vec<String>>,
    ) -> Result<RevsDiffResponse> { todo!() }

    async fn bulk_get(&self, docs: Vec<BulkGetItem>) -> Result<BulkGetResponse> { todo!() }

    async fn put_attachment(
        &self,
        doc_id: &str,
        att_id: &str,
        rev: &str,
        data: Vec<u8>,
        content_type: &str,
    ) -> Result<DocResult> { todo!() }

    async fn get_attachment(
        &self,
        doc_id: &str,
        att_id: &str,
        opts: GetAttachmentOptions,
    ) -> Result<Vec<u8>> { todo!() }

    async fn remove_attachment(
        &self,
        doc_id: &str,
        att_id: &str,
        rev: &str,
    ) -> Result<DocResult> { todo!() }

    async fn get_local(&self, id: &str) -> Result<serde_json::Value> { todo!() }

    async fn put_local(&self, id: &str, doc: serde_json::Value) -> Result<()> { todo!() }

    async fn remove_local(&self, id: &str) -> Result<()> { todo!() }

    async fn compact(&self) -> Result<()> { todo!() }

    async fn destroy(&self) -> Result<()> { todo!() }

    // close, purge, get_security, put_security have default implementations
    // Override them if your adapter needs custom behavior.
}
}

Building Results

The result types an adapter returns (DbInfo, DocResult, AllDocsResponse, ChangesResponse, ChangeEvent, RevsDiffResponse, BulkGetResponse, PurgeResponse, ChangeNotice, …) are #[non_exhaustive], so that fields can be added without a breaking release: build them with their constructors, not struct literals.

#![allow(unused)]
fn main() {
use rouchdb_core::adapter::ChangeNotice;
use rouchdb_core::document::*;
use serde_json::json;

// bulk_docs, put_attachment, remove_attachment
let written = DocResult::ok("doc1", "2-abc");
let failed = DocResult::error("doc2", "conflict", "Document update conflict.");

// all_docs
let row = AllDocsRow::document("doc1", AllDocsRowValue::new("2-abc", false))
    .with_doc(json!({"_id": "doc1", "_rev": "2-abc"}));
let all = AllDocsResponse::new(1, 0, vec![row]).with_update_seq(Seq::Num(7));

// changes
let event = ChangeEvent::new(7, "doc1", ["2-abc"]).with_deleted(false);
let changes = ChangesResponse::new(vec![event], 7);

// bulk_get
let found = BulkGetDoc::ok(json!({"_id": "doc1", "_rev": "2-abc"}));
let missing = BulkGetDoc::error(BulkGetError::new("doc3", "1-x", "not_found", "missing"));
let bulk = BulkGetResponse::new(vec![BulkGetResult::new("doc1", vec![found, missing])]);

// subscribe
let notice = ChangeNotice::new(7, "doc1");
}

Fields stay public, so tests and callers read them as before. See API Stability.

Key Implementation Notes

bulk_docs is the most complex method. When opts.new_edits is true, you must:

  • Generate new revision IDs for each document.
  • Check that the provided _rev matches the current winning revision (otherwise return a conflict error).
  • Merge the new revision into the document’s revision tree.

When opts.new_edits is false (replication mode), you must:

  • Accept revision IDs as-is without generating new ones.
  • Merge incoming revisions into the existing revision tree using merge_tree() from rouchdb_core::merge.
  • Never reject a write due to conflicts.

changes must return events ordered by sequence number. Each write to the database increments the sequence.

get_local / put_local / remove_local manage local documents (prefixed with _local/ in CouchDB). These are used by the replication protocol to store checkpoints. They do not participate in the changes feed or replication.

revs_diff is used during replication. Given a map of {doc_id: [rev1, rev2, ...]}, return which revisions the adapter does not have. This avoids transferring documents the target already has.

Using Your Custom Adapter

#![allow(unused)]
fn main() {
use std::sync::Arc;
use rouchdb::Database;

let my_adapter = Arc::new(MyAdapter::new(/* ... */));
let db = Database::from_adapter(my_adapter);

// Now use db normally -- put, get, replicate, etc.
db.put("doc1", serde_json::json!({"works": true})).await?;
}

Because the Database type erases the adapter behind Arc<dyn Adapter>, all RouchDB features (replication, queries, changes feed) work transparently with any conforming adapter.

Attachments

RouchDB supports binary attachments on documents, following the CouchDB attachment model. Attachments are binary blobs (images, PDFs, any file) stored alongside a document and identified by a name. Each attachment has a content type, a length, and a digest for deduplication.

Overview

Attachments are accessed through the Database methods put_attachment, get_attachment, get_attachment_with_opts, and remove_attachment.

Storing an Attachment

Use put_attachment to add or replace an attachment on a document. You must provide the current document revision, just like an update.

#![allow(unused)]
fn main() {
use rouchdb::Database;
use serde_json::json;

let db = Database::memory("mydb");

// First, create the document
let result = db.put("photo:1", json!({"title": "Sunset"})).await?;
let rev = result.rev.unwrap();

// Attach a binary file
let image_bytes: Vec<u8> = std::fs::read("sunset.jpg")?;

let att_result = db.put_attachment(
    "photo:1",       // document ID
    "sunset.jpg",    // attachment name
    &rev,            // current document revision
    image_bytes,     // raw bytes
    "image/jpeg",    // MIME content type
).await?;

assert!(att_result.ok);
println!("New rev after attachment: {}", att_result.rev.unwrap());
}

The put_attachment method signature:

#![allow(unused)]
fn main() {
async fn put_attachment(
    &self,
    doc_id: &str,
    att_id: &str,
    rev: &str,
    data: Vec<u8>,
    content_type: &str,
) -> Result<DocResult>;
}
ParameterDescription
doc_idThe document ID to attach to.
att_idThe attachment name (e.g., "photo.png", "report.pdf").
revThe current revision of the document. A new revision is created.
dataThe raw binary data as Vec<u8>.
content_typeMIME type string (e.g., "image/png", "application/pdf").

The returned DocResult contains the new revision string. Subsequent updates to the document or its attachments must use this new revision.

Retrieving an Attachment

Use get_attachment to read the raw bytes of an attachment:

#![allow(unused)]
fn main() {
use rouchdb::GetAttachmentOptions;

let bytes = db.get_attachment_with_opts(
    "photo:1",
    "sunset.jpg",
    GetAttachmentOptions::default(),
).await?;

println!("Attachment size: {} bytes", bytes.len());

// Write to a file
std::fs::write("downloaded_sunset.jpg", &bytes)?;
}

The get_attachment method signature:

#![allow(unused)]
fn main() {
async fn get_attachment(
    &self,
    doc_id: &str,
    att_id: &str,
    opts: GetAttachmentOptions,
) -> Result<Vec<u8>>;
}

GetAttachmentOptions

#![allow(unused)]
fn main() {
use rouchdb::GetAttachmentOptions;

let opts = GetAttachmentOptions {
    rev: Some("2-abc123...".into()), // fetch from a specific revision
};
}
FieldTypeDescription
revOption<String>Retrieve the attachment from a specific document revision. If None, uses the current winning revision.

AttachmentMeta

When you retrieve a document, its attachments field is a HashMap<String, AttachmentMeta>. The metadata describes each attachment without including the raw data (unless explicitly requested):

#![allow(unused)]
fn main() {
use rouchdb::AttachmentMeta;

let doc = db.get("photo:1").await?;

for (name, meta) in &doc.attachments {
    println!("Attachment: {}", name);
    println!("  Content-Type: {}", meta.content_type);
    println!("  Digest:       {}", meta.digest);
    println!("  Length:        {} bytes", meta.length);
    println!("  Stub:         {}", meta.stub);
}
}

The AttachmentMeta fields:

FieldTypeDescription
content_typeStringThe MIME type (e.g., "image/png").
revposu64Generation of the revision that uploaded the data, as in CouchDB: a write that sends the bytes sets it, stubs and body-only edits keep it, and replication carries it both ways.
digestStringA content-addressed hash of the data (e.g., "md5-abc123...").
lengthu64Size of the attachment in bytes.
stubboolIf true, the data field is absent and only metadata is present. This is the common case when reading documents.
encoding, encoded_lengthOption<String>, Option<u64>How the source stores the bytes (CouchDB’s "gzip" for compressed attachments), kept from stubs that carry it.
dataOption<Vec<u8>>The raw binary data, present only when explicitly included.

Digest-Based Deduplication

Attachments are stored by their content digest. If two documents have the same attachment data, the bytes are stored only once. This is particularly beneficial during replication: if the target already has the attachment data (identified by digest), it does not need to be transferred again.

The digest is computed when the attachment is written and stored in the AttachmentMeta. It follows CouchDB’s format (e.g., "md5-<base64hash>").

Multiple Attachments

A document can have any number of named attachments. Each put_attachment call creates a new document revision:

#![allow(unused)]
fn main() {
let r1 = db.put("report:q1", json!({"title": "Q1 Report"})).await?;
let rev1 = r1.rev.unwrap();

// Add first attachment
let r2 = db.adapter().put_attachment(
    "report:q1", "summary.pdf", &rev1,
    pdf_bytes, "application/pdf",
).await?;
let rev2 = r2.rev.unwrap();

// Add second attachment (using the new revision)
let r3 = db.adapter().put_attachment(
    "report:q1", "charts.png", &rev2,
    png_bytes, "image/png",
).await?;

// The document now has two attachments
let doc = db.get("report:q1").await?;
assert_eq!(doc.attachments.len(), 2);
}

Attachments in Document JSON

When converting a document to JSON with doc.to_json(), attachments appear under the _attachments key:

{
    "_id": "photo:1",
    "_rev": "2-def456...",
    "title": "Sunset",
    "_attachments": {
        "sunset.jpg": {
            "content_type": "image/jpeg",
            "digest": "md5-abc123...",
            "length": 524288,
            "stub": true
        }
    }
}

When creating documents from JSON using Document::from_json(), the _attachments field is automatically parsed into the attachments HashMap.

Inline Base64 Attachments

When creating documents from JSON (e.g., from CouchDB responses or imported data), attachments can be included inline as Base64-encoded strings under the _attachments key:

{
    "_id": "photo:1",
    "title": "Sunset",
    "_attachments": {
        "image.txt": {
            "content_type": "text/plain",
            "data": "SGVsbG8gV29ybGQ="
        }
    }
}

When Document::from_json() encounters a data field as a Base64 string, it automatically decodes it into the AttachmentMeta.data field as Vec<u8>. This is compatible with CouchDB’s inline attachment format.

#![allow(unused)]
fn main() {
let json = serde_json::json!({
    "_id": "doc1",
    "_attachments": {
        "note.txt": {
            "content_type": "text/plain",
            "data": "SGVsbG8gV29ybGQ="
        }
    }
});

let doc = Document::from_json(json)?;
let att = &doc.attachments["note.txt"];
assert_eq!(att.data.as_ref().unwrap(), b"Hello World");
assert_eq!(att.content_type, "text/plain");
}

Removing an Attachment

Use remove_attachment() to remove an attachment from a document, creating a new revision:

#![allow(unused)]
fn main() {
let result = db.remove_attachment("photo:1", "sunset.jpg", &rev).await?;
// The document now has one fewer attachment
}

Attachments and Replication

Attachments participate in replication automatically. When a document with attachments is replicated, the attachment data is included in the transfer. Thanks to digest-based deduplication, attachments that already exist on the target are not transferred again, which saves bandwidth for large binary files.

Design Documents & Views

Design documents are special documents with IDs starting with _design/. They define views, filters, and validation functions. In RouchDB, design documents are stored as DesignDocument structs and can be used with the ViewEngine for Rust-native map/reduce queries.

Design Document CRUD

Creating a Design Document

#![allow(unused)]
fn main() {
use rouchdb::{Database, DesignDocument, ViewDef};

let db = Database::memory("mydb");

let ddoc = DesignDocument::new("myapp")
    .with_view(
        "by_type",
        ViewDef::new("function(doc) { emit(doc.type, 1); }").with_reduce("_count"),
    );

let result = db.put_design(ddoc).await?;
assert!(result.ok);
}

DesignDocument also implements Default, so a struct literal only lists what it sets: DesignDocument { id: "_design/myapp".into(), language: Some("javascript".into()), ..Default::default() }.

Reading a Design Document

Pass the short name (without _design/ prefix):

#![allow(unused)]
fn main() {
let ddoc = db.get_design("myapp").await?;
println!("ID: {}", ddoc.id); // "_design/myapp"
println!("Views: {:?}", ddoc.views.keys().collect::<Vec<_>>());
}

Updating a Design Document

Read the document, modify it, and put it back with the current revision:

#![allow(unused)]
fn main() {
let mut ddoc = db.get_design("myapp").await?;
ddoc.views.insert("by_name".into(), ViewDef::new("function(doc) { emit(doc.name); }"));
let result = db.put_design(ddoc).await?;
}

put_design writes exactly the given document, like a CouchDB PUT. Since DesignDocument keeps every member of the design document (see below), a get_design + put_design round trip changes only what you edited, and a member you remove from the struct is removed from the database.

Deleting a Design Document

#![allow(unused)]
fn main() {
let ddoc = db.get_design("myapp").await?;
let rev = ddoc.rev.unwrap();
db.delete_design("myapp", &rev).await?;
}

DesignDocument Fields

FieldTypeDescription
idStringMust start with _design/.
revOption<String>Current revision (set after reading).
viewsHashMap<String, ViewDef>JavaScript views (a string map and optional reduce), by name.
other_viewsserde_json::Map<String, Value>The other members of views, verbatim: the lib CommonJS library and the views of a Mango (language: "query") index design document, whose map is an object. A name in both maps is written from views.
filtersHashMap<String, Value>Filter functions (CouchDB accepts a string or an object).
validate_doc_updateOption<String>Validation function source.
showsHashMap<String, Value>Show functions.
listsHashMap<String, Value>List functions.
updatesHashMap<String, Value>Update handler functions.
languageOption<String>Language for the functions (e.g., "javascript", "query").
extraserde_json::Map<String, Value>Every other member, verbatim: options, autoupdate, rewrites, _attachments, custom fields, …

ViewDef has map, reduce and extra (the other members of the view, such as options). Empty views, filters, shows, lists and updates objects are not written back (CouchDB treats them like absent ones).

ViewEngine (Rust-Native Views)

For local databases, RouchDB provides a ViewEngine that runs map/reduce using Rust closures instead of JavaScript. This is faster and type-safe.

Registering a View

#![allow(unused)]
fn main() {
use rouchdb::{Database, ViewEngine, ViewQueryOptions, query_view, ReduceFn};

let db = Database::memory("mydb");
db.put("alice", serde_json::json!({"type": "user", "name": "Alice", "age": 30})).await?;
db.put("bob", serde_json::json!({"type": "user", "name": "Bob", "age": 25})).await?;
db.put("inv1", serde_json::json!({"type": "invoice", "amount": 100})).await?;

let mut engine = ViewEngine::new();

// Register a Rust map function for "myapp/by_type"
engine.register_map("myapp", "by_type", |doc| {
    let doc_type = doc.get("type").and_then(|v| v.as_str()).unwrap_or("unknown");
    vec![(serde_json::json!(doc_type), serde_json::json!(1))]
});
}

Updating and Querying a View

The ViewEngine builds an index by scanning the changes feed. Call update_index() to build or refresh, then get_index() to access the results:

#![allow(unused)]
fn main() {
// Build/refresh the index
engine.update_index(db.adapter(), "myapp", "by_type").await?;

// Access the index entries
if let Some(index) = engine.get_index("myapp", "by_type") {
    for (doc_id, entries) in &index.entries {
        for (key, value) in entries {
            println!("{}: key={} value={}", doc_id, key, value);
        }
    }
}
}

For full map/reduce queries (with reduce, grouping, sorting), use the standalone query_view() function:

#![allow(unused)]
fn main() {
let map_fn = |doc: &serde_json::Value| -> Vec<(serde_json::Value, serde_json::Value)> {
    let doc_type = doc.get("type").and_then(|v| v.as_str()).unwrap_or("unknown");
    vec![(serde_json::json!(doc_type), serde_json::json!(1))]
};

let results = query_view(
    db.adapter(),
    &map_fn,
    Some(&ReduceFn::Count),
    ViewQueryOptions {
        reduce: true,
        group: true,
        ..ViewQueryOptions::new()
    },
).await?;

for row in &results.rows {
    println!("{}: {} documents", row.key, row.value);
}
}

Incremental Updates

The ViewEngine tracks the last sequence number and only processes new/changed documents on subsequent update_index() calls, making it efficient for large databases.

Cleanup

Remove unused view indexes:

#![allow(unused)]
fn main() {
db.view_cleanup().await?;
}

Plugins

RouchDB has a plugin system that lets you hook into the document lifecycle. Plugins can modify documents before they are written, react to writes after they happen, and perform cleanup when a database is destroyed.

The Plugin Trait

#![allow(unused)]
fn main() {
use rouchdb::{Plugin, Document, DocResult, Result};
use async_trait::async_trait;

#[async_trait]
pub trait Plugin: Send + Sync {
    /// The plugin name (used for identification).
    fn name(&self) -> &str;

    /// Called before documents are written. Can modify or reject documents.
    async fn before_write(&self, docs: &mut Vec<Document>) -> Result<()> {
        Ok(()) // default: no-op
    }

    /// Called after documents are written with the results.
    async fn after_write(&self, results: &[DocResult]) -> Result<()> {
        Ok(()) // default: no-op
    }

    /// Called when the database is destroyed.
    async fn on_destroy(&self) -> Result<()> {
        Ok(()) // default: no-op
    }
}
}

Adding Plugins

Use with_plugin() to register a plugin on a database:

#![allow(unused)]
fn main() {
use rouchdb::Database;
use std::sync::Arc;

let my_plugin = Arc::new(TimestampPlugin);
let db = Database::memory("mydb").with_plugin(my_plugin);
}

Note: with_plugin() consumes self and returns a new Database, so use the builder pattern.

Multiple plugins can be added. They execute in registration order.

Example: Automatic Timestamps

#![allow(unused)]
fn main() {
use rouchdb::{Plugin, Document, Result};
use async_trait::async_trait;

struct TimestampPlugin;

#[async_trait]
impl Plugin for TimestampPlugin {
    fn name(&self) -> &str { "timestamp" }

    async fn before_write(&self, docs: &mut Vec<Document>) -> Result<()> {
        for doc in docs.iter_mut() {
            if let Some(obj) = doc.data.as_object_mut() {
                obj.insert(
                    "updated_at".into(),
                    serde_json::json!(chrono::Utc::now().to_rfc3339()),
                );
            }
        }
        Ok(())
    }
}
}

Example: Validation Plugin

Return an error from before_write to reject the entire batch:

#![allow(unused)]
fn main() {
use rouchdb::{Plugin, Document, Result, RouchError};
use async_trait::async_trait;

struct RequireTypeField;

#[async_trait]
impl Plugin for RequireTypeField {
    fn name(&self) -> &str { "require-type" }

    async fn before_write(&self, docs: &mut Vec<Document>) -> Result<()> {
        for doc in docs {
            if doc.data.get("type").is_none() && !doc.deleted {
                return Err(RouchError::Forbidden(
                    "All documents must have a 'type' field".into(),
                ));
            }
        }
        Ok(())
    }
}
}

Plugin Hooks

HookWhenCan Modify?Can Reject?
before_writeBefore bulk_docs writesYes (mutable &mut Vec<Document>)Yes (return Err)
after_writeAfter successful writesNo (read-only &[DocResult])Yes (return Err)
on_destroyWhen db.destroy() is calledN/AYes (return Err)

Plugins are called for all write paths: put(), update(), remove(), post(), bulk_docs(), put_attachment(), remove_attachment() and documents replicated into the database. For attachment writes and replicated documents before_write acts like CouchDB’s validate_doc_update: it sees the revision being written (for an attachment write, the parent’s body with the attachment added or removed) and can reject it, but its changes are not stored.

Partitioned Databases

Partitions let you scope queries to a subset of documents that share a common ID prefix. This is useful for multi-tenant applications or logical grouping.

How It Works

Documents are partitioned by their ID prefix. A document with ID "tenant-a:invoice-001" belongs to the "tenant-a" partition. All queries on a partition are automatically scoped to documents whose ID starts with "{partition}:".

Creating a Partition

#![allow(unused)]
fn main() {
use rouchdb::Database;

let db = Database::memory("mydb");

// Insert documents with partition prefixes
db.put("tenant-a:doc1", serde_json::json!({"name": "Alice"})).await?;
db.put("tenant-a:doc2", serde_json::json!({"name": "Bob"})).await?;
db.put("tenant-b:doc1", serde_json::json!({"name": "Charlie"})).await?;

// Create a partition view
let partition = db.partition("tenant-a");
}

Partition Operations

Get

Retrieve a document within the partition. The partition prefix is added automatically:

#![allow(unused)]
fn main() {
let partition = db.partition("tenant-a");

// These are equivalent:
let doc = partition.get("doc1").await?;
let doc = partition.get("tenant-a:doc1").await?;
}

Put

Create or update a document in the partition:

#![allow(unused)]
fn main() {
let partition = db.partition("tenant-a");
partition.put("doc3", serde_json::json!({"name": "Diana"})).await?;
// Creates document with ID "tenant-a:doc3"
}

All Docs

Query all documents in the partition:

#![allow(unused)]
fn main() {
use rouchdb::AllDocsOptions;

let partition = db.partition("tenant-a");
let result = partition.all_docs(AllDocsOptions {
    include_docs: true,
    ..AllDocsOptions::new()
}).await?;

// Only returns tenant-a documents
for row in &result.rows {
    assert!(row.key.starts_with("tenant-a:"));
}
}

Find (Mango Query)

Run a Mango query scoped to the partition:

#![allow(unused)]
fn main() {
use rouchdb::FindOptions;

let partition = db.partition("tenant-a");
let result = partition.find(FindOptions {
    selector: serde_json::json!({"name": {"$regex": "^A"}}),
    ..Default::default()
}).await?;

// Only searches within tenant-a documents
}

Partition Isolation

Partitions are enforced at query time. Documents from other partitions are never included in results:

#![allow(unused)]
fn main() {
let a = db.partition("tenant-a");
let b = db.partition("tenant-b");

let a_docs = a.all_docs(AllDocsOptions::new()).await?;
let b_docs = b.all_docs(AllDocsOptions::new()).await?;

// No overlap between partitions
}

Database API Reference

The Database struct is the primary entry point for all RouchDB operations. It wraps any Adapter implementation behind an Arc<dyn Adapter>, providing a high-level API similar to PouchDB’s JavaScript interface.

#![allow(unused)]
fn main() {
use rouchdb::Database;
}

Constructors

MethodSignatureDescription
memoryfn memory(name: &str) -> SelfCreate an in-memory database. Data is lost when the Database is dropped. Useful for testing.
openfn open(path: impl AsRef<Path>, name: &str) -> Result<Self>Open or create a persistent database backed by redb. Returns an error if the file cannot be opened or created, and RouchError::UpgradeRequired (without modifying it) for a file written by rouchdb 0.4 or earlier.
open_withfn open_with(path: impl AsRef<Path>, name: &str, options: OpenOptions) -> Result<Self>Like open; OpenOptions::new().upgrade(UpgradePolicy::WithBackup(None)) upgrades a 0.4 file after writing a verified backup (see Migrating from 0.4 to 0.5).
httpfn http(url: &str) -> SelfConnect to a remote CouchDB-compatible server. The URL should include the database name (e.g., http://localhost:5984/mydb).
http_with_authfn http_with_auth(url: &str, auth: &AuthClient) -> SelfConnect to CouchDB with cookie authentication. The AuthClient must have been logged in via auth.login() first.
from_adapterfn from_adapter(adapter: Arc<dyn Adapter>) -> SelfCreate a Database from any custom adapter implementation. Use this when you need to provide your own storage backend.

Examples

#![allow(unused)]
fn main() {
// In-memory (for tests)
let db = Database::memory("mydb");

// Persistent (redb file)
let db = Database::open("path/to/mydb.redb", "mydb")?;

// Remote CouchDB
let db = Database::http("http://localhost:5984/mydb");
}

Document Operations

These methods correspond to CouchDB’s core document API. See the Core Types Reference for details on the option and response structs.

MethodSignatureReturn TypeDescription
infoasync fn info(&self)Result<DbInfo>Get database metadata: name, document count, and current update sequence.
getasync fn get(&self, id: &str)Result<Document>Retrieve a document by its _id. Returns RouchError::NotFound if the document does not exist or has been deleted.
get_with_optsasync fn get_with_opts(&self, id: &str, opts: GetOptions)Result<Document>Retrieve a document with options: specific revision, conflict info, all open revisions, or full revision history.
postasync fn post(&self, data: serde_json::Value)Result<DocResult>Create a new document with an auto-generated UUID v4 as the ID. Equivalent to PouchDB’s db.post().
putasync fn put(&self, id: &str, data: serde_json::Value)Result<DocResult>Create a new document. If a document with the same _id already exists and has no previous revision, this creates it; otherwise it may conflict.
updateasync fn update(&self, id: &str, rev: &str, data: serde_json::Value)Result<DocResult>Update an existing document. You must provide the current _rev string. Returns RouchError::Conflict if the rev does not match.
removeasync fn remove(&self, id: &str, rev: &str)Result<DocResult>Delete a document by marking it as deleted. Requires the current _rev. The document remains in the database as a deletion tombstone. Like CouchDB’s DELETE, a missing or already deleted document is RouchError::NotFound.
bulk_docsasync fn bulk_docs(&self, docs: Vec<Document>, opts: BulkDocsOptions)Result<Vec<DocResult>>Write multiple documents atomically. See BulkDocsOptions for user mode vs. replication mode.
all_docsasync fn all_docs(&self, opts: AllDocsOptions)Result<AllDocsResponse>Query all documents, optionally filtered by key range. Supports pagination, descending order, and including full document bodies.
changesasync fn changes(&self, opts: ChangesOptions)Result<ChangesResponse>Get the list of changes since a given sequence. Used for change tracking, live feeds, and replication.

Examples

#![allow(unused)]
fn main() {
// Post (auto-generated ID)
let result = db.post(json!({"name": "Alice", "age": 30})).await?;
println!("Generated ID: {}", result.id);

// Put (explicit ID)
let result = db.put("user:alice", json!({"name": "Alice", "age": 30})).await?;
let doc = db.get("user:alice").await?;

// Update (requires current rev)
let updated = db.update("user:alice", &result.rev.unwrap(), json!({"name": "Alice", "age": 31})).await?;

// Delete
db.remove("user:alice", &updated.rev.unwrap()).await?;

// Bulk write
let docs = vec![
    Document::new("a", json!({})),
    Document::new("b", json!({})),
];
let results = db.bulk_docs(docs, BulkDocsOptions::new()).await?;

// All docs with options
let response = db.all_docs(AllDocsOptions {
    include_docs: true,
    limit: Some(10),
    ..AllDocsOptions::new()
}).await?;
}

Attachment Operations

MethodSignatureReturn TypeDescription
put_attachmentasync fn put_attachment(&self, doc_id: &str, att_id: &str, rev: &str, data: Vec<u8>, content_type: &str)Result<DocResult>Add or replace an attachment on a document. Creates a new revision.
get_attachmentasync fn get_attachment(&self, doc_id: &str, att_id: &str)Result<Vec<u8>>Retrieve raw attachment bytes using the current revision.
get_attachment_with_optsasync fn get_attachment_with_opts(&self, doc_id: &str, att_id: &str, opts: GetAttachmentOptions)Result<Vec<u8>>Retrieve raw attachment bytes with options (e.g., specific revision).
remove_attachmentasync fn remove_attachment(&self, doc_id: &str, att_id: &str, rev: &str)Result<DocResult>Remove an attachment from a document. Creates a new revision with the attachment removed.

Example

#![allow(unused)]
fn main() {
// Put attachment
let att_result = db.put_attachment("doc1", "photo.jpg", &rev, data, "image/jpeg").await?;

// Get attachment
let bytes = db.get_attachment("doc1", "photo.jpg").await?;

// Remove the attachment
let rm = db.remove_attachment("doc1", "photo.jpg", &att_result.rev.unwrap()).await?;
}

Query Operations

MethodSignatureReturn TypeDescription
findasync fn find(&self, opts: FindOptions)Result<FindResponse>Run a Mango find query with selectors, field projection, sorting, and pagination. If a matching index exists, it will be used. See FindOptions.

Example

#![allow(unused)]
fn main() {
let result = db.find(FindOptions {
    selector: json!({"age": {"$gte": 21}}),
    fields: Some(vec!["name".into(), "age".into()]),
    sort: Some(vec![SortField::Simple("age".into())]),
    limit: Some(25),
    ..Default::default()
}).await?;

for doc in &result.docs {
    println!("{}", doc);
}
}

Index Operations

MethodSignatureReturn TypeDescription
create_indexasync fn create_index(&self, def: IndexDefinition)Result<CreateIndexResponse>Create a Mango index for faster queries. The index is built immediately by scanning all documents. Returns "created" or "exists".
get_indexesasync fn get_indexes(&self)Vec<IndexInfo>List all indexes defined on this database.
delete_indexasync fn delete_index(&self, name: &str)Result<()>Delete an index by name. Returns NotFound if the index does not exist.

Example

#![allow(unused)]
fn main() {
use rouchdb::{IndexDefinition, SortField};

// Create an index on the "age" field
let result = db.create_index(IndexDefinition {
    name: String::new(), // auto-generated as "idx-age"
    fields: vec![SortField::Simple("age".into())],
    ddoc: None,
}).await?;
println!("Index: {} ({})", result.name, result.result);

// Queries on "age" now use the index instead of a full scan
let found = db.find(FindOptions {
    selector: json!({"age": {"$gte": 21}}),
    ..Default::default()
}).await?;

// List indexes
let indexes = db.get_indexes().await;

// Delete an index
db.delete_index("idx-age").await?;
}

Replication

All replication methods implement the CouchDB replication protocol: checkpoint reading, changes feed, revision diff, bulk document fetch, and checkpoint saving. See the Replication chapter for a conceptual overview.

MethodSignatureReturn TypeDescription
replicate_toasync fn replicate_to(&self, target: &Database)Result<ReplicationResult>One-shot push replication from this database to the target. Uses default options (batch size 100).
replicate_fromasync fn replicate_from(&self, source: &Database)Result<ReplicationResult>One-shot pull replication from the source into this database.
replicate_to_with_optsasync fn replicate_to_with_opts(&self, target: &Database, opts: ReplicationOptions)Result<ReplicationResult>Push replication with custom ReplicationOptions (batch size, batches limit).
replicate_to_with_eventsasync fn replicate_to_with_events(&self, target: &Database, opts: ReplicationOptions)Result<(ReplicationResult, Receiver<ReplicationEvent>)>Push replication with event streaming. Returns the result and a channel receiver for progress events.
replicate_to_livefn replicate_to_live(&self, target: &Database, opts: ReplicationOptions)(Receiver<ReplicationEvent>, ReplicationHandle)Start continuous (live) replication. Returns an event receiver and a handle to cancel. Dropping the handle also cancels.
syncasync fn sync(&self, other: &Database)Result<(ReplicationResult, ReplicationResult)>Bidirectional sync: pushes to other, then pulls from other. Returns a tuple of (push_result, pull_result).

ReplicationOptions

FieldTypeDefaultDescription
batch_sizeu64100Number of documents to process per batch.
batches_limitu6410Maximum number of batches to buffer.
filterOption<ReplicationFilter>NoneOptional filter for selective replication.
sinceOption<Seq>NoneOverride the starting sequence. Skips checkpoint and starts from this sequence.
checkpointbooltrueSet to false to disable checkpoint saving/reading.
liveboolfalseEnable continuous replication (used with replicate_to_live).
retryboolfalseAutomatically retry on failure (live mode).
poll_intervalDuration500msHow often to poll for new changes in live mode when the adapter cannot announce them (Adapter::subscribe returns None, e.g. HTTP); memory and redb wake the stream up on each change.
back_off_functionOption<Box<dyn Fn(u32) -> Duration>>NoneCustom backoff function for retries. Receives retry count, returns delay.

ReplicationFilter

VariantDescription
DocIds(Vec<String>)Replicate only the listed document IDs. Filtering at the changes feed level (most efficient).
Selector(serde_json::Value)Replicate documents matching a Mango selector. Evaluated after fetching documents.
Custom(Arc<dyn Fn(&ChangeEvent) -> bool + Send + Sync>)Replicate documents passing a custom predicate applied to each change event.

ReplicationResult

FieldTypeDescription
okbooltrue if replication completed with no errors.
docs_readu64Total number of documents read from the source changes feed.
docs_writtenu64Total number of documents written to the target.
errorsVec<String>List of error messages encountered during replication.
last_seqSeqThe last sequence processed, used as the checkpoint for the next replication.

Example

#![allow(unused)]
fn main() {
let local = Database::open("local.redb", "mydb")?;
let remote = Database::http("http://localhost:5984/mydb");

// Push local changes to CouchDB
let push = local.replicate_to(&remote).await?;
println!("Pushed {} docs", push.docs_written);

// Full bidirectional sync
let (push, pull) = local.sync(&remote).await?;
}

Query Planning

MethodSignatureReturn TypeDescription
explainasync fn explain(&self, opts: FindOptions)ExplainResponseAnalyze a Mango query and return which index would be used, without executing the query. Useful for optimizing queries.

Example

#![allow(unused)]
fn main() {
let explanation = db.explain(FindOptions {
    selector: serde_json::json!({"age": {"$gt": 20}}),
    ..Default::default()
}).await;

println!("Index: {} ({})", explanation.index.name, explanation.index.index_type);
}

Design Document Operations

MethodSignatureReturn TypeDescription
put_designasync fn put_design(&self, ddoc: DesignDocument)Result<DocResult>Create or update a design document, written exactly as given (DesignDocument keeps every member, so a get_design + put_design round trip is lossless). Like put, a failed write (e.g. RouchError::Conflict) is an error.
get_designasync fn get_design(&self, name: &str)Result<DesignDocument>Retrieve a design document by short name (without _design/ prefix).
delete_designasync fn delete_design(&self, name: &str, rev: &str)Result<DocResult>Delete a design document.
view_cleanupasync fn view_cleanup(&self)Result<()>Remove unused view indexes.

See the Design Documents & Views guide for details.


Security

MethodSignatureReturn TypeDescription
get_securityasync fn get_security(&self)Result<SecurityDocument>Retrieve the database security document (admins and members).
put_securityasync fn put_security(&self, doc: SecurityDocument)Result<()>Update the database security document.

Changes Feed (Event-Based)

MethodSignatureReturn TypeDescription
live_changesfn live_changes(&self, opts: ChangesStreamOptions)(Receiver<ChangeEvent>, ChangesHandle)Start a live changes stream returning raw change events.
live_changes_eventsfn live_changes_events(&self, opts: ChangesStreamOptions)(Receiver<ChangesEvent>, ChangesHandle)Start a live changes stream returning lifecycle events (Active, Paused, Complete, Error, Change).

See the Changes Feed guide for details.


Partitioned Queries

MethodSignatureReturn TypeDescription
partitionfn partition(&self, name: &str)Partition<'_>Create a partitioned view scoped to documents with ID prefix "{name}:".

The returned Partition supports get(), put(), all_docs(), and find(). See the Partitioned Databases guide.


Plugin System

MethodSignatureDescription
with_pluginfn with_plugin(self, plugin: Arc<dyn Plugin>) -> SelfRegister a plugin that hooks into the document lifecycle. Consumes and returns self (builder pattern).

See the Plugins guide for details.


Maintenance

MethodSignatureReturn TypeDescription
closeasync fn close(&self)Result<()>Close the database connection. No-op for HTTP adapter.
compactasync fn compact(&self)Result<()>Compact the database: removes old revisions and cleans up unreferenced attachment data.
purgeasync fn purge(&self, id: &str, revs: Vec<String>)Result<PurgeResponse>Permanently remove specific revisions of a document. Unlike remove(), purged revisions do not replicate.
destroyasync fn destroy(&self)Result<()>Destroy the database and all its data (local documents, attachments, security and Mango indexes included). This is irreversible. The handle stays usable as a new, empty database; an http database is re-created on its next use.

Accessing the Adapter

MethodSignatureReturn TypeDescription
adapterfn adapter(&self) -> &dyn Adapter&dyn AdapterGet a reference to the underlying adapter. Useful when you need to call adapter-level methods not exposed on Database (e.g., revs_diff, bulk_get, local documents).

Example

#![allow(unused)]
fn main() {
let db = Database::memory("test");

// Access adapter directly for replication-level operations
let diff = db.adapter().revs_diff(rev_map).await?;
let local = db.adapter().get_local("_local/my-checkpoint").await?;
}

Adapter Trait Reference

The Adapter trait is the storage abstraction at the heart of RouchDB. Every storage backend – in-memory, redb, CouchDB HTTP – implements this trait. The high-level Database struct delegates all operations to its underlying Adapter.

This trait mirrors PouchDB’s internal adapter interface (the underscore-prefixed methods in JavaScript), where each method corresponds to a CouchDB API endpoint.

#![allow(unused)]
fn main() {
use rouchdb_core::adapter::Adapter;
}

Full Trait Definition

#![allow(unused)]
fn main() {
#[async_trait]
pub trait Adapter: Send + Sync {
    async fn info(&self) -> Result<DbInfo>;

    async fn get(&self, id: &str, opts: GetOptions) -> Result<Document>;

    async fn bulk_docs(
        &self,
        docs: Vec<Document>,
        opts: BulkDocsOptions,
    ) -> Result<Vec<DocResult>>;

    async fn all_docs(&self, opts: AllDocsOptions) -> Result<AllDocsResponse>;

    async fn changes(&self, opts: ChangesOptions) -> Result<ChangesResponse>;

    async fn revs_diff(
        &self,
        revs: HashMap<String, Vec<String>>,
    ) -> Result<RevsDiffResponse>;

    async fn bulk_get(&self, docs: Vec<BulkGetItem>) -> Result<BulkGetResponse>;

    async fn put_attachment(
        &self,
        doc_id: &str,
        att_id: &str,
        rev: &str,
        data: Vec<u8>,
        content_type: &str,
    ) -> Result<DocResult>;

    async fn get_attachment(
        &self,
        doc_id: &str,
        att_id: &str,
        opts: GetAttachmentOptions,
    ) -> Result<Vec<u8>>;

    async fn remove_attachment(
        &self,
        doc_id: &str,
        att_id: &str,
        rev: &str,
    ) -> Result<DocResult>;

    async fn get_local(&self, id: &str) -> Result<serde_json::Value>;

    async fn put_local(&self, id: &str, doc: serde_json::Value) -> Result<()>;

    async fn remove_local(&self, id: &str) -> Result<()>;

    async fn compact(&self) -> Result<()>;

    async fn destroy(&self) -> Result<()>;

    // Methods with default implementations:

    fn subscribe(&self) -> Option<tokio::sync::broadcast::Receiver<ChangeNotice>> { None }

    async fn close(&self) -> Result<()> { Ok(()) }

    async fn purge(
        &self,
        req: HashMap<String, Vec<String>>,
    ) -> Result<PurgeResponse> { /* default: error */ }

    async fn get_security(&self) -> Result<SecurityDocument> { /* default: empty */ }

    async fn put_security(&self, doc: SecurityDocument) -> Result<()> { /* default: no-op */ }
}
}

Method Reference

Database Information

MethodSignatureDescription
infoasync fn info(&self) -> Result<DbInfo>Returns the database name, document count, and current update sequence.

Behavior contract: Must always succeed for a valid, non-destroyed database. The update_seq should reflect the latest write.


Document Retrieval

MethodSignatureDescription
getasync fn get(&self, id: &str, opts: GetOptions) -> Result<Document>Retrieve a single document by its _id.

Behavior contract:

  • With default GetOptions: returns the winning revision of the document. Returns RouchError::NotFound if the document does not exist or the winning revision is a deletion.
  • With opts.rev = Some(rev): returns the specific revision, even if it is not the winner. Returns NotFound if that revision does not exist (including revisions stemmed by the revision limit or compacted away) and InvalidRev if rev is malformed. A 32-digit hex revision id is case-insensitive (CouchDB stores it in lower case).
  • An id starting with _local/ reads the local document of that name (as CouchDB does).
  • With opts.conflicts = true: the returned document includes information about conflicting leaf revisions.
  • With opts.open_revs = Some(OpenRevs::All): returns all leaf revisions of the document.
  • With opts.revs = true: includes full revision history in the response.

When it is called: Every Database::get and Database::get_with_opts call delegates here. Also called internally during replication to fetch specific revisions.


Document Writing

MethodSignatureDescription
bulk_docsasync fn bulk_docs(&self, docs: Vec<Document>, opts: BulkDocsOptions) -> Result<Vec<DocResult>>Write multiple documents in a single atomic operation.

Behavior contract:

This method has two distinct modes controlled by opts.new_edits:

User mode (new_edits: true – the default)

This is the mode used for normal application writes. The adapter:

  1. Generates a new revision ID for each document (incrementing the pos and computing a new hash).
  2. Checks for conflicts – if the document already exists, the provided _rev must match the current winning revision. If it does not match, the adapter returns a DocResult with ok: false and an error of "conflict".
  3. Appends the new revision as a child of the provided parent revision in the document’s revision tree.
  4. Increments the database sequence number for each successful write.

Replication mode (new_edits: false)

This is the mode used by the replication protocol. The adapter:

  1. Accepts revision IDs as provided – does not generate new revisions.
  2. Does not check for conflicts – merges the incoming revision tree with the existing one.
  3. Inserts the revision into the tree at the correct position, potentially creating new branches.
  4. Determines the new winning revision using the deterministic algorithm (highest pos, then lexicographic hash comparison).

This distinction is critical for correct replication behavior. When documents arrive from a remote peer, they carry their own revision history and must be merged without conflict rejection.

When it is called: All Database::put, Database::update, Database::remove, and Database::bulk_docs calls delegate here. The replication protocol calls this with BulkDocsOptions::replication().


Querying

MethodSignatureDescription
all_docsasync fn all_docs(&self, opts: AllDocsOptions) -> Result<AllDocsResponse>Query all documents, optionally filtered by key range.
changesasync fn changes(&self, opts: ChangesOptions) -> Result<ChangesResponse>Get changes since a given sequence number.

all_docs behavior contract:

  • Returns documents sorted by _id in CouchDB collation order.
  • Supports key range filtering via start_key, end_key, key, and keys.
  • When include_docs is true, the full document body is included in each row.
  • Deleted documents are excluded from results unless requested with keys.
  • With keys, one row per requested key in request order (reversed for descending, duplicates kept): a deleted document is a row with value.deleted == Some(true) and no doc, an unknown ID is AllDocsRow::not_found(key).
  • Supports descending order, skip, and limit for pagination (with keys, they count every row, error rows included).

changes behavior contract:

  • Returns change events in sequence order, starting after opts.since.
  • Each ChangeEvent contains the document _id, the sequence number, and the list of changed revisions.
  • When include_docs is true, the full document body is included.
  • Supports limit to cap the number of results, descending for reverse order, and doc_ids to filter by specific document IDs.
  • The last_seq in the response can be passed as since in the next call for incremental polling.

subscribe (optional): an adapter that can announce its changes returns a tokio::sync::broadcast::Receiver<ChangeNotice> and sends one ChangeNotice::new(seq, doc_id) per changed document once the write is committed (visible to readers); local documents are not announced. The default returns None. Live changes streams and live replication wait on these notices and poll changes only for adapters without them. The memory and redb adapters implement it; the HTTP adapter does not. Notices are wake-ups: a subscriber that lags (RecvError::Lagged) re-reads the changes feed from its last sequence. An adapter that implements it must also send ChangeNotice::reset() (reset: true) once destroy has emptied the database and renewed its id: live replication listens for it on both peers, so a target destroyed while the source is idle is refilled. Adapters without notices have their id compared every poll_interval instead.

When they are called: all_docs is called by Database::all_docs and internally by the Mango query engine and map/reduce views. changes is called by Database::changes and is the starting point of every replication cycle.


Replication Support

MethodSignatureDescription
revs_diffasync fn revs_diff(&self, revs: HashMap<String, Vec<String>>) -> Result<RevsDiffResponse>Compare document revision sets against the adapter’s local state to determine which revisions are missing.
bulk_getasync fn bulk_get(&self, docs: Vec<BulkGetItem>) -> Result<BulkGetResponse>Fetch multiple documents by ID and optionally by specific revision in a single request.

revs_diff behavior contract:

  • Input: a map of document IDs to lists of revision strings.
  • Output: for each document, the list of revisions the adapter does not have, plus any possible_ancestors (revisions the adapter does have that could be ancestors of the missing ones).
  • This avoids transferring documents the target already has during replication.

bulk_get behavior contract:

  • Input: a list of BulkGetItem structs, each with an id and an optional rev.
  • Output: for each requested item, the document JSON (in ok) or an error (in error).
  • When rev is None, the winning revision is returned.
  • Used during replication to efficiently fetch all missing documents in a single round trip.

When they are called: Both are called by the replication protocol. revs_diff is called in step 3 (after fetching changes from the source), and bulk_get is called in step 4 (to fetch the actual missing documents).


Attachments

MethodSignatureDescription
put_attachmentasync fn put_attachment(&self, doc_id: &str, att_id: &str, rev: &str, data: Vec<u8>, content_type: &str) -> Result<DocResult>Store binary attachment data on a document.
get_attachmentasync fn get_attachment(&self, doc_id: &str, att_id: &str, opts: GetAttachmentOptions) -> Result<Vec<u8>>Retrieve raw binary attachment data.
remove_attachmentasync fn remove_attachment(&self, doc_id: &str, att_id: &str, rev: &str) -> Result<DocResult>Remove an attachment from a document. Creates a new revision.

put_attachment behavior contract:

  • Requires a valid current rev for the document (same conflict semantics as bulk_docs in user mode).
  • Creates or replaces the named attachment.
  • Returns a DocResult with the new revision.

get_attachment behavior contract:

  • Returns the raw bytes of the attachment.
  • Optionally accepts a specific revision via GetAttachmentOptions.
  • Returns RouchError::NotFound if the document, revision, or attachment does not exist.

Local Documents

Local documents are a special class of documents that are never replicated. They exist only in the local adapter and are used primarily for storing replication checkpoints.

MethodSignatureDescription
get_localasync fn get_local(&self, id: &str) -> Result<serde_json::Value>Retrieve a local document by its ID.
put_localasync fn put_local(&self, id: &str, doc: serde_json::Value) -> Result<()>Write a local document. Creates or overwrites.
remove_localasync fn remove_local(&self, id: &str) -> Result<()>Delete a local document.

Behavior contract:

  • Local documents do not participate in the changes feed.
  • Local documents do not have revision trees – they are simple key-value entries.
  • The id does not need the _local/ prefix; the adapter handles namespacing internally.
  • get_local returns RouchError::NotFound if the local document does not exist.

Role in replication checkpoints:

The replication protocol uses local documents to store checkpoints on both the source and target databases. The checkpoint contains the last replicated sequence number, enabling incremental replication:

#![allow(unused)]
fn main() {
// The replication protocol does this internally:
let checkpoint_id = "_local/replication-checkpoint-abc123";

// Read the last checkpoint
let last_seq = adapter.get_local(checkpoint_id).await
    .map(|doc| doc["last_seq"].clone())
    .unwrap_or(Seq::zero());

// After replicating, save the new checkpoint
adapter.put_local(checkpoint_id, json!({
    "last_seq": current_seq,
    "session_id": session_id,
})).await?;
}

This is why local documents are excluded from replication – each side maintains its own checkpoint independently.


Maintenance

MethodSignatureDescription
compactasync fn compact(&self) -> Result<()>Remove old (non-leaf) revisions and clean up unreferenced attachment data.
destroyasync fn destroy(&self) -> Result<()>Destroy the database and all its data (local documents and security included). The adapter stays usable and behaves as a new, empty database (the http adapter re-creates the remote database on its next use).
closeasync fn close(&self) -> Result<()>Release resources (default: no-op).
purgeasync fn purge(&self, req: HashMap<String, Vec<String>>) -> Result<PurgeResponse>Permanently remove specific revisions. Purged revisions do not replicate. Default returns an error.
get_securityasync fn get_security(&self) -> Result<SecurityDocument>Get the database security document (default: empty document).
put_securityasync fn put_security(&self, doc: SecurityDocument) -> Result<()>Set the database security document (default: no-op).

When they are called: compact is called by Database::compact, typically as a periodic maintenance task. destroy is called by Database::destroy when the user wants to permanently delete the database. close, purge, get_security, and put_security have default implementations so existing adapters don’t need to implement them.


Built-in Adapter Implementations

RouchDB ships with three adapter implementations:

AdapterCrateDescription
MemoryAdapterrouchdb-adapter-memoryIn-memory storage. Fast, no persistence. Ideal for tests.
RedbAdapterrouchdb-adapter-redbPersistent storage backed by redb. Pure Rust, no C dependencies.
HttpAdapterrouchdb-adapter-httpRemote CouchDB client using HTTP/JSON via reqwest.

Implementing a Custom Adapter

To implement your own storage backend, implement all methods of the Adapter trait:

#![allow(unused)]
fn main() {
use async_trait::async_trait;
use rouchdb_core::adapter::Adapter;
use rouchdb_core::document::*;
use rouchdb_core::error::Result;
use std::collections::HashMap;

pub struct MyAdapter { /* ... */ }

#[async_trait]
impl Adapter for MyAdapter {
    async fn info(&self) -> Result<DbInfo> {
        // db_name, doc_count, doc_del_count, update_seq
        Ok(DbInfo::new("mydb", 0, 0, 0))
    }

    async fn get(&self, id: &str, opts: GetOptions) -> Result<Document> {
        // ...
    }

    // ... implement all other methods
}
}

The result types adapters return are #[non_exhaustive]: build them with their constructors (DbInfo::new, DocResult::ok / DocResult::error, AllDocsResponse::new, AllDocsRow::document / not_found, ChangesResponse::new, ChangeEvent::new, RevsDiffResponse::new, BulkGetResponse::new, BulkGetDoc::ok / error, PurgeResponse::new, ChangeNotice::new, …). See Building Results and API Stability.

Then use it with Database:

#![allow(unused)]
fn main() {
let adapter = Arc::new(MyAdapter::new());
let db = Database::from_adapter(adapter);
}

Core Types Reference

All core types are defined in the rouchdb-core crate and re-exported from the top-level rouchdb crate. You can import them with:

#![allow(unused)]
fn main() {
use rouchdb::*; // Re-exports all types from rouchdb-core::document
}

Types marked #[non_exhaustive] below are read by applications but built by the library (or by custom adapters, through their constructors); option and data structs are built with a struct literal ending in ..Default::default(). See API Stability for the rules.


Document

The fundamental unit of data in RouchDB.

#![allow(unused)]
fn main() {
pub struct Document {
    pub id: String,
    pub rev: Option<Revision>,
    pub deleted: bool,
    pub data: serde_json::Value,
    pub attachments: HashMap<String, AttachmentMeta>,
}
}
FieldTypeDescription
idStringThe document’s unique identifier (_id in CouchDB).
revOption<Revision>The current revision. None for new documents that have not yet been written.
deletedboolWhether this document is a deletion tombstone (_deleted in CouchDB).
dataserde_json::ValueThe document body as a JSON value. Does not include underscore-prefixed CouchDB metadata fields (_id, _rev, _deleted, _attachments).
attachmentsHashMap<String, AttachmentMeta>Map of attachment names to their metadata.

Methods

MethodSignatureDescription
newfn new(id: impl Into<String>, data: serde_json::Value) -> SelfA document to create: no revision, not deleted, no attachments. Document also implements Default, so a struct literal can end with ..Default::default() (or ..Document::new(id, data)).
from_jsonfn from_json(value: serde_json::Value) -> Result<Self>Parse a CouchDB-style JSON object. Extracts _id, _rev, _deleted, and _attachments from the value and puts the remaining fields in data. Returns RouchError::BadRequest if the value is not a JSON object.
to_jsonfn to_json(&self) -> serde_json::ValueConvert back to a CouchDB-style JSON object with underscore-prefixed metadata fields included.

Example

#![allow(unused)]
fn main() {
// Parse from CouchDB JSON
let doc = Document::from_json(json!({
    "_id": "user:alice",
    "_rev": "3-abc123",
    "name": "Alice",
    "age": 30
}))?;

assert_eq!(doc.id, "user:alice");
assert_eq!(doc.data["name"], "Alice");

// Convert back
let json = doc.to_json();
assert_eq!(json["_id"], "user:alice");
assert_eq!(json["_rev"], "3-abc123");
}

Revision

A CouchDB revision identifier in the format {pos}-{hash}.

#![allow(unused)]
fn main() {
pub struct Revision {
    pub pos: u64,
    pub hash: String,
}
}
FieldTypeDescription
posu64The generation number. Starts at 1, increments with each edit.
hashStringA hex digest string identifying this specific revision.

Trait Implementations

TraitBehavior
DisplayFormats as "{pos}-{hash}" (e.g., "3-abc123").
FromStrParses from "{pos}-{hash}" format. Returns RouchError::InvalidRev on failure.
Ord / PartialOrdOrders by pos first, then by hash lexicographically. This is the deterministic winning revision algorithm used by CouchDB.
Eq / HashTwo revisions are equal if both pos and hash match.
Serialize / DeserializeSerializes as a JSON object with pos and hash fields.

Example

#![allow(unused)]
fn main() {
// Create directly
let rev = Revision::new(3, "abc123".into());
assert_eq!(rev.to_string(), "3-abc123");

// Parse from string
let parsed: Revision = "3-abc123".parse()?;
assert_eq!(parsed.pos, 3);
assert_eq!(parsed.hash, "abc123");

// Ordering (deterministic winner)
let r1 = Revision::new(2, "aaa".into());
let r2 = Revision::new(2, "bbb".into());
assert!(r1 < r2); // Same pos, "bbb" > "aaa" lexicographically
}

Seq

A database sequence identifier. Local adapters use numeric sequences; CouchDB 3.x uses opaque string sequences.

#![allow(unused)]
fn main() {
pub enum Seq {
    Num(u64),
    Str(String),
}
}
VariantDescription
Num(u64)Numeric sequence, used by local adapters (memory, redb). Starts at 0.
Str(String)Opaque string sequence, used by CouchDB 3.x. Must be passed back as-is.

Methods

MethodSignatureDescription
zerofn zero() -> SelfReturns Seq::Num(0) – the starting point (beginning of changes).
as_numfn as_num(&self) -> u64Extract the numeric value. For Str variants, parses the numeric prefix before the first - (e.g., "13-g1A..." returns 13). Returns 0 if unparseable.
to_query_stringfn to_query_string(&self) -> StringFormat for use in HTTP query parameters. Num becomes its decimal string, Str is returned as-is.

Trait Implementations

TraitBehavior
DefaultReturns Seq::Num(0).
DisplayFormats the numeric value or string.
From<u64>Creates Seq::Num(n).
Serialize / DeserializeUses #[serde(untagged)] – serializes as either a number or string in JSON.

DbInfo

Database metadata returned by Adapter::info().

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct DbInfo {
    pub db_name: String,
    pub doc_count: u64,
    pub doc_del_count: u64,
    pub update_seq: Seq,
}
}
FieldTypeDescription
db_nameStringThe name of the database.
doc_countu64Number of non-deleted documents.
doc_del_countu64Number of deleted documents.
update_seqSeqThe current update sequence number. Increments with every write.

Custom adapters build it with DbInfo::new(db_name, doc_count, doc_del_count, update_seq).


DocResult

The result of a single document write operation.

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct DocResult {
    pub ok: bool,
    pub id: String,
    pub rev: Option<String>,
    pub error: Option<String>,
    pub reason: Option<String>,
}
}
FieldTypeDescription
okbooltrue if the write succeeded.
idStringThe document ID.
revOption<String>The new revision string if the write succeeded.
errorOption<String>Error type (e.g., "conflict") if the write failed.
reasonOption<String>Human-readable error description.

Custom adapters and plugins build it with DocResult::ok(id, rev) or DocResult::error(id, error, reason).


AttachmentMeta

Metadata for a document attachment.

#![allow(unused)]
fn main() {
pub struct AttachmentMeta {
    pub content_type: String,
    pub revpos: u64,
    pub digest: String,
    pub length: u64,
    pub stub: bool,
    pub encoding: Option<String>,
    pub encoded_length: Option<u64>,
    pub data: Option<Vec<u8>>,
}
}
FieldTypeDescription
content_typeStringMIME type (e.g., "image/png").
revposu64Generation of the revision that uploaded the data (CouchDB’s revpos). A write that sends the data (inline or with put_attachment) sets it to the new generation, even for identical bytes; stubs, body edits and replication keep it. 0 when unknown (stored before 0.5), and then omitted from the JSON.
digestStringContent digest for deduplication.
lengthu64Size in bytes.
stubboolIf true, only metadata is present (no inline data). Defaults to false.
encodingOption<String>How the source stores the bytes (CouchDB reports "gzip" with att_encoding_info=true). Kept from stubs only: rouchdb stores and serves decoded bytes.
encoded_lengthOption<u64>Size of the encoded bytes at the source.
dataOption<Vec<u8>>Inline binary data, if available. Omitted from serialization when None.

AttachmentMeta implements Default; AttachmentMeta::new(content_type, bytes) builds an inline attachment (digest and length computed), and meta.to_json(data) gives the CouchDB _attachments member (inline with data, a stub otherwise).


DocMetadata

Internal metadata stored per document in an adapter. Not typically used in application code.

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct DocMetadata {
    pub id: String,
    pub rev_tree: RevTree,
    pub seq: u64,
}
}
FieldTypeDescription
idStringThe document ID.
rev_treeRevTreeThe full revision tree for this document.
sequ64The last sequence number at which this document was modified.

PutResponse

Simple response for a successful document write (used in some internal paths).

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct PutResponse {
    pub ok: bool,
    pub id: String,
    pub rev: String,
}
}
FieldTypeDescription
okboolAlways true for success.
idStringThe document ID.
revStringThe new revision string.

Options Structs

GetOptions

Options for document retrieval.

#![allow(unused)]
fn main() {
pub struct GetOptions {
    pub rev: Option<String>,
    pub conflicts: bool,
    pub open_revs: Option<OpenRevs>,
    pub revs: bool,
    pub revs_info: bool,
    pub latest: bool,
    pub attachments: bool,
}
}
FieldTypeDefaultDescription
revOption<String>NoneRetrieve a specific revision instead of the winner.
conflictsboolfalseInclude conflicting revisions in the response.
open_revsOption<OpenRevs>NoneReturn all open (leaf) revisions.
revsboolfalseInclude full revision history.
revs_infoboolfalseInclude revision info with status (available, missing, deleted) for each revision.
latestboolfalseIf rev is specified and is not a leaf, return the latest leaf revision instead.
attachmentsboolfalseInclude inline Base64 attachment data in the response.

OpenRevs

#![allow(unused)]
fn main() {
pub enum OpenRevs {
    All,
    Specific(Vec<String>),
}
}
VariantDescription
AllReturn all leaf revisions.
Specific(Vec<String>)Return only these specific revisions.

BulkDocsOptions

Options for bulk document writes.

#![allow(unused)]
fn main() {
pub struct BulkDocsOptions {
    pub new_edits: bool,
}
}
FieldTypeDefaultDescription
new_editsbooltrueWhen true, the adapter generates new revisions and checks for conflicts. When false (replication mode), revisions are accepted as-is and merged into the revision tree.

BulkDocsOptions::default() is the same as BulkDocsOptions::new() (user-mode writes, new_edits: true); replication mode must be requested explicitly with BulkDocsOptions::replication(). (Before 0.5, Default meant replication mode.)

Constructornew_edits ValueUse Case
BulkDocsOptions::new()trueNormal application writes.
BulkDocsOptions::replication()falseReplication protocol writes.

AllDocsOptions

Options for querying all documents.

#![allow(unused)]
fn main() {
pub struct AllDocsOptions {
    pub start_key: Option<String>,
    pub end_key: Option<String>,
    pub key: Option<String>,
    pub keys: Option<Vec<String>>,
    pub include_docs: bool,
    pub descending: bool,
    pub skip: u64,
    pub limit: Option<u64>,
    pub inclusive_end: bool,
    pub conflicts: bool,
    pub update_seq: bool,
}
}
FieldTypeDefaultDescription
start_keyOption<String>NoneStart of the key range (inclusive).
end_keyOption<String>NoneEnd of the key range (inclusive by default, see inclusive_end).
keyOption<String>NoneReturn only the row matching this exact key.
keysOption<Vec<String>>NoneReturn only the rows matching these exact keys.
include_docsboolfalseInclude the full document body in each row.
descendingboolfalseReturn rows in descending key order.
skipu640Number of rows to skip before returning results.
limitOption<u64>NoneMaximum number of rows to return.
inclusive_endbooltrueWhether the end_key is included in the range.
conflictsboolfalseInclude _conflicts for each document (requires include_docs).
update_seqboolfalseInclude the current update_seq in the response.

AllDocsOptions::default() is the same as AllDocsOptions::new(): inclusive_end: true, like CouchDB. (Before 0.5, Default set inclusive_end: false.)


ChangesOptions

Options for the changes feed.

#![allow(unused)]
fn main() {
pub struct ChangesOptions {
    pub since: Seq,
    pub limit: Option<u64>,
    pub descending: bool,
    pub include_docs: bool,
    pub live: bool,
    pub doc_ids: Option<Vec<String>>,
    pub selector: Option<serde_json::Value>,
    pub conflicts: bool,
    pub style: ChangesStyle,
}
}
FieldTypeDefaultDescription
sinceSeqSeq::Num(0)Return changes after this sequence. Use Seq::zero() for all changes.
limitOption<u64>NoneMaximum number of change events to return.
descendingboolfalseReturn changes in reverse sequence order.
include_docsboolfalseInclude the full document body in each change event.
liveboolfalseEnable continuous (live) changes feed.
doc_idsOption<Vec<String>>NoneFilter changes to only these document IDs.
selectorOption<serde_json::Value>NoneMango selector to filter changes. Only changes matching the selector are returned.
conflictsboolfalseInclude conflicting revisions per change event.
styleChangesStyleMainOnlyMainOnly returns only the winning revision; AllDocs returns all leaf revisions.

FindOptions

Options for a Mango find query (from rouchdb-query).

#![allow(unused)]
fn main() {
pub struct FindOptions {
    pub selector: serde_json::Value,
    pub fields: Option<Vec<String>>,
    pub sort: Option<Vec<SortField>>,
    pub limit: Option<u64>,
    pub skip: Option<u64>,
}
}
FieldTypeDefaultDescription
selectorserde_json::ValueValue::NullThe Mango selector (query) to match documents against. Must be a JSON object.
fieldsOption<Vec<String>>NoneField projection – only include these fields in results. _id is always included.
sortOption<Vec<SortField>>NoneSort specification. Each entry is a field name or a {field: direction} map.
limitOption<u64>NoneMaximum number of matching documents to return.
skipOption<u64>NoneNumber of matching documents to skip.

SortField

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub enum SortField {
    Simple(String),
    WithDirection(HashMap<String, String>),
}
}
VariantExample JSONDescription
Simple(String)"name"Sort by field in ascending order.
WithDirection(HashMap<String, String>){"age": "desc"}Sort by field with explicit direction ("asc" or "desc").

ViewQueryOptions

Options for map/reduce view queries (from rouchdb-query).

#![allow(unused)]
fn main() {
pub struct ViewQueryOptions {
    pub key: Option<serde_json::Value>,
    pub keys: Option<Vec<serde_json::Value>>,
    pub start_key: Option<serde_json::Value>,
    pub end_key: Option<serde_json::Value>,
    pub inclusive_end: bool,
    pub descending: bool,
    pub skip: u64,
    pub limit: Option<u64>,
    pub include_docs: bool,
    pub reduce: bool,
    pub group: bool,
    pub group_level: Option<u64>,
    pub stale: StaleOption,
}
}
FieldTypeDefaultDescription
keyOption<serde_json::Value>NoneReturn only rows with this exact key (with start_key or end_key, the other bound of the range).
keysOption<Vec<serde_json::Value>>NoneReturn only rows matching any of these keys, in the given order. Several keys exclude key, start_key and end_key.
start_keyOption<serde_json::Value>NoneStart of key range (inclusive). A range no row can be in is a BadRequest.
end_keyOption<serde_json::Value>NoneEnd of key range (inclusive by default).
inclusive_endbooltrueWhether the end_key is included in the range.
descendingboolfalseReverse row order.
skipu640Number of rows to skip.
limitOption<u64>NoneMaximum number of rows to return.
include_docsboolfalseInclude full document body in each row.
reducebooltrueWhether to run the reduce function, if one is given.
groupboolfalseGroup results by key (requires a reduce; without one it is a BadRequest).
group_levelOption<u64>NoneGroup to this many array elements of the key (above 0 it requires a reduce).
staleStaleOptionFalseFalse rebuilds the index before querying (default). Ok uses a potentially stale index. UpdateAfter returns stale results then rebuilds.

ViewQueryOptions::default() is the same as ViewQueryOptions::new() (CouchDB’s defaults above). (Before 0.5, Default left reduce and inclusive_end off.)


GetAttachmentOptions

Options for retrieving attachments.

#![allow(unused)]
fn main() {
pub struct GetAttachmentOptions {
    pub rev: Option<String>,
}
}
FieldTypeDefaultDescription
revOption<String>NoneRetrieve the attachment from a specific revision.

Response Structs

AllDocsResponse

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct AllDocsResponse {
    pub total_rows: u64,
    pub offset: u64,
    pub rows: Vec<AllDocsRow>,
    pub update_seq: Option<Seq>,
}
}
FieldTypeDescription
total_rowsu64Total number of non-deleted documents in the database.
offsetu64Local adapters: the skip applied (like PouchDB). CouchDB / HTTP adapter: the global position of the first row (0 for keys queries, where CouchDB sends null). See Differences from CouchDB.
rowsVec<AllDocsRow>The result rows.
update_seqOption<Seq>The current update sequence, present when update_seq: true was requested.

AllDocsRow

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct AllDocsRow {
    pub id: Option<String>,
    pub key: String,
    pub value: Option<AllDocsRowValue>,
    pub doc: Option<serde_json::Value>,
    pub error: Option<String>,
}
}
FieldTypeDescription
idOption<String>The document ID; None for an error row.
keyStringThe requested key; for a document row, its ID. Set on every row.
valueOption<AllDocsRowValue>The winning revision and deletion flag; None for an error row.
docOption<serde_json::Value>Full document body, present only when include_docs is true and the document is not deleted.
errorOption<String>Some("not_found") when a key requested with keys names no document.

Range and key queries only return live documents, so id and value are always set there. A keys query returns exactly one row per requested key, in request order (reversed with descending, duplicates kept), so rows[i] answers keys[i], as in CouchDB and PouchDB:

Requested keyRow
a live documentid, key, value, and doc with include_docs
a deleted documentvalue.deleted == Some(true), never a doc (CouchDB sends "doc": null)
an unknown IDonly key and error: Some("not_found")

skip and limit count every kind of row. Helpers: row.rev() (the winning revision, if any), row.is_deleted(), row.is_error(), and the constructors AllDocsRow::document(id, value) and AllDocsRow::not_found(key).

The row is a struct with optional members rather than an enum so it maps one-to-one onto the CouchDB/PouchDB JSON row and row.key works for every kind of row.

AllDocsRowValue

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct AllDocsRowValue {
    pub rev: String,
    pub deleted: Option<bool>,
}
}
FieldTypeDescription
revStringThe winning revision string.
deletedOption<bool>Set to Some(true) for deleted documents. Omitted from JSON when None.

ChangesResponse

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct ChangesResponse {
    pub results: Vec<ChangeEvent>,
    pub last_seq: Seq,
}
}
FieldTypeDescription
resultsVec<ChangeEvent>The list of change events.
last_seqSeqThe sequence of the last change. Pass this as since for the next poll.

ChangeEvent

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct ChangeEvent {
    pub seq: Seq,
    pub id: String,
    pub changes: Vec<ChangeRev>,
    pub deleted: bool,
    pub doc: Option<serde_json::Value>,
    pub conflicts: Option<Vec<String>>,
}
}
FieldTypeDescription
seqSeqThe sequence number for this change.
idStringThe document ID.
changesVec<ChangeRev>List of changed revision strings.
deletedbooltrue if the document was deleted. Defaults to false.
docOption<serde_json::Value>Full document body, present only when include_docs is true.
conflictsOption<Vec<String>>Conflicting revisions, present when conflicts: true was requested.

ChangeRev

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct ChangeRev {
    pub rev: String,
}
}
FieldTypeDescription
revStringThe revision string for this change.

FindResponse

Result of a Mango find query (from rouchdb-query).

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct FindResponse {
    pub docs: Vec<serde_json::Value>,
}
}
FieldTypeDescription
docsVec<serde_json::Value>Matching documents as JSON values. Includes _id and _rev fields. If fields was specified in FindOptions, only the projected fields are present (plus _id).

ViewResult

Result of a map/reduce view query (from rouchdb-query).

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct ViewResult {
    pub total_rows: u64,
    pub offset: u64,
    pub rows: Vec<ViewRow>,
}
}
FieldTypeDescription
total_rowsu64Total number of rows emitted by the map function (before skip/limit).
offsetu64Local adapters: the skip applied (like PouchDB). CouchDB / HTTP adapter: the global position of the first row (0 for keys queries, where CouchDB sends null). See Differences from CouchDB.
rowsVec<ViewRow>The result rows.

ViewRow

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct ViewRow {
    pub id: Option<String>,
    pub key: serde_json::Value,
    pub value: serde_json::Value,
    pub doc: Option<serde_json::Value>,
}
}
FieldTypeDescription
idOption<String>The source document ID. None for reduce results.
keyserde_json::ValueThe emitted key.
valueserde_json::ValueThe emitted value (or reduced value).
docOption<serde_json::Value>Full document body, present only when include_docs is true.

Replication Types

BulkGetItem

A request to fetch a specific document (optionally at a specific revision).

#![allow(unused)]
fn main() {
pub struct BulkGetItem {
    pub id: String,
    pub rev: Option<String>,
}
}
FieldTypeDescription
idStringThe document ID to fetch.
revOption<String>Specific revision to fetch. If None, returns the winning revision.

BulkGetResponse

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct BulkGetResponse {
    pub results: Vec<BulkGetResult>,
}
}

BulkGetResult

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct BulkGetResult {
    pub id: String,
    pub docs: Vec<BulkGetDoc>,
}
}

BulkGetDoc

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct BulkGetDoc {
    pub ok: Option<serde_json::Value>,
    pub error: Option<BulkGetError>,
}
}
FieldTypeDescription
okOption<serde_json::Value>The document JSON if fetch succeeded.
errorOption<BulkGetError>Error details if fetch failed.

BulkGetError

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct BulkGetError {
    pub id: String,
    pub rev: String,
    pub error: String,
    pub reason: String,
}
}
FieldTypeDescription
idStringThe document ID that failed.
revStringThe revision that was requested.
errorStringError type (e.g., "not_found").
reasonStringHuman-readable error description.

RevsDiffResponse

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct RevsDiffResponse {
    pub results: HashMap<String, RevsDiffResult>,
}
}

The results field is flattened during serialization (#[serde(flatten)]), so it serializes as a flat JSON object keyed by document ID.

RevsDiffResult

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct RevsDiffResult {
    pub missing: Vec<String>,
    pub possible_ancestors: Vec<String>,
}
}
FieldTypeDescription
missingVec<String>Revisions the adapter does not have.
possible_ancestorsVec<String>Revisions the adapter does have that could be ancestors of the missing ones. Empty vec is omitted from JSON.

ReduceFn

Built-in reduce functions for map/reduce views (from rouchdb-query).

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub enum ReduceFn {
    Sum,
    Count,
    Stats,
    Custom(Box<dyn Fn(&[serde_json::Value], &[serde_json::Value], bool) -> serde_json::Value>),
}
}
VariantDescription
SumSum all numeric values.
CountCount the number of rows.
StatsCompute statistics: sum, count, min, max, sumsqr.
Custom(Fn)Custom reduce function. Arguments: (keys, values, rereduce).

Error Handling

RouchDB uses a single error enum, RouchError, for all error conditions across every crate in the workspace. All fallible functions return Result<T>, which is an alias for std::result::Result<T, RouchError>.

#![allow(unused)]
fn main() {
use rouchdb::{Result, RouchError};
}

The Result Type

#![allow(unused)]
fn main() {
pub type Result<T> = std::result::Result<T, RouchError>;
}

Every async method on Database and every Adapter trait method returns Result<T>. This makes error handling consistent throughout the entire API.


RouchError Variants

RouchError is #[non_exhaustive]: new kinds of errors may be added in minor releases, so a match on it needs a catch-all arm (Err(e) => ...), as in the examples below.

#![allow(unused)]
fn main() {
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RouchError {
    NotFound(String),
    Conflict,
    BadRequest(String),
    Unauthorized,
    Forbidden(String),
    InvalidRev(String),
    MissingId,
    DatabaseExists(String),
    DatabaseError(String),
    UpgradeRequired { path: std::path::PathBuf },
    Io(#[from] std::io::Error),
    Json(#[from] serde_json::Error),
}
}

Variant Reference

VariantDisplay FormatWhen It Occurs
NotFound(String)"not found: {0}"A document, revision, attachment, or local document does not exist. The string contains the ID or a descriptive message.
Conflict"conflict: document update conflict"An update or delete was attempted without the correct current _rev. The document has been modified since the revision you provided.
BadRequest(String)"bad request: {0}"The request is malformed. Examples: document body is not a JSON object, invalid query parameters, or invalid selector syntax.
Unauthorized"unauthorized"Authentication is required but not provided. Returned by the HTTP adapter when CouchDB responds with 401.
Forbidden(String)"forbidden: {0}"The authenticated user does not have permission for this operation. Returned by the HTTP adapter when CouchDB responds with 403.
InvalidRev(String)"invalid revision format: {0}"A revision string could not be parsed. Revisions must be in {pos}-{hash} format where pos is a positive integer (e.g., "3-abc123").
MissingId"missing document id"A document write was attempted without a document ID.
DatabaseExists(String)"database already exists: {0}"An attempt was made to create a database that already exists.
DatabaseError(String)"database error: {0}"A general database-level error (storage corruption, adapter failure, unexpected internal state).
UpgradeRequired { path }"{path} was written by rouchdb 0.4 or earlier and must be upgraded …"Database::open / RedbAdapter::open found a redb file written by rouchdb 0.4 or earlier. The file was not modified. Upgrade it once with rouchdb migrate <path>, RedbAdapter::upgrade or Database::open_with (see Migrating from 0.4 to 0.5).
Io(std::io::Error)"io error: {0}"An I/O error from the underlying storage layer (file system, network). Automatically converted from std::io::Error via #[from].
Json(serde_json::Error)"json error: {0}"A JSON serialization or deserialization error. Automatically converted from serde_json::Error via #[from].

Matching on Specific Errors

Use Rust’s match expression to handle different error conditions:

#![allow(unused)]
fn main() {
use rouchdb::{Database, RouchError};

async fn handle_get(db: &Database, id: &str) {
    match db.get(id).await {
        Ok(doc) => {
            println!("Found: {}", doc.data);
        }
        Err(RouchError::NotFound(_)) => {
            println!("Document {} does not exist", id);
        }
        Err(RouchError::Unauthorized) => {
            eprintln!("Authentication required");
        }
        Err(e) => {
            eprintln!("Unexpected error: {}", e);
        }
    }
}
}

Common Error Patterns

Conflict Resolution

The most common error in document databases is the update conflict. It occurs when you try to update a document but someone else has modified it since you last read it.

#![allow(unused)]
fn main() {
use rouchdb::{Database, RouchError};

async fn safe_update(db: &Database, id: &str) -> rouchdb::Result<()> {
    loop {
        // Read the current version
        let doc = db.get(id).await?;
        let rev = doc.rev.as_ref().unwrap().to_string();

        // Modify the data
        let mut data = doc.data.clone();
        data["counter"] = json!(data["counter"].as_i64().unwrap_or(0) + 1);

        // Attempt the update
        match db.update(id, &rev, data).await {
            Ok(result) => {
                println!("Updated to rev {}", result.rev.unwrap());
                return Ok(());
            }
            Err(RouchError::Conflict) => {
                // Someone else updated the doc -- retry with the new version
                println!("Conflict detected, retrying...");
                continue;
            }
            Err(e) => return Err(e),
        }
    }
}
}

Create-if-not-exists

#![allow(unused)]
fn main() {
use rouchdb::{Database, RouchError};

async fn ensure_doc(db: &Database, id: &str) -> rouchdb::Result<()> {
    match db.get(id).await {
        Ok(_) => {
            // Document already exists, nothing to do
            Ok(())
        }
        Err(RouchError::NotFound(_)) => {
            // Document does not exist, create it
            db.put(id, json!({"created_at": "2026-02-07"})).await?;
            Ok(())
        }
        Err(e) => Err(e),
    }
}
}

Handling Bulk Write Results

bulk_docs does not return an error for individual document failures. Instead, check each DocResult:

#![allow(unused)]
fn main() {
let results = db.bulk_docs(docs, BulkDocsOptions::new()).await?;

for result in &results {
    if result.ok {
        println!("Wrote {} at rev {}", result.id, result.rev.as_deref().unwrap_or("?"));
    } else {
        eprintln!(
            "Failed to write {}: {} - {}",
            result.id,
            result.error.as_deref().unwrap_or("unknown"),
            result.reason.as_deref().unwrap_or("no reason"),
        );
    }
}
}

Using the ? Operator

Since RouchError implements std::error::Error (via thiserror), it works seamlessly with the ? operator and with error types from other crates:

#![allow(unused)]
fn main() {
async fn process(db: &Database) -> rouchdb::Result<()> {
    let doc = db.get("config").await?;         // RouchError on failure
    let info = db.info().await?;               // RouchError on failure
    db.put("status", json!({"ok": true})).await?; // RouchError on failure
    Ok(())
}
}

Converting from External Errors

RouchError has automatic From implementations for common external error types:

Source TypeConverts To
std::io::ErrorRouchError::Io
serde_json::ErrorRouchError::Json

This means I/O and JSON errors from third-party code are automatically converted when using ?:

#![allow(unused)]
fn main() {
async fn read_and_store(db: &Database, path: &str) -> rouchdb::Result<()> {
    let content = std::fs::read_to_string(path)?;  // io::Error -> RouchError::Io
    let value: serde_json::Value = serde_json::from_str(&content)?;  // -> RouchError::Json
    db.put("imported", value).await?;
    Ok(())
}
}

Replication Error Handling

Replication errors are handled differently. The replicate function returns a ReplicationResult rather than failing outright for individual document errors:

#![allow(unused)]
fn main() {
let result = local.replicate_to(&remote).await?;

if result.ok {
    println!("Replication complete: {} docs written", result.docs_written);
} else {
    eprintln!("Replication completed with errors:");
    for err in &result.errors {
        eprintln!("  - {}", err);
    }
}
}

The top-level Result only returns Err for catastrophic failures (network down, source database unreachable). Individual document write failures are collected in result.errors, and result.ok is false if any errors occurred.


Display and Debug

RouchError implements both Display (for user-facing messages) and Debug (for developer diagnostics):

#![allow(unused)]
fn main() {
let err = RouchError::NotFound("doc123".into());

// Display: "not found: doc123"
println!("{}", err);

// Debug: NotFound("doc123")
println!("{:?}", err);
}

All variants produce clear, actionable error messages suitable for logging.

API Stability

RouchDB is pre-1.0, so a minor release (0.5 → 0.6) may still break the API. 0.5 is the last release in which adding a field to a struct or a variant to an enum is a breaking change: its public types are shaped so that the library can grow without breaking code that follows the rules below. The next goal is stabilizing the API and the on-disk (redb) format toward 1.0.

The rules apply to every published crate: rouchdb, rouchdb-core, rouchdb-adapter-memory, rouchdb-adapter-redb, rouchdb-adapter-http, rouchdb-changes, rouchdb-query, rouchdb-views and rouchdb-replication. The server and the CLI are applications, not libraries.

Enums that may grow: add a catch-all arm

These enums are #[non_exhaustive]: new variants may be added in a minor release, so a match on them needs a _ arm.

EnumWhy it may grow
RouchErrorNew error conditions (for example a storage format that needs an upgrade).
ChangesEventNew lifecycle events of a changes stream. Complete { last_seq, .. } may gain fields.
ReplicationEventNew progress events. Change { docs_read, .. } may gain fields.
ReplicationFilterCouchDB design-document filters, _view filters, …
ReduceFnMore CouchDB built-ins, such as _approx_count_distinct.
StaleOptionCouchDB’s newer update=true/false/lazy parameter.
SortFieldA typed {field, direction} form. Use SortField::try_field_and_direction instead of matching.
#![allow(unused)]
fn main() {
match db.get("doc").await {
    Ok(doc) => println!("{}", doc.data),
    Err(RouchError::NotFound(_)) => println!("no such document"),
    Err(e) => return Err(e), // every other error, including future ones
}
}

These enums are exhaustive on purpose, because their set of cases is closed and code must be able to handle every case without a catch-all:

EnumWhy it is closed
SeqA sequence is a JSON number (local adapters) or a JSON string (CouchDB) on the wire; code that compares or passes sequences back must handle both.
OpenRevsCouchDB’s open_revs is "all" or a list of revisions.
ChangesStyleCouchDB’s style is main_only or all_docs.
SortDirectionAscending or descending.
RevStatusA revision body is stored or it is not.
MergeResult, ReplicatedWrite, LocalWriteOutcomes of the shared write planner (rouchdb_core::merge / write). An adapter must handle each one to store a write correctly, so a new outcome must be a compile error in the adapter rather than a silently ignored _ arm.

Result types: read them, build them with constructors

Types the library returns are #[non_exhaustive]. Their fields stay public, so you read them as usual, but they cannot be built with a struct literal outside their crate. Code that has to build them (a custom Adapter, a plugin, a test double) uses their constructors:

TypeConstructors
DocResultDocResult::ok(id, rev), DocResult::error(id, error, reason)
DbInfoDbInfo::new(db_name, doc_count, doc_del_count, update_seq)
AllDocsResponseAllDocsResponse::new(total_rows, offset, rows), .with_update_seq(seq)
AllDocsRowAllDocsRow::document(id, value), .with_doc(doc), AllDocsRow::not_found(key)
AllDocsRowValueAllDocsRowValue::new(rev, deleted)
ChangesResponseChangesResponse::new(results, last_seq)
ChangeEventChangeEvent::new(seq, id, revs), .with_deleted(..), .with_doc(..), .with_conflicts(..)
ChangeRevChangeRev::new(rev)
RevsDiffResponse, RevsDiffResultRevsDiffResponse::new(map), RevsDiffResult::new(missing, possible_ancestors)
BulkGetResponse, BulkGetResultBulkGetResponse::new(results), BulkGetResult::new(id, docs)
BulkGetDoc, BulkGetErrorBulkGetDoc::ok(json), BulkGetDoc::error(err), BulkGetError::new(id, rev, error, reason)
PurgeResponsePurgeResponse::new(purge_seq, purged)
ChangeNoticeChangeNotice::new(seq, doc_id), ChangeNotice::reset()
DocMetadataDocMetadata::new(id, rev_tree, seq)
FindResponseFindResponse::new(docs)
IndexInfo, IndexFieldsIndexInfo::new(name, ddoc, fields), IndexFields::new(fields)
CreateIndexResponseCreateIndexResponse::new(result, name)
ExplainResponse, ExplainIndexExplainResponse::new(dbname, index, selector, fields), ExplainIndex::new(ddoc, name, type, fields)
BuiltIndexbuild_index(..), BuiltIndex::new(def)
EmittedRowEmittedRow::new(id, key, value)

These are #[non_exhaustive] without a public constructor because only the library builds them: PutResponse, RevInfo, ViewResult, ViewRow, ReplicationResult, PersistentViewIndex, Session, UserContext, PlannedWrite, LeafInfo.

See Building Results for a custom adapter.

Options and data: use ..Default::default()

Option structs are not #[non_exhaustive] (that would forbid the ..Default::default() syntax outside their crate). They implement Default; set the fields you need and fill the rest from the default:

#![allow(unused)]
fn main() {
let rows = db.all_docs(AllDocsOptions {
    include_docs: true,
    limit: Some(10),
    ..Default::default()
}).await?;
}

A literal that lists every field stops compiling when a field is added; adding a field with a default value is not considered a breaking change, so always end the literal with ..Default::default() (for every option struct that has a new(), default() is the same). This covers GetOptions, BulkDocsOptions, AllDocsOptions, ChangesOptions, GetAttachmentOptions, FindOptions, ViewQueryOptions, ChangesStreamOptions, ReplicationOptions, HttpAdapterOptions and IndexDefinition.

Data you both build and read follows the same rule, and has constructors where they read better:

TypeBuild it with
DocumentDocument::new(id, data), Document::from_json(json), or ..Default::default()
AttachmentMetaAttachmentMeta::new(content_type, bytes) or ..Default::default()
DesignDocument, ViewDefDesignDocument::new(name).with_view(..), ViewDef::new(map).with_reduce(..), or ..Default::default()
SecurityDocument, SecurityGroup..Default::default() (unknown members round-trip through extra)
BulkGetItemBulkGetItem::new(id).with_rev(rev) or ..Default::default()
RevisionRevision::new(pos, hash) or "1-abc".parse(); a revision id is {pos}-{hash} by definition, so this type will not grow.

Low-level modules

rouchdb_core::rev_tree, merge, write, collation and json are the building blocks of the bundled adapters. The revision tree types (RevPath, RevNode, NodeOpts) are plain data that adapters build and persist; they mirror the stored format and will be stabilized together with the on-disk format on the way to 1.0.

Traits

New Adapter and Plugin methods are added with a default implementation, so existing implementations keep compiling (as Adapter::subscribe and Adapter::id were in 0.5).

Differences from CouchDB

RouchDB follows CouchDB 3.5 (and PouchDB where the two disagree and RouchDB behaves like a local PouchDB database). The differences below are known and accepted; each is pinned by a test named accepted_divergence_*, so a change to one of them has to be deliberate.

Object key order

CouchDB keeps the members of a JSON object in the order they were written, and that order is significant: objects collate key by key in document order, and Mango compares them the same way. RouchDB uses serde_json without its preserve_order feature, so every object keeps its keys sorted:

CouchDB 3.5.1RouchDB
View keys {"b":2,"a":1} and {"b":1}{"b":1} first{"a":1,"b":2} first
{"m": {"$eq": {"a": 2, "b": 1}}} on {"m": {"b": 1, "a": 2}}no matchmatch
A document read backmembers as writtenmembers sorted
Mango index map.fields of a design documentwritten ordersorted (the options.def.fields array keeps the order)
Revision hashover the document as writtenover the sorted document: key order never changes a revision id

Enabling preserve_order would change every serde_json map of the whole dependency graph (and so every revision hash), which is why it is not done. Revision ids are opaque in any case: the same edit gets different ids on CouchDB and on RouchDB.

Pinned by accepted_divergence_objects_collate_in_sorted_key_order (rouchdb-core collation), accepted_divergence_object_key_order_is_not_significant (rouchdb-query Mango) and accepted_divergence_rev_hash_ignores_key_order (rouchdb-core document).

all_docs offset of the local adapters

AllDocsResponse::offset is, for the memory and redb adapters, the skip that was applied, as in PouchDB’s local adapters. CouchDB (and so the HTTP adapter) reports the global position of the first returned row, including the rows before start_key: with a .. e stored, startkey="c"&skip=1 returns d, e with offset: 3 on CouchDB and 1 locally. Knowing the global position needs a counted index that the local stores do not keep.

For a keys query CouchDB sends "offset": null: the HTTP adapter reads it as 0, the local adapters still report skip, and rouchdb-server sends null like CouchDB.

Pinned by accepted_divergence_all_docs_offset_is_the_skip (memory and redb, adapter_conformance.rs) and offset_is_the_global_position_on_couchdb (CouchDB, all_docs.rs). Views (query_view) report CouchDB’s offset.

Numbers

By default, numbers are serde_json numbers: integers that fit i64 or u64 are exact, other numbers are f64 (18446744073709551616 becomes 1.8446744073709552e19, 1.50 becomes 1.5). CouchDB keeps integers of any size exactly and rounds decimals to doubles.

With the opt-in arbitrary-precision feature every number keeps the text it was written with, through storage, replication and the HTTP adapter: big integers round-trip with CouchDB, and decimals are kept more precisely than by CouchDB, which rewrites them as doubles (1.50 as 1.5, -0.0 as 0.0). It turns on serde_json’s arbitrary_precision for the whole build: numbers then compare by their text (1.0 is not 1.00), and revision ids of documents holding such numbers differ from those computed without the feature. See Installation; pinned by big_numbers.rs.

Attachment storage

CouchDB gzip-compresses attachments of compressible types (text/*, JSON, JavaScript, XML) and their digest is the MD5 of the compressed bytes; RouchDB stores the bytes as they are and its digest is their MD5. The same text attachment therefore has different digests on the two sides (binary types are never compressed and match). The encoding and encoded_length members CouchDB reports (with att_encoding_info=true) are kept on stubs that carry them, but RouchDB always serves decoded bytes.

Architecture Overview

RouchDB is a local-first document database for Rust that implements the CouchDB replication protocol. It is the Rust equivalent of PouchDB: applications store data locally (in-memory, on disk, or against a remote CouchDB) and synchronize between replicas using the same protocol that CouchDB uses natively.

Design Philosophy

Local-first. Reads and writes hit local storage. The network is only involved when you explicitly replicate.

Adapter pattern. Every storage backend implements a single Adapter trait. Application code does not depend on a concrete backend – you can swap in-memory storage for disk-backed storage for a remote CouchDB endpoint without changing a line of business logic.

CouchDB compatibility. Revision trees, the winning-rev algorithm, the _changes feed, _revs_diff, _bulk_get, new_edits=false – all of the machinery required by the CouchDB replication protocol is present and matches CouchDB/PouchDB semantics exactly.

serde_json::Value as the document model. Documents are dynamic JSON. The crate does not impose any schema; users may layer typed Rust structs on top via Serde when desired.

Crate Dependency Graph

                      +------------------+
                      |     rouchdb      |  (umbrella: re-exports everything)
                      +------------------+
                     /  |    |    |    \   \
                    /   |    |    |     \   \
          +-----------+ +--------+ +----------+ +-----------+ +--------+
          | adapter-  | | changes| | replica- | |   query   | | views  |
          |   redb    | |        | |   tion   | |           | |        |
          +-----------+ +--------+ +----------+ +-----------+ +--------+
          |  adapter- |      |        |   \         |           |
          |  memory   |      |        |    +-----+  |           |
          +-----------+      |        |          |  |           |
          |  adapter- |      |        |          |  |           |
          |   http    |      |        |          |  |           |
          +-----------+      |        |          |  |           |
                \            |       /          /  /           /
                 \           |      /          /  /           /
                  +----------+-----+----------+--+-----------+
                  |               rouchdb-core                 |
                  +--------------------------------------------+

All arrows point downward. Every crate ultimately depends on rouchdb-core. The rouchdb umbrella crate depends on all other eight crates and re-exports their public APIs.

The Crates

1. rouchdb-core

The foundation. Contains:

  • Adapter trait – the async interface every storage backend implements (info, get, bulk_docs, changes, revs_diff, bulk_get, put_local/get_local, compact, destroy).
  • Document types – Document, Revision, Seq, and all the option/response structs (GetOptions, BulkDocsOptions, ChangesOptions, AllDocsOptions, etc.).
  • Revision tree (rev_tree.rs) – RevTree, RevPath, RevNode, traversal helpers, leaf collection, ancestry lookup.
  • Merge algorithm (merge.rs) – merge incoming revision paths, determine the winning rev, collect conflicts, stem old revisions.
  • Collation (collation.rs) – CouchDB-compatible comparison and lexicographic encoding of JSON values for ordered storage.
  • Error types – RouchError enum with thiserror.

External dependencies: async-trait, base64, serde, serde_json, thiserror.

2. rouchdb-adapter-memory

An in-memory Adapter implementation. All state lives in Arc<RwLock<...>> behind a Tokio read-write lock. Primarily used for testing and as a reference implementation.

Dependencies: rouchdb-core, tokio, md-5, uuid, base64.

3. rouchdb-adapter-redb

Persistent local storage backed by redb, a pure-Rust embedded key-value store with ACID transactions. Six tables store document metadata, revision data, the changes log, local documents, attachments, and global metadata.

Dependencies: rouchdb-core, redb, tokio, serde, serde_json, md-5, uuid.

See Storage Layer for full table schema documentation.

4. rouchdb-adapter-http

A remote Adapter that talks to a CouchDB (or CouchDB-compatible) server over HTTP using reqwest. Translates each Adapter method into the corresponding CouchDB REST endpoint.

Dependencies: rouchdb-core, reqwest (with json and cookies features), percent-encoding, serde, serde_json.

5. rouchdb-changes

Implements the live/continuous changes feed. Wraps an Adapter’s one-shot changes() call in a Tokio stream that polls for new changes, emitting ChangeEvent items as they arrive.

Dependencies: rouchdb-core, tokio, tokio-util, serde_json.

6. rouchdb-replication

Implements the CouchDB replication protocol: reading and writing checkpoints, computing revision diffs, fetching missing documents, and writing them to the target with new_edits=false.

Dependencies: rouchdb-core, rouchdb-query, tokio, tokio-util, serde, serde_json, md-5, uuid.

See Replication Protocol for a step-by-step walkthrough.

7. rouchdb-query

Mango selectors ($eq, $gt, $in, $regex, etc.) and map/reduce view support. Evaluates selectors against serde_json::Value documents using CouchDB collation order.

Dependencies: rouchdb-core, fancy-regex, serde, serde_json.

8. rouchdb-views

Design documents and the persistent view engine. Stores DesignDocument structs (with views, filters, validate_doc_update) and provides ViewEngine for incrementally-updated Rust-native map/reduce indexes.

Dependencies: rouchdb-core, serde, serde_json.

9. rouchdb (umbrella)

The crate users add to their Cargo.toml. Re-exports types from all eight inner crates so consumers do not need to depend on individual sub-crates.

Dependencies: all of the above, plus serde_json, uuid, async-trait.

10. rouchdb-server

A CouchDB-compatible HTTP server built on Axum. Wraps an Arc<Database> and exposes REST endpoints that Fauxton, PouchDB, or any CouchDB client can connect to. Serves the Fauxton web dashboard via rust-embed for embedded static files.

Dependencies: rouchdb, rouchdb-core, axum, tower-http, rust-embed, clap, serde, serde_json, tokio, uuid, md-5.

11. rouchdb-cli

A command-line tool for inspecting and querying redb database files. Provides subcommands for info, get, all-docs, find, changes, dump, replicate, and compact.

Dependencies: rouchdb, clap, serde_json, tokio.

Data Flow: Writing a Document

Application
    |
    v
rouchdb::Database::put(doc)
    |
    v
Adapter::bulk_docs(docs, BulkDocsOptions { new_edits: true })
    |
    |-- generate revision hash (MD5 of prev_rev + deleted flag + JSON body)
    |-- load existing DocRecord from storage
    |-- build new RevPath from [new_hash, prev_hash]
    |-- merge_tree(existing_tree, new_path, rev_limit)
    |     |
    |     |-- try_merge_path: find overlap, graft new nodes
    |     |-- stem: prune revisions beyond rev_limit
    |     '-- winning_rev: determine deterministic winner
    |
    |-- write DocRecord (rev tree + seq) to DOC_TABLE
    |-- write RevDataRecord (JSON body) to REV_DATA_TABLE
    |-- write ChangeRecord to CHANGES_TABLE
    '-- increment update_seq in META_TABLE

Data Flow: Replication

Source Adapter                         Target Adapter
      |                                      |
      |--- 1. read checkpoint (both) --------|
      |                                      |
      |--- 2. source.changes(since) -------->|
      |                                      |
      |<-- 3. target.revs_diff(revs) --------|
      |                                      |
      |--- 4. source.bulk_get(missing) ----->|
      |                                      |
      |--- 5. target.bulk_docs(new_edits=false) ->|
      |                                      |
      |--- 6. write checkpoint (both) -------|
      |                                      |
      v          (repeat per batch)          v

Both source and target are dyn Adapter. This means replication works between any combination of backends: memory-to-disk, disk-to-CouchDB, CouchDB-to-memory, and so on.

Revision Trees

CouchDB’s multi-version concurrency control (MVCC) model stores the entire revision history of a document as a tree. Every edit creates a new leaf node. Concurrent edits from different replicas create branches (conflicts). A deterministic algorithm picks the “winning” revision so that every replica independently converges on the same answer.

RouchDB implements this model in rouchdb-core, split across two files:

  • rev_tree.rs – data structures and traversal helpers
  • merge.rs – merging incoming revisions, winning-rev selection, conflict collection, and stemming

Data Structures

RevNode

A single node in the revision tree.

#![allow(unused)]
fn main() {
pub struct RevNode {
    pub hash: String,          // the hash portion of "pos-hash"
    pub status: RevStatus,     // Available | Missing
    pub opts: NodeOpts,        // metadata flags (e.g. deleted)
    pub children: Vec<RevNode>,// child revisions
}
}
  • hash – the hex MD5 digest that, together with pos, forms the full revision ID (e.g., "3-a1b2c3...").
  • status – Available means the full document body for this revision is still stored; Missing means only the hash remains (after compaction or stemming).
  • opts.deleted – whether this revision marks a deletion (_deleted: true).
  • children – zero children means this is a leaf; two or more children means a conflict branch point.

RevPath

A rooted sub-tree within the revision forest.

#![allow(unused)]
fn main() {
pub struct RevPath {
    pub pos: u64,        // generation number of the root node
    pub tree: RevNode,   // the root node of this sub-tree
}
}

pos is the generation number (the integer before the dash in "3-abc"). If a tree has been stemmed and its earliest stored revision is generation 5, then pos = 5.

RevTree

#![allow(unused)]
fn main() {
pub type RevTree = Vec<RevPath>;
}

A document’s full revision history. Most documents have a single RevPath entry. Multiple entries appear when stemming creates disjoint sub-trees that are later reconnected during replication.

LeafInfo

#![allow(unused)]
fn main() {
pub struct LeafInfo {
    pub pos: u64,
    pub hash: String,
    pub deleted: bool,
    pub status: RevStatus,
}
}

A lightweight snapshot of a leaf node’s identity and state, used by collect_leaves and the winning-rev algorithm.

Tree Structure: ASCII Examples

Linear History (No Conflicts)

  1-aaa --> 2-bbb --> 3-ccc

Represented as:

RevTree = [
    RevPath {
        pos: 1,
        tree: RevNode("aaa", children: [
            RevNode("bbb", children: [
                RevNode("ccc", children: [])   // <-- leaf
            ])
        ])
    }
]

One root at position 1, one leaf at position 3.

Conflict (Two Branches)

Two replicas independently edit generation 1, producing two conflicting generation-2 revisions:

            +-- 2-bbb   (branch A)
            |
  1-aaa ----+
            |
            +-- 2-ccc   (branch B)
RevTree = [
    RevPath {
        pos: 1,
        tree: RevNode("aaa", children: [
            RevNode("bbb", children: []),   // leaf A
            RevNode("ccc", children: []),   // leaf B
        ])
    }
]

Both 2-bbb and 2-ccc are leaves. The one with the lexicographically greater hash ("ccc" > "bbb") wins.

Divergent History After Stemming

After aggressive stemming, a document might have two disjoint sub-trees:

  3-ddd --> 4-eee --> 5-fff       (sub-tree A)

  3-ggg --> 4-hhh                 (sub-tree B, re-introduced by replication)
RevTree = [
    RevPath { pos: 3, tree: RevNode("ddd", ...) },
    RevPath { pos: 3, tree: RevNode("ggg", ...) },
]

Two roots, each with their own pos. This is normal and handled correctly by all traversal and merge operations.

Traversal Helpers

traverse_rev_tree

Depth-first traversal of every node in the forest. The callback receives (absolute_pos, &RevNode, root_pos) so it always knows each node’s full generation number.

collect_leaves

Returns all leaf nodes (nodes with no children), sorted by the winning-rev ordering:

  1. Non-deleted before deleted
  2. Higher generation first
  3. Lexicographically greater hash first

This means collect_leaves(&tree)[0] is always the winner.

root_to_leaf

Decomposes the tree into every root-to-leaf path. Used during replication to provide _revisions ancestry data.

rev_exists

Checks whether a specific (pos, hash) pair exists anywhere in the tree. Used by revs_diff to determine which revisions the target is missing.

find_rev_ancestry

Given a target (pos, hash), walks the tree and returns the chain of hashes from the target back to the root: [target_hash, parent_hash, grandparent_hash, ...]. This is the data that populates the _revisions field in bulk_get responses.

build_path_from_revs

Constructs a single-path RevPath from an array of revision hashes. The input is newest-first (leaf-first): [newest_hash, ..., oldest_hash]. The leaf gets RevStatus::Available; all ancestors get RevStatus::Missing. This is how incoming revisions from replication are turned into a structure that can be merged.

The Merge Algorithm

When a new revision arrives (either from a local write or from replication), it must be merged into the existing tree. The entry point is merge_tree:

#![allow(unused)]
fn main() {
pub fn merge_tree(
    tree: &RevTree,
    new_path: &RevPath,
    rev_limit: u64,
) -> (RevTree, MergeResult)
}

MergeResult is one of:

VariantMeaning
NewLeafThe new path extended an existing branch (normal edit)
NewBranchThe new path created a new branch (conflict)
InternalNodeThe new path’s leaf already existed (duplicate / no-op)

Step-by-step walkthrough

1. Try each existing root. For each RevPath in the tree, call try_merge_path to see if the new path overlaps with it.

2. Find the overlap. find_overlap flattens the new path into a linear chain of (pos, hash) pairs, then searches the existing tree for any matching node. If found, it records the navigation path (a Vec<usize> of child indices) and the remaining new nodes that need to be grafted.

Existing:     1-aaa --> 2-bbb
New path:     2-bbb --> 3-ccc

Overlap at:   2-bbb
Remainder:    [RevNode("ccc")]

3. Graft the remainder. graft_nodes navigates to the overlap node and appends the remainder as children.

After graft:  1-aaa --> 2-bbb --> 3-ccc

If the overlap node already had a different child, the new node becomes an additional child – creating a conflict branch:

Before:       1-aaa --> 2-bbb
New path:     1-aaa --> 2-ccc

After:        1-aaa --+--> 2-bbb
                      |
                      +--> 2-ccc    (new branch = conflict)

4. No overlap: new root. If no existing root shares any node with the new path, the new path is pushed onto the RevTree as a separate root. This happens when stemmed trees diverge during offline replication.

5. Stemming. After the merge, stem is called with the configured rev_limit (default 1000). Stemming walks from the root toward the leaves and removes nodes once the tree exceeds the depth limit.

Example: Full Merge Sequence

Starting state (empty tree):

[]

Write doc1 – local write generates 1-a1b2:

merge([], RevPath{pos:1, tree: RevNode("a1b2")})
  -> [RevPath{pos:1, tree: RevNode("a1b2")}]
  -> MergeResult::NewBranch   (first revision always creates a new root)

Update doc1 with _rev: "1-a1b2" – generates 2-c3d4:

merge(tree, RevPath{pos:1, tree: RevNode("a1b2" -> "c3d4")})
  -> overlap at 1-a1b2, graft "c3d4" as child
  -> 1-a1b2 --> 2-c3d4
  -> MergeResult::NewLeaf

Meanwhile replica B writes doc1 independently, producing 2-e5f6. Replication delivers it:

merge(tree, RevPath{pos:1, tree: RevNode("a1b2" -> "e5f6")})
  -> overlap at 1-a1b2, graft "e5f6" as second child
  -> 1-a1b2 --+--> 2-c3d4
              |
              +--> 2-e5f6
  -> MergeResult::NewBranch

Winning Revision Algorithm

#![allow(unused)]
fn main() {
pub fn winning_rev(tree: &RevTree) -> Option<Revision>
}

The algorithm is completely deterministic – every replica arrives at the same answer without communication:

  1. Non-deleted beats deleted. A leaf that is not marked _deleted: true always wins over a leaf that is deleted, regardless of generation or hash.

  2. Higher generation wins. Among leaves with the same deleted status, the one with the higher pos (generation number) wins.

  3. Lexicographic hash breaks ties. If two leaves have the same pos and same deleted status, the one with the lexicographically greater hash wins.

Implementation-wise, winning_rev calls collect_leaves (which sorts by exactly this ordering) and returns the first element.

Examples

1-aaa --+--> 2-bbb
        |
        +--> 2-ccc

Winner: 2-ccc   (same pos, "ccc" > "bbb")
1-aaa --+--> 2-bbb --> 3-ddd
        |
        +--> 2-ccc

Winner: 3-ddd   (pos 3 > pos 2)
1-aaa --+--> 2-bbb           (not deleted)
        |
        +--> 2-zzz [deleted]

Winner: 2-bbb   (non-deleted beats deleted, even though "zzz" > "bbb")

Collecting Conflicts

#![allow(unused)]
fn main() {
pub fn collect_conflicts(tree: &RevTree) -> Vec<Revision>
}

Returns all non-winning, non-deleted leaf revisions. These are the revisions that a user or application must resolve. The function calls collect_leaves, skips the first entry (the winner), and filters out deleted leaves.

Stemming (Pruning)

#![allow(unused)]
fn main() {
pub fn stem(tree: &mut RevTree, depth: u64) -> Vec<String>
}

Over time, revision trees grow indefinitely. Stemming prunes ancestor nodes that are more than depth generations away from any leaf.

The algorithm:

  1. For each RevPath, compute the maximum depth from root to any leaf.
  2. If the depth exceeds the limit, remove nodes from the root until the deepest path has at most depth nodes.
  3. When the root is removed, the root’s single child becomes the new root and pos is incremented by 1.
  4. Stemming stops at branch points – you cannot remove a node that has multiple children, because that would disconnect branches.
  5. Any RevPath that becomes empty is removed from the tree.
Before (depth limit = 3):

  1-aaa --> 2-bbb --> 3-ccc --> 4-ddd --> 5-eee

After:

  3-ccc --> 4-ddd --> 5-eee
  (pos adjusted from 1 to 3; "aaa" and "bbb" are returned as stemmed)

The default rev_limit of the memory and redb adapters is 1000, which matches CouchDB’s default. PouchDB defaults to 1000 as well; like PouchDB’s revs_limit option it can be changed per handle with MemoryAdapter::with_rev_limit / RedbAdapter::with_rev_limit (0 means no limit). As in CouchDB, a stemmed revision no longer exists: its stored body is dropped by the write that stems it, and reading it is not_found.

Revision Hash Generation

When new_edits=true (normal local write), the adapter generates revision hashes deterministically:

#![allow(unused)]
fn main() {
fn generate_rev_hash(
    doc_data: &serde_json::Value,
    deleted: bool,
    prev_rev: Option<&str>,
) -> String
}

The hash is MD5 over: previous_rev_string + ("1" if deleted else "0") + JSON-serialized body.

This means the same edit on the same predecessor always produces the same revision ID, which is important for idempotency.

When new_edits=false (replication mode), the adapter accepts the revision ID as-is from the source and merges it into the tree without generating a new hash.

CouchDB Collation

CouchDB defines a specific ordering for JSON values that differs from naive lexicographic comparison of serialized JSON strings. This ordering is inherited from Erlang’s term comparison and is used everywhere keys are compared: view indexes, _all_docs key ranges, Mango query evaluation, and internal storage engine key encoding.

RouchDB implements this ordering in rouchdb-core/src/collation.rs.

Type Ordering

Different JSON types sort in this fixed order, from lowest to highest:

null  <  boolean  <  number  <  string  <  array  <  object

A null value is always less than false, which is always less than -1000, which is always less than "", and so on. The type boundary is absolute – there is no number large enough to sort after even the empty string.

Internally, each type is assigned a numeric rank:

JSON TypeRank
null1
boolean2
number3
string4
array5
object6

When two values have different ranks, the comparison is immediate. Same-rank values are compared with type-specific rules described below.

Within-Type Comparison Rules

Null

All nulls are equal.

Boolean

false < true.

Number

Compared by exact numeric value: integers beyond 2^53 are not rounded, an integer and a float are compared exactly, and -0.0 equals 0. -100 < -1 < 0 < 1 < 1.5 < 2.

String

Compared by UTF-16 code units, like PouchDB (JavaScript). "a" < "aa" < "b", and "B" < "a". CouchDB itself uses ICU collation for strings ("a" < "B"), which RouchDB does not implement.

Array

Element-by-element using recursive collate. If all shared elements are equal, the shorter array sorts first.

[]       < [1]
[1]      < [2]
[1]      < [1, 2]
[1, "a"] < [1, "b"]

Object

Compared key-by-key in map order. For each key pair, the key strings are compared; if equal, the values are compared recursively. If all shared key-value pairs are equal, the object with fewer keys sorts first. CouchDB uses the document’s key order; serde_json keeps keys sorted, so RouchDB compares them in sorted order (an accepted difference, see Differences from CouchDB).

{}           < {"a": 1}
{"a": 1}     < {"a": 2}
{"a": 1}     < {"b": 1}
{"a": 1}     < {"a": 1, "b": 2}

The collate Function

#![allow(unused)]
fn main() {
pub fn collate(a: &Value, b: &Value) -> Ordering
}

This is the primary comparison entry point. It first compares type ranks, then delegates to type-specific comparison. It can be used anywhere you need CouchDB-compatible ordering of arbitrary JSON values.

Usage Examples

#![allow(unused)]
fn main() {
use serde_json::json;
use rouchdb_core::collation::collate;
use std::cmp::Ordering;

// Cross-type: null < number
assert_eq!(collate(&json!(null), &json!(42)), Ordering::Less);

// Cross-type: number < string
assert_eq!(collate(&json!(9999), &json!("")), Ordering::Less);

// Same type: numeric comparison
assert_eq!(collate(&json!(-1), &json!(0)), Ordering::Less);

// Same type: array element-by-element
assert_eq!(collate(&json!([1, 2]), &json!([1, 3])), Ordering::Less);
}

Indexable String Encoding

Storage engines like redb store keys as byte arrays and compare them lexicographically. To preserve CouchDB collation order in a byte-ordered key-value store, JSON values must be encoded into strings that sort lexicographically in the same order as collate.

#![allow(unused)]
fn main() {
pub fn to_indexable_string(v: &Value) -> String
}

Encoding Scheme

Each value is encoded with a type-prefix character that preserves the cross-type ordering:

TypePrefixEncoding
Null1Just the prefix character
Boolean22F for false, 2T for true
Number33 + encoded number (see below)
String44 + the string, remapped so byte order is UTF-16 order
Array55 + encoded elements
Object66 + encoded keys and values

Every encoded value ends with a \0 terminator (\0, \1 and \2 inside strings are escaped), so an array or object that is a prefix of another sorts first.

Because the prefix characters are 1 through 6, the inter-type ordering is automatically correct: any null-encoded string ("1...") sorts before any boolean-encoded string ("2..."), and so on.

Number Encoding

Numbers require special treatment because naive string representations do not sort correctly ("9" > "10" lexicographically). A number is written as its significant decimal digits d.ddd and a decimal exponent, exactly (integers use all their digits; other floats use the shortest representation that round-trips):

Zero (including -0.0): "1".

Positive numbers: "2", the exponent plus 500 in 3 digits, then the significant digits.

Negative numbers: "0", 500 minus the exponent in 3 digits, then the nines’ complement of the significant digits, then ":" (which sorts after every digit), so that numbers closer to zero sort later.

-100      ->  "3" + "0" + "498" + "8:"   (sorts first)
-1        ->  "3" + "0" + "500" + "8:"
 0        ->  "3" + "1"
 1        ->  "3" + "2" + "500" + "1"
 1.5      ->  "3" + "2" + "500" + "15"
 100      ->  "3" + "2" + "502" + "1"    (sorts last)

(each followed by the \0 terminator).

Array and Object Encoding

Arrays encode each element recursively. Because every element ends with the \0 terminator and \0 is the lowest byte value, a shorter array with a matching prefix always sorts before a longer one (and [[1], 2] differs from [[1, 2]]).

Objects encode alternating keys and values in map order.

Why This Matters

Views

Map/reduce views emit keys that are stored in sorted order. The storage engine must compare these keys in CouchDB collation order. By encoding them with to_indexable_string, ordinary byte-level comparison produces the correct ordering.

_all_docs Key Ranges

The startkey/endkey parameters on _all_docs use CouchDB collation. The adapter encodes the boundary values and performs byte-range scans.

Mango Queries

Mango $gt, $gte, $lt, $lte operators compare values according to CouchDB collation. The rouchdb-query crate uses collate for these comparisons.

Replication Correctness

If two replicas sort the same view index differently, they will produce different results for the same query. CouchDB collation ensures all replicas agree on ordering.

Verifying Correctness

The test suite confirms that comparing to_indexable_string encodings gives the same result as collate for every pair of a mixed corpus (large integers, floats, -0.0, control and supplementary characters, nested arrays and objects), and that it preserves ordering across the full type spectrum:

#![allow(unused)]
fn main() {
let values = vec![
    json!(null), json!(false), json!(true),
    json!(0), json!(1), json!(100),
    json!("a"), json!("b"),
    json!([]), json!({}),
];
let encoded: Vec<String> = values.iter().map(to_indexable_string).collect();

for i in 0..encoded.len() {
    for j in (i + 1)..encoded.len() {
        assert!(encoded[i] < encoded[j]);
    }
}
}

The negative-number tests further verify that -100 < -1 < 0 is preserved in the encoded representation.

Replication Protocol

RouchDB implements the CouchDB replication protocol, enabling bidirectional synchronization between any two Adapter implementations. The protocol is defined in rouchdb-replication/src/protocol.rs (the replication loop) and rouchdb-replication/src/checkpoint.rs (checkpoint management).

Because both source and target are dyn Adapter, replication works between any combination of backends: memory-to-memory, redb-to-CouchDB, CouchDB-to-memory, and so on.

Overview

The replication protocol is a batched, checkpoint-based, one-directional data transfer. To achieve bidirectional sync, you run two replications in opposite directions.

Sequence Diagram

 Source                  Replicator                    Target
   |                        |                            |
   |                   1. Generate replication ID        |
   |                        |                            |
   |<-- get_local(rep_id) --|-- get_local(rep_id) ------>|
   |--- checkpoint_doc ---->|<--- checkpoint_doc --------|
   |                        |                            |
   |                   2. Compare checkpoints            |
   |                      (find `since` seq)             |
   |                        |                            |
   |                   === batch loop ===                |
   |                        |                            |
   |<-- changes(since,      |                            |
   |        limit=batch) ---|                            |
   |--- change_events ----->|                            |
   |                        |                            |
   |                        |-- revs_diff(revs) -------->|
   |                        |<--- missing revs ----------|
   |                        |                            |
   |<-- bulk_get(missing) --|                            |
   |--- docs + _revisions ->|                            |
   |                        |                            |
   |                        |-- bulk_docs(docs,          |
   |                        |     new_edits=false) ----->|
   |                        |<--- write results ---------|
   |                        |                            |
   |<-- put_local(cp) ------|---- put_local(cp) -------->|
   |                        |                            |
   |                   === end batch loop ===            |
   |                        |                            |
   |                   Return ReplicationResult          |

The Seven Steps

Step 1: Generate Replication ID

Before anything else, the replicator generates a deterministic replication identifier by hashing the source and target database names with MD5:

#![allow(unused)]
fn main() {
fn generate_replication_id(source_id: &str, target_id: &str) -> String {
    let mut hasher = Md5::new();
    hasher.update(source_id.as_bytes());
    hasher.update(target_id.as_bytes());
    format!("{:x}", hasher.finalize())
}
}

The inputs are the peers’ identities (Adapter::id(), not their names) and a fingerprint of the filter. This ID is used as the key for checkpoint documents on both sides. The same source+target pair always produces the same ID, so a replication that is stopped and restarted will find its previous checkpoint.

When both peers report the same identity, the two directions (and any other such pair) would share one ID, and a pass could resume from a checkpoint another replication wrote, skipping documents. Checkpoints are then neither read nor written: every run scans the whole changes feed, and the result lists a warning (ReplicationResult::warnings, and a ReplicationEvent::Warning when events are streamed).

A random session_id (UUID v4) is also generated per replication run to detect stale checkpoints.

Step 2: Read Checkpoints

The replicator reads checkpoint documents from both source and target using get_local(replication_id). Checkpoint documents are stored as local documents (_local/{replication_id}), which are not replicated themselves.

The checkpoint document structure:

{
  "last_seq": 42,
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "version": 1,
  "replicator": "rouchdb",
  "history": [
    { "last_seq": 42, "session_id": "550e8400-..." },
    { "last_seq": 30, "session_id": "previous-session-..." }
  ]
}

The compare_checkpoints function finds the last agreed-upon sequence:

  1. Same session: If source and target have the same session_id, take the minimum of their two last_seq values. This handles the case where one side’s checkpoint write succeeded but the other’s failed.

  2. Different sessions: Walk through the history arrays looking for a common session_id. When found, use the minimum last_seq from that shared session.

  3. No common session: Return Seq::zero() (start from the beginning).

If either checkpoint read fails (e.g., first replication, or checkpoint was deleted), the replicator starts from sequence 0.

Step 3: Fetch Changes

The replicator calls source.changes() starting from the checkpoint sequence:

#![allow(unused)]
fn main() {
let changes = source.changes(ChangesOptions {
    since: current_seq,
    limit: Some(opts.batch_size),  // default: 100
    include_docs: false,
    ..Default::default()
}).await?;
}

The changes response contains a list of ChangeEvent items, each with a document ID and its current leaf revision(s). The last_seq field marks the high-water mark for this batch.

If the response is empty, replication is complete.

Step 4: Compute revs_diff

The replicator builds a map of doc_id -> [rev_strings] from the changes and sends it to the target:

#![allow(unused)]
fn main() {
let diff = target.revs_diff(rev_map).await?;
}

The target checks its own revision trees and returns only the revisions it is missing. This avoids transferring documents the target already has. The response also includes possible_ancestors – revisions the target does have that are ancestors of the missing ones, which helps the source minimize the revision history it sends.

If the diff is empty (target has everything), the batch is skipped and the replicator advances to the next batch.

Step 5: Fetch Missing Documents with bulk_get

For each missing revision, the replicator constructs a BulkGetItem and fetches the documents from the source:

#![allow(unused)]
fn main() {
let bulk_get_response = source.bulk_get(bulk_get_items).await?;
}

The source returns full document bodies along with _revisions metadata – the chain of revision hashes from the requested revision back to the root. This ancestry data is critical: it allows the target to reconstruct the revision tree and merge it correctly.

A typical bulk_get response for one document looks like:

{
  "_id": "doc1",
  "_rev": "3-ccc",
  "name": "Alice",
  "_revisions": {
    "start": 3,
    "ids": ["ccc", "bbb", "aaa"]
  }
}

Step 6: Write to Target with new_edits=false

The fetched documents are written to the target using the replication mode of bulk_docs:

#![allow(unused)]
fn main() {
let write_results = target.bulk_docs(
    docs_to_write,
    BulkDocsOptions::replication(),  // new_edits: false
).await?;
}

When new_edits=false:

  • The target does not generate new revision IDs. It accepts the revision IDs from the source as-is.
  • The target does not check for conflicts in the traditional sense. Instead, it uses the _revisions ancestry to reconstruct a RevPath and merges it into the existing revision tree using merge_tree.
  • If the revision already exists (was previously replicated), the merge returns MergeResult::InternalNode and the write is a no-op.
  • If the revision extends an existing branch, the merge returns MergeResult::NewLeaf.
  • If the revision creates a new branch (concurrent edit on a different replica), the merge returns MergeResult::NewBranch, creating a conflict that users can resolve later.

Step 7: Save Checkpoint

After each batch is successfully written, the replicator saves a checkpoint document to both source and target:

#![allow(unused)]
fn main() {
checkpointer.write_checkpoint(source, target, current_seq).await;
}

The checkpoint is written to both sides so that either side can resume even if the other is unavailable. If one write fails but the other succeeds, the replication can still resume (the compare_checkpoints function handles asymmetric checkpoints gracefully).

Checkpoint writes are fire-and-forget within a batch – the replicator only fails if both writes fail.

Batching

The replication loop processes changes in batches of batch_size (default 100). This provides:

  • Progress tracking: Checkpoints are saved after each batch, so a replication that is interrupted can resume from the last completed batch rather than starting over.
  • Memory management: Only one batch worth of documents is held in memory at a time.
  • Incremental progress: The ReplicationResult tracks docs_read and docs_written across all batches.

The loop terminates when a changes response returns fewer results than batch_size, indicating all changes have been consumed.

Configuration

#![allow(unused)]
fn main() {
pub struct ReplicationOptions {
    pub batch_size: u64,                   // default: 100
    pub batches_limit: u64,                // default: 10
    pub filter: Option<ReplicationFilter>, // default: None
    pub since: Option<Seq>,                // default: None (use checkpoint)
    pub checkpoint: bool,                  // default: true
    pub live: bool,                        // default: false
    pub retry: bool,                       // default: false
    pub poll_interval: Duration,           // default: 500ms
    pub back_off_function: Option<Box<dyn Fn(u32) -> Duration + Send + Sync>>,
}
}
  • batch_size – number of change events to process per iteration.
  • batches_limit – maximum number of batches to buffer (reserved for future pipelining).
  • filter – optional filter for selective replication. Supports DocIds(Vec<String>), Selector(serde_json::Value), or Custom(Arc<dyn Fn(&ChangeEvent) -> bool + Send + Sync>). When DocIds is used, filtering happens at the changes feed level. Selector filters after bulk_get. Custom filters after fetching changes.
  • live – enable continuous replication mode.
  • retry – automatically retry on transient errors in live mode.
  • poll_interval – how often to poll for new changes in live mode, when the source cannot announce them (Adapter::subscribe).
  • back_off_function – custom backoff for retries; receives retry count, returns delay duration.

Result

#![allow(unused)]
fn main() {
pub struct ReplicationResult {
    pub ok: bool,              // true if no errors occurred
    pub docs_read: u64,        // total change events processed
    pub docs_written: u64,     // total documents written to target
    pub errors: Vec<String>,   // individual doc errors (non-fatal)
    pub last_seq: Seq,         // final sequence reached
}
}

Individual document parse errors or write errors are collected in the errors vector but do not abort the replication. The ok field is true only when errors is empty.

Error Handling

Network errors: If source.changes(), target.revs_diff(), source.bulk_get(), or target.bulk_docs() returns an Err, the entire replication aborts and the error propagates to the caller. The last successfully saved checkpoint allows the next attempt to resume.

Document-level errors: If a document cannot be parsed from the bulk_get response, or if a bulk_docs write reports ok: false for a specific document, the error message is appended to ReplicationResult.errors but replication continues with the remaining documents.

Checkpoint errors: If writing a checkpoint fails on one side, the replicator continues. If both sides fail, the error is returned. On the next replication attempt, compare_checkpoints falls back to the most recent common session in the history array, or to sequence 0 if no common point exists.

Auth errors: These manifest as adapter-level Err results (e.g., HTTP 401/403 from the http adapter) and abort the replication immediately.

Incremental Replication

Because checkpoints are persisted, subsequent replications between the same source and target are incremental. They start from the last checkpointed sequence and only process new changes. This is the key to efficient ongoing synchronization:

First replication:   0 -----> 50  (processes 50 changes)
                              ^ checkpoint saved

Second replication: 50 -----> 53  (processes only 3 new changes)
                              ^ checkpoint updated

Live (Continuous) Replication

The replicate_live() function extends the one-shot protocol into a continuous loop:

 ┌─────────────────────────────────────────────────┐
 │              Live Replication Loop               │
 │                                                  │
 │  ┌──────────────┐                                │
 │  │  One-shot    │──(changes found)──→ emit       │
 │  │  replicate() │       Complete event           │
 │  └──────┬───────┘                                │
 │         │                                        │
 │    (no changes)                                  │
 │         │                                        │
 │    emit Paused                                   │
 │         │                                        │
 │    wait: source change notice, or               │
 │    sleep(poll_interval) without notices         │
 │         │                                        │
 │    ┌────▼────┐                                   │
 │    │cancelled?│──yes──→ stop                     │
 │    └────┬────┘                                   │
 │         │ no                                     │
 │         └───────→ loop back                      │
 └─────────────────────────────────────────────────┘

Key implementation details:

  • CancellationToken from tokio_util controls the loop. The caller receives a ReplicationHandle that wraps the token. Calling handle.cancel() or dropping the handle stops the loop.
  • Event channel: A tokio::sync::mpsc::Sender<ReplicationEvent> streams progress events (Active, Change, Complete, Paused, Error, Warning) to the caller.
  • Retry with backoff: When retry: true and an error occurs, the loop sleeps for back_off_function(retry_count) before retrying. The retry counter resets after a successful cycle.
  • Waiting for changes: When the source announces its changes (Adapter::subscribe, as the memory and redb adapters do), the loop subscribes before the first pass and, once caught up (emitting Paused), waits for the next notice: a local write is replicated immediately and a change made during a pass triggers another pass. A source that cannot announce changes (a remote CouchDB) is polled: between cycles the loop sleeps for poll_interval.
  • Resets: every pass compares both peers’ Adapter::id() with the ones its cursor belongs to and starts over (new checkpointer, cursor from the new pair’s checkpoint) when one changed. While waiting, the loop also listens to the target’s notices for a ChangeNotice::reset() (sent by destroy), and compares the ids of a target without notices every poll_interval, so a target destroyed and reused while the source is idle is refilled without waiting for a source change.

Event Streaming

The replicate_with_events() function is a one-shot replication that emits events through an mpsc channel as it progresses:

  • Active – emitted when replication starts.
  • Change { docs_read } – emitted after each batch of changes is written to the target.
  • Complete(ReplicationResult) – emitted when replication finishes.
  • Error(String) – emitted on per-document errors (non-fatal).

This enables UI progress tracking, logging, and monitoring without blocking the replication process.

Bidirectional Sync

The protocol is unidirectional by design. To achieve bidirectional sync, run two replications:

#![allow(unused)]
fn main() {
// A -> B
replicate(&adapter_a, &adapter_b, opts.clone()).await?;

// B -> A
replicate(&adapter_b, &adapter_a, opts).await?;
}

Each direction maintains its own checkpoint (different replication_id because the source/target order is reversed in the hash). Conflicts created by concurrent edits on both sides are handled naturally by the revision tree merge algorithm – both branches are preserved and the deterministic winning-rev algorithm ensures convergence.

Storage Layer

The rouchdb-adapter-redb crate provides persistent local storage backed by redb, a pure-Rust embedded key-value store with ACID transactions. This document describes the on-disk format of rouchdb 0.5 (tables, keys and values), how files written by older versions are upgraded, and the transactional guarantees.

Why redb

  • Pure Rust, no C dependencies. Eliminates build complexity and cross-compilation issues.
  • ACID transactions. Crash-safe reads and writes out of the box.
  • Typed tables. redb::TableDefinition encodes key and value types, and redb checks them every time a table is opened (the format guard below relies on this).
  • Single-file database. One .redb file per database, easy to manage.

Table Schema

A 0.5 file has seven tables:

+--------------------------+----------------+-------------+-----------------+
| Constant                 | Table name     | Key type    | Value type      |
+--------------------------+----------------+-------------+-----------------+
| DOC_TABLE                | "docs"         | &str        | &[u8]           |
| REV_DATA_TABLE           | "rev_data"     | &str        | &[u8]           |
| CHANGES_TABLE            | "changes"      | u64         | &[u8]           |
| LOCAL_TABLE              | "local_docs"   | &str        | &[u8]           |
| ATTACHMENT_TABLE         | "attachments"  | &str        | &[u8]           |
| META_TABLE               | "rouchdb_meta" | &str        | &[u8]           |
| GUARD_TABLE (the guard)  | "metadata"     | &str        | FormatGuard     |
+--------------------------+----------------+-------------+-----------------+

Except for the guard, values are JSON (serde_json::to_vec / from_slice) or raw attachment bytes.

DOC_TABLE ("docs")

Purpose: Stores each document’s revision tree and current sequence number.

Key: Document ID (&str).

Value: JSON DocRecord: the revision tree as a flat, pre-order list of nodes, each pointing at its parent’s index. Flat storage keeps (de)serialization depth constant however long the history is.

#![allow(unused)]
fn main() {
struct DocRecord {
    revs: Vec<FlatRevNode>,
    seq: u64,
}

struct FlatRevNode {
    pos: u64,
    hash: String,
    parent: Option<u32>, // index of the parent node; absent for a root
    missing: bool,       // omitted when false; true: the body is not stored
    deleted: bool,       // omitted when false
}
}

Example stored value (1-a1b2… compacted away, 2-f7e8… its child):

{"revs":[{"pos":1,"hash":"a1b2c3d4...","missing":true},
         {"pos":2,"hash":"f7e8d9c0...","parent":0}],
 "seq":42}

rouchdb ≤ 0.4 stored a nested record ({"rev_tree": [{"pos", "tree": {"hash", "status", "deleted", "children": [...]}}], "seq"}), two JSON levels per generation. Such records are still read (deep ones on a thread with a large stack and serde_json’s recursion limit disabled) and are rewritten flat the next time the document is written. A record that cannot be decoded is an error, never a missing document.

REV_DATA_TABLE ("rev_data")

Purpose: Stores the body and attachment metadata of each stored revision.

Key: "{doc_id}\0{rev}", e.g. "doc1\03-abc123…". Revision ids are stored in canonical form (32-digit hex ids in lower case).

Document ids may themselves contain NUL (CouchDB accepts "a\u0000b"), so the key range of a ("a\0" .. "a\x01") also holds the keys of a\0b. Revision ids never contain NUL (they are rejected), so a key in that range whose remainder contains a NUL belongs to a longer id and is skipped when the bodies of a are listed (compaction, purge).

Value: JSON RevDataRecord:

#![allow(unused)]
fn main() {
struct RevDataRecord {
    data: serde_json::Value,                        // body without _id, _rev, ...
    deleted: bool,
    attachments: HashMap<String, AttachmentRecord>, // omitted when empty
}

struct AttachmentRecord {
    content_type: String,
    digest: String,              // "md5-<base64>", the ATTACHMENT_TABLE key
    length: u64,
    revpos: u64,                 // omitted when 0 (attachments stored by 0.4)
    encoding: Option<String>,    // omitted when absent
    encoded_length: Option<u64>, // omitted when absent
}
}

CHANGES_TABLE ("changes")

Purpose: The changes feed. Each document has one entry, at its most recent sequence.

Key: Sequence number (u64), incremented by 1 on every document write.

Value: JSON ChangeRecord { doc_id: String, deleted: bool } (deleted: whether the document’s winning revision is a deletion).

When a document is written, the entry at its previous sequence is removed and a new one inserted, so changes(since: N) is a range scan over (N+1..):

Seq | doc_id   | deleted
----|----------|--------
  3 | "doc1"   | false      (doc1 was written at seq 1, updated at seq 3)
  4 | "doc2"   | false
  5 | "doc3"   | true       (doc3 was deleted)

LOCAL_TABLE ("local_docs")

Purpose: Local documents: not replicated, not in all_docs or the changes feed, no revision tree. Replication checkpoints live here.

Key: The id without its _local/ prefix.

Value: The JSON body. A local document written through bulk_docs / Database::put with a _local/ id carries its revision ("_rev": "0-N") in the body, like the server’s PUT /{db}/_local/{id}.

rouchdb ≤ 0.4 stored _local/… documents written through bulk_docs in docs like any other document; the upgrade moves them here (see below).

ATTACHMENT_TABLE ("attachments")

Purpose: Attachment bytes, content-addressed.

Key: The digest ("md5-<base64 of the MD5>"), as referenced by AttachmentRecord::digest.

Value: The raw bytes.

Identical bytes are stored once, whichever documents and revisions reference them. Bytes stay while any stored revision references them; compact() deletes the rest. (rouchdb ≤ 0.4 keyed bytes by "{doc_id}\0{name}", so re-attaching a name overwrote the bytes older revisions referenced.)

META_TABLE ("rouchdb_meta")

Purpose: Database metadata.

Keys: "meta" (the MetaRecord) and "security" (the security document, once one is set).

#![allow(unused)]
fn main() {
struct MetaRecord {
    update_seq: u64,    // highest sequence number
    db_uuid: String,    // random, reset by destroy(); part of Adapter::id()
    schema: u32,        // on-disk layout version (currently 2)
    purge_seq: u64,     // number of purge requests applied
    doc_count: u64,     // live documents, maintained on every write
    doc_del_count: u64, // deleted documents
}
}

info() and all_docs().total_rows read the counts from this record instead of scanning documents.

Adapter::id() is db_uuid followed by the MD5 of the file’s canonical path (resolved when the file is opened). A copy of a file holds the same db_uuid but lives at another path, so it is a different database for replication; reopening a file at the same path, or through a symlink, keeps its id and so its checkpoints, while moving it costs one rescan per peer. A file whose schema is higher than this version knows is refused unchanged.

The format guard ("metadata")

rouchdb ≤ 0.4 kept its metadata in a table named "metadata" and opens it as Table<&str, &[u8]> in RedbAdapter::open. A 0.4 build must never use a 0.5 file: it cannot decode flat document records, would treat those documents as missing and replace their history on its next write, and cannot find digest-keyed attachments.

So in a 0.5 file "metadata" is a guard table whose value type, FormatGuard, is zero-sized and named rouchdb-format-2 (this file requires rouchdb >= 0.5). redb refuses to open a table with a different type, so rouchdb 0.1–0.4 fail in open with

database error: metadata is of type Table<&str, rouchdb-format-2 (this file requires rouchdb >= 0.5)>

before writing anything. The guard is created in the same transaction that creates or upgrades the file, destroy() keeps it, and its type name must never change (every 0.5 file stores it). A future incompatible format would install a different guard type; 0.5 reports that type’s name when it cannot open such a file.

Serialization Approach

Structured values are JSON. This was chosen over binary formats (bincode, MessagePack) for:

  1. Debuggability. JSON values can be inspected with standard tools.
  2. Compatibility. The serialized format closely mirrors what CouchDB stores and returns.
  3. Flexibility. Document bodies are already serde_json::Value, so no format conversion is needed.

Document bodies may be nested up to MAX_NESTING_DEPTH (1000) levels; they are decoded with a matching recursion limit.

Opening and Initialization

RedbAdapter::open (and open_with) opens or creates the file, then looks at it in a read transaction:

The file hasopen
no tables (a new file)creates, in one write transaction, the seven tables, a fresh MetaRecord (update_seq 0, new UUID, current schema) and the guard
the guard (a 0.5 file)writes nothing (unless a table is missing); a newer schema is refused
an unguarded "metadata" without schema (rouchdb ≤ 0.4)returns RouchError::UpgradeRequired and changes nothing, unless OpenOptions::upgrade allows the upgrade
an unguarded "metadata" with schema 1 or 2 (unreleased 0.5 builds)upgrades it automatically, after a verified backup to <file>.rouchdb-0.5-pre.bak (unless the policy is InPlaceNoBackup)
other tables onlyrefuses the file (“not a rouchdb database”)
a "metadata" table of another typerefuses the file, naming that type

Upgrading Files Written by rouchdb ≤ 0.4

RedbAdapter::upgrade(path, policy), rouchdb migrate <path> and open_with with UpgradePolicy::WithBackup / InPlaceNoBackup run the upgrade in three steps. The file is opened with a 32 MiB page cache (redb’s default is 1 GiB) and records and attachments are handled one at a time, so memory stays small whatever the file size; open_with then reopens the upgraded file with the default cache.

1. Analysis (read only). From one read transaction, the upgrade works out everything it will write, and the report:

  1. Read every entry of the old "metadata" table.
  2. Compute the digest of every attachment stored under "{doc_id}\0{name}" (reading one entry at a time).
  3. Plan the move of _local/… documents from docs to local_docs: the winning revision’s body, with "_rev": "0-N" (N = its generation); deleted ones are dropped; their bodies and change entries are removed. A collision with an existing local document, or a live document whose id is exactly _local/ (an empty local name 0.5 cannot address), stops the upgrade.
  4. Plan the lower-casing of upper-case 32-digit hex revision ids in revision trees and body keys. A revision stored under several spellings becomes one node (the root-to-leaf paths are merged again); of several stored bodies, an identical copy is dropped, and when they differ the body of the spelling 0.4 ranked first is kept (leaf before inner revision, live leaf before deleted, then the greater id in byte order: 0.4’s winner order) and each other body is listed in UpgradeReport::case_duplicate_bodies_discarded. Documents whose winning revision differs once ids are lower case are counted.
  5. Count live and deleted documents, and collect the report’s facts (attachment references without bytes, old revision bodies, attachment bytes only old revisions reference), as the file will be after the upgrade.

A record that cannot be decoded stops the upgrade here with an error naming it; nothing is skipped. RedbAdapter::inspect_upgrade (rouchdb migrate --dry-run) is this step alone: it never opens a write transaction, so the file is not modified (not even redb’s free-page bookkeeping) and no disk space is used.

2. Backup (WithBackup, default path <file>.rouchdb-0.4.bak, or <file>.rouchdb-0.5-pre.bak for a development-build file). While the file is open (redb’s file lock is held), every table is copied entry by entry from one read transaction into <backup>.partial with the same table types, committed, reopened and compared entry by entry (keys and values) with the source, synced (the file is opened for writing, which Windows requires to flush it), then renamed to the backup path and the directory synced. The backup path must not exist; on any error before the rename the partial copy is removed and the source, which is only read, is unchanged. If only the directory sync fails, the backup is complete and the upgrade goes on with a warning in the report. The backup opens in 0.4 exactly like the original. A table rouchdb did not create makes the backup fail (copy the file yourself and upgrade without a backup). After a crash, a leftover <backup>.partial is not a backup (delete it); a <backup> is complete, as it is only renamed into place once verified.

3. Upgrade. One write transaction with two-phase commit applies the plan, so the file is upgraded completely or not at all (an error or a crash before the commit leaves it as it was): re-key the attachment bytes, move the _local/ documents, rewrite the revision trees and body keys, write the MetaRecord (and the security document, if any) to rouchdb_meta, delete the old "metadata" table and create the guard. If this step fails before the commit, the backup made for the attempt is removed (it would block a retry); if the commit itself fails, it is kept, since the file may then be either the old one or the upgraded one.

Disk space. redb copies every page the transaction changes and keeps the old pages until the commit, so the upgrade needs about twice the file size of free space; the backup needs about the file size again (about three times in total).

The first compact() after the upgrade deletes every non-leaf body 0.4 kept (0.4’s compact did nothing) and the attachment bytes only those bodies referenced.

Write Serialization

Every storage operation runs on Tokio’s blocking thread pool (spawn_blocking), since redb commits fsync. Writers first take an async Mutex, so waiting writers do not each hold a blocking thread, then run one redb write transaction:

1. Acquire the write lock
2. Begin a redb write transaction
3. Read the MetaRecord (update_seq, counts)
4. For each document:
   a. Read its DocRecord (a decode error aborts the batch)
   b. Plan the edit with rouchdb_core::write (conflicts, revision id,
      attachment inheritance, stemming)
   c. Store new attachment bytes under their digest
   d. Increment update_seq; remove the old CHANGES_TABLE entry
   e. Write the DocRecord, the RevDataRecord and the ChangeRecord; drop the
      bodies of stemmed revisions; adjust the document counts
5. Write the MetaRecord
6. Commit, then notify change subscribers

If any step fails, the transaction is not committed and nothing changes.

Two Write Modes

new_edits=true (Normal Writes)

Used for local application writes:

  1. The provided _rev is checked against the tree as CouchDB does (a stale or unknown revision is a conflict).
  2. The new revision id comes from generate_rev_hash: MD5 over the parent revision, the deleted flag, the body and the attachment set.
  3. The path [new, parent] is merged into the tree and stemmed to the revision limit.

new_edits=false (Replication Writes)

Used during replication:

  1. No conflict check.
  2. The revision id is accepted as sent (in canonical form).
  3. With _revisions, the whole ancestry is merged (build_path_from_revs); _revisions is not stored.
  4. Attachment stubs must refer to bytes already stored (missing_stub otherwise).

Transactional Guarantees

  • Atomicity. A bulk_docs call, an attachment write, a purge, a compaction and an upgrade are each one redb transaction.
  • Durability. Once commit() returns, data is on disk (redb fsyncs).
  • Consistency. The sequence and counts in the MetaRecord always match the documents; each document has exactly one change entry.
  • Isolation. Reads (begin_read) see a consistent snapshot and are not blocked by writes.

Compaction, Purge and Destroy

  • compact() keeps only the bodies of leaf revisions (marking the others missing) and deletes attachment bytes no remaining body references.
  • purge() removes leaf revisions (and ancestors no other leaf needs), their bodies and, when the tree becomes empty, the document.
  • destroy() deletes and recreates every table except the guard, and resets the metadata (new UUID, update_seq 0, no security document). The file stays on disk, still a 0.5 file, and the handle keeps working as a new, empty database. Once committed, it sends ChangeNotice::reset() to the subscribers, so live replications to or from the file start over.

Key Format Summary

docs:          "doc1"                 -> DocRecord JSON (flat; legacy nested readable)
rev_data:      "doc1\03-a1b2c3..."    -> RevDataRecord JSON
changes:       42                     -> ChangeRecord JSON
local_docs:    "replication-id-hash"  -> JSON body
attachments:   "md5-<base64>"         -> raw bytes
rouchdb_meta:  "meta" / "security"    -> MetaRecord JSON / security document
metadata:      "format"               -> FormatGuard (zero bytes; its type is the point)

Development Setup

This guide walks you through setting up a local development environment for RouchDB.

Prerequisites

Rust Toolchain

RouchDB requires a recent stable Rust toolchain. Install it via rustup:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Verify your installation:

rustc --version
cargo --version

The minimum supported Rust version (MSRV) is 1.88, declared as rust-version in the root Cargo.toml. The toolchain used for development and CI is pinned in rust-toolchain.toml; rustup installs it automatically the first time you run cargo inside the repository.

Docker (for integration tests)

Integration tests run against a real CouchDB instance. You need Docker and Docker Compose installed:

Docker is not required for building the project or running unit tests.

Clone and Build

git clone https://github.com/rubylab-app/rouchdb.git
cd rouchdb
cargo build

The workspace contains these crates:

CratePurpose
rouchdb-coreTypes, traits, revision tree, merge, collation, errors
rouchdb-adapter-memoryIn-memory adapter (testing and ephemeral use)
rouchdb-adapter-redbPersistent local storage via redb
rouchdb-adapter-httpCouchDB HTTP client adapter
rouchdb-changesStreaming changes feed
rouchdb-replicationCouchDB replication protocol
rouchdb-queryMango selectors and map/reduce views
rouchdb-viewsDesign documents and persistent view engine
rouchdbUmbrella crate that re-exports everything
rouchdb-serverCouchDB-compatible HTTP server (not published)
rouchdb-cliCommand-line tool (published; installs the rouchdb binary)
rouchdb-benchCriterion benchmarks (not published)
rouchdb-compat-testsCross-version tests against rouchdb 0.4.0 from crates.io (not published)

Running Unit Tests

Unit tests live inside each crate as #[cfg(test)] modules. They do not require any external services:

cargo test

This runs all unit tests across every crate in the workspace. To test a single crate:

cargo test -p rouchdb-core
cargo test -p rouchdb-adapter-memory

Docker Compose Setup for CouchDB

The project includes a docker-compose.yml at the repository root that starts a CouchDB 3.5.1 instance (the version CI tests against) on port 15984, with the admin admin:password. A one-shot couchdb-setup service then turns off CouchDB’s account lockout, which would otherwise lock the admin out after a few failed logins and fail the rest of the suite with 403.

Start CouchDB:

docker compose up -d

Wait for it to be healthy:

docker compose ps

You should see the couchdb service listed as healthy. CouchDB is now accessible at http://localhost:15984 with credentials admin:password.

To stop CouchDB when you are done:

docker compose down

Running Integration Tests

Integration tests verify RouchDB against a real CouchDB server. They are marked #[ignore = "requires CouchDB"] so they do not run during normal cargo test. Run them explicitly (see Testing):

docker compose up -d
bash scripts/test-couchdb.sh

Environment Variables

COUCHDB_URL

Override the default CouchDB connection URL used by integration tests. The default is:

http://admin:password@localhost:15984

To use a different CouchDB instance:

export COUCHDB_URL="http://myuser:mypass@couchdb.example.com:5984"
bash scripts/test-couchdb.sh

Editor Setup

RouchDB is a standard Cargo workspace. Any editor with rust-analyzer support works well:

  • VS Code with the rust-analyzer extension
  • RustRover (JetBrains)
  • Neovim / Helix with rust-analyzer LSP

No special configuration files are needed beyond what Cargo provides.

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 – The Adapter trait that all storage backends implement. Defines methods like get, 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 – RouchError enum and Result type 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 – Full Adapter trait implementation with StoredDoc internal 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 – RedbAdapter struct implementing the Adapter trait 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 – HttpAdapter struct, CouchDB JSON response deserialization types, and the full Adapter trait implementation.

rouchdb-changes

Responsibility: Streaming changes feed with one-shot and live/continuous modes.

Key files:

  • src/lib.rs – ChangeSender, ChangeReceiver, ChangeNotification (= core’s ChangeNotice), and LiveChangesStream, which waits on the adapter’s change notifications (Adapter::subscribe, Tokio broadcast channels) and polls adapters without them.

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 _local documents 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/design_doc.rs – the lossless DesignDocument struct (typed views, filters, validate_doc_update, plus other_views and extra for every other member); src/engine.rs – ViewEngine for 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 – Database struct with user-friendly methods (put, get, update, remove, replicate_to, replicate_from, find, all_docs, changes). Wraps any Adapter behind an Arc<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 – AppState shared across all route handlers.
  • src/auth.rs – Admin authentication (Basic auth and _session cookies).
  • src/host.rs – Host header check against DNS rebinding (--allowed-host).
  • src/error.rs – Maps RouchError to 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 via scripts/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, migrate, compact, put, delete, post, import.

rouchdb-compat-tests (not published)

Responsibility: Cross-version tests. Depends on the released rouchdb-core and rouchdb-adapter-redb 0.4.0 from crates.io (renamed rouchdb04-*) next to the workspace crates, and checks that 0.4 files are refused and upgraded without loss by this version and that 0.4 refuses 0.5 files.

Key files:

  • tests/redb_0_4_to_0_5.rs

Adding an Adapter

To add a new storage backend (e.g., SQLite, IndexedDB via wasm):

  1. Create a new crate: crates/rouchdb-adapter-yourbackend/
  2. Add it to the workspace members list in the root Cargo.toml
  3. Depend on rouchdb-core for the Adapter trait and document types
  4. Implement the Adapter trait from rouchdb_core::adapter::Adapter
  5. Add the crate as a dependency in the umbrella rouchdb crate and re-export it

The Adapter trait requires implementing these async methods:

  • info() – Database metadata
  • get() – Fetch a single document
  • bulk_docs() – Write multiple documents atomically
  • all_docs() – List/query all documents
  • changes() – Get changes since a sequence
  • revs_diff() – Compare revision sets (for replication)
  • bulk_get() – Fetch multiple documents by ID and revision
  • get_local() / put_local() / remove_local() – Local (non-replicated) document operations
  • get_attachment() / put_attachment() / remove_attachment() – Attachment storage
  • close() – 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.

Testing

RouchDB has unit tests inside each crate, integration tests of the Database API that run on the local backends (memory and redb), and CouchDB tests that need a running CouchDB instance.

Unit Tests

Unit tests are defined as #[cfg(test)] modules inside each crate’s source files. They cover internal logic without any external services.

Running All Unit Tests

cargo test

This runs every test that does not need CouchDB: the unit tests of the 12 workspace crates and the integration tests in crates/rouchdb/tests/ that use the memory and redb backends.

Running Tests for a Single Crate

cargo test -p rouchdb-core
cargo test -p rouchdb-adapter-memory
cargo test -p rouchdb-query

Running a Specific Test

cargo test -p rouchdb-core winning_rev_simple

Integration Tests

Integration tests live in crates/rouchdb/tests/. Most of them exercise the Database API on the local backends and run with plain cargo test:

  • adapter_conformance.rs runs every scenario on both the memory and the redb adapter (the conformance! macro);
  • contract suites such as plugin_contract.rs, partition.rs and error_conditions.rs loop over both backends with the backends() helper of crates/rouchdb/tests/backends/mod.rs.

The tests that verify RouchDB against a real CouchDB server (protocol compliance, replication, parity of results) are spread over the same files (http_crud.rs, replication.rs, couchdb_query_parity.rs, data_diversity.rs, etc.) and are marked #[ignore = "requires CouchDB"].

Prerequisites

Start CouchDB via Docker Compose:

docker compose up -d

Wait for the health check to pass (the service should report healthy):

docker compose ps

The default connection URL is http://admin:password@localhost:15984.

Running Integration Tests

The CouchDB tests are marked #[ignore = "requires CouchDB"] so they are skipped during cargo test. Run them (every crate, in parallel) with:

bash scripts/test-couchdb.sh

The script runs cargo test --workspace --tests --no-fail-fast -- --ignored --skip blocked_on_, which leaves out the tests blocked on a known bug (see below). Extra arguments go to cargo test, e.g. bash scripts/test-couchdb.sh -p rouchdb. It fails if a test leaves one of its databases behind; bash scripts/test-couchdb.sh --sweep deletes every rouchdb_test_* database (leftovers of a killed run; do not run it while another suite uses the same server).

To run a single integration test by name:

cargo test -p rouchdb --test http_crud http_put_and_get -- --ignored

Custom CouchDB URL

To point tests at a different CouchDB instance, set the COUCHDB_URL environment variable:

COUCHDB_URL="http://user:pass@myhost:5984" bash scripts/test-couchdb.sh

Tests Blocked on a Known Bug

A test that pins down a known, not yet fixed library bug keeps its assertions. It is ignored with the finding it is waiting for, and its name contains blocked_on_ (a blocked_on_fxx_ prefix, or a mod blocked_on_fxx):

#![allow(unused)]
fn main() {
#[tokio::test]
#[ignore = "blocked on F03"]
async fn blocked_on_f03_inline_base64_attachment_decoding() { /* ... */ }
}

The name is what the scripts filter on, so no script has to parse the source:

  • scripts/test-couchdb.sh (and therefore CI) skips them with --skip blocked_on_.
  • scripts/test-blocked.sh runs only them and fails if any of them passes. CI runs it as a non-blocking step, so a fix that forgets to unblock its test shows up.

Run one explicitly with cargo test -p rouchdb --test parity_core blocked_on_f03 -- --ignored, and remove both the marker and the name prefix in the PR that fixes the bug. #[ignore] takes one of these two reasons only; CI rejects a bare #[ignore] or any other reason.

Writing New Unit Tests

Unit tests go in a #[cfg(test)] module at the bottom of the source file they are testing.

Synchronous Tests (pure logic)

For functions that do not involve async I/O, use standard #[test]:

#![allow(unused)]
fn main() {
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn my_pure_logic_test() {
        let tree = build_some_rev_tree();
        let winner = winning_rev(&tree).unwrap();
        assert_eq!(winner.pos, 3);
        assert_eq!(winner.hash, "abc");
    }
}
}

This pattern is used extensively in rouchdb-core for revision tree operations, merge algorithms, and collation ordering.

Async Tests (adapter operations)

For tests that exercise adapter methods, use #[tokio::test]:

#![allow(unused)]
fn main() {
#[cfg(test)]
mod tests {
    use super::*;
    use rouchdb_core::document::{AllDocsOptions, BulkDocsOptions, GetOptions};

    async fn new_db() -> MemoryAdapter {
        MemoryAdapter::new("test")
    }

    #[tokio::test]
    async fn put_and_get_document() {
        let db = new_db().await;

        let doc = Document::new("doc1", serde_json::json!({"name": "Alice"}));

        let results = db
            .bulk_docs(vec![doc], BulkDocsOptions::new())
            .await
            .unwrap();
        assert!(results[0].ok);

        let fetched = db.get("doc1", GetOptions::default()).await.unwrap();
        assert_eq!(fetched.data["name"], "Alice");
    }
}
}

Guidelines for Unit Tests

  • Place tests in the same file as the code they exercise.
  • Use a helper function (e.g., new_db()) to create a fresh adapter instance per test.
  • Test both the success path and error conditions.
  • Keep tests focused – one logical assertion per test function when practical.

Writing New Integration Tests

Integration tests go in crates/rouchdb/tests/ as separate test files. They test the high-level Database API.

Tests on the Local Backends

Run a Database test on both local backends unless it is about one of them:

#![allow(unused)]
fn main() {
mod backends;

use backends::backends;

#[tokio::test]
async fn my_local_test() {
    for b in backends("test") {
        let r = b.db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
        let doc = b.db.get("doc1").await.unwrap();
        assert_eq!(doc.rev.unwrap().to_string(), r.rev.unwrap(), "{}", b.name);
        assert_eq!(doc.data, serde_json::json!({"v": 1}), "{}", b.name);
    }
}
}

A scenario that every adapter must pass belongs in adapter_conformance.rs.

Structure of a CouchDB Test

Every CouchDB integration test follows this pattern:

#![allow(unused)]
fn main() {
mod common;

use common::fresh_remote_db;

#[tokio::test]
#[ignore = "requires CouchDB"]
async fn my_couchdb_test() {
    // 1. Create a fresh database with a unique name. `url` is a guard that
    //    deletes the database when it is dropped, even if the test fails.
    let url = fresh_remote_db("my_test_label").await;
    let db = Database::http(&url);

    // 2. Perform operations
    let result = db.put("doc1", serde_json::json!({"key": "value"})).await.unwrap();
    assert!(result.ok);

    // 3. Verify results
    let doc = db.get("doc1").await.unwrap();
    assert_eq!(doc.data["key"], "value");
}
}

Key points:

  • Always add #[ignore = "requires CouchDB"] so the test does not run in cargo test, and so the CouchDB suite picks it up.
  • Always use fresh_remote_db() (or unique_remote_db() when the code under test creates the database itself) to get a uniquely named database. This prevents test interference, so the suite can run in parallel.
  • No manual cleanup. The returned RemoteDb guard deletes the database on drop, also when an assert fails. Set ROUCHDB_KEEP_TEST_DBS=1 to keep the databases for debugging.
  • Do not hardcode the server or credentials. Take them from common::couchdb() (url, anonymous_url, user, password), which parses COUCHDB_URL once.
  • Never log in as the admin with a wrong password. CouchDB 3.4+ locks the account after a few failures and the rest of the suite then fails with 403. Use a random, nonexistent user name to test a failed login.

When to Write an Integration Test

Add an integration test when you need to verify:

  • HTTP adapter correctness against a real CouchDB server
  • Replication between a local adapter and CouchDB
  • Protocol-level compatibility (e.g., _revs_diff, _bulk_get responses)
  • Edge cases that depend on CouchDB-specific behavior

Test Patterns

Memory Adapter for Fast Tests

The MemoryAdapter is the primary tool for fast, isolated unit tests. It implements the full Adapter trait in memory with no I/O, making tests instant and deterministic. The redb adapter is almost as fast on a tempfile directory, so Database tests should run on both (see above).

Use MemoryAdapter when testing:

  • Replication protocol logic (by replicating between two memory adapters)
  • Changes feed behavior
  • Query/selector matching
  • Any feature that works at the Adapter trait level

Example from the replication crate:

#![allow(unused)]
fn main() {
let source = MemoryAdapter::new("source");
let target = MemoryAdapter::new("target");

// ... write docs to source ...

replicate(&source, &target, ReplicationOptions::default()).await.unwrap();

// ... verify docs appear in target ...
}

Real CouchDB for Protocol Compliance

Integration tests with a real CouchDB instance catch issues that in-memory tests cannot:

  • JSON serialization/deserialization mismatches
  • HTTP header requirements
  • CouchDB-specific revision handling quirks
  • Sequence format differences between CouchDB versions
  • Attachment encoding edge cases

Helper Functions in Integration Tests

The integration tests share these helpers in crates/rouchdb/tests/common/mod.rs (the CLI tests include the same file):

  • couchdb() – The server under test, parsed once from the COUCHDB_URL environment variable: URL with and without credentials, user and password.
  • fresh_remote_db(label) – Creates a CouchDB database named rouchdb_test_<label>_<uuid> and returns a RemoteDb guard that derefs to its URL and deletes it when dropped.
  • unique_remote_db(label) – The same guard for a database that is not created up front.

Every test database starts with rouchdb_test_. If a run is killed before the guards run, remove the leftovers with bash scripts/test-couchdb.sh --sweep.

Assertions

A test must be able to fail. In particular:

  • put, update, remove and post return a failed write as an error (Err(RouchError::Conflict), Err(RouchError::NotFound(_)), Err(RouchError::Forbidden(_)), …), never as Ok with ok: false. bulk_docs reports failures per document (ok: false and error: Some("conflict")), so check each result’s ok or error.
  • Check the exact error with matches!(result, Err(RouchError::Conflict)), not is_err(): an unrelated failure passes is_err() too.
  • Compare exact values: the list of ids in order, the whole body, the revision. A count passes with the wrong documents, and for row in &rows { assert!(..) } passes when there are no rows.
  • Check the preconditions a test relies on (for example, which revision of a conflict wins) and that a rejected write left the database unchanged (same update_seq, same revision).
  • Do not accept every outcome (assert!(r.is_ok() || r.is_err()), match arms that all do nothing). Assert the behavior CouchDB has, and verify it against a real CouchDB when in doubt.

On-Disk Compatibility Tests

crates/rouchdb-compat-tests (never published) links the rouchdb 0.4.0 release from crates.io next to this version. Its tests write realistic data with 0.4, then check that 0.5 refuses the file without changing it, that the upgrade keeps every document, revision body, attachment and local document, that the backup is identical to the original, and that 0.4 refuses upgraded and newly created files without writing to them. They run with the unit tests (cargo test --workspace). Any change to the redb on-disk format must keep them passing, and should add a scenario there.

Benchmarks

Criterion benchmarks live in the rouchdb-bench crate:

  • benches/core_ops.rs: bulk_docs, get, all_docs, changes, Mango find with and without an index, and replication, on the memory and redb backends.
  • benches/revtree.rs: merge_tree, stem, winning_rev and collect_conflicts on deep and wide revision trees.
cargo bench -p rouchdb-bench                   # everything
cargo bench -p rouchdb-bench -- find/redb      # filter by benchmark name
ROUCHDB_BENCH_N=100000 cargo bench -p rouchdb-bench --bench core_ops
cargo bench -p rouchdb-bench -- --save-baseline main   # then compare with --baseline main

Results depend heavily on the machine; compare runs on the same machine only.

Continuous Integration

GitHub Actions (.github/workflows/ci.yml) runs these jobs on every pull request, each with a timeout-minutes:

JobWhat it runs
Check & Lintcargo fmt --check, every #[ignore] has a known reason, cargo clippy --all-targets -D warnings, and the TLS feature combinations
Testscargo test --workspace --no-fail-fast (including the README examples as doctests)
CouchDB integration testsscripts/test-couchdb.sh against a couchdb:3.5.1 service container (account lockout off), the non-blocking scripts/test-blocked.sh xfail check, and a check that no database was left behind
Benchmarks (build + smoke run)cargo bench --no-run, then every benchmark once in criterion’s --test mode on 1000 documents
MSRV (1.88)cargo check --all-targets --all-features on Rust 1.88
Clippy on stable/betaNon-blocking early warning about lints from newer toolchains

minimal-versions.yml resolves every direct dependency to the lowest version its Cargo.toml requirement allows (cargo +nightly update -Z direct-minimal-versions) and builds, when a manifest changes and weekly. When it fails, raise the requirement to the version the code actually needs, in every crate that declares it.

Three more workflows measure test quality. They never block a merge:

WorkflowWhenWhat
coverage.ymlpush to mainLine coverage (cargo-llvm-cov) of the unit tests, doctests and CouchDB suite: job summary plus an lcov artifact
mutants.ymlpull requests that change crates/*/srccargo mutants --in-diff on the changed code; every mutant runs the whole workspace suite, CouchDB tests included, against the same couchdb:3.5.1 service as the CI job (--timeout 300 per test run, 75 minutes in all). Mutants no test catches (missed or timed out) are listed in the job summary and the mutants-out artifact, and the job still passes; it fails only if cargo-mutants does, e.g. when the unmutated code fails its tests
nightly.ymldaily, or by handFlaky-test detection: scripts/repeat-tests.sh runs the unit suite (Linux and macOS) and the CouchDB suite 5 times and lists every test that failed in any run

bash scripts/repeat-tests.sh 5 bash scripts/test-couchdb.sh does the same locally.

To run the mutants of one file locally the way mutants.yml does, with CouchDB running (arguments after the first -- go to cargo test, after the second to the test binaries):

cargo mutants -f crates/rouchdb-adapter-http/src/lib.rs --test-workspace=true --timeout 300 \
  -- --tests -- --include-ignored --skip blocked_on_

The blocking jobs use the toolchain pinned in rust-toolchain.toml. To move to a newer Rust, bump it there and fix any new lints in the same PR. The benchmarks can be run on a GitHub runner from the Actions tab (the manual “Benchmarks” workflow); shared runners are noisy, so use those numbers for trends only.

RouchDB

Una base de datos de documentos local-first para Rust con soporte del protocolo de replicacion de CouchDB.

RouchDB es el equivalente en Rust de PouchDB — te da un almacen local de documentos JSON que se sincroniza bidireccionalmente con Apache CouchDB y servidores compatibles.

Por que RouchDB?

  • No existe un equivalente en Rust. Crates como couch_rs proveen clientes HTTP para CouchDB, pero ninguno ofrece almacenamiento local con replicacion. RouchDB llena este vacio.
  • Disenado para funcionar sin conexion. Tu aplicacion funciona sin red. Cuando vuelve la conectividad, RouchDB sincroniza los cambios automaticamente.
  • Compatibilidad total con CouchDB. Implementa el protocolo de replicacion de CouchDB, el modelo de arbol de revisiones y el lenguaje de consultas Mango.

Caracteristicas

  • Operaciones CRUD — put, get, update, remove, bulk_docs, all_docs
  • Consultas Mango — $eq, $gt, $in, $regex, $or y mas de 15 operadores
  • Vistas map/reduce — closures de Rust con reduces integrados Sum, Count, Stats
  • Feed de cambios — consulta unica y streaming en vivo de mutaciones de documentos
  • Replicacion — push, pull y sincronizacion bidireccional con CouchDB
  • Resolucion de conflictos — algoritmo determinista de ganador, utilidades de deteccion de conflictos
  • Almacenamiento flexible — en memoria, persistente (redb) o remoto (HTTP)
  • Servidor HTTP compatible con CouchDB — navega bases de datos con Fauxton, usa cualquier cliente CouchDB
  • Sin librerias del sistema — almacenamiento en Rust puro (redb) y HTTPS con rustls por defecto en lugar de OpenSSL

Casos de uso

  • Aplicaciones de escritorio con Tauri que necesitan sincronizacion offline
  • Herramientas CLI que almacenan datos localmente y sincronizan con un servidor
  • Servicios backend que replican entre instancias de CouchDB
  • Cualquier aplicacion Rust que necesite una base de datos de documentos local

Ejemplo rapido

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    // Crear un documento
    let result = db.put("user:alice", serde_json::json!({
        "name": "Alice",
        "age": 30
    })).await?;

    // Leerlo de vuelta
    let doc = db.get("user:alice").await?;
    println!("{}: {}", doc.id, doc.data);

    Ok(())
}

Listo para empezar? Ve a Inicio Rapido.

Inicio Rapido

Esta guia te lleva por las funcionalidades principales de RouchDB en 5 minutos.

Instalacion como libreria

Agrega RouchDB a tu proyecto:

[dependencies]
rouchdb = "0.5"
tokio = { version = "1", features = ["full"] }
serde_json = "1"

Herramienta de linea de comandos

RouchDB incluye un CLI para inspeccionar y consultar bases de datos redb:

cargo install --path crates/rouchdb-cli

Ejemplos de uso:

# Informacion de la base de datos
rouchdb info mydb.redb

# Obtener un documento por ID
rouchdb get mydb.redb user:alice

# Listar todos los documentos
rouchdb all-docs mydb.redb --include-docs

# Consulta Mango
rouchdb find mydb.redb --selector '{"age": {"$gte": 30}}'

# Exportar todos los documentos como JSON
rouchdb dump mydb.redb --pretty

# Crear o actualizar un documento
rouchdb put mydb.redb user:alice '{"name":"Alice","age":30}'
rouchdb put mydb.redb user:alice '{"name":"Alice","age":31}' --rev 1-abc

# Upsert — obtiene el rev actual automaticamente, crea si no existe
rouchdb put mydb.redb user:alice '{"name":"Alice","age":32}' --force

# Crear un documento con ID auto-generado
rouchdb post mydb.redb '{"name":"Bob","age":25}'

# Eliminar un documento
rouchdb delete mydb.redb user:alice --rev 2-def

# Importar documentos desde un archivo JSON
rouchdb import mydb.redb docs.json

# Replicar a CouchDB
rouchdb replicate mydb.redb http://admin:password@localhost:5984/mydb

Servidor HTTP

RouchDB incluye un servidor HTTP compatible con CouchDB con la interfaz web Fauxton:

# Instalar el servidor
cargo install --path crates/rouchdb-server

# Descargar Fauxton (opcional, para la interfaz web)
bash scripts/download-fauxton.sh

# Iniciar el servidor
rouchdb-server mydb.redb --port 5984

# Abrir Fauxton en el navegador
open http://localhost:5984/_utils/

El servidor expone una API REST compatible con CouchDB — documentos, consultas, feed de cambios, attachments, seguridad, design docs y mas — asi que cualquier cliente CouchDB (Fauxton, PouchDB, curl) puede conectarse.

Crear una base de datos

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    // En memoria (datos se pierden al soltar — ideal para pruebas)
    let db = Database::memory("mydb");

    // Persistente (almacenado en disco via redb)
    // let db = Database::open("mydb.redb", "mydb")?;

    // CouchDB remoto
    // let db = Database::http("http://admin:password@localhost:5984/mydb");

    Ok(())
}

Crear y leer documentos

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    // Crear un documento
    let result = db.put("user:alice", serde_json::json!({
        "name": "Alice",
        "email": "alice@example.com",
        "age": 30
    })).await?;

    println!("Creado con rev: {}", result.rev.unwrap());

    // Leerlo de vuelta
    let doc = db.get("user:alice").await?;
    println!("Nombre: {}", doc.data["name"]); // "Alice"

    Ok(())
}

Actualizar y eliminar

Cada actualizacion requiere la revision actual para prevenir conflictos:

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    // Crear
    let r1 = db.put("user:alice", serde_json::json!({"name": "Alice", "age": 30})).await?;
    let rev = r1.rev.unwrap();

    // Actualizar (debe proveer la rev actual)
    let r2 = db.update("user:alice", &rev, serde_json::json!({
        "name": "Alice",
        "age": 31
    })).await?;

    // Eliminar (debe proveer la rev actual)
    let rev2 = r2.rev.unwrap();
    db.remove("user:alice", &rev2).await?;

    Ok(())
}

Consultas con Mango

Encuentra documentos que coincidan con un selector:

use rouchdb::{Database, FindOptions};

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let db = Database::memory("mydb");

    db.put("alice", serde_json::json!({"name": "Alice", "age": 30})).await?;
    db.put("bob", serde_json::json!({"name": "Bob", "age": 25})).await?;
    db.put("carol", serde_json::json!({"name": "Carol", "age": 35})).await?;

    // Encontrar usuarios mayores de 28
    let result = db.find(FindOptions {
        selector: serde_json::json!({"age": {"$gte": 28}}),
        ..Default::default()
    }).await?;

    for doc in &result.docs {
        println!("{}: edad {}", doc["name"], doc["age"]);
    }
    // Alice: edad 30
    // Carol: edad 35

    Ok(())
}

Sincronizar dos bases de datos

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    let local = Database::memory("local");
    let remote = Database::memory("remote");

    // Agregar datos a cada lado
    local.put("doc1", serde_json::json!({"desde": "local"})).await?;
    remote.put("doc2", serde_json::json!({"desde": "remote"})).await?;

    // Sincronizacion bidireccional
    let (push, pull) = local.sync(&remote).await?;
    println!("Push: {} docs escritos", push.docs_written);
    println!("Pull: {} docs escritos", pull.docs_written);

    // Ambas bases de datos ahora tienen ambos documentos
    let info = local.info().await?;
    println!("Local tiene {} docs", info.doc_count); // 2

    Ok(())
}

Siguientes pasos

Operaciones CRUD

Esta guia cubre las operaciones fundamentales de documentos en RouchDB.

Crear un documento

Usa put para crear un documento con un ID especifico:

#![allow(unused)]
fn main() {
use rouchdb::Database;

let db = Database::memory("mydb");

let result = db.put("user:1", serde_json::json!({
    "name": "Alice",
    "email": "alice@example.com"
})).await?;

assert!(result.ok);
println!("Rev: {}", result.rev.unwrap()); // "1-abc123..."
}

El resultado contiene:

  • ok — true si la operacion fue exitosa
  • id — el ID del documento
  • rev — la cadena de revision asignada

Leer un documento

#![allow(unused)]
fn main() {
let doc = db.get("user:1").await?;

println!("ID: {}", doc.id);
println!("Rev: {}", doc.rev.unwrap());
println!("Datos: {}", doc.data);
println!("Nombre: {}", doc.data["name"]); // "Alice"
}

Si el documento no existe, se retorna RouchError::NotFound.

Actualizar un documento

Para actualizar, debes proveer la revision actual. Esto previene conflictos de escritura:

#![allow(unused)]
fn main() {
// Leer primero para obtener la revision actual
let doc = db.get("user:1").await?;
let rev = doc.rev.unwrap().to_string();

// Actualizar con la revision actual
let result = db.update("user:1", &rev, serde_json::json!({
    "name": "Alice Smith",
    "email": "alice.smith@example.com"
})).await?;

println!("Nueva rev: {}", result.rev.unwrap()); // "2-def456..."
}

Si la revision proporcionada no coincide con la actual, se retorna RouchError::Conflict.

Eliminar un documento

La eliminacion es un “soft delete” — marca el documento como eliminado pero mantiene el historial de revisiones:

#![allow(unused)]
fn main() {
let doc = db.get("user:1").await?;
let rev = doc.rev.unwrap().to_string();

let result = db.remove("user:1", &rev).await?;
assert!(result.ok);

// Intentar leer ahora retorna NotFound
let err = db.get("user:1").await;
assert!(err.is_err());
}

Operaciones en lote

bulk_docs escribe multiples documentos en una sola operacion:

#![allow(unused)]
fn main() {
use rouchdb::{Document, BulkDocsOptions};

let docs = vec![
    Document::new("user:1", serde_json::json!({"name": "Alice"})),
    Document::new("user:2", serde_json::json!({"name": "Bob"})),
];

let results = db.bulk_docs(docs, BulkDocsOptions::new()).await?;

for r in &results {
    println!("{}: ok={}", r.id, r.ok);
}
}

Listar todos los documentos

all_docs lista documentos con opciones de paginacion y filtrado:

#![allow(unused)]
fn main() {
use rouchdb::AllDocsOptions;

// Todos los documentos con sus datos
let response = db.all_docs(AllDocsOptions {
    include_docs: true,
    ..AllDocsOptions::new()
}).await?;

println!("Total: {} documentos", response.total_rows);

for row in &response.rows {
    // `key` es el ID del documento; `rev()`, su revision ganadora.
    println!("{}: rev {}", row.key, row.rev().unwrap_or_default());
    if let Some(ref doc) = row.doc {
        println!("  datos: {}", doc);
    }
}
}

Paginacion y rangos

#![allow(unused)]
fn main() {
// Documentos del 10 al 20
let page = db.all_docs(AllDocsOptions {
    skip: 10,
    limit: Some(10),
    ..AllDocsOptions::new()
}).await?;

// Solo documentos con IDs que empiezan con "user:"
let users = db.all_docs(AllDocsOptions {
    start_key: Some("user:".into()),
    end_key: Some("user:\u{ffff}".into()),
    include_docs: true,
    ..AllDocsOptions::new()
}).await?;

// Documentos especificos por ID: una fila por clave, en orden. Una clave
// sin documento da una fila de error (`row.is_error()`, sin `id` ni `value`)
// y un documento borrado, una fila con `row.is_deleted()` y sin `doc`.
let specific = db.all_docs(AllDocsOptions {
    keys: Some(vec!["user:1".into(), "user:2".into()]),
    include_docs: true,
    ..AllDocsOptions::new()
}).await?;
}

Manejo de errores

#![allow(unused)]
fn main() {
use rouchdb::RouchError;

match db.get("no-existe").await {
    Ok(doc) => println!("Encontrado: {}", doc.data),
    Err(RouchError::NotFound(_)) => println!("Documento no encontrado"),
    Err(e) => eprintln!("Error inesperado: {}", e),
}

// Manejar conflictos de actualizacion
match db.update("user:1", "rev-incorrecta", serde_json::json!({})).await {
    Ok(_) => println!("Actualizado"),
    Err(RouchError::Conflict) => println!("Conflicto: el documento fue modificado"),
    Err(e) => eprintln!("Error: {}", e),
}
}

Consultas

RouchDB ofrece dos formas de consultar documentos: consultas Mango (selectores declarativos) y vistas map/reduce (funciones personalizadas).

Consultas Mango

Mango te permite buscar documentos usando selectores JSON, similar a MongoDB:

#![allow(unused)]
fn main() {
use rouchdb::{Database, FindOptions};

let db = Database::memory("mydb");

// Insertar datos de ejemplo
db.put("alice", serde_json::json!({"name": "Alice", "age": 30, "role": "admin"})).await?;
db.put("bob", serde_json::json!({"name": "Bob", "age": 25, "role": "user"})).await?;
db.put("carol", serde_json::json!({"name": "Carol", "age": 35, "role": "admin"})).await?;

// Buscar admins mayores de 28
let result = db.find(FindOptions {
    selector: serde_json::json!({
        "role": "admin",
        "age": {"$gte": 28}
    }),
    ..Default::default()
}).await?;

for doc in &result.docs {
    println!("{}", doc["name"]); // Alice, Carol
}
}

Operadores disponibles

OperadorDescripcionEjemplo
$eqIgual a{"name": {"$eq": "Alice"}} o {"name": "Alice"}
$neNo igual a{"role": {"$ne": "admin"}}
$gtMayor que{"age": {"$gt": 25}}
$gteMayor o igual que{"age": {"$gte": 25}}
$ltMenor que{"age": {"$lt": 30}}
$lteMenor o igual que{"age": {"$lte": 30}}
$inEsta en la lista{"role": {"$in": ["admin", "mod"]}}
$ninNo esta en la lista{"role": {"$nin": ["banned"]}}
$existsEl campo existe{"email": {"$exists": true}}
$regexCoincide con regex{"name": {"$regex": "^A"}}
$orLogico OR{"$or": [{"age": 25}, {"age": 30}]}
$andLogico AND{"$and": [{"age": {"$gt": 20}}, {"role": "admin"}]}
$notNegacion{"age": {"$not": {"$gt": 30}}}
$elemMatchCoincide en array{"tags": {"$elemMatch": {"$eq": "rust"}}}
$allContiene todos{"tags": {"$all": ["rust", "db"]}}
$sizeTamano del array{"tags": {"$size": 3}}
$modModulo{"age": {"$mod": [5, 0]}}
$typeTipo de valor{"name": {"$type": "string"}}

Proyeccion de campos

Selecciona solo los campos que necesitas:

#![allow(unused)]
fn main() {
let result = db.find(FindOptions {
    selector: serde_json::json!({"role": "admin"}),
    fields: Some(vec!["name".into(), "age".into()]),
    ..Default::default()
}).await?;
// Los documentos solo contienen _id, name, age
}

Ordenamiento

#![allow(unused)]
fn main() {
use rouchdb::SortField;

let result = db.find(FindOptions {
    selector: serde_json::json!({"age": {"$gt": 0}}),
    sort: Some(vec![
        SortField::Simple("age".into()),  // ascendente por defecto
    ]),
    ..Default::default()
}).await?;
}

Paginacion

#![allow(unused)]
fn main() {
let result = db.find(FindOptions {
    selector: serde_json::json!({"age": {"$gt": 0}}),
    skip: Some(10),
    limit: Some(5),
    ..Default::default()
}).await?;
}

Vistas Map/Reduce

Para consultas mas complejas, usa query_view con funciones map y reduce de Rust:

#![allow(unused)]
fn main() {
use rouchdb::{query_view, ViewQueryOptions, ReduceFn};

// Map: emite pares clave-valor por cada documento
let result = query_view(
    db.adapter(),
    &|doc| {
        // Emitir el rol como clave y 1 como valor
        if let Some(role) = doc.get("role").and_then(|r| r.as_str()) {
            vec![(serde_json::json!(role), serde_json::json!(1))]
        } else {
            vec![]
        }
    },
    Some(&ReduceFn::Count),  // Contar por grupo
    ViewQueryOptions {
        reduce: true,
        group: true,
        ..ViewQueryOptions::new()
    },
).await?;

for row in &result.rows {
    println!("{}: {} usuarios", row.key, row.value);
}
// "admin": 2 usuarios
// "user": 1 usuarios
}

Reduces integrados

FuncionDescripcion
ReduceFn::SumSuma todos los valores numericos
ReduceFn::CountCuenta el numero de filas
ReduceFn::StatsCalcula estadisticas: sum, count, min, max, sumsqr
ReduceFn::Custom(fn)Funcion reduce personalizada

Rangos de claves

#![allow(unused)]
fn main() {
let result = query_view(
    db.adapter(),
    &|doc| {
        let age = doc.get("age").cloned().unwrap_or(serde_json::json!(null));
        let name = doc.get("name").cloned().unwrap_or(serde_json::json!(null));
        vec![(age, name)]
    },
    None,
    ViewQueryOptions {
        start_key: Some(serde_json::json!(25)),
        end_key: Some(serde_json::json!(35)),
        ..ViewQueryOptions::new()
    },
).await?;
}

Mango vs Map/Reduce: cuando usar cada uno

CriterioMangoMap/Reduce
SimplicidadSelectores JSON, facil de usarRequiere escribir closures
FlexibilidadLimitado a operadores predefinidosLogica arbitraria de Rust
AgregacionesNo soportadoSum, Count, Stats, Custom
AgrupamientoNo soportadogroup, group_level
Uso tipicoFiltrar documentosReportes y analisis

Replicacion

RouchDB implementa el protocolo de replicacion de CouchDB, permitiendo sincronizacion bidireccional entre bases de datos locales y remotas.

Conceptos basicos

La replicacion copia cambios de una base de datos fuente a una base de datos destino. Es incremental: solo transfiere los documentos que han cambiado desde la ultima sincronizacion.

Replicacion basica

Push (local a remoto)

#![allow(unused)]
fn main() {
use rouchdb::Database;

let local = Database::open("mydb.redb", "mydb")?;
let remote = Database::http("http://admin:password@localhost:5984/mydb");

let result = local.replicate_to(&remote).await?;
println!("Docs leidos: {}", result.docs_read);
println!("Docs escritos: {}", result.docs_written);
}

Pull (remoto a local)

#![allow(unused)]
fn main() {
let result = local.replicate_from(&remote).await?;
}

Sync (bidireccional)

#![allow(unused)]
fn main() {
let (push, pull) = local.sync(&remote).await?;
println!("Push: {} escritos", push.docs_written);
println!("Pull: {} escritos", pull.docs_written);
}

Configurar CouchDB

Para pruebas, usa Docker Compose:

docker compose up -d

Esto levanta CouchDB en http://localhost:15984 con credenciales admin:password.

Para crear una base de datos en CouchDB:

curl -X PUT http://admin:password@localhost:15984/mydb

Opciones de replicacion

#![allow(unused)]
fn main() {
use rouchdb::ReplicationOptions;

let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    batch_size: 50,
    ..Default::default()
}).await?;
}
CampoTipoDefaultDescripcion
batch_sizeu64100Numero de cambios a procesar por iteracion
batches_limitu6410Maximo numero de lotes a buffear
filterOption<ReplicationFilter>NoneFiltro opcional para replicacion selectiva
sinceOption<Seq>NoneSecuencia inicial (en vez de leer checkpoint)
checkpointbooltrueDeshabilitar con false para no guardar/leer checkpoints
liveboolfalseHabilitar replicacion continua
retryboolfalseReintentar automaticamente en caso de error
poll_intervalDuration500msIntervalo de sondeo en modo continuo
back_off_functionOption<Box<dyn Fn(u32) -> Duration + Send + Sync>>NoneFuncion de backoff para reintentos

Replicacion filtrada

Se puede replicar un subconjunto de documentos usando ReplicationFilter:

#![allow(unused)]
fn main() {
use rouchdb::{ReplicationOptions, ReplicationFilter};

// Por IDs de documento
let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    filter: Some(ReplicationFilter::DocIds(vec!["doc1".into(), "doc2".into()])),
    ..Default::default()
}).await?;

// Por selector Mango
let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    filter: Some(ReplicationFilter::Selector(serde_json::json!({"type": "invoice"}))),
    ..Default::default()
}).await?;

// Por closure personalizado
let result = local.replicate_to_with_opts(&remote, ReplicationOptions {
    filter: Some(ReplicationFilter::Custom(std::sync::Arc::new(|change| {
        change.id.starts_with("public:")
    }))),
    ..Default::default()
}).await?;
}

Checkpoints

Los checkpoints permiten que la replicacion se reanude despues de una interrupcion. RouchDB guarda el progreso en documentos locales (_local/{replication_id}) en ambos lados.

Cuando la replicacion se reinicia:

  1. Lee el checkpoint de ambos lados
  2. Encuentra la ultima secuencia comun
  3. Reanuda desde ahi (no necesita empezar desde cero)

Resultado de la replicacion

#![allow(unused)]
fn main() {
let result = local.replicate_to(&remote).await?;

if result.ok {
    println!("Replicacion exitosa");
    println!("  Docs leidos: {}", result.docs_read);
    println!("  Docs escritos: {}", result.docs_written);
    println!("  Ultima secuencia: {:?}", result.last_seq);
} else {
    println!("Replicacion con errores:");
    for err in &result.errors {
        println!("  - {}", err);
    }
}
}
CampoTipoDescripcion
okbooltrue si no hubo errores
docs_readu64Cambios procesados
docs_writtenu64Documentos escritos en el destino
errorsVec<String>Errores individuales por documento
last_seqSeqUltima secuencia alcanzada

Ejemplo completo

Sincronizar una base de datos local redb con CouchDB:

use rouchdb::Database;

#[tokio::main]
async fn main() -> rouchdb::Result<()> {
    // Base de datos local persistente
    let local = Database::open("app.redb", "app")?;

    // CouchDB remoto
    let remote = Database::http("http://admin:password@localhost:5984/app");

    // Agregar datos localmente (funciona offline)
    local.put("nota:1", serde_json::json!({
        "titulo": "Mi primera nota",
        "contenido": "Hola desde RouchDB!"
    })).await?;

    // Cuando hay conexion, sincronizar
    let (push, pull) = local.sync(&remote).await?;

    println!("Sincronizacion completa:");
    println!("  Push: {} docs", push.docs_written);
    println!("  Pull: {} docs", pull.docs_written);

    Ok(())
}

Replicacion con eventos

Usa replicate_to_with_events() para recibir eventos de progreso durante la replicacion:

#![allow(unused)]
fn main() {
use rouchdb::ReplicationEvent;

let (result, mut rx) = local.replicate_to_with_events(
    &remote,
    ReplicationOptions::default(),
).await?;

while let Ok(event) = rx.try_recv() {
    match event {
        ReplicationEvent::Active => println!("Replicacion iniciada"),
        ReplicationEvent::Change { docs_read, .. } => {
            println!("Progreso: {} docs leidos", docs_read);
        }
        ReplicationEvent::Complete(r) => {
            println!("Completado: {} escritos", r.docs_written);
        }
        ReplicationEvent::Error(msg) => println!("Error: {}", msg),
        ReplicationEvent::Paused => println!("Esperando cambios..."),
        // `ReplicationEvent` es `#[non_exhaustive]`: puede tener nuevos eventos.
        _ => {}
    }
}
}

Replicacion continua (live)

La replicacion continua se ejecuta en segundo plano, sondeando periodicamente por nuevos cambios. Equivalente a { live: true } en PouchDB.

#![allow(unused)]
fn main() {
use rouchdb::{ReplicationOptions, ReplicationEvent};

let (mut rx, handle) = local.replicate_to_live(&remote, ReplicationOptions {
    live: true,
    poll_interval: std::time::Duration::from_millis(500),
    retry: true,
    ..Default::default()
});

// Procesar eventos en un loop
tokio::spawn(async move {
    while let Some(event) = rx.recv().await {
        match event {
            ReplicationEvent::Complete(r) => {
                println!("Lote completado: {} docs escritos", r.docs_written);
            }
            ReplicationEvent::Paused => {
                println!("Actualizado, esperando nuevos cambios...");
            }
            _ => {}
        }
    }
});

// Cancelar cuando sea necesario
handle.cancel();
}

El ReplicationHandle controla la replicacion:

  • handle.cancel() detiene la replicacion.
  • Si se descarta el handle (Drop), la replicacion tambien se cancela.

Manejo de errores

  • Error de red: la replicacion se detiene. El checkpoint guarda el progreso para reanudar despues.
  • Error de autenticacion (401/403): la replicacion se detiene inmediatamente.
  • Error en documento individual: se registra en result.errors pero la replicacion continua con los demas documentos.
  • Conflicto de checkpoint (409): se reintenta con la ultima revision.