class CouchDB::Adapter::SQLite

Overview

Local-storage adapter backed by SQLite3.

All reads and writes go to a single SQLite file, making the database available without any network connection. Use ":memory:" for a transient in-memory database (useful for testing).

Four tables underpin the adapter:

The winning revision for a given document is the one with the highest seq value. Deleted documents are soft-deleted (a deleted=1 row is stored) so their revision history remains available for replication.

Included Modules

Defined in:

couchdb/adapter/sqlite.cr

Constant Summary

SCHEMA = " CREATE TABLE IF NOT EXISTS docs (\n id TEXT NOT NULL,\n rev TEXT NOT NULL,\n seq INTEGER NOT NULL,\n deleted INTEGER NOT NULL DEFAULT 0,\n body TEXT NOT NULL,\n PRIMARY KEY (id, rev)\n );\n CREATE INDEX IF NOT EXISTS idx_docs_id_seq ON docs(id, seq DESC);\n\n CREATE TABLE IF NOT EXISTS revs (\n id TEXT NOT NULL,\n rev TEXT NOT NULL,\n parent_rev TEXT,\n PRIMARY KEY (id, rev)\n );\n\n CREATE TABLE IF NOT EXISTS local_docs (\n id TEXT PRIMARY KEY,\n body TEXT NOT NULL\n );\n\n CREATE TABLE IF NOT EXISTS update_seq (\n seq INTEGER PRIMARY KEY AUTOINCREMENT,\n doc_id TEXT NOT NULL,\n doc_rev TEXT NOT NULL\n );\n\n CREATE TABLE IF NOT EXISTS attachments (\n doc_id TEXT NOT NULL,\n name TEXT NOT NULL,\n content_type TEXT NOT NULL DEFAULT 'application/octet-stream',\n data BLOB NOT NULL,\n PRIMARY KEY (doc_id, name)\n );"

Constructors

Instance Method Summary

Instance methods inherited from module CouchDB::Adapter

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)) all_docs, bulk_docs(docs : Array(Document), new_edits : Bool) : Array(NamedTuple(id: String, rev: String, ok: Bool)) bulk_docs, bulk_get(id_revs : Array(NamedTuple(id: String, rev: String))) : Array(Document) bulk_get, changes(since : String, limit : Int32 | Nil, include_docs : Bool) : NamedTuple(last_seq: String, results: Array(JSON::Any)) changes, changes_feed(since : String, heartbeat : Int32, include_docs : Bool, &block : JSON::Any -> _) changes_feed, close close, delete_attachment(id : String, attname : String, rev : String) : NamedTuple(ok: Bool, id: String, rev: String) delete_attachment, get(id : String) : Document get, get_attachment(id : String, attname : String) : NamedTuple(data: Bytes, content_type: String) get_attachment, get_local(id : String) : Document get_local, info : NamedTuple(db_name: String, doc_count: Int64, update_seq: Int64) info, put(doc : Document) : NamedTuple(ok: Bool, id: String, rev: String) put, put_attachment(id : String, attname : String, rev : String, data : Bytes, content_type : String) : NamedTuple(ok: Bool, id: String, rev: String) put_attachment, put_local(doc : Document) : NamedTuple(ok: Bool, id: String, rev: String) put_local, remove(id : String, rev : String) : NamedTuple(ok: Bool) remove, revs_diff(id_revs : Hash(String, Array(String))) : Hash(String, NamedTuple(missing: Array(String))) revs_diff

Constructor Detail

def self.new(path : String) #

Opens (or creates) a SQLite3 database at path.

Pass ":memory:" for a transient in-memory database. If path does not end in .db, the extension is appended automatically by Database.new. The connection pool is limited to a single connection (max_pool_size=1) to keep :memory: databases on one connection and avoid SQLite write-lock contention on file databases.


[View source]

Instance Method Detail

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

SQLite implementation of Adapter#all_docs. See Adapter#all_docs for the contract.


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

SQLite implementation of Adapter#bulk_docs. See Adapter#bulk_docs for the contract.


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

SQLite implementation of Adapter#bulk_get. See Adapter#bulk_get for the contract.


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

SQLite implementation of Adapter#changes. See Adapter#changes for the contract.


[View source]
def changes_feed(since : String = "0", heartbeat : Int32 = 1000, include_docs : Bool = false, & : JSON::Any -> _) #

SQLite implementation of Adapter#changes_feed. See Adapter#changes_feed for the contract.


[View source]
def close #

Closes the underlying database connection pool.


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

SQLite implementation of Adapter#delete_attachment. See Adapter#delete_attachment for the contract.


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

SQLite implementation of Adapter#get. See Adapter#get for the contract.


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

SQLite implementation of Adapter#get_attachment. See Adapter#get_attachment for the contract.


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

SQLite implementation of Adapter#get_local. See Adapter#get_local for the contract.


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

SQLite implementation of Adapter#info. See Adapter#info for the contract.


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

SQLite implementation of Adapter#put. See Adapter#put for the contract.


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

SQLite implementation of Adapter#put_attachment. See Adapter#put_attachment for the contract.


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

SQLite implementation of Adapter#put_local. See Adapter#put_local for the contract.


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

SQLite implementation of Adapter#remove. See Adapter#remove for the contract.


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

SQLite implementation of Adapter#revs_diff. See Adapter#revs_diff for the contract.


[View source]