# 0024. Client collections with TanStack DB

The repository's architectural decision, rationale and consequences.

# 0024. Client collections with TanStack DB

Accepted · 2026-11-29 · merges after TanStack DB 1.0.0 and the two-day quarantine

## Decision [#decision]

A view that shows a bounded read whole and whose rows a Member changes often is a **collection**: the web Board (four lanes of 50) and Calendar (500 per window) are the first. Every other read stays TanStack Query, and money, access and account data never get one.

- **Where it lives.** `features/<feature>/<feature>.collections.ts` holds the views and `<feature>.intents.ts` the writes; both are private to the feature. `platform/db/` holds the scope, the `DbClient` lifecycle and the write queue. Only those files import the library (W19).
- **The Worker neither bundles nor runs it.** The server renders from the Query cache the loader filled, and after hydration the browser's collection starts from that same entry with no request. In every server environment the three packages resolve to `platform/db/server-stub.ts`.
- **Scope.** One `DbClient` per User, Session and Organization. Every collection call carries that identity in `x-template-rpc-scope` and the server refuses a mismatch (ADR-0015). The first sign of an identity change closes a fence: reads are aborted, the queue stops, late answers are dropped. Only a successful session read opens it again, under a new generation, so after A → B → A the work of the first A is not current. The next client is created only after the previous one's readers unmounted and its cleanup resolved.
- **Writes are intents.** An intent is a record (procedure, arguments, a UUIDv7 idempotency key, the scope) made when the person acts. It changes the view at once, and one queue per scope sends one record at a time. Rows are keyed by `clientId`.
- **Three ends.** A definite refusal undoes the change and what depended on it, with one error. A write nobody can confirm (a network failure, a 5xx, an answer lost after the commit) stays on screen and the same record is sent again: three fast attempts, then on reconnect, focus and a timer, until the server gives a definite answer or the person discards it. A confirmed write whose local apply failed is repaired by a read, never by a rollback. `@fofo/errors/write-outcome` decides from the code and status, never from the text.
- **Membership.** A view mirrors a capped, filtered read. One pure rule per view says whether a row belongs to it. Where the client cannot tell, or a cap or a count of the envelope may change, the view reads again once the queue is idle; otherwise the answer's row is the confirmation and nothing is read.

Not decided here: the mobile list (its section follows with that change), the change cursor in clients, offline. The owner's answers to two questions are still open and this record follows the recommended ones: the server stub instead of 178 KiB gzip of unused code in the Worker, and keeping an unconfirmed change instead of undoing it after two retries.

## Evidence [#evidence]

Spike S1 measured the two library facts behind the client lifecycle: a cleanup while a reader is mounted throws in that reader, and a cleanup after the next collection started leaves it loading for good. Unit tests run the real library; five end-to-end cases run the built Worker in workerd. The build fails when a startup chunk, the Worker or an unlisted route holds a module of the library. Sizes and timings: [measurement](../measurements/2026-11-29-web-collections.md).

## Why not the alternatives [#why-not-the-alternatives]

- **A collection for every list.** The Grid pages, sorts and counts on the server; a mirror would be a second, worse copy.
- **The library's own insert, update and delete handlers.** They see row diffs; the server speaks intents (a move with neighbours).
- **A refetch after every write.** That is the second request this decision removes.
- **Rejecting after two retries.** A write whose answer was lost after the commit would be reported as refused.
- **`routerWithDbClient` in the router.** About 1 MB of unminified code in the startup graph.
