@lunora/cli 1.0.0-alpha.39 → 1.0.0-alpha.391

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/LICENSE.md +33 -0
  2. package/README.md +1 -1
  3. package/dist/bin.mjs +2 -10
  4. package/dist/index.d.mts +1019 -374
  5. package/dist/index.d.ts +1019 -374
  6. package/dist/index.mjs +1 -19
  7. package/dist/packem_chunks/dispatch.mjs +1 -0
  8. package/dist/packem_chunks/handler.mjs +1 -160
  9. package/dist/packem_chunks/handler10.mjs +1 -16
  10. package/dist/packem_chunks/handler11.mjs +1 -22
  11. package/dist/packem_chunks/handler12.mjs +1 -192
  12. package/dist/packem_chunks/handler13.mjs +3 -131
  13. package/dist/packem_chunks/handler14.mjs +1 -65
  14. package/dist/packem_chunks/handler15.mjs +1 -58
  15. package/dist/packem_chunks/handler16.mjs +1 -80
  16. package/dist/packem_chunks/handler17.mjs +7 -43
  17. package/dist/packem_chunks/handler18.mjs +1 -105
  18. package/dist/packem_chunks/handler19.mjs +1 -170
  19. package/dist/packem_chunks/handler2.mjs +1 -114
  20. package/dist/packem_chunks/handler20.mjs +3 -94
  21. package/dist/packem_chunks/handler21.mjs +3 -94
  22. package/dist/packem_chunks/handler22.mjs +1 -0
  23. package/dist/packem_chunks/handler23.mjs +2 -0
  24. package/dist/packem_chunks/handler24.mjs +2 -0
  25. package/dist/packem_chunks/handler25.mjs +99 -0
  26. package/dist/packem_chunks/handler26.mjs +9 -0
  27. package/dist/packem_chunks/handler27.mjs +1 -0
  28. package/dist/packem_chunks/handler3.mjs +1 -204
  29. package/dist/packem_chunks/handler4.mjs +3 -33
  30. package/dist/packem_chunks/handler5.mjs +1 -49
  31. package/dist/packem_chunks/handler6.mjs +1 -91
  32. package/dist/packem_chunks/handler7.mjs +1 -42
  33. package/dist/packem_chunks/handler8.mjs +2 -174
  34. package/dist/packem_chunks/handler9.mjs +1 -315
  35. package/dist/packem_chunks/runCodegenCommand.mjs +4 -52
  36. package/dist/packem_chunks/runDeployCommand.mjs +11 -594
  37. package/dist/packem_chunks/runDevCommand.mjs +2 -0
  38. package/dist/packem_chunks/runInitCommand.mjs +164 -1432
  39. package/dist/packem_chunks/runResetCommand.mjs +1 -41
  40. package/dist/packem_chunks/runRpcCommand.mjs +1 -68
  41. package/dist/packem_shared/COMMANDS-JClcR9JT.mjs +1 -0
  42. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-CyIsI3S1.mjs +1 -0
  43. package/dist/packem_shared/EXIT_CODE-D_XCixv8.mjs +1 -0
  44. package/dist/packem_shared/admin-token-VdUnvnKW.mjs +1 -0
  45. package/dist/packem_shared/admin-url-DJB8P6Jh.mjs +1 -0
  46. package/dist/packem_shared/binding-manifest-file-CQ1eQYy1.mjs +2 -0
  47. package/dist/packem_shared/buildRegistryIndex-Dc8D8AR6.mjs +1 -0
  48. package/dist/packem_shared/cap-error-body-YBKO32BF.mjs +1 -0
  49. package/dist/packem_shared/catalog-CahzmDLV.mjs +1 -0
  50. package/dist/packem_shared/cli-DwEpuZm9.mjs +2 -0
  51. package/dist/packem_shared/cli-manifest-CRet0egA.mjs +1 -0
  52. package/dist/packem_shared/cloudflare-credentials-BVmZnDZk.mjs +1 -0
  53. package/dist/packem_shared/codegen-error-AmH54ofi.mjs +3 -0
  54. package/dist/packem_shared/command-CAXl2A1S.mjs +1 -0
  55. package/dist/packem_shared/commands-DgUDxkA8.mjs +13 -0
  56. package/dist/packem_shared/createLogger-Cl18I8AX.mjs +2 -0
  57. package/dist/packem_shared/createRecordingSpawner-Ch7zuQQE.mjs +1 -0
  58. package/dist/packem_shared/deploy-target-DCWwiuAe.mjs +1 -0
  59. package/dist/packem_shared/diffSnapshots-htqNZT8Q.mjs +7 -0
  60. package/dist/packem_shared/docker-AqLAkOyY.mjs +1 -0
  61. package/dist/packem_shared/health-probe-Bw9JFfIy.mjs +1 -0
  62. package/dist/packem_shared/import-BRM1hs7j.mjs +12 -0
  63. package/dist/packem_shared/index-oHZBGBwB.mjs +1 -0
  64. package/dist/packem_shared/insertSchemaExtension-DuiV6cba.mjs +8 -0
  65. package/dist/packem_shared/lint-ignore-report-DKZagpqk.mjs +2 -0
  66. package/dist/packem_shared/mcp-config-file-CcDTeXIq.mjs +2 -0
  67. package/dist/packem_shared/open-url-EnKy--w-.mjs +1 -0
  68. package/dist/packem_shared/output-format-B4eQHi5y.mjs +2 -0
  69. package/dist/packem_shared/parseManifest-CwPTKdtS.mjs +1 -0
  70. package/dist/packem_shared/path-containment-CgxYZggb.mjs +1 -0
  71. package/dist/packem_shared/plan-Dl57T11P.mjs +5 -0
  72. package/dist/packem_shared/planDevCommand-D8sm56hO.mjs +1 -0
  73. package/dist/packem_shared/platform-diagnostics-Cn2g6Jh-.mjs +4 -0
  74. package/dist/packem_shared/prompt-cancelled-BvsNxg_Q.mjs +1 -0
  75. package/dist/packem_shared/render-lunora-error-C3OJY5wk.mjs +2 -0
  76. package/dist/packem_shared/resolve-BrsV4Qyc.mjs +5 -0
  77. package/dist/packem_shared/resolve-target-C_ZloTvd.mjs +1 -0
  78. package/dist/packem_shared/runAddCommand-DTvfp4h2.mjs +1 -0
  79. package/dist/packem_shared/runExportCommand-slAq5BCF.mjs +5 -0
  80. package/dist/packem_shared/runMigrateGenerateCommand-BwemPIOz.mjs +11 -0
  81. package/dist/packem_shared/schemaIrToSnapshot-BHuNOfgs.mjs +1 -0
  82. package/dist/packem_shared/secret-key-6bCftsxj.mjs +1 -0
  83. package/dist/packem_shared/shared-D-zCOmgY.mjs +1 -0
  84. package/dist/packem_shared/storage-DWhTX8eS.mjs +1 -0
  85. package/dist/packem_shared/tui-prompts-B3YwUhGw.mjs +4 -0
  86. package/dist/packem_shared/variables-CPYyJtsL.mjs +1 -0
  87. package/dist/packem_shared/wrangler-name-Dsk5K1f-.mjs +1 -0
  88. package/dist/packem_shared/wrangler-secrets-3chIVv3H.mjs +1 -0
  89. package/package.json +39 -19
  90. package/skills/README.md +35 -17
  91. package/skills/lunora/SKILL.md +154 -64
  92. package/skills/lunora-create-package/SKILL.md +97 -84
  93. package/skills/lunora-deploy/SKILL.md +175 -105
  94. package/skills/lunora-functions/SKILL.md +362 -56
  95. package/skills/lunora-migration-helper/SKILL.md +162 -137
  96. package/skills/lunora-performance-audit/SKILL.md +137 -95
  97. package/skills/lunora-quickstart/SKILL.md +137 -136
  98. package/skills/lunora-realtime/SKILL.md +140 -99
  99. package/skills/lunora-setup-auth/SKILL.md +162 -102
  100. package/skills/lunora-setup-hyperdrive/SKILL.md +114 -97
  101. package/skills/lunora-setup-hyperdrive-global/SKILL.md +93 -90
  102. package/skills/lunora-setup-mail/SKILL.md +85 -113
  103. package/skills/lunora-setup-scheduler/SKILL.md +132 -96
  104. package/skills/lunora-setup-storage/SKILL.md +189 -95
  105. package/dist/packem_chunks/planDevCommand.mjs +0 -541
  106. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +0 -397
  107. package/dist/packem_shared/COMMANDS-B0ftFD_3.mjs +0 -948
  108. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +0 -244
  109. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +0 -19
  110. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +0 -13
  111. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +0 -38
  112. package/dist/packem_shared/codegen-error-DJG-ghs_.mjs +0 -31
  113. package/dist/packem_shared/command-D3lB_4Az.mjs +0 -19
  114. package/dist/packem_shared/commands-B-gR09Z_.mjs +0 -845
  115. package/dist/packem_shared/createLogger-B40gPzQo.mjs +0 -78
  116. package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +0 -43
  117. package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +0 -61
  118. package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +0 -161
  119. package/dist/packem_shared/docker-hMQ97KSQ.mjs +0 -21
  120. package/dist/packem_shared/insertSchemaExtension-DAqbfr9Z.mjs +0 -64
  121. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +0 -41
  122. package/dist/packem_shared/output-format-wUvAN6AL.mjs +0 -17
  123. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +0 -56
  124. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +0 -94
  125. package/dist/packem_shared/prompt-cancelled-APzX1Im-.mjs +0 -9
  126. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +0 -16
  127. package/dist/packem_shared/runAddCommand-bnY6-HKb.mjs +0 -4
  128. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +0 -79
  129. package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +0 -43
  130. package/dist/packem_shared/storage-BIsph-Vk.mjs +0 -84
  131. package/dist/packem_shared/tui-prompts-BjEN8XgP.mjs +0 -658
  132. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +0 -12
  133. package/dist/packem_shared/wrangler-secrets-P2_ZUR-k.mjs +0 -47
