module CouchDB::Adapter

Overview

Abstract interface implemented by both Adapter::SQLite and Adapter::HTTP.

Users interact with CouchDB::Database, which wraps an adapter and delegates all operations to it. Adapters are not instantiated directly in normal usage.

Direct including types

Defined in:

couchdb/adapter/base.cr
couchdb/adapter/http.cr
couchdb/adapter/sqlite.cr

Instance Method Summary

Instance Method Detail

abstract def all_docs(include_docs : Bool, limit : Int32 | Nil, skip : Int32, startkey : String | Nil, endkey : String | Nil) : NamedTuple(total_rows: Int64, offset: Int32, rows: Array(JSON::Any)) #

Returns all non-deleted winning revisions, sorted by _id.

Pass include_docs: true to embed full document bodies in each row's "doc" field. Use limit and skip for pagination. Use startkey and endkey for range queries (both bounds are inclusive).


[View source]
abstract def bulk_docs(docs : Array(Document), new_edits : Bool) : Array(NamedTuple(id: String, rev: String, ok: Bool)) #

Writes multiple documents in a single operation.

When new_edits is true (default), normal conflict detection applies and new revisions are generated. When new_edits is false, documents are stored with their supplied revision strings exactly as given — this is the replication write path and bypasses conflict checking.


[View source]
abstract def bulk_get(id_revs : Array(NamedTuple(id: String, rev: String))) : Array(Document) #

Fetches specific {id, rev} pairs in bulk.

Returns only documents that were found; missing pairs are silently skipped. Used internally by the replication engine after #revs_diff.


[View source]
abstract def changes(since : String, limit : Int32 | Nil, include_docs : Bool) : NamedTuple(last_seq: String, results: Array(JSON::Any)) #

Returns the changes feed since a given sequence number.

since is the last sequence number already seen (use "0" to fetch all changes). Pass include_docs: true to embed full document bodies in each change entry.


[View source]
abstract def changes_feed(since : String, heartbeat : Int32, include_docs : Bool, &block : JSON::Any -> _) #

Streams changes continuously, yielding each change as a JSON::Any to the block. The caller can break from the block to stop the feed.

since — starting sequence ("0" for all). heartbeat — polling interval in ms (SQLite) or CouchDB heartbeat interval in ms (HTTP). include_docs: true embeds full document bodies.


[View source]
abstract def close #

Releases the underlying connection or database handle.

For Adapter::SQLite this closes the connection pool. For Adapter::HTTP this is a no-op (connections are request-scoped). Database delegates this when called.


[View source]
abstract def delete_attachment(id : String, attname : String, rev : String) : NamedTuple(ok: Bool, id: String, rev: String) #

Removes attachment attname from document id at revision rev. Writes a new document revision. Raises Conflict on rev mismatch.


[View source]
abstract def get(id : String) : Document #

Fetches the winning revision of a document by ID. Raises NotFound if the document is absent or has been deleted.


[View source]
abstract def get_attachment(id : String, attname : String) : NamedTuple(data: Bytes, content_type: String) #

Returns the raw bytes and content-type for attname on document id. Raises NotFound if the document or attachment is absent.


[View source]
abstract def get_local(id : String) : Document #

Reads a _local/ checkpoint document by ID.

_local/ documents are never replicated. Raises NotFound if absent.


[View source]
abstract def info : NamedTuple(db_name: String, doc_count: Int64, update_seq: Int64) #

Returns database metadata: name, live document count, and latest update sequence.


[View source]
abstract def put(doc : Document) : NamedTuple(ok: Bool, id: String, rev: String) #

Creates or updates a document.

For updates, doc.rev must match the current winning revision; raises Conflict on a mismatch. For new documents, doc.rev must be nil or empty. Returns {ok: true, id:, rev:} on success.


[View source]
abstract def put_attachment(id : String, attname : String, rev : String, data : Bytes, content_type : String) : NamedTuple(ok: Bool, id: String, rev: String) #

Stores data as attachment attname on document id at revision rev. Writes a new document revision (as CouchDB does). Raises Conflict on rev mismatch.


[View source]
abstract def put_local(doc : Document) : NamedTuple(ok: Bool, id: String, rev: String) #

Writes a _local/ checkpoint document, creating or replacing it.

The _local/ prefix is added automatically if not already present.


[View source]
abstract def remove(id : String, rev : String) : NamedTuple(ok: Bool) #

Soft-deletes a document by writing a tombstone revision. Raises Conflict when rev does not match the current winning revision.


[View source]
abstract def revs_diff(id_revs : Hash(String, Array(String))) : Hash(String, NamedTuple(missing: Array(String))) #

Given a map of id → [revs], returns only the revisions missing from this adapter.

Used by the replication engine to determine which documents need to be transferred. Returns an empty hash when all supplied revisions are already present.


[View source]