Tearleads

documents Client SQLite table

Durable Loro-backed document records shared by app features.

Declared in packages/client-sdk/src/data/sqlite/schema/documentRecordSchema.ts

Description

This table stores encrypted document runtime state for multiple app domains. appKind namespaces records for documents and container metadata. User-facing list data is projected into feature-specific read models.

Indexes:

  • (appKind, localId) is the primary key and scopes local document lookups.
  • (appKind, documentId) where documentId is not null supports resolving a server document id back to the local row during discovery and relinking.
  • (appKind, localId) over records whose pendingBaseVersion differs from a non-empty snapshotEndVersion serves the pending-write queue's deferred-tail scan, which repeats that condition, so it reads only unsettled records instead of every synced one.

Columns

  • app_kindtext
    • not null

    Local domain namespace for the row, such as documents or container-metadata.

  • local_idtext
    • not null

    Stable local id inside appKind. It may be created before a server documentId exists.

  • document_idtext
    • nullable

    Server document id once the record is known remotely, or null while the document is local-only.

  • access_epochinteger
    • not null
    • default 1

    Latest access epoch persisted for the document. Defaults to the initial epoch.

  • access_state_hashtext
    • nullable

    Current document access/manifest state hash returned by sync, when known.

  • effective_access_leveltext
    • not null
    • default 'read'

    This user's access to the document as the server lists it: read, write, or admin. Defaults to read, and readers treat an unknown value as read; only write and admin allow local edits.

  • last_commit_lsntext
    • nullable

    Last remote commit cursor applied to this local document.

  • document_manifest_bundletext
    • nullable

    Serialized document manifest bundle used for encrypted document sync.

  • content_key_bundletext
    • nullable

    Serialized content-key material needed to open the local document.

  • document_kek_targetstext
    • nullable

    Serialized KEK target state for document key wraps.

  • pending_base_versiontext
    • nullable

    Encoded oplog version through which every persisted op is already enqueued or synced. Persisted so a restart restores the outgoing-delta marker instead of re-seeding it past a device-first deferRemoteSync write's un-advanced op. null when unset.

  • pull_continuationtext
    • nullable

    Durable progress for the next bounded remote pull.

  • peer_salttext
    • nullable

    Salt that moved this record's writer peer after the server refused its old one as another user's (recordPeerSeed); null while the record writes on its first derived peer.

  • committed_versiontext
    • nullable

    Encoded version vector of what sync settlements showed the server to hold of the record's history: per peer, the highest counter of any update a pull served and this device accepted, or of any outgoing update the server acknowledged. A settlement and a peer recovery's install raise it, by merging; no save, queue change, relay, rebase, or rotation lowers it. A remote reset and a discard clear it. null until the first settlement. A peer refusal is judged against it and against where the queue starts (knownCommittedOf).

  • recovery_generationinteger
    • not null
    • default 0

    Atomic raw-history recovery writer fence.

  • recovery_document_idtext
    • nullable

    Former server id for cross-client post-purge recreation.

  • snapshot_end_versiontext
    • not null
    • default ''

    Encoded end version vector of the persisted content (which lives in the durable-history tables, never on this row), written on every content persist. Lets the pending-write queue's deferred-tail scan compare version coverage without loading content. Empty string when the document has never hydrated content, which readers treat as "no deferred tail claimable".

  • sync_failure_codetext
    • nullable

    Closed SyncRefusalCode of the recorded failure, or null when it has none. remotely_purged marks a copy with unsynced edits that a verified remote purge moved into Recovery; it is terminal: no retry or later failure replaces or clears it until the local copy is discarded.

  • sync_failure_messagetext
    • nullable

    The last terminal sync failure or quarantined incoming update for this scope. The durable write queue (queued rows of documentHistoryUpdates) has no status columns, so these let the write-queue view and footer explain why the document is blocked. syncFailureMessage is non-null exactly when a failure is recorded; a newer failure replaces the old, and a later fully validated sync pass, a remote reset, or the scope's teardown clears both.

  • sync_failure_attempted_attext
    • nullable

    The last terminal sync failure or quarantined incoming update for this scope. The durable write queue (queued rows of documentHistoryUpdates) has no status columns, so these let the write-queue view and footer explain why the document is blocked. syncFailureMessage is non-null exactly when a failure is recorded; a newer failure replaces the old, and a later fully validated sync pass, a remote reset, or the scope's teardown clears both.

  • updated_attext
    • not null

    Local timestamp for the last persisted runtime-state update.

Keys and constraints

  • Primary key(app_kind, local_id)

Indexes

  • documents_app_document_idx(app_kind, document_id)
    • partial: where "document_id" IS NOT NULL
  • documents_deferred_tail_idx(app_kind, local_id)
    • partial: where "snapshot_end_version" <> '' AND "pending_base_version" IS NOT "snapshot_end_version"
  • documents_sync_failure_idx(app_kind, local_id)
    • partial: where "sync_failure_message" IS NOT NULL

DDL

The statements the client runs to create this table and its indexes.

CREATE TABLE IF NOT EXISTS "documents" (
  "app_kind" TEXT NOT NULL,
  "local_id" TEXT NOT NULL,
  "document_id" TEXT,
  "access_epoch" INTEGER NOT NULL DEFAULT 1,
  "access_state_hash" TEXT,
  "effective_access_level" TEXT NOT NULL DEFAULT 'read',
  "last_commit_lsn" TEXT,
  "document_manifest_bundle" TEXT,
  "content_key_bundle" TEXT,
  "document_kek_targets" TEXT,
  "pending_base_version" TEXT,
  "pull_continuation" TEXT,
  "peer_salt" TEXT,
  "committed_version" TEXT,
  "recovery_generation" INTEGER NOT NULL DEFAULT 0,
  "recovery_document_id" TEXT,
  "snapshot_end_version" TEXT NOT NULL DEFAULT '',
  "sync_failure_code" TEXT,
  "sync_failure_message" TEXT,
  "sync_failure_attempted_at" TEXT,
  "updated_at" TEXT NOT NULL,
  PRIMARY KEY ("app_kind", "local_id")
);

CREATE INDEX IF NOT EXISTS "documents_app_document_idx" ON "documents" ("app_kind", "document_id") WHERE "document_id" IS NOT NULL;

CREATE INDEX IF NOT EXISTS "documents_deferred_tail_idx" ON "documents" ("app_kind", "local_id") WHERE "snapshot_end_version" <> '' AND "pending_base_version" IS NOT "snapshot_end_version";

CREATE INDEX IF NOT EXISTS "documents_sync_failure_idx" ON "documents" ("app_kind", "local_id") WHERE "sync_failure_message" IS NOT NULL;