@@ -1,37 +1,30 @@
1
1
  ---
2
2
  name: lunora-realtime
3
- description: Wires Lunora's live data into a client. Use for `LunoraClient`/`LunoraProvider`,
4
- reactive `useQuery`/`useSubscription`, `useMutation` with optimistic updates,
5
- pagination, connection status, the React/Vue/Solid/Svelte adapters, and the
6
- `@lunora/db` TanStack binding.
3
+ description: Wires Lunora's live data into a client app. Covers `LunoraClient` and the per-framework provider, reactive `useQuery`/`useSubscription`, `useMutation` with optimistic updates, `usePaginatedQuery`, `shardKey` routing, connection status, presence, the React/Vue/Solid/Svelte/Angular/React Native adapters, and the `@lunora/db` TanStack DB binding. Use when building UI that reads or writes Lunora functions, when asked to "show live data", "make this update in real time", "add optimistic updates", "add infinite scroll/pagination", or "show offline/reconnecting state", when editing components that import `@lunora/react`/`vue`/`solid`/`svelte`/`angular`/`react-native` or `lunorash/client`, or when a live query stays `undefined`, freezes, or doesn't update.
7
4
  ---
8
5
 
9
6
  # Lunora Realtime
10
7
 
11
8
  Consume Lunora functions reactively from the client. Queries are live
