Tearleads

document_projection Client SQLite table

Queryable document summary and detail projection.

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

Description

The durable Loro state lives in documents. This table stores the fields the app needs for document lists, summaries, and fast detail hydration without opening the full Loro snapshot on every read.

The projected full text lives in documentProjectionText, keyed by the same localId. Keeping it out of this row keeps list/window scans narrow: the sidebar and summary queries read titles and placement from rows whose size does not grow with document content.

Indexes:

  • localId is the primary key for document projection lookups.
  • (containerId, updatedAt, localId) where containerId is not null serves container-scoped window/count scans without walking the whole table.
  • (organizationId) where containerId is null serves the orphan-recovery existence and window scans.
  • (documentId) where documentId is not null serves the linked-placement join from documentContainerProjection and server-id lookups.

Columns

  • local_idtext
    • primary key
    • nullable

    Stable local document id. This joins to documents.localId for rows where documents.appKind = documents.

  • document_idtext
    • nullable

    Server document id when known.

  • container_idtext
    • nullable

    Selected container for primary document placement, or null if the document is not currently linked to a visible container.

  • organization_idtext
    • nullable

    Last-known owning organization. Stamped from the container row when the document is detached because its container was removed locally (e.g. access to a shared org was revoked and the container tombstoned), so pending writes keep their org attribution after the containers join source disappears. Read as a fallback only — a live container join always wins.

  • document_kindtext
    • not null
    • default 'note'

    Stored document kind. Defaults to note.

  • icontext
    • nullable

    Optional custom icon id (<pack>:<name>) projected from document metadata.

  • titletext
    • not null
    • default 'Untitled note'

    Plain projected title. Defaults to the note fallback title.

  • updated_attext
    • not null

    Timestamp used for document list ordering.

Indexes

  • document_projection_container_updated_idx(container_id, updated_at, local_id)
    • partial: where "container_id" IS NOT NULL
  • document_projection_orphan_organization_idx(organization_id)
    • partial: where "container_id" IS NULL
  • document_projection_document_idx(document_id)
    • partial: where "document_id" IS NOT NULL

DDL

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

CREATE TABLE IF NOT EXISTS "document_projection" (
  "local_id" TEXT PRIMARY KEY,
  "document_id" TEXT,
  "container_id" TEXT,
  "organization_id" TEXT,
  "document_kind" TEXT NOT NULL DEFAULT 'note',
  "icon" TEXT,
  "title" TEXT NOT NULL DEFAULT 'Untitled note',
  "updated_at" TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS "document_projection_container_updated_idx" ON "document_projection" ("container_id", "updated_at", "local_id") WHERE "container_id" IS NOT NULL;

CREATE INDEX IF NOT EXISTS "document_projection_orphan_organization_idx" ON "document_projection" ("organization_id") WHERE "container_id" IS NULL;

CREATE INDEX IF NOT EXISTS "document_projection_document_idx" ON "document_projection" ("document_id") WHERE "document_id" IS NOT NULL;