Document

interface Document<T>(source)

A typed, document-oriented view over a single key in storage.

Each document is stored as one key per field. Reads and writes are synchronous and non-blocking; writes to the same document are serialized so a builder-style update is atomic. Reactive observation is available through flow and stateFlow.

Type Parameters

T

the document's value type, which must be @Serializable.

Functions

Link copied to clipboard
abstract fun delete()

Removes the document and all of its field keys. A subsequent get returns null.

Link copied to clipboard
abstract fun exists(): Boolean

Returns true when the document has at least one stored field key.

Link copied to clipboard
inline fun <V> Document<*>.field(prop: KProperty1<*, V>, default: V): ReadWriteProperty<Any?, V>

A property delegate backed by the single decomposed key for prop on this document.

Link copied to clipboard
inline fun <V> Document<*>.fieldFlow(prop: KProperty1<*, V>, default: V): Flow<V>

A cold Flow of the value of the single field prop on this document.

Link copied to clipboard
abstract fun flow(): Flow<T?>

A cold Flow that emits the current value on collection, then the new value after each committed write to this document. Emits null when the document is deleted or absent. Emissions are conflated and happen only after the write is durably committed.

Link copied to clipboard
abstract fun get(): T?

Returns the current value, or null when the document is absent.

Link copied to clipboard
abstract fun set(value: T)

Replaces the document with value, removing any field keys not present in it.

Link copied to clipboard
abstract fun stateFlow(scope: CoroutineScope): StateFlow<T?>

A hot StateFlow over flow, shared within scope. Holds the current value as its initial state and updates after each committed write. Emits null when the document is deleted or absent. Sharing stays active while there are active subscribers.

Link copied to clipboard
abstract fun update(builder: (T) -> T)

Updates the document by applying builder to the current value, preserving untouched fields. The baseline is the current value, or the type's defaults when the document is absent. The read-modify-write runs under the document's write lock.

Link copied to clipboard
inline fun <T, V> Document<T>.update(prop: KProperty1<T, V>, value: V)

Writes value directly to the single decomposed key for prop on this document, with no read of the rest of the document. Emits on the document's Document.flow.