12
- subscriptions over WebSocket — a `useQuery` re-renders the instant a mutation
9
+ subscriptions over WebSocket — a `useQuery` re-renders as soon as a mutation
13
10
  changes the rows it reads — and mutations can paint optimistically with
14
11
  automatic rollback.
15
12
 
16
- ## When to Use
17
-
18
- - Wiring a frontend to a Lunora backend (React, Vue, Solid, Svelte).
19
- - Adding optimistic updates, pagination, or presence to the UI.
20
- - Choosing between live hooks and the `@lunora/db` collection layer.
21
-
22
13
  ## When Not to Use
23
14
 
24
15
  - Writing the server functions themselves — that's `lunora-functions`.
25
16
  - Initial project/provider scaffolding — that's `lunora-quickstart`.
17
+ - Sign-in UI and the `Authenticated` gates — that's `lunora-setup-auth`.
26
18
 
27
19
  ## Provider Setup
28
20
 
29
- Create the `LunoraClient` once at module scope and wrap the app. The client owns
30
- the WebSocket, the optimistic cache, and the offline queue.
21
+ Create the `LunoraClient` once at module scope; it owns the WebSocket, the
22
+ optimistic cache, and the offline queue, so creating it inside a component
23
+ reopens the socket on every render.
31
24
 
