class CouchDB::Document

Overview

An open-schema CouchDB document with typed _id, _rev, and _deleted fields.

Document uses JSON::Serializable for round-trip JSON encoding and JSON::Serializable::Unmapped to preserve any extra fields in json_unmapped. Subclass it to add strongly-typed application fields:

class Note < CouchDB::Document
  property title : String = ""
  property body : String = ""
end

Subclass properties are serialized as top-level JSON keys alongside _id and _rev. Unknown fields are transparently preserved through json_unmapped and survive replication without loss.

Included Modules

Defined in:

couchdb/document.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(pull : JSON::PullParser) #

[View source]
def self.new #

Creates a new empty document. Set #id before calling put.


[View source]
def self.new(*, __pull_for_json_serializable pull : JSON::PullParser) #

[View source]

Instance Method Detail

def [](key : String) : JSON::Any #

Reads a field by JSON key.

Routes "_id", "_rev", and "_deleted" to their typed properties; all other keys are read from json_unmapped. Raises KeyError if the key is absent (use #[]? for a nil-safe variant).


[View source]
def []=(key : String, value : JSON::Any) #

Writes a field by JSON key.

Routes "_id", "_rev", and "_deleted" to their typed properties; all other keys are stored in json_unmapped.


[View source]
def []?(key : String) : JSON::Any | Nil #

Reads a field by JSON key, returning nil if the key is absent.

Routes "_id", "_rev", and "_deleted" to their typed properties; all other keys are read from json_unmapped.


[View source]
def deleted : Bool | Nil #

[View source]
def deleted=(deleted : Bool | Nil) #

[View source]
def deleted? : Bool #

Returns true when #deleted is explicitly set to true.


[View source]
def id : String #

[View source]
def id=(id : String) #

[View source]
def next_rev : String #

Computes the next revision string "N-<md5>" without mutating self.

The hash is computed over the document JSON with _rev removed, matching the CouchDB wire format. Delegates to DocumentHelper.next_rev.


[View source]
def rev : String | Nil #

[View source]
def rev=(rev : String | Nil) #

[View source]
def rev_num : Int32 #

Returns the integer generation counter from the current #rev (e.g. "3-abc"3). Returns 0 for new documents that have no #rev.


[View source]