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

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.
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.
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 { id: "a".into(), rev: None, deleted: false, data: json!({}), attachments: HashMap::new() },
    Document { id: "b".into(), rev: None, deleted: false, data: json!({}), attachments: HashMap::new() },
];
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.
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.
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. This is irreversible.

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?;
}