32
25
  ```tsx
33
- import { LunoraClient } from "@lunora/client";
34
26
  import { LunoraProvider } from "@lunora/react";
27
+ import { LunoraClient } from "lunorash/client";
35
28
 
36
29
  const url = (import.meta.env.VITE_LUNORA_URL as string | undefined) ?? globalThis.location.origin;
37
30
  const client = new LunoraClient({ url });
@@ -39,80 +32,97 @@ const client = new LunoraClient({ url });
39
32
  // <LunoraProvider client={client}>…</LunoraProvider>
40
33
  ```
41
34
 
42
- Vue / Solid / Svelte have matching providers in `@lunora/vue`, `@lunora/solid`,
43
- `@lunora/svelte`; the hook names and semantics below mirror across them.
35
+ The adapters differ in how they register the client and name their primitives:
36
+
37
+ | Package | Register the client | Live read / write |
38
+ | ---------------------- | ------------------------------------------------------------ | ---------------------------------------------- |
39
+ | `@lunora/react` | `<LunoraProvider client={client}>` | `useQuery` / `useMutation` |
40
+ | `@lunora/vue` | `app.use(createLunora(client))` (or `provideLunora(client)`) | `useQuery` / `useMutation` |
41
+ | `@lunora/solid` | `<LunoraProvider client={client}>` | `createQuery` / `createMutation` |
42
+ | `@lunora/svelte` | `setLunoraClient(client)` in the root layout | `query` / `mutation` stores |
43
+ | `@lunora/angular` | `provideLunora()` in `ApplicationConfig` | `liveQuery` / `mutate`, `injectLunoraClient()` |
44
+ | `@lunora/react-native` | `createLunoraClient({ url, storage })` + `LunoraProvider` | re-exports `@lunora/react` hooks |
45
+
46
+ The rest of this skill uses the React names; the others mirror the same
47
+ semantics (`usePaginatedQuery` → `createPaginatedQuery` / `paginatedQuery`,
48
+ `useConnectionStatus` → `createConnectionStatus` / `connectionStatus`, …).
49
+ React Native passes `storage: AsyncStorage` so the offline queue survives a
50
+ restart; `@lunora/react-native/auth` bridges a better-auth Expo session.
51
+ `@lunora/astro` and `@lunora/nuxt` (Nitro) integrate Lunora into the SSR server.
44
52
 
45
53
  ## Live Queries
46
54
 
47
- `useQuery(reference, args)` opens a subscription and returns the value, or
48
- `undefined` while it loads. The reference comes from codegen
49
- (`api.<file>.<name>`).
55
+ `useQuery(reference, args, options?)` opens a subscription and returns the
56
+ value, or `undefined` while it loads. References come from codegen and are
57
+ typed end to end, so no cast is needed:
50
58
 
51
59
  ```tsx
52
60
  import { useQuery } from "@lunora/react";
53
61
 
54
- import { api } from "../../lunora/_generated/api";
55
- import type { Doc } from "../../lunora/_generated/dataModel";
62
+ import { api } from "../lunora/_generated/api";
56
63
 
57
- const todos = useQuery(api.todos.list, {}) as Doc<"todos">[] | undefined;
64
+ const todos = useQuery(api.todos.list, {});
65
+ const messages = useQuery(api.messages.list, { channelId }, { shardKey: channelId, onError: report });
58
66
  ```
59
67
 
