@lunora/cli 1.0.0-alpha.33 → 1.0.0-alpha.330
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.
- package/LICENSE.md +33 -0
- package/README.md +1 -1
- package/dist/bin.mjs +2 -10
- package/dist/index.d.mts +965 -372
- package/dist/index.d.ts +965 -372
- package/dist/index.mjs +1 -19
- package/dist/packem_chunks/dispatch.mjs +1 -0
- package/dist/packem_chunks/handler.mjs +1 -160
- package/dist/packem_chunks/handler10.mjs +2 -16
- package/dist/packem_chunks/handler11.mjs +1 -22
- package/dist/packem_chunks/handler12.mjs +1 -192
- package/dist/packem_chunks/handler13.mjs +1 -131
- package/dist/packem_chunks/handler14.mjs +1 -65
- package/dist/packem_chunks/handler15.mjs +3 -58
- package/dist/packem_chunks/handler16.mjs +1 -80
- package/dist/packem_chunks/handler17.mjs +1 -43
- package/dist/packem_chunks/handler18.mjs +1 -105
- package/dist/packem_chunks/handler19.mjs +7 -170
- package/dist/packem_chunks/handler2.mjs +1 -114
- package/dist/packem_chunks/handler20.mjs +1 -94
- package/dist/packem_chunks/handler21.mjs +1 -94
- package/dist/packem_chunks/handler22.mjs +1 -0
- package/dist/packem_chunks/handler23.mjs +3 -0
- package/dist/packem_chunks/handler24.mjs +3 -0
- package/dist/packem_chunks/handler25.mjs +1 -0
- package/dist/packem_chunks/handler26.mjs +2 -0
- package/dist/packem_chunks/handler27.mjs +99 -0
- package/dist/packem_chunks/handler28.mjs +10 -0
- package/dist/packem_chunks/handler3.mjs +1 -204
- package/dist/packem_chunks/handler4.mjs +1 -33
- package/dist/packem_chunks/handler5.mjs +1 -49
- package/dist/packem_chunks/handler6.mjs +3 -91
- package/dist/packem_chunks/handler7.mjs +1 -42
- package/dist/packem_chunks/handler8.mjs +1 -174
- package/dist/packem_chunks/handler9.mjs +1 -315
- package/dist/packem_chunks/runCodegenCommand.mjs +4 -52
- package/dist/packem_chunks/runDeployCommand.mjs +7 -594
- package/dist/packem_chunks/runDevCommand.mjs +3 -0
- package/dist/packem_chunks/runInitCommand.mjs +162 -1430
- package/dist/packem_chunks/runResetCommand.mjs +1 -41
- package/dist/packem_chunks/runRpcCommand.mjs +1 -68
- package/dist/packem_shared/COMMANDS-u8aPCKxf.mjs +1 -0
- package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-CyIsI3S1.mjs +1 -0
- package/dist/packem_shared/EXIT_CODE-D_XCixv8.mjs +1 -0
- package/dist/packem_shared/admin-token-VdUnvnKW.mjs +1 -0
- package/dist/packem_shared/admin-url-DJB8P6Jh.mjs +1 -0
- package/dist/packem_shared/binding-manifest-file-CQ1eQYy1.mjs +2 -0
- package/dist/packem_shared/buildRegistryIndex-Dc8D8AR6.mjs +1 -0
- package/dist/packem_shared/catalog-CahzmDLV.mjs +1 -0
- package/dist/packem_shared/cli-BQ3AyV_K.mjs +2 -0
- package/dist/packem_shared/codegen-error-AmH54ofi.mjs +3 -0
- package/dist/packem_shared/command-CAXl2A1S.mjs +1 -0
- package/dist/packem_shared/commands-Cg06JNO2.mjs +13 -0
- package/dist/packem_shared/createLogger-Cl18I8AX.mjs +2 -0
- package/dist/packem_shared/createRecordingSpawner-sS7LEN7x.mjs +1 -0
- package/dist/packem_shared/deploy-target-DCWwiuAe.mjs +1 -0
- package/dist/packem_shared/diffSnapshots-WWwx-KvZ.mjs +5 -0
- package/dist/packem_shared/docker-DpVxvYpL.mjs +1 -0
- package/dist/packem_shared/health-probe-Bw9JFfIy.mjs +1 -0
- package/dist/packem_shared/import-BRM1hs7j.mjs +12 -0
- package/dist/packem_shared/insertSchemaExtension-DuiV6cba.mjs +8 -0
- package/dist/packem_shared/lint-ignore-report-DKZagpqk.mjs +2 -0
- package/dist/packem_shared/open-url-EnKy--w-.mjs +1 -0
- package/dist/packem_shared/output-format-B4eQHi5y.mjs +2 -0
- package/dist/packem_shared/parseManifest-CwPTKdtS.mjs +1 -0
- package/dist/packem_shared/path-containment-CgxYZggb.mjs +1 -0
- package/dist/packem_shared/plan-CtBB0rrT.mjs +5 -0
- package/dist/packem_shared/planDevCommand-tVTRd2QE.mjs +1 -0
- package/dist/packem_shared/platform-diagnostics-Cn2g6Jh-.mjs +4 -0
- package/dist/packem_shared/prompt-cancelled-BvsNxg_Q.mjs +1 -0
- package/dist/packem_shared/render-lunora-error-C3OJY5wk.mjs +2 -0
- package/dist/packem_shared/resolve-DUCSc7jQ.mjs +5 -0
- package/dist/packem_shared/resolve-target-C_ZloTvd.mjs +1 -0
- package/dist/packem_shared/runAddCommand-BsOrlyfD.mjs +1 -0
- package/dist/packem_shared/runExportCommand-slAq5BCF.mjs +5 -0
- package/dist/packem_shared/runMigrateGenerateCommand-Ii_WMZEA.mjs +11 -0
- package/dist/packem_shared/schemaIrToSnapshot-BjK-0IRo.mjs +1 -0
- package/dist/packem_shared/secret-key-DsMeUoEg.mjs +1 -0
- package/dist/packem_shared/shared-D-zCOmgY.mjs +1 -0
- package/dist/packem_shared/storage-iyNnV-2G.mjs +1 -0
- package/dist/packem_shared/tui-prompts-B3YwUhGw.mjs +4 -0
- package/dist/packem_shared/variables-acxcvVQa.mjs +1 -0
- package/dist/packem_shared/wrangler-name-Dsk5K1f-.mjs +1 -0
- package/dist/packem_shared/wrangler-secrets-CuUdFBaA.mjs +1 -0
- package/package.json +39 -19
- package/skills/README.md +35 -17
- package/skills/lunora/SKILL.md +123 -9
- package/skills/lunora-create-package/SKILL.md +4 -3
- package/skills/lunora-deploy/SKILL.md +42 -11
- package/skills/lunora-functions/SKILL.md +120 -15
- package/skills/lunora-migration-helper/SKILL.md +74 -21
- package/skills/lunora-performance-audit/SKILL.md +78 -9
- package/skills/lunora-quickstart/SKILL.md +95 -27
- package/skills/lunora-realtime/SKILL.md +73 -39
- package/skills/lunora-setup-auth/SKILL.md +33 -6
- package/skills/lunora-setup-hyperdrive/SKILL.md +33 -13
- package/skills/lunora-setup-hyperdrive-global/SKILL.md +5 -0
- package/skills/lunora-setup-mail/SKILL.md +34 -28
- package/skills/lunora-setup-scheduler/SKILL.md +18 -14
- package/skills/lunora-setup-storage/SKILL.md +187 -27
- package/dist/packem_chunks/planDevCommand.mjs +0 -541
- package/dist/packem_chunks/runMigrateGenerateCommand.mjs +0 -397
- package/dist/packem_shared/COMMANDS-B0ftFD_3.mjs +0 -948
- package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +0 -244
- package/dist/packem_shared/admin-url-4UzT-CI4.mjs +0 -19
- package/dist/packem_shared/api-spec-CtA6ilu4.mjs +0 -13
- package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +0 -38
- package/dist/packem_shared/codegen-error-DJG-ghs_.mjs +0 -31
- package/dist/packem_shared/command-D3lB_4Az.mjs +0 -19
- package/dist/packem_shared/commands-B-gR09Z_.mjs +0 -845
- package/dist/packem_shared/createLogger-B40gPzQo.mjs +0 -78
- package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +0 -43
- package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +0 -61
- package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +0 -161
- package/dist/packem_shared/docker-hMQ97KSQ.mjs +0 -21
- package/dist/packem_shared/insertSchemaExtension-DAqbfr9Z.mjs +0 -64
- package/dist/packem_shared/open-url-Dfq6fAyT.mjs +0 -41
- package/dist/packem_shared/output-format-wUvAN6AL.mjs +0 -17
- package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +0 -56
- package/dist/packem_shared/parseManifest--vZf2FY1.mjs +0 -94
- package/dist/packem_shared/prompt-cancelled-APzX1Im-.mjs +0 -9
- package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +0 -16
- package/dist/packem_shared/runAddCommand-bnY6-HKb.mjs +0 -4
- package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +0 -79
- package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +0 -43
- package/dist/packem_shared/storage-BIsph-Vk.mjs +0 -84
- package/dist/packem_shared/tui-prompts-BjEN8XgP.mjs +0 -658
- package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +0 -12
- package/dist/packem_shared/wrangler-secrets-P2_ZUR-k.mjs +0 -47
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
name: lunora-performance-audit
|
|
3
3
|
description: Diagnoses and fixes Lunora performance problems — full-table scans, missing
|
|
4
4
|
indexes, OCC write conflicts, oversized subscriptions, and sharding/`.global()`
|
|
5
|
-
scaling. Use when queries are slow, mutations conflict,
|
|
6
|
-
flags a table.
|
|
5
|
+
scaling. Use when queries are slow, mutations conflict, `lunora insights`
|
|
6
|
+
reports a hot-spot, or `@lunora/advisor` flags a table.
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
# Lunora Performance Audit
|
|
@@ -14,7 +14,7 @@ apply it across sibling functions consistently.
|
|
|
14
14
|
## When to Use
|
|
15
15
|
|
|
16
16
|
- Queries feel slow or read far more rows than they return.
|
|
17
|
-
- Mutations
|
|
17
|
+
- Mutations conflict (409 `CONFLICT`) under load (OCC).
|
|
18
18
|
- Subscriptions fan out updates too broadly or re-run too often.
|
|
19
19
|
- The Lunora Studio **Advisors** tab or `@lunora/advisor` flags a table.
|
|
20
20
|
|
|
@@ -34,12 +34,35 @@ apply it across sibling functions consistently.
|
|
|
34
34
|
|
|
35
35
|
## Signal Gathering
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
### Runtime signal: `lunora insights`
|
|
38
|
+
|
|
39
|
+
When the worker is running and has served traffic, start here — it reports the
|
|
40
|
+
measured problem, not a suspected one:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
lunora insights # against the local dev worker
|
|
44
|
+
lunora insights --shard channel:demo # scope to one shard
|
|
45
|
+
lunora insights --limit 25 --format json # machine-readable, more rows
|
|
46
|
+
lunora insights --prod --url https://app.example.com --token $LUNORA_ADMIN_TOKEN
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
It ranks per-function **write-conflict hot-spots** (OCC contention — the
|
|
50
|
+
sharding signal), **error rates**, and **latency outliers**. A function at the
|
|
51
|
+
top of the write-conflict list is the direct input to the OCC section below; a
|
|
52
|
+
latency outlier usually resolves to the read-amplification section.
|
|
53
|
+
|
|
54
|
+
The Studio **Issues** panel and `lunora logs` cover the error side in more
|
|
55
|
+
detail once `insights` tells you where to look.
|
|
56
|
+
|
|
57
|
+
### Static signal: the advisors
|
|
58
|
+
|
|
59
|
+
These need no traffic, so they also work on a cold codebase:
|
|
38
60
|
|
|
39
61
|
- **Lunora Studio → Advisors tab** surfaces `@lunora/advisor` findings live in
|
|
40
62
|
dev.
|
|
41
|
-
- `@lunora/advisor` runs static lints over `defineSchema` + discovered query
|
|
42
|
-
reads / insert writes.
|
|
63
|
+
- `@lunora/advisor` runs ~90 static lints over `defineSchema` + discovered query
|
|
64
|
+
reads / insert writes (plus a few runtime lints). The performance/schema rules
|
|
65
|
+
most relevant here:
|
|
43
66
|
- `filter-without-index` — a query filters a table with no covering index.
|
|
44
67
|
- `unindexed-foreign-key` — a relation/FK column has no index.
|
|
45
68
|
- `duplicate-index` / `empty-index` — wasted or malformed indexes.
|
|
@@ -73,11 +96,49 @@ const mine = await ctx.db
|
|
|
73
96
|
Index columns are ordered: put equality columns first, then the range/sort
|
|
74
97
|
column. Fix every sibling query on the table the same way.
|
|
75
98
|
|
|
99
|
+
## Problem Class: Relation Traversal Cost
|
|
100
|
+
|
|
101
|
+
**Symptom:** a query calling `ctx.db.related` is slow, or one page of it reads
|
|
102
|
+
far more rows than it returns.
|
|
103
|
+
|
|
104
|
+
A traversal is not one read. It is one batched `findMany` **per edge, per hop**,
|
|
105
|
+
and the frontier grows multiplicatively — which is why every option is bounded,
|
|
106
|
+
and why the bounds **refuse rather than clamp**:
|
|
107
|
+
|
|
108
|
+
| Option | Default | Cap | What the cap is protecting |
|
|
109
|
+
| ------------- | ------- | -------- | ---------------------------------------------------------------------------- |
|
|
110
|
+
| `depth` | `1` | `4` | Past 4 hops a real schema is bounded by request time, not by `limit`. |
|
|
111
|
+
| `limit` | `50` | `200` | Nodes returned in one page. |
|
|
112
|
+
| cursor offset | `0` | `10 000` | The offset is re-walk work, not a skip: each hop reads `N + limit + 1` rows. |
|
|
113
|
+
|
|
114
|
+
A caller asking for `depth: 9` gets a `BAD_REQUEST`, not a silent depth 4 — so a
|
|
115
|
+
traversal that throws is a mis-sized request, not a bug to route around.
|
|
116
|
+
|
|
117
|
+
**Fixes, in order of preference:**
|
|
118
|
+
|
|
119
|
+
1. **Narrow with `edges`.** Name only the `"<table>.<column>"` edges the feature
|
|
120
|
+
needs. Following every declared foreign key is the usual reason a walk is
|
|
121
|
+
wide — one unrelated `v.id(...)` column can double the frontier per hop.
|
|
122
|
+
2. **Narrow with `direction`.** `"in"` or `"out"` instead of the default
|
|
123
|
+
`"both"` halves the edges expanded at each hop.
|
|
124
|
+
3. **Drop `depth` before raising `limit`.** Each hop multiplies; a page is
|
|
125
|
+
linear.
|
|
126
|
+
4. **Index the foreign keys.** Every inward hop is a `WHERE fk IN (…)` read, so
|
|
127
|
+
an unindexed FK makes each hop a scan. `@lunora/advisor` flags it as
|
|
128
|
+
`unindexed-foreign-key`.
|
|
129
|
+
5. **Stop deep-paging.** An offset past 10 000 is refused outright. That is the
|
|
130
|
+
signal the traversal is the wrong tool for the job — narrow the walk instead
|
|
131
|
+
of paging through it.
|
|
132
|
+
|
|
76
133
|
## Problem Class: Write Conflicts (OCC)
|
|
77
134
|
|
|
78
|
-
**Symptom:** mutations on hot rows
|
|
79
|
-
|
|
80
|
-
|
|
135
|
+
**Symptom:** mutations on hot rows fail under concurrency, or the function tops
|
|
136
|
+
the write-conflict section of `lunora insights`. ShardDO uses optimistic
|
|
137
|
+
concurrency control — concurrent writes to the same DO that touch overlapping
|
|
138
|
+
state conflict. There is **no server-side retry loop**: the loser throws
|
|
139
|
+
`ConflictError` (code `CONFLICT`, HTTP 409) and the caller decides, so a client
|
|
140
|
+
that never handles it (`isConflictError` from `@lunora/client`) just surfaces
|
|
141
|
+
409s to the user.
|
|
81
142
|
|
|
82
143
|
**Fixes, in order of preference:**
|
|
83
144
|
|
|
@@ -115,6 +176,11 @@ cross-region reads (with read-your-writes via the Sessions API). Reserve it for
|
|
|
115
176
|
read-mostly tables — `.global()` adds the D1 migration flow (see the
|
|
116
177
|
`lunora-migration-helper` skill) and write-path cost.
|
|
117
178
|
|
|
179
|
+
If the dataset outgrows D1, `.global({ backend: "hyperdrive" })` serves the same
|
|
180
|
+
reactive `.global()` contract from Postgres/MySQL over Cloudflare Hyperdrive —
|
|
181
|
+
see the `lunora-setup-hyperdrive-global` skill (and `lunora migrate
|
|
182
|
+
d1-to-hyperdrive` to move an existing dataset).
|
|
183
|
+
|
|
118
184
|
### `.shardBy(key)` vs `.global()` — choose one per table
|
|
119
185
|
|
|
120
186
|
- `.shardBy(key)`: partitions a table across Durable Objects by key — scales
|
|
@@ -135,8 +201,11 @@ read-mostly tables — `.global()` adds the D1 migration flow (see the
|
|
|
135
201
|
## Checklist
|
|
136
202
|
|
|
137
203
|
- [ ] Scoped one concrete flow; traced every `ctx.db` read/write.
|
|
204
|
+
- [ ] Ran `lunora insights` (if the worker has traffic) for the measured signal.
|
|
138
205
|
- [ ] Checked the Studio Advisors tab / `@lunora/advisor` findings.
|
|
139
206
|
- [ ] Read amplification: replaced `.filter()` with an indexed `.withIndex()`.
|
|
207
|
+
- [ ] Relation traversals: narrowed with `edges` / `direction` before `depth`;
|
|
208
|
+
foreign keys indexed.
|
|
140
209
|
- [ ] Write conflicts: narrowed writes and/or partitioned with `.shardBy(key)`.
|
|
141
210
|
- [ ] Subscription cost: scoped query args so live queries depend on few rows.
|
|
142
211
|
- [ ] Cross-region: applied `.global()` only to read-mostly tables.
|
|
@@ -31,9 +31,12 @@ Set up a working Lunora project as fast as possible.
|
|
|
31
31
|
Lunora into the current project.
|
|
32
32
|
4. Run `lunora codegen` to generate `lunora/_generated/` and typecheck the
|
|
33
33
|
schema + functions. This is the agent's feedback loop.
|
|
34
|
-
5. Start the dev loop
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
5. Start the dev loop. As an agent, run `lunora dev --background` — it starts
|
|
35
|
+
the server as a managed detached process, blocks until it accepts requests,
|
|
36
|
+
prints the URL + PID, and returns (under a detected AI agent, plain
|
|
37
|
+
`lunora dev` does this automatically, with JSON logs). Never leave a bare
|
|
38
|
+
`lunora dev` running in your own shell — it is long-running and does not
|
|
39
|
+
exit.
|
|
37
40
|
6. Verify a query/mutation round-trip works end to end.
|
|
38
41
|
|
|
39
42
|
## Path 1: New Project (Recommended)
|
|
@@ -42,27 +45,71 @@ Set up a working Lunora project as fast as possible.
|
|
|
42
45
|
plugin + `lunora/` already wired together).
|
|
43
46
|
|
|
44
47
|
```bash
|
|
45
|
-
lunora init my-app --
|
|
48
|
+
lunora init my-app --vite react
|
|
46
49
|
cd my-app
|
|
47
50
|
pnpm install
|
|
48
51
|
```
|
|
49
52
|
|
|
50
|
-
### Pick a template
|
|
53
|
+
### Pick a stack: `--vite` (SPA) or `-t` (bespoke template)
|
|
54
|
+
|
|
55
|
+
There are **two scaffold paths**, and they take different flags:
|
|
56
|
+
|
|
57
|
+
**`--vite <framework>` — the create-vite overlay.** Fetches the official
|
|
58
|
+
create-vite base and applies the Lunora layer on top. Use it for a plain SPA:
|
|
59
|
+
|
|
60
|
+
| `--vite` value | Stack |
|
|
61
|
+
| -------------- | ----------------------------------------------- |
|
|
62
|
+
| `react` | React SPA (**the default**) |
|
|
63
|
+
| `vue` | Vue SPA |
|
|
64
|
+
| `solid` | Solid SPA |
|
|
65
|
+
| `svelte` | Svelte SPA |
|
|
66
|
+
| `vanilla` | No framework (overlay-only — not in the picker) |
|
|
67
|
+
|
|
68
|
+
**`-t` / `--template <type>` — a bespoke Lunora template.** Whole-project
|
|
69
|
+
templates fetched remotely (via `giget`) from
|
|
70
|
+
`gh:anolilab/lunora/templates/<type>`:
|
|
71
|
+
|
|
72
|
+
| `-t` value | Stack |
|
|
73
|
+
| ----------------------------- | ---------------------------------------------------------- |
|
|
74
|
+
| `next` | Next.js (App Router, OpenNext on Cloudflare) |
|
|
75
|
+
| `tanstack-start-react` | TanStack Start (React) — SSR with live-loader routes |
|
|
76
|
+
| `tanstack-start-solid` | TanStack Start (Solid) |
|
|
77
|
+
| `tanstack-start-react-rspack` | TanStack Start (React) on Rsbuild, single worker |
|
|
78
|
+
| `solid-v2` | Solid 2.0 SPA (`@solidjs/web`, `vite-plugin-solid` 3) |
|
|
79
|
+
| `rspack-react` | React SPA on Rsbuild (`@lunora/rspack`) |
|
|
80
|
+
| `react-router` | React Router v7 (framework mode), SSR in the Lunora worker |
|
|
81
|
+
| `astro` | Astro + a standalone Lunora worker |
|
|
82
|
+
| `analog` | AnalogJS (Angular) — single worker, Lunora in Nitro |
|
|
83
|
+
| `nuxt` | Nuxt (Vue) — single worker, Lunora in Nitro |
|
|
84
|
+
| `sveltekit` | SvelteKit + a standalone Lunora worker |
|
|
85
|
+
| `expo` | React Native (Expo) — iOS/Android/web + a Lunora worker |
|
|
86
|
+
| `standalone` | Worker-only Lunora backend, no frontend |
|
|
87
|
+
|
|
88
|
+
> There is **no `--template vite`.** SPAs go through `--vite <framework>`; `-t`
|
|
89
|
+
> is only for the bespoke templates above. The one exception is `solid-v2`:
|
|
90
|
+
> create-vite's Solid base is still 1.x, and Solid 2.0 needs its own renderer
|
|
91
|
+
> package, `jsxImportSource`, and Vite plugin major — so it ships as a template
|
|
92
|
+
> rather than an overlay. `--vite solid` stays on Solid 1.x.
|
|
93
|
+
|
|
94
|
+
With neither flag, an interactive run shows the framework picker (defaulting to
|
|
95
|
+
the React overlay) and a **non-interactive run errors out** — so as an agent,
|
|
96
|
+
always pass `--vite` or `-t` explicitly. If the user stated no preference,
|
|
97
|
+
use `--vite react`.
|
|
98
|
+
|
|
99
|
+
### Useful `init` flags
|
|
51
100
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
| `sveltekit` | SvelteKit |
|
|
59
|
-
| `tanstack-start-react` | TanStack Start (React) |
|
|
60
|
-
| `tanstack-start-solid` | TanStack Start (Solid) |
|
|
101
|
+
```bash
|
|
102
|
+
lunora init my-app --vite react --ci github # + a GitHub Actions deploy pipeline (or --ci gitlab)
|
|
103
|
+
lunora init my-app -t next --add auth,email # scaffold capabilities non-interactively
|
|
104
|
+
lunora init my-app --vite react --yes # skip the interactive auth/email offer
|
|
105
|
+
lunora init my-app --vite react --dry-run # walk every step, write nothing
|
|
106
|
+
```
|
|
61
107
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
local
|
|
108
|
+
`--add` accepts a comma-separated list of `ai | auth | backup | browser |
|
|
109
|
+
cloudflare-access | crons | email | flags | hyperdrive | payment | presence |
|
|
110
|
+
queue | storage | workflow`. `--ref <branch|tag|commit>` pins the template
|
|
111
|
+
source (e.g. `--ref alpha`); `--from <dir>` copies from a local templates root
|
|
112
|
+
offline (expects `<type>/` subdirs).
|
|
66
113
|
|
|
67
114
|
### Generate types and push the first run
|
|
68
115
|
|
|
@@ -87,7 +134,23 @@ not exit, so:
|
|
|
87
134
|
|
|
88
135
|
- **Local development (user at the keyboard):** ask the user to run `lunora dev`
|
|
89
136
|
in a terminal.
|
|
90
|
-
- **
|
|
137
|
+
- **Agents:** run `lunora dev --background`. It detaches the server, waits until
|
|
138
|
+
it answers HTTP, prints `Dev server running at <url> (pid <n>)`, and exits —
|
|
139
|
+
no orphaned shell, no PID bookkeeping. When Lunora detects an AI agent
|
|
140
|
+
(Claude Code, Cursor, Codex, …), plain `lunora dev` flips into this mode
|
|
141
|
+
automatically with JSON logs; `LUNORA_AGENT_MODE=0` opts out.
|
|
142
|
+
|
|
143
|
+
Manage the running server afterwards:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
lunora dev status --json # machine-readable: url, pid, uptime, logFile
|
|
147
|
+
lunora dev logs --lines 50 # tail the captured output (.lunora/dev.log)
|
|
148
|
+
lunora dev stop # idempotent — succeeds even if nothing runs
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
A second `lunora dev` never double-starts: it reports the existing instance
|
|
152
|
+
(`.lunora/dev.json` is the lockfile). Probe readiness or liveness at
|
|
153
|
+
`GET /_lunora/status` (`{"ok":true}`).
|
|
91
154
|
|
|
92
155
|
Vite serves on `http://localhost:5173` by default; the Worker is served on the
|
|
93
156
|
same origin via `@cloudflare/vite-plugin`.
|
|
@@ -132,9 +195,12 @@ createRoot(document.querySelector("#root")!).render(
|
|
|
132
195
|
);
|
|
133
196
|
```
|
|
134
197
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
`
|
|
198
|
+
Every client adapter has a matching provider: `@lunora/vue`, `@lunora/solid`,
|
|
199
|
+
`@lunora/svelte`, `@lunora/angular` (`provideLunora` / `injectLunoraClient`),
|
|
200
|
+
and `@lunora/react-native` (`createLunoraClient`, re-exporting `@lunora/react`).
|
|
201
|
+
For meta-frameworks, `@lunora/astro` and `@lunora/nuxt` mount Lunora on the
|
|
202
|
+
server side. `VITE_LUNORA_URL` is optional — it defaults to `location.origin`,
|
|
203
|
+
which is correct for the single-origin dev setup.
|
|
138
204
|
|
|
139
205
|
## Writing Your First Function
|
|
140
206
|
|
|
@@ -143,7 +209,7 @@ Create a schema and a query/mutation to verify the full loop.
|
|
|
143
209
|
`lunora/schema.ts`:
|
|
144
210
|
|
|
145
211
|
```ts
|
|
146
|
-
import { defineSchema, defineTable, v } from "
|
|
212
|
+
import { defineSchema, defineTable, v } from "lunorash/server";
|
|
147
213
|
|
|
148
214
|
export default defineSchema({
|
|
149
215
|
todos: defineTable({
|
|
@@ -157,8 +223,8 @@ export default defineSchema({
|
|
|
157
223
|
`lunora/todos.ts`:
|
|
158
224
|
|
|
159
225
|
```ts
|
|
160
|
-
import type { Id } from "
|
|
161
|
-
import { mutation, query, v } from "
|
|
226
|
+
import type { Id } from "#lunora/_generated/server.js";
|
|
227
|
+
import { mutation, query, v } from "#lunora/_generated/server.js";
|
|
162
228
|
|
|
163
229
|
export const list = query.query(async ({ ctx }) => ctx.db.query("todos").withIndex("by_creation").collect());
|
|
164
230
|
|
|
@@ -233,8 +299,10 @@ ids, `.dev.vars` secrets, and container exports.
|
|
|
233
299
|
## Checklist
|
|
234
300
|
|
|
235
301
|
- [ ] Determined starting point: new project or existing app.
|
|
236
|
-
- [ ] New project: scaffolded with `lunora init --
|
|
302
|
+
- [ ] New project: scaffolded with `lunora init --vite <framework>` (SPA) or
|
|
303
|
+
`lunora init -t <template>` (bespoke) — never `--template vite`.
|
|
237
304
|
- [ ] Existing app: ran `lunora init --here` and wired `LunoraProvider`.
|
|
238
305
|
- [ ] Ran `lunora codegen`: `lunora/_generated/` exists and typecheck is clean.
|
|
239
|
-
- [ ]
|
|
306
|
+
- [ ] Dev server is running — user terminal, or `lunora dev --background`
|
|
307
|
+
(check with `lunora dev status`).
|
|
240
308
|
- [ ] Verified a query/mutation round-trip re-renders the client live.
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
name: lunora-realtime
|
|
3
3
|
description: Wires Lunora's live data into a client. Use for `LunoraClient`/`LunoraProvider`,
|
|
4
4
|
reactive `useQuery`/`useSubscription`, `useMutation` with optimistic updates,
|
|
5
|
-
pagination, connection status, the React/Vue/Solid/Svelte
|
|
6
|
-
`@lunora/db` TanStack binding.
|
|
5
|
+
pagination, connection status, the React/Vue/Solid/Svelte/Angular/React Native
|
|
6
|
+
adapters, and the `@lunora/db` TanStack binding.
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
# Lunora Realtime
|
|
@@ -15,7 +15,8 @@ automatic rollback.
|
|
|
15
15
|
|
|
16
16
|
## When to Use
|
|
17
17
|
|
|
18
|
-
- Wiring a frontend to a Lunora backend (React, Vue, Solid, Svelte
|
|
18
|
+
- Wiring a frontend to a Lunora backend (React, Vue, Solid, Svelte, Angular,
|
|
19
|
+
React Native/Expo, or an Astro/Nuxt meta-framework).
|
|
19
20
|
- Adding optimistic updates, pagination, or presence to the UI.
|
|
20
21
|
- Choosing between live hooks and the `@lunora/db` collection layer.
|
|
21
22
|
|
|
@@ -42,6 +43,19 @@ const client = new LunoraClient({ url });
|
|
|
42
43
|
Vue / Solid / Svelte have matching providers in `@lunora/vue`, `@lunora/solid`,
|
|
43
44
|
`@lunora/svelte`; the hook names and semantics below mirror across them.
|
|
44
45
|
|
|
46
|
+
Two adapters differ in shape:
|
|
47
|
+
|
|
48
|
+
- **`@lunora/angular`** is signal-based rather than hook-based: register with
|
|
49
|
+
`provideLunora(...)`, read the client via `injectLunoraClient()`, and use
|
|
50
|
+
`liveQuery(...)` / `mutate(...)` / `connectionStatus(...)` in place of the
|
|
51
|
+
hooks below.
|
|
52
|
+
- **`@lunora/react-native`** re-exports all of `@lunora/react` and adds
|
|
53
|
+
`createLunoraClient` (plus `@lunora/react-native/auth` for the better-auth
|
|
54
|
+
Expo bridge) — build the client with that instead of `new LunoraClient`.
|
|
55
|
+
|
|
56
|
+
For SSR meta-frameworks, `@lunora/astro` and `@lunora/nuxt` compose Lunora into
|
|
57
|
+
the server (Nitro for Nuxt) and ship reactive-loader server helpers.
|
|
58
|
+
|
|
45
59
|
## Live Queries
|
|
46
60
|
|
|
47
61
|
`useQuery(reference, args)` opens a subscription and returns the value, or
|
|
@@ -67,39 +81,38 @@ const todos = useQuery(api.todos.list, {}) as Doc<"todos">[] | undefined;
|
|
|
67
81
|
|
|
68
82
|
## Authorization & Live Queries
|
|
69
83
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
`
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
actions/queries) where identity is always present.
|
|
84
|
+
**Live queries are identity-aware.** At the WebSocket upgrade the runtime
|
|
85
|
+
stamps the caller's verified identity onto the socket (from the server-minted
|
|
86
|
+
`x-lunora-userid` / `x-lunora-identity` headers, which a client cannot forge).
|
|
87
|
+
Every subscription re-run, shape resolution, and poke-driven refresh executes
|
|
88
|
+
under that socket's own identity, passed by value so a concurrent RPC can't
|
|
89
|
+
clobber it.
|
|
90
|
+
|
|
91
|
+
Practically, this means:
|
|
92
|
+
|
|
93
|
+
- `.use(rls(...))` and `ctx.auth.userId` work the same in a subscribed query as
|
|
94
|
+
in a one-shot read. Shape subscriptions AND-merge the shape predicate with
|
|
95
|
+
the table's RLS read base-where, so the membership query the poke protocol
|
|
96
|
+
runs is RLS-correct by construction — never the client's word for it.
|
|
97
|
+
- An **anonymous** socket carries no identity, so an RLS/`ctx.auth` query fails
|
|
98
|
+
closed (empty/denied) rather than leaking another user's rows.
|
|
99
|
+
- **Token expiry is enforced on the socket.** When the resolved credential
|
|
100
|
+
carries an expiry, the DO sends a `TOKEN_EXPIRED` error frame and closes with
|
|
101
|
+
code `4001` at the next send at or after that instant. `LunoraClient`
|
|
102
|
+
reconnects automatically and re-resolves a fresh identity — but surface it in
|
|
103
|
+
the UI if a re-login is required.
|
|
104
|
+
|
|
105
|
+
You can still scope data structurally when it fits the domain — `.shardBy(userId)`
|
|
106
|
+
(or tenant/room) partitions state so a subscription is narrow by construction,
|
|
107
|
+
and explicit query args keep subscriptions cheap (see the args-scoping note
|
|
108
|
+
above). Prefer those for _performance_; use `rls()` / `ctx.auth` for
|
|
109
|
+
_authorization_. They compose.
|
|
97
110
|
|
|
98
111
|
## Mutations + Optimistic Updates
|
|
99
112
|
|
|
100
|
-
`useMutation(reference)` returns `{ mutate, pending }`. Pass an
|
|
101
|
-
callback to paint the next state immediately; if the server
|
|
102
|
-
runtime rolls the cache back automatically.
|
|
113
|
+
`useMutation(reference)` returns `{ mutate, pending }`. Pass an
|
|
114
|
+
`optimisticUpdate` callback to paint the next state immediately; if the server
|
|
115
|
+
rejects the call the runtime rolls the cache back automatically.
|
|
103
116
|
|
|
104
117
|
```tsx
|
|
105
118
|
import { useMutation } from "@lunora/react";
|
|
@@ -111,8 +124,8 @@ const { mutate: add, pending } = useMutation(api.todos.add);
|
|
|
111
124
|
await add(
|
|
112
125
|
{ text },
|
|
113
126
|
{
|
|
114
|
-
|
|
115
|
-
const list = (
|
|
127
|
+
optimisticUpdate: (store) => {
|
|
128
|
+
const list = store.getQuery(api.todos.list, {}) ?? [];
|
|
116
129
|
const provisional: Doc<"todos"> = {
|
|
117
130
|
_id: `optimistic_${Date.now()}` as Id<"todos">,
|
|
118
131
|
_creationTime: Date.now(),
|
|
@@ -120,15 +133,21 @@ await add(
|
|
|
120
133
|
done: false,
|
|
121
134
|
createdAt: Date.now(),
|
|
122
135
|
};
|
|
123
|
-
|
|
136
|
+
store.setQuery(api.todos.list, {}, [provisional, ...list]);
|
|
124
137
|
},
|
|
125
138
|
},
|
|
126
139
|
);
|
|
127
140
|
```
|
|
128
141
|
|
|
129
|
-
-
|
|
130
|
-
|
|
131
|
-
entry; on failure the cache
|
|
142
|
+
- `optimisticUpdate` names the query it patches, because `todos.add` and
|
|
143
|
+
`todos.list` are different functions and nothing can infer the link. When the
|
|
144
|
+
server delta arrives it replaces the optimistic entry; on failure the cache
|
|
145
|
+
reverts. `store.getAllQueries(fn)` patches every subscribed variant of a query
|
|
146
|
+
at once.
|
|
147
|
+
- The per-call `optimistic: (current) => next` shortcut exists too, but it only
|
|
148
|
+
patches a subscription registered under the **mutation's own** reference and
|
|
149
|
+
args — a counter or a document by id. It is a silent no-op for the
|
|
150
|
+
`add`-updates-`list` shape above.
|
|
132
151
|
- `pending` is `true` while the call is in flight — disable the submit button
|
|
133
152
|
with it.
|
|
134
153
|
- **Offline queue:** mutations made while disconnected are queued by
|
|
@@ -154,6 +173,13 @@ schema + functions into live TanStack DB collections). Reach for it when the app
|
|
|
154
173
|
needs client-side indexes/joins or a persistent optimistic outbox; raw `useQuery`
|
|
155
174
|
/`useMutation` are enough for straightforward live lists.
|
|
156
175
|
|
|
176
|
+
Call `defineCollections` **once** at module scope and treat the returned
|
|
177
|
+
collections as the single source of truth for each table. Don't mirror rows into
|
|
178
|
+
a parallel store, and never build derived indexes (trees, search, undo captures)
|
|
179
|
+
from a copy — optimistic writes and sync deltas land only on the Lunora
|
|
180
|
+
collection, so code reading a copy silently reads stale rows while `useLiveQuery`
|
|
181
|
+
renders fresh data.
|
|
182
|
+
|
|
157
183
|
## Common Pitfalls
|
|
158
184
|
|
|
159
185
|
1. **Creating `LunoraClient` inside a component.** It re-opens the socket every
|
|
@@ -165,6 +191,12 @@ needs client-side indexes/joins or a persistent optimistic outbox; raw `useQuery
|
|
|
165
191
|
4. **Optimistic shape drift.** The provisional value must match the query's
|
|
166
192
|
element shape (including `_id`/`_creationTime`) or the UI flickers when the
|
|
167
193
|
real delta lands.
|
|
194
|
+
5. **Two sources of truth for the same rows.** `defineCollections` more than once
|
|
195
|
+
per table (or a parallel store of your own) mints copies that drift. Reads
|
|
196
|
+
through `useLiveQuery` look fine, but derived indexes built from the copy can
|
|
197
|
+
**miss rows** — rows written to the collection after the copy was taken never
|
|
198
|
+
appear in the index — or **hold stale rows** — rows updated or deleted in the
|
|
199
|
+
collection still show their old value. One instance, one collection per table.
|
|
168
200
|
|
|
169
201
|
## Checklist
|
|
170
202
|
|
|
@@ -175,3 +207,5 @@ needs client-side indexes/joins or a persistent optimistic outbox; raw `useQuery
|
|
|
175
207
|
- [ ] Query `args` scoped narrowly to avoid over-broad re-renders.
|
|
176
208
|
- [ ] Pagination via `usePaginatedQuery`/`useInfiniteQuery` over `.paginate`.
|
|
177
209
|
- [ ] Considered `@lunora/db` if the app needs local indexes/joins or an outbox.
|
|
210
|
+
- [ ] `@lunora/db`: one `defineCollections` instance; no parallel copy of rows;
|
|
211
|
+
derived indexes built from the same collection the UI renders.
|
|
@@ -9,8 +9,10 @@ description: Adds authentication to a Lunora app. Use for sign-up/sign-in, email
|
|
|
9
9
|
|
|
10
10
|
Wire authentication into a Lunora app using the `auth` registry item, which is
|
|
11
11
|
built on `@lunora/auth` (a thin wrapper over
|
|
12
|
-
[better-auth](https://www.better-auth.com)) with
|
|
13
|
-
`SessionDO`
|
|
12
|
+
[better-auth](https://www.better-auth.com)) with identity **and session** tables
|
|
13
|
+
in D1. (`SessionDO` is a standalone TTL'd token store `@lunora/auth` never
|
|
14
|
+
calls — don't wire it for auth. The Durable Object option is `LunoraAuthDO` /
|
|
15
|
+
`.auth({ namespace })`, which holds the whole better-auth schema.)
|
|
14
16
|
|
|
15
17
|
## When to Use
|
|
16
18
|
|
|
@@ -104,7 +106,9 @@ dependency). OAuth items need the provider's client id/secret added to
|
|
|
104
106
|
The runtime resolves the session and exposes the user on every context:
|
|
105
107
|
|
|
106
108
|
```ts
|
|
107
|
-
import { LunoraError
|
|
109
|
+
import { LunoraError } from "lunorash/server";
|
|
110
|
+
|
|
111
|
+
import { mutation, v } from "#lunora/_generated/server.js";
|
|
108
112
|
|
|
109
113
|
export const createDocument = mutation.input({ title: v.string() }).mutation(async ({ ctx, args: { title } }) => {
|
|
110
114
|
if (!ctx.auth.userId) {
|
|
@@ -119,22 +123,35 @@ call the better-auth server API — see the scaffolded `lunora/auth/index.ts`.
|
|
|
119
123
|
|
|
120
124
|
### In the UI (React)
|
|
121
125
|
|
|
126
|
+
`useAuth()` returns exactly `{ setToken, status, token, user }` — there is no
|
|
127
|
+
`signIn` / `signOut`. Lunora owns the **token**, not the sign-in flow: run the
|
|
128
|
+
flow with the better-auth client (`authClient.signIn.email(...)`), then hand the
|
|
129
|
+
resulting JWT to `setToken`. `setToken(null)` signs out.
|
|
130
|
+
|
|
122
131
|
```tsx
|
|
123
132
|
import { Authenticated, Unauthenticated, useAuth } from "@lunora/react";
|
|
124
133
|
|
|
134
|
+
import { authClient } from "./auth-client";
|
|
135
|
+
|
|
125
136
|
function Account() {
|
|
126
|
-
const {
|
|
137
|
+
const { setToken, user } = useAuth();
|
|
138
|
+
|
|
139
|
+
const signIn = async () => {
|
|
140
|
+
const { data } = await authClient.signIn.email({ email, password });
|
|
141
|
+
|
|
142
|
+
setToken(data?.token ?? null);
|
|
143
|
+
};
|
|
127
144
|
|
|
128
145
|
return (
|
|
129
146
|
<>
|
|
130
147
|
<Authenticated>
|
|
131
148
|
<span>Signed in as {user?.email}</span>
|
|
132
|
-
<button type="button" onClick={() =>
|
|
149
|
+
<button type="button" onClick={() => setToken(null)}>
|
|
133
150
|
Sign out
|
|
134
151
|
</button>
|
|
135
152
|
</Authenticated>
|
|
136
153
|
<Unauthenticated>
|
|
137
|
-
<button type="button" onClick={
|
|
154
|
+
<button type="button" onClick={signIn}>
|
|
138
155
|
Sign in
|
|
139
156
|
</button>
|
|
140
157
|
</Unauthenticated>
|
|
@@ -146,6 +163,16 @@ function Account() {
|
|
|
146
163
|
`@lunora/react` also exports `AuthLoading` and `useAuthState` for the loading
|
|
147
164
|
window before the session resolves.
|
|
148
165
|
|
|
166
|
+
**Branch on `status`, never on `user === null`.** `status` is one of
|
|
167
|
+
`"unauthenticated"`, `"loading"`, `"authenticated"` or `"unreachable"`, and the
|
|
168
|
+
same four values back the gates in every adapter (`@lunora/vue`,
|
|
169
|
+
`@lunora/solid`, `@lunora/svelte`, `@lunora/angular`). `"unreachable"` means the
|
|
170
|
+
credential is held but the session endpoint could not be reached — an offline
|
|
171
|
+
reload, or a transient 5xx. It gates as **authenticated**, so the app renders,
|
|
172
|
+
but `user` may still be `null`; a UI that treats that `null` as signed out shows
|
|
173
|
+
a signed-out screen to a signed-in user. The full contract is documented on
|
|
174
|
+
`AuthStatus` in `@lunora/client/auth`.
|
|
175
|
+
|
|
149
176
|
## Common Pitfalls
|
|
150
177
|
|
|
151
178
|
1. **Declaring better-auth tables in `lunora/schema.ts`.** They live in D1 and
|
|
@@ -92,24 +92,41 @@ that's a remote resource only `wrangler hyperdrive create` can mint. Importing
|
|
|
92
92
|
lunora codegen
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
-
When codegen sees `ctx.sql` used, it adds `sql: SqlClient` to
|
|
96
|
-
only** — never `QueryCtx`/`MutationCtx` — with a JSDoc restating
|
|
97
|
-
determinism/realtime caveat
|
|
95
|
+
When codegen sees `ctx.sql` used, it adds `readonly sql: SqlClient` to
|
|
96
|
+
**`ActionCtx` only** — never `QueryCtx`/`MutationCtx` — with a JSDoc restating
|
|
97
|
+
the determinism/realtime caveat, and emits a `.hyperdrive()` method on the
|
|
98
|
+
generated app builder for you to supply the client (step 4).
|
|
98
99
|
|
|
99
|
-
## Step 4:
|
|
100
|
+
## Step 4: Wire the client once, on the app builder
|
|
100
101
|
|
|
101
|
-
|
|
102
|
+
`ctx.sql` is `readonly` — assigning to it inside a handler is a `TS2540`.
|
|
103
|
+
Codegen cannot build the client for you (turning a connection string into a
|
|
104
|
+
`SqlClient` needs the driver you chose), so it emits a config thunk you fill in
|
|
105
|
+
at the app level. It is called once per shard construction, not per request:
|
|
106
|
+
|
|
107
|
+
```ts title="src/server/index.ts"
|
|
108
|
+
import type { HyperdriveLike } from "@lunora/hyperdrive";
|
|
102
109
|
import { createHyperdrive, fromPostgresJs } from "@lunora/hyperdrive";
|
|
103
110
|
import postgres from "postgres";
|
|
104
111
|
|
|
105
|
-
import {
|
|
112
|
+
import { defineApp } from "../lunora/_generated/app.js";
|
|
113
|
+
|
|
114
|
+
const app = defineApp<Env>()
|
|
115
|
+
.shard((env) => env.SHARD)
|
|
116
|
+
.hyperdrive((env) => fromPostgresJs(postgres(createHyperdrive(env.HYPERDRIVE as HyperdriveLike).connectionString)))
|
|
117
|
+
.build();
|
|
118
|
+
|
|
119
|
+
export const ShardDO = app.ShardDO;
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Then read it from any action:
|
|
106
123
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
ctx.sql = fromPostgresJs(postgres(connectionString));
|
|
124
|
+
```ts
|
|
125
|
+
import { action, v } from "@/lunora/_generated/server";
|
|
110
126
|
|
|
111
|
-
|
|
112
|
-
})
|
|
127
|
+
export const listLegacyOrders = action
|
|
128
|
+
.input({ orgId: v.string() })
|
|
129
|
+
.action(async ({ ctx, args: { orgId } }) => ctx.sql.query<{ id: string; total: number }>("select id, total from orders where org = $1", [orgId]));
|
|
113
130
|
```
|
|
114
131
|
|
|
115
132
|
The package never rewrites SQL — use your driver's native placeholders (`$1` for
|
|
@@ -122,10 +139,13 @@ Postgres change. To make external data reactive, write a projection into a
|
|
|
122
139
|
`defineSchema` table from the same action — that write _is_ tracked:
|
|
123
140
|
|
|
124
141
|
```ts
|
|
142
|
+
import { api } from "@/lunora/_generated/api";
|
|
143
|
+
|
|
125
144
|
const [row] = await ctx.sql.query<{ id: string; total: number }>("select id, total from orders where id = $1", [id]);
|
|
126
145
|
|
|
127
|
-
// This write re-runs live queries reading `orders
|
|
128
|
-
|
|
146
|
+
// This write re-runs live queries reading `orders`. Pass the generated
|
|
147
|
+
// reference — `ctx.run*` takes a reference, not a "file:fn" string.
|
|
148
|
+
await ctx.runMutation(api.orders.upsert, { id: row.id, total: row.total });
|
|
129
149
|
```
|
|
130
150
|
|
|
131
151
|
## Common Pitfalls
|