60
- - `undefined` means "loading" — render a skeleton/spinner for it.
61
- - The subscription tears down on unmount and re-runs only when `args` change or
62
- the underlying rows change. Keep `args` narrow so a write elsewhere doesn't
63
- re-push unrelated data (see `lunora-performance-audit`).
64
- - `useSubscription` is the lower-level primitive for streaming subscriptions;
65
- `usePreloadedQuery` + `lunoraQueryOptions` support SSR/preload handoff via
66
- `@lunora/react/server`.
68
+ - `undefined` means loading and `[]` means loaded-and-empty; branch on both.
69
+ - Pass `"skip"` as `args` to disable the query (no network call, no
70
+ subscription) — e.g. until a required id is known.
71
+ - Pass `onError` to see a server-pushed error (an RLS denial, a query that starts
72
+ throwing). Without it the error is dropped and the value silently freezes at
73
+ its last good result.
74
+ - `shardKey`: for a `.shardBy(...)` table, pin every read and write to the
75
+ shard's key (`{ shardKey: channelId }` on both `useQuery` and the mutation
76
+ call). Without it the runtime has to fan out across every shard.
77
+ - The subscription re-runs only when `args` or the rows it read change. Keep
78
+ `args` narrow so unrelated writes don't re-push data (see
79
+ `lunora-performance-audit`).
80
+ - `useSubscription` is the push-only variant (no initial HTTP fetch) returning
81
+ `{ data, error }`. For SSR, `@lunora/react/server` exports `preloadQuery`,
82
+ `fetchQuery`, and `lunoraQueryOptions`; hand the result to
83
+ `usePreloadedQuery` on the client.
84
+ - References in folders are nested: `lunora/billing/invoices.ts` →
85
+ `api.billing.invoices.<fn>`. Client code only ever imports `api`;
86
+ `internal` functions are not callable from the client.
67
87
 
68
88
  ## Authorization & Live Queries
69
89
 
70
- Subscriptions re-run the query handler **server-side under anonymous
71
- identity**. The one-shot `fetch` RPC behind the initial load carries the
72
- caller's identity, but the live WebSocket channel (the subscription seed and
73
- every write-driven refresh) does not — it evaluates as anonymous.
90
+ Live queries are identity-aware. At the WebSocket upgrade the runtime stamps
91
+ the caller's verified identity onto the socket, and every subscription re-run
92
+ executes under that identity, so:
74
93
 
75
- This matters for any query that authorizes or filters on the authenticated
76
- user:
94
+ - `.use(rls(...))` and `ctx.auth.userId` behave the same in a subscription as in
95
+ a one-shot read. Shape subscriptions AND-merge their predicate with the
96
+ table's RLS read policy.
97
+ - An anonymous socket carries no identity, so an RLS/`ctx.auth` query fails
98
+ closed (empty or denied) rather than leaking rows.
99
+ - When the credential carries an expiry, the server sends `TOKEN_EXPIRED` and
100
+ closes with code `4001`. `LunoraClient` reconnects and re-resolves identity
101
+ automatically; surface it in the UI only if a re-login is required.
77
102
 
78
- - A query guarded by `.use(rls(...))` or one that reads `ctx.auth.userId`
79
- directly returns the user's rows on the **initial** HTTP fetch, but its
80
- **live** updates evaluate anonymously and may resolve to an empty/denied
81
- set.
82
- - This **fails closed** — the live channel shows _less_ data, never another
83
- user's data, so there is no leak. But it is a correctness caveat: the
84
- initial render and the live updates can disagree.
85
-
86
- The supported pattern today is to scope per-user data **outside** of
87
- `ctx.auth` inside a subscribed query:
88
-
89
- - Partition the data by shard with `.shardBy(userId)` (or tenant/room), so the
90
- subscription is already scoped to the right state, or
91
- - Pass the identifier as an **explicit query arg**
92
- (`useQuery(api.todos.list, { userId })`) and filter on the arg rather than on
93
- `ctx.auth`.
94
-
95
- Reserve `rls()` / `ctx.auth`-based filtering for non-subscribed reads (one-shot
96
- actions/queries) where identity is always present.
103
+ Use `.shardBy(...)` and narrow args for performance, `rls()` / `ctx.auth` for
104
+ authorization. They compose.
97
105
 
98
106
  ## Mutations + Optimistic Updates
99
107
 
100
- `useMutation(reference)` returns `{ mutate, pending }`. Pass an `optimistic`
101
- callback to paint the next state immediately; if the server rejects the call the
102
- runtime rolls the cache back automatically.
108
+ `useMutation(reference)` returns `{ mutate, pending, data, error, reset }`.
109
+ Destructure it — the hook result itself isn't callable. `mutate` returns a
110
+ promise that rejects on failure. Pass `optimisticUpdate` to paint the next state
111
+ immediately; if the server rejects the call the cache rolls back.
103
112
 
104
113
  ```tsx
105
114
  import { useMutation } from "@lunora/react";
106
115
 
107
- import type { Doc, Id } from "../../lunora/_generated/dataModel";
116
+ import { api } from "../lunora/_generated/api";
117
+ import type { Doc, Id } from "../lunora/_generated/dataModel";
108
118
 
109
119
  const { mutate: add, pending } = useMutation(api.todos.add);
110
120
 
111
121
  await add(
112
122
  { text },
113
123
  {
114
- optimistic: (current) => {
115
- const list = (current as Doc<"todos">[] | undefined) ?? [];
124
+ optimisticUpdate: (store) => {
125
+ const list = store.getQuery(api.todos.list, {}) ?? [];
116
126
  const provisional: Doc<"todos"> = {
117
127
  _id: `optimistic_${Date.now()}` as Id<"todos">,
118
128
  _creationTime: Date.now(),
@@ -120,58 +130,89 @@ await add(
120
130
  done: false,
121
131
  createdAt: Date.now(),
122
132
  };
123
- return [provisional, ...list];
133
+ store.setQuery(api.todos.list, {}, [provisional, ...list]);
124
134
  },
125
135
  },
126
136
  );
127
137
  ```
128
138
 
129
- - The `optimistic` callback receives the current cached value and returns the
130
- provisional one. When the server delta arrives it replaces the optimistic
131
- entry; on failure the cache reverts.
132
- - `pending` is `true` while the call is in flight — disable the submit button
133
- with it.
134
- - **Offline queue:** mutations made while disconnected are queued by
135
- `LunoraClient` and replayed on reconnect (client-id-keyed, so they aren't
136
- double-applied).
139
+ - `optimisticUpdate` names the query it patches because nothing can infer that
140
+ `todos.add` affects `todos.list`. `store.getAllQueries(fn)` lists every
141
+ subscribed args variant of a query so you can patch each one.
142
+ - The provisional row must match the query's element shape, including
143
+ `_id`/`_creationTime`, or the UI flickers when the real row lands.
144
+ - The per-call `optimistic: (current) => next` shortcut only patches a
145
+ subscription under the mutation's own reference and args (a counter, a doc by
146
+ id). For the add-updates-list shape above it is a silent no-op.
147
+ - `pending` stays `true` until every overlapping call from this hook settles;
148
+ use it to disable the submit button.
149
+ - Offline, mutations are queued by `LunoraClient` and replayed on reconnect,
150
+ keyed by client id so they aren't applied twice.
137
151
 
138
152
  ## Pagination, Connection, Presence
139
153
 
140
- - `usePaginatedQuery` / `useInfiniteQuery` — cursor pagination over a query that
141
- ends in `.paginate(...)` on the server.
154
+ The server query takes `paginationOpts` and returns `.paginate(...)` (see
155
+ `lunora-functions`). The hook supplies the cursor:
156
+
157
+ ```tsx
158
+ const { results, status, loadMore, error } = usePaginatedQuery(api.messages.page, { channelId }, { initialNumItems: 20 });
159
+ // status: "LoadingFirstPage" | "CanLoadMore" | "LoadingMore" | "Exhausted"
160
+ ```
161
+
162
+ Pages are live ranges: inserts and deletes inside a loaded page grow or shrink
163
+ it in place without duplicating rows. There is no total count; subscribe to a
164
+ separate count query for "42 of 1,203". `useInfiniteQuery` is the
165
+ TanStack-style alternative.
166
+
142
167
  - `useConnectionStatus` — live socket state for an offline/reconnecting banner.
143
- - `usePresence` — who's-here + heartbeat (pairs with the `presence` registry
168
+ - `usePresence` — who's-here with heartbeat (pairs with the `presence` registry
144
169
  item).
145
- - `useAuth` + the `Authenticated` / `Unauthenticated` / `AuthLoading` gates —
146
- see `lunora-setup-auth`.
170
+ - `useAuth` + `Authenticated` / `Unauthenticated` / `AuthLoading` — see
171
+ `lunora-setup-auth`.
147
172
 
148
173
  ## `@lunora/db` — TanStack DB Collections
149
174
 
150
- For richer client state (indexed local collections, cross-query joins, a durable
151
- offline-transactions outbox), use `@lunora/db` instead of raw hooks. Scaffold
152
- with `vis generate lunora-collections` (wires `defineCollections` from your
153
- schema + functions into live TanStack DB collections). Reach for it when the app
154
- needs client-side indexes/joins or a persistent optimistic outbox; raw `useQuery`
155
- /`useMutation` are enough for straightforward live lists.
156
-
157
- ## Common Pitfalls
158
-
159
- 1. **Creating `LunoraClient` inside a component.** It re-opens the socket every
160
- render — create it once at module scope.
161
- 2. **Treating `undefined` as empty.** `undefined` is "loading", `[]` is "loaded,
162
- empty" — branch on both.
163
- 3. **Broad query args.** A subscription keyed too broadly re-renders on
164
- unrelated writes; scope `args` to what the component shows.
165
- 4. **Optimistic shape drift.** The provisional value must match the query's
166
- element shape (including `_id`/`_creationTime`) or the UI flickers when the
167
- real delta lands.
175
+ For indexed local collections, cross-query joins, or a durable offline outbox,
176
+ use `@lunora/db` (peer deps `@tanstack/db`, `@tanstack/offline-transactions`).
177
+ Plain `useQuery` / `useMutation` are enough for straightforward live lists.
178
+
179
+ ```ts
180
+ import { defineCollections } from "@lunora/db";
181
+ import type { LunoraClient } from "lunorash/client";
182
+
183
+ import { api } from "../lunora/_generated/api";
184
+ import type { Doc, Id } from "../lunora/_generated/dataModel";
185
+
186
+ export const createCollections = (client: LunoraClient) =>
187
+ defineCollections(client, {
188
+ messages: {
189
+ list: api.messages.list,
190
+ scopeBy: "channelId", // the .shardBy column
191
+ insert: {
192
+ mutation: api.messages.send,
193
+ optimistic: (input: Omit<Doc<"messages">, "_id" | "_creationTime">, id) => ({ _id: id as Id<"messages">, _creationTime: Date.now(), ...input }),
194
+ toArgs: (row) => ({ channelId: row.channelId, id: row._id, text: row.text }),
195
+ },
196
+ },
197
+ });
198
+ // → db.collections.* (feed useLiveQuery), db.actions.* (optimistic writes),
199
+ // db.scope.* (re-point sharded collections), db.executor (the outbox)
200
+ ```
201
+
202
+ Call `defineCollections` once and treat its collections as the single source of
203
+ truth per table. Optimistic writes and sync deltas land only on that instance,
204
+ so a second instance or a copy of the rows in your own store drifts: derived
205
+ indexes (trees, search, undo captures) built from the copy miss new rows and
206
+ hold stale ones, while `useLiveQuery` still looks correct.
168
207
 
169
208
  ## Checklist
170
209
 
171
- - [ ] `LunoraClient` created once at module scope; app wrapped in the provider.
172
- - [ ] Live reads use `useQuery`; `undefined` handled as loading.
173
- - [ ] Writes use `useMutation`; `pending` disables submit; `optimistic` matches
174
- the row shape.
175
- - [ ] Query `args` scoped narrowly to avoid over-broad re-renders.
176
- - [ ] Pagination via `usePaginatedQuery`/`useInfiniteQuery` over `.paginate`.
177
- - [ ] Considered `@lunora/db` if the app needs local indexes/joins or an outbox.
210
+ - [ ] `LunoraClient` created once at module scope and registered the way the
211
+ adapter expects.
212
+ - [ ] `undefined` handled as loading; `"skip"` used while args are incomplete;
213
+ `onError` wired where a failure must be visible.
214
+ - [ ] Reads and writes on `.shardBy` tables pass `shardKey`.
215
+ - [ ] Mutations destructure `mutate`; `pending` disables submit;
216
+ `optimisticUpdate` rows match the query's element shape.
217
+ - [ ] Lists that grow use `usePaginatedQuery` over a `.paginate` query.
218
+ - [ ] `@lunora/db`: one `defineCollections` instance; no parallel copy of rows.