@lunora/server 1.0.0-alpha.7 → 1.0.0-alpha.71

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 (78) hide show
  1. package/LICENSE.md +219 -0
  2. package/README.md +46 -0
  3. package/dist/data-model.d.mts +260 -164
  4. package/dist/data-model.d.ts +260 -164
  5. package/dist/data-model.mjs +0 -1
  6. package/dist/drizzle.mjs +1 -1
  7. package/dist/index.d.mts +1837 -1163
  8. package/dist/index.d.ts +1837 -1163
  9. package/dist/index.mjs +1 -28
  10. package/dist/otel.d.mts +543 -0
  11. package/dist/otel.d.ts +543 -0
  12. package/dist/otel.mjs +1 -0
  13. package/dist/packem_shared/DEFAULT_LIMIT-yHJ5O96W.mjs +1 -0
  14. package/dist/packem_shared/LunoraEnvError-CgpI2Mm_.mjs +3 -0
  15. package/dist/packem_shared/LunoraError-LVhdU0Lo.mjs +1 -0
  16. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-C24x2Cox.mjs +1 -0
  17. package/dist/packem_shared/allowAll-BnyNbJZT.mjs +1 -0
  18. package/dist/packem_shared/asBucketStorage-BthCnWop.mjs +1 -0
  19. package/dist/packem_shared/bindOrm-Bp9hsM2q.mjs +1 -0
  20. package/dist/packem_shared/buildMaskRegistry-BCIzKxdK.mjs +1 -0
  21. package/dist/packem_shared/buildRlsReadRegistry-CE5k6akj.mjs +1 -0
  22. package/dist/packem_shared/composePluginMiddleware-ByhCqIGt.mjs +1 -0
  23. package/dist/packem_shared/context-identity-GunnA6La.mjs +1 -0
  24. package/dist/packem_shared/createPolicyDsl-sV1swpkD.mjs +1 -0
  25. package/dist/packem_shared/createSecrets-CgVPiW2C.mjs +1 -0
  26. package/dist/packem_shared/defineAggregateIndex-BoiwhUKL.mjs +1 -0
  27. package/dist/packem_shared/defineIdentity-B7gfAgxx.mjs +1 -0
  28. package/dist/packem_shared/defineMigration-Bfpwxv2f.mjs +1 -0
  29. package/dist/packem_shared/defineMutator-BgpQ-xUo.mjs +1 -0
  30. package/dist/packem_shared/defineShape-Ds8uNqzX.mjs +1 -0
  31. package/dist/packem_shared/defineStorageRule-BDu01PUn.mjs +1 -0
  32. package/dist/packem_shared/functions-CDC08CWY.mjs +1 -0
  33. package/dist/packem_shared/httpAction-BpEJYwzV.mjs +5 -0
  34. package/dist/packem_shared/initLunora-BilEHyNs.mjs +1 -0
  35. package/dist/packem_shared/mask-CD7SWEvR.mjs +1 -0
  36. package/dist/packem_shared/middleware-D9_39C5C.mjs +1 -0
  37. package/dist/packem_shared/onConnect-CEtRmUpJ.mjs +1 -0
  38. package/dist/packem_shared/plugin-184MITA3.mjs +1 -0
  39. package/dist/packem_shared/policy-tag-Dprt9JWo.mjs +1 -0
  40. package/dist/packem_shared/protectPublic-BhKewPqm.mjs +1 -0
  41. package/dist/packem_shared/rls-CI2149Ve.mjs +1 -0
  42. package/dist/packem_shared/run-middleware-BeEEqmdE.mjs +1 -0
  43. package/dist/packem_shared/storageRules-6_G8JpCv.mjs +1 -0
  44. package/dist/packem_shared/types.d-B-pIn8rE.d.mts +141 -0
  45. package/dist/packem_shared/types.d-CdPDEjhz.d.ts +141 -0
  46. package/dist/rls/testing.d.mts +31 -31
  47. package/dist/rls/testing.d.ts +31 -31
  48. package/dist/rls/testing.mjs +1 -49
  49. package/dist/types.d.mts +1478 -479
  50. package/dist/types.d.ts +1478 -479
  51. package/dist/types.mjs +1 -31
  52. package/package.json +17 -4
  53. package/dist/packem_shared/LunoraEnvError-DjFkpkSP.mjs +0 -187
  54. package/dist/packem_shared/LunoraError-DhggBJZF.mjs +0 -51
  55. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-DH9zLyXa.mjs +0 -114
  56. package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +0 -11
  57. package/dist/packem_shared/bindOrm-Ce57S3N9.mjs +0 -128
  58. package/dist/packem_shared/buildRlsReadRegistry-CkxuS69B.mjs +0 -107
  59. package/dist/packem_shared/composePluginMiddleware-Ck5_TUO8.mjs +0 -100
  60. package/dist/packem_shared/createPolicyDsl-De67zPDS.mjs +0 -29
  61. package/dist/packem_shared/createSecrets-TsIP9lOa.mjs +0 -55
  62. package/dist/packem_shared/defineAggregateIndex-C2gT1GzM.mjs +0 -252
  63. package/dist/packem_shared/defineMigration-CAJLr6fx.mjs +0 -8
  64. package/dist/packem_shared/defineMutator-EIXAWhs9.mjs +0 -11
  65. package/dist/packem_shared/defineShape-CJ27Wx7o.mjs +0 -17
  66. package/dist/packem_shared/defineStorageRule-qu0mpilX.mjs +0 -20
  67. package/dist/packem_shared/functions-Di9FUNkf.mjs +0 -5
  68. package/dist/packem_shared/httpAction-B7FYUEgr.mjs +0 -340
  69. package/dist/packem_shared/initLunora-lxwHTEV3.mjs +0 -100
  70. package/dist/packem_shared/mask-eCUYOwhd.mjs +0 -211
  71. package/dist/packem_shared/onConnect-CIPXKPyw.mjs +0 -13
  72. package/dist/packem_shared/policy-tag-DvpVH2tv.mjs +0 -13
  73. package/dist/packem_shared/protectPublic-BjFkQ_Or.mjs +0 -15
  74. package/dist/packem_shared/rls-ClmjkkxZ.mjs +0 -569
  75. package/dist/packem_shared/run-middleware-CYQOuoV6.mjs +0 -18
  76. package/dist/packem_shared/storageRules-4a30FSpI.mjs +0 -88
  77. package/dist/packem_shared/types.d-BDY0FYHK.d.ts +0 -135
  78. package/dist/packem_shared/types.d-DmvyEMD6.d.mts +0 -135
package/dist/types.d.ts CHANGED
@@ -1,4 +1,68 @@
1
1
  import { ValidatorMap, InferValidatorMap, Id, Validator, Infer } from '@lunora/values';
2
+ /**
3
+ * The set of languages a `.searchIndex()` can declare, in one place.
4
+ *
5
+ * Three layers need it and none of them can import the others: `@lunora/server`
6
+ * types the option and validates it at schema-build time, and `@lunora/do` maps
7
+ * it onto a stopword list at analysis time — with no dependency edge between
8
+ * them (nor should there be: the schema builder must stay usable without the DO
9
+ * runtime). So it lived in three hand-maintained copies, where adding a
10
+ * language silently produced a schema the builder accepts and the analyzer
11
+ * treats as `"none"` — an index that quietly stops dropping stopwords.
12
+ *
13
+ * The list is the single source: the type is derived from it, so the two cannot
14
+ * drift. `@lunora/do` still owns the stopword *lists*; it keys them by these
15
+ * names, and its own tests assert the two agree.
16
+ *
17
+ * Inlined by the bundler into each `dist` rather than published, so this stays
18
+ * a shared *constant*, not a dependency edge. See the `shared/` section of
19
+ * AGENTS.md.
20
+ */
21
+ /**
22
+ * Every accepted value, sorted, so an error message can list what *is*
23
+ * accepted without re-sorting. `"none"` is explicit rather than absent: it
24
+ * means "fold, but drop nothing", which is also what an index with no declared
25
+ * language gets.
26
+ */
27
+ declare const SEARCH_LANGUAGES: readonly ["de", "en", "es", "fr", "it", "nl", "none", "pt"];
28
+ /** A declared analysis language, derived from the list above. */
29
+ type SearchLanguage = (typeof SEARCH_LANGUAGES)[number];
30
+ /**
31
+ * How a search index is stored. `"portable"` promises identical behaviour on
32
+ * every backend; `"native"` opts into the engine's own full-text index where it
33
+ * has one, trading that promise for its speed.
34
+ *
35
+ * Lives beside the languages for the same reason they do: it is declared in
36
+ * `@lunora/server`, acted on in the storage layer, and there is no dependency
37
+ * edge between them — so a third hand-maintained copy is how a typo turns into
38
+ * a silently different *physical layout*.
39
+ */
40
+ declare const SEARCH_STRATEGIES: readonly ["native", "portable"];
41
+ /** A declared storage strategy, derived from the list above. */
42
+ type SearchStrategy = (typeof SEARCH_STRATEGIES)[number];
43
+ /**
44
+ * Declared HTTP caching for an exposed endpoint (`.expose({ cache })`). Lives
45
+ * here, alongside the path/method contract, for the same reason: the runtime
46
+ * WRITES these headers and the OpenAPI emitter DESCRIBES them, and the two must
47
+ * not be able to disagree. Deriving both from {@link cacheControlValue} /
48
+ * {@link cacheVaryValue} makes "the published spec matches what the runtime
49
+ * actually sends" structural rather than a hand-kept invariant.
50
+ */
51
+ interface RestCachePolicy {
52
+ /**
53
+ * Extra request headers this app authenticates on, beyond
54
+ * {@link CREDENTIAL_HEADERS}. Declare these whenever `resolveIdentity` reads
55
+ * something else (`x-api-key`, a tenant header, …): they join both the
56
+ * credential check and the emitted `Vary`. Without them, a caller
57
+ * authenticating that way is treated as anonymous.
58
+ */
59
+ readonly credentialHeaders?: ReadonlyArray<string>;
60
+ readonly maxAge: number;
61
+ readonly scope: "private" | "public";
62
+ readonly staleWhileRevalidate?: number;
63
+ readonly tag?: string;
64
+ readonly vary?: string;
65
+ }
2
66
  /** Map of validators describing a function's args record. Alias of `@lunora/values`' shared {@link ValidatorMap}. */
3
67
  type ArgsValidator = ValidatorMap;
4
68
  /** Infer the args object type from an {@link ArgsValidator}. Alias of `@lunora/values`' shared {@link InferValidatorMap}. */
@@ -6,12 +70,12 @@ type InferArgs<A extends ArgsValidator> = InferValidatorMap<A>;
6
70
  /** Storage backend for a `.global()` table: D1 (default) or a Postgres/MySQL database via Cloudflare Hyperdrive (PlanetScale, Neon, …). */
7
71
  type GlobalBackend = "d1" | "hyperdrive";
8
72
  /**
9
- * Cloudflare Durable Object data-residency jurisdiction declared via
10
- * `defineSchema(...).jurisdiction("…")`. Restricts where every DO the app
11
- * reaches runs and persists data (GDPR, FedRAMP, US data residency). Widening
12
- * union — Cloudflare adds values over time.
13
- * @see https://developers.cloudflare.com/durable-objects/reference/data-location/
14
- */
73
+ * Cloudflare Durable Object data-residency jurisdiction declared via
74
+ * `defineSchema(...).jurisdiction("…")`. Restricts where every DO the app
75
+ * reaches runs and persists data (GDPR, FedRAMP, US data residency). Widening
76
+ * union — Cloudflare adds values over time.
77
+ * @see https://developers.cloudflare.com/durable-objects/reference/data-location/
78
+ */
15
79
  type DurableObjectJurisdiction = "eu" | "fedramp" | "us";
16
80
  /** How a table is routed at runtime. */
17
81
  type ShardMode = {
@@ -23,31 +87,181 @@ type ShardMode = {
23
87
  } | {
24
88
  kind: "root";
25
89
  };
90
+ /** Poll cadence for a sourced table — `"manual"` (pull only on an explicit trigger) or a fixed interval. */
91
+ type ExternalSourceRefresh = "manual" | {
92
+ everyMs: number;
93
+ };
94
+ /**
95
+ * Delete-detection mode for external-source ingest (plan 077 / 136).
96
+ *
97
+ * `"full-pull"` (the default) reads the **whole** tenant membership each tick and
98
+ * diffs it, so it observes upstream deletes for free — but costs a full read per tick
99
+ * (the Phase-0 bench put the ceiling at ~10k rows).
100
+ *
101
+ * `"incremental"` pulls **only rows past a durable watermark** (`cursor`), cheap for
102
+ * large low-churn tables above the full-pull cap. Because an absent row then means
103
+ * "unchanged", not "deleted", incremental requires a delete-visibility path: either a
104
+ * `reconcileEveryMs` periodic full-pull sweep, or a `softDeleteColumn` whose
105
+ * tombstones the pull returns. `defineSchema` throws (and the
106
+ * `external_source_incremental_no_delete_path` advisor lint fails the build) when an
107
+ * incremental source declares neither.
108
+ */
109
+ type ExternalSourceMode = "full-pull" | "incremental";
110
+ /**
111
+ * Incremental-ingest cursor (plan 136): the monotonic watermark column plus the
112
+ * watermark-parameterized pull query. `column` names the field in the pulled rows
113
+ * whose max becomes the next watermark (e.g. `"updated_at"`). `query` is a second
114
+ * SQL that returns only rows changed since the watermark — the watermark binds as
115
+ * the parameter AFTER `tenantBy`'s params (e.g. Postgres
116
+ * `... WHERE tenant_id = $1 AND updated_at >= $2 ORDER BY updated_at`). Prefer `>=`
117
+ * with the idempotent upsert apply so rows sharing the boundary timestamp are never
118
+ * skipped (re-pulling them is a no-op).
119
+ */
120
+ interface ExternalSourceCursor {
121
+ /** The monotonic watermark column in the pulled rows; its max advances the stored watermark. */
122
+ column: string;
123
+ /** The incremental pull SQL. `tenantBy`'s params bind first, then the watermark as the trailing param. */
124
+ query: string;
125
+ }
126
+ /**
127
+ * Config for `.source(...)` (plan 077): declares a table as **materialized from an
128
+ * external Postgres/MySQL behind Cloudflare Hyperdrive**, not written by user
129
+ * mutations. A system-driven poll loop reads the tenant slice and lands it in the
130
+ * DO's SQLite (via the validated CDC writer), after which `defineShape` carries it
131
+ * to clients unchanged. Orthogonal to `shardMode` — a sourced table almost always
132
+ * also `.shardBy()`s, in which case `tenantBy` is the mandatory tenant-isolation
133
+ * boundary (enforced by the `external_source_unscoped` advisor lint).
134
+ */
135
+ interface ExternalSourceDefinition {
136
+ /** The wrangler Hyperdrive binding name the poll loop reads from. */
137
+ binding: string;
138
+ /** Project the materialized rows to these columns (passed to the membership diff). Omit ⇒ the full mapped document. */
139
+ columns?: ReadonlyArray<string>;
140
+ /** **Required for `mode: "incremental"`**: the watermark column + watermark-parameterized pull query (plan 136). Rejected on a `"full-pull"` source. */
141
+ cursor?: ExternalSourceCursor;
142
+ /** Column whose value becomes the Lunora `_id`. Defaults to `"id"`. */
143
+ idColumn?: string;
144
+ /** Transform an external row into the stored document body. Omit ⇒ every selected column except `idColumn` is copied. */
145
+ map?: (row: Record<string, unknown>) => Record<string, unknown>;
146
+ /** Delete-detection mode. `"full-pull"` (the default) diffs the whole membership; `"incremental"` pulls past a `cursor` watermark. */
147
+ mode?: ExternalSourceMode;
148
+ /** The full tenant-membership query, with driver-native placeholders (`$1` / `?`). `tenantBy` binds its params. */
149
+ query: string;
150
+ /**
151
+ * **Incremental delete-visibility (plan 136)**: run a full-pull sweep at most
152
+ * this often (millis) to GC upstream deletes an incremental slice can't see.
153
+ * One of `reconcileEveryMs` / `softDeleteColumn` is required for incremental;
154
+ * rejected on a `"full-pull"` source.
155
+ */
156
+ reconcileEveryMs?: number;
157
+ /** Poll cadence, or `"manual"`. Omit ⇒ the runtime's size-scaled default. */
158
+ refresh?: ExternalSourceRefresh;
159
+ /**
160
+ * **Incremental delete-visibility (plan 136)**: the upstream soft-delete
161
+ * tombstone column (e.g. `"deleted_at"`). When set, the incremental pull must
162
+ * return tombstoned rows and the ingest turns each into a local delete — an
163
+ * alternative to `reconcileEveryMs`. Rejected on a `"full-pull"` source.
164
+ */
165
+ softDeleteColumn?: string;
166
+ /**
167
+ * **Mandatory under `.shardBy()`**: map this DO's shard key → the query's bound
168
+ * params, so a tenant DO can only ever pull its own rows. An unscoped sourced +
169
+ * sharded table replicates the whole multitenant table into every shard — the
170
+ * `external_source_unscoped` advisor lint fails the build when this is absent.
171
+ */
172
+ tenantBy?: (shardKey: string) => ReadonlyArray<unknown>;
173
+ }
26
174
  interface IndexDefinition {
27
175
  fields: ReadonlyArray<string>;
28
176
  name: string;
29
177
  unique?: boolean;
30
178
  }
31
179
  interface SearchIndexDefinition {
180
+ /** Indexed text column; a dot-separated path (`"properties.name"`) reads a nested field. */
32
181
  field: string;
182
+ /** Columns `.eq()` may narrow by inside the search builder. At most 16. */
33
183
  filterFields?: ReadonlyArray<string>;
184
+ /**
185
+ * Text analysis for this index. Accent folding is always applied — it is
186
+ * what makes `café` and `cafe` the same token on every backend, which they
187
+ * otherwise are not. Naming a language additionally drops that language's
188
+ * stopwords from both documents and queries.
189
+ *
190
+ * Analysis is baked into the stored index, so changing this rebuilds it:
191
+ * the runtime records which profile a companion was built with and
192
+ * re-indexes when it no longer matches.
193
+ */
194
+ language?: SearchLanguage;
195
+ name: string;
196
+ /**
197
+ * Skip the migration-time backfill. By default, creating the index's
198
+ * companion also indexes the rows already in the table, so a search index
199
+ * added to a populated table works immediately. On a very large table that
200
+ * scan is expensive to run inside a deploy: `staged: true` maintains the
201
+ * index on write only and leaves the initial population to an out-of-band
202
+ * `backfillSearchIndexes`.
203
+ */
204
+ staged?: boolean;
205
+ /**
206
+ * How the index is stored and matched.
207
+ *
208
+ * `"portable"` (default) keeps one implementation on every backend, with
209
+ * identical matching *and* ranking — the invariant the rest of the search
210
+ * docs rest on.
211
+ *
212
+ * `"native"` hands matching to the engine's own full-text index where it has
213
+ * one (Postgres `tsvector` + GIN today; ignored elsewhere). It scales far
214
+ * better on large corpora and common terms, because the portable path
215
+ * aggregates every matching token row before ranking. The trade: the engine
216
+ * ranks, so results are ordered by its formula rather than the shared
217
+ * scorer. Matching still agrees — the vector is built from the same analyzed
218
+ * tokens — but a `.global()` table using it will not return rows in the same
219
+ * order as the sharded twin.
220
+ */
221
+ strategy?: SearchStrategy;
222
+ }
223
+ /**
224
+ * A geospatial index declared via `.geoIndex(name, { field })`. The runtime
225
+ * maintains a geohash companion table over the `v.geoPoint()` column `field` so
226
+ * `withGeoIndex(name, q => q.near(point, radius) | q.within(bbox))` resolves a
227
+ * proximity / bounding-box read as a geohash-prefix range scan plus a Haversine
228
+ * refine/sort on the candidate rows.
229
+ *
230
+ * - `field` — the `v.geoPoint()` column whose lat/lng feed the geohash.
231
+ * - `precision` — geohash character length on the companion (default 9, ~4.8 m cells); higher precision narrows each cell.
232
+ */
233
+ interface GeoIndexDefinition {
234
+ field: string;
34
235
  name: string;
236
+ precision?: number;
237
+ }
238
+ /**
239
+ * Declarative table-level TTL declared via `.ttl(field, { after? })`. A DO
240
+ * alarm-driven sweep deletes (or, when the table also `.softDelete()`s,
241
+ * soft-deletes) rows whose expiry timestamp has passed.
242
+ *
243
+ * - `field` — an epoch-millisecond column. Without `after`, its value is the absolute expiry instant; with `after`, `field` is a base timestamp and the row expires `after` milliseconds later (`field + after`).
244
+ * - `after` — optional millisecond offset added to `field` to derive the expiry.
245
+ */
246
+ interface TtlDefinition {
247
+ after?: number;
248
+ field: string;
35
249
  }
36
250
  /** Reducer applied by an aggregate index. */
37
251
  type AggregateOp = "avg" | "count" | "max" | "min" | "sum";
38
252
  /**
39
- * Declared aggregate index — the schema-level seam that lets the runtime keep
40
- * O(1) counters/sums in step with row writes (via the trigger runner) and
41
- * route matching reads through them.
42
- *
43
- * - `on` — the table whose rows feed the aggregate.
44
- * - `op` — the reducer. `count` is field-less; the others take `field`.
45
- * - `field` — the column the reducer applies to (required for non-count ops).
46
- * - `by` — group keys. When all `where` keys in a read participate in `by`, the
47
- * reader can answer from the counter table without scanning rows.
48
- * - `where` — optional static predicate baked into the counter (only the rows
49
- * matching it ever land in the counter).
50
- */
253
+ * Declared aggregate index — the schema-level seam that lets the runtime keep
254
+ * O(1) counters/sums in step with row writes (via the trigger runner) and
255
+ * route matching reads through them.
256
+ *
257
+ * - `on` — the table whose rows feed the aggregate.
258
+ * - `op` — the reducer. `count` is field-less; the others take `field`.
259
+ * - `field` — the column the reducer applies to (required for non-count ops).
260
+ * - `by` — group keys. When all `where` keys in a read participate in `by`, the
261
+ * reader can answer from the counter table without scanning rows.
262
+ * - `where` — optional static predicate baked into the counter (only the rows
263
+ * matching it ever land in the counter).
264
+ */
51
265
  interface AggregateIndexDefinition {
52
266
  by?: ReadonlyArray<string>;
53
267
  field?: string;
@@ -57,32 +271,32 @@ interface AggregateIndexDefinition {
57
271
  where?: Record<string, unknown>;
58
272
  }
59
273
  /**
60
- * One ordering key on a `rankIndex.sortBy`: which column to sort by, and the
61
- * direction. The runtime breaks ties on the row's `_id` ASC so the order is
62
- * total and `rank()` always returns a deterministic 1-based position.
63
- */
274
+ * One ordering key on a `rankIndex.sortBy`: which column to sort by, and the
275
+ * direction. The runtime breaks ties on the row's `_id` ASC so the order is
276
+ * total and `rank()` always returns a deterministic 1-based position.
277
+ */
64
278
  interface RankSortKey {
65
279
  direction: "asc" | "desc";
66
280
  field: string;
67
281
  }
68
282
  /**
69
- * Declared rank index — a sorted companion table per `(partition tuple, sortBy)`
70
- * maintained by triggers, so:
71
- *
72
- * - `rank(row)` returns the row's 1-based position within its partition under
73
- * the declared `sortBy` order, plus the partition's total row count, in
74
- * O(log n) lookups against the SQLite btree on the companion table.
75
- * - `rankPage({ where, take, from })` walks the same companion table to return
76
- * rows in the declared order — a sorted-pagination accelerator.
77
- *
78
- * Fields mirror `AggregateIndexDefinition`:
79
- *
80
- * - `on` — the source table whose rows feed the rank.
81
- * - `sortBy` — ordered keys driving the rank. Required.
82
- * - `partitionBy` — columns that scope each rank context (e.g. `["channelId"]`
83
- * to rank within a channel). Omitted ⇒ one global rank across the table.
84
- * - `where` — static predicate baked into the index; only matching rows enter.
85
- */
283
+ * Declared rank index — a sorted companion table per `(partition tuple, sortBy)`
284
+ * maintained by triggers, so:
285
+ *
286
+ * - `rank(row)` returns the row's 1-based position within its partition under
287
+ * the declared `sortBy` order, plus the partition's total row count, in
288
+ * O(log n) lookups against the SQLite btree on the companion table.
289
+ * - `rankPage({ where, take, from })` walks the same companion table to return
290
+ * rows in the declared order — a sorted-pagination accelerator.
291
+ *
292
+ * Fields mirror `AggregateIndexDefinition`:
293
+ *
294
+ * - `on` — the source table whose rows feed the rank.
295
+ * - `sortBy` — ordered keys driving the rank. Required.
296
+ * - `partitionBy` — columns that scope each rank context (e.g. `["channelId"]`
297
+ * to rank within a channel). Omitted ⇒ one global rank across the table.
298
+ * - `where` — static predicate baked into the index; only matching rows enter.
299
+ */
86
300
  interface RankIndexDefinition {
87
301
  name: string;
88
302
  on: string;
@@ -93,16 +307,16 @@ interface RankIndexDefinition {
93
307
  /** FK behavior when a referenced parent row is deleted (mirrors SQL `ON DELETE`). */
94
308
  type OnDeleteAction = "cascade" | "restrict" | "set null";
95
309
  /**
96
- * A declared relation between two tables, recorded by `.relations((r) => …)`.
97
- *
98
- * - `one` (many-to-one): the FK column `field` lives on **this** table and
99
- * points at `table`.`references` (default `_id`). Loads a single doc.
100
- * - `many` (one-to-many): the FK column `field` lives on the **target** table
101
- * and points back at this table's `references` (default `_id`). Loads an array.
102
- *
103
- * `onDelete` is meaningful only on `one`: it is the action applied to the
104
- * holder rows when the referenced parent row is deleted.
105
- */
310
+ * A declared relation between two tables, recorded by `.relations((r) => …)`.
311
+ *
312
+ * - `one` (many-to-one): the FK column `field` lives on **this** table and
313
+ * points at `table`.`references` (default `_id`). Loads a single doc.
314
+ * - `many` (one-to-many): the FK column `field` lives on the **target** table
315
+ * and points back at this table's `references` (default `_id`). Loads an array.
316
+ *
317
+ * `onDelete` is meaningful only on `one`: it is the action applied to the
318
+ * holder rows when the referenced parent row is deleted.
319
+ */
106
320
  interface RelationDefinition {
107
321
  field: string;
108
322
  kind: "many" | "one";
@@ -113,15 +327,15 @@ interface RelationDefinition {
113
327
  /** Distance metric used by a Vectorize index. */
114
328
  type VectorMetric = "cosine" | "dot-product" | "euclidean";
115
329
  /**
116
- * Bring-your-own-embedder: a user-supplied fn turning a source string into a
117
- * numeric vector. The runtime calls it at upsert/query time so the framework
118
- * never couples to a single embedding provider.
119
- */
330
+ * Bring-your-own-embedder: a user-supplied fn turning a source string into a
331
+ * numeric vector. The runtime calls it at upsert/query time so the framework
332
+ * never couples to a single embedding provider.
333
+ */
120
334
  type VectorEmbedder = (input: string) => Promise<ReadonlyArray<number>> | ReadonlyArray<number>;
121
335
  /**
122
- * Vector index declared inline on a table via `.vectorize(field, opts)`
123
- * (DSL Shape A). The source is always a single column on the owning table.
124
- */
336
+ * Vector index declared inline on a table via `.vectorize(field, opts)`
337
+ * (DSL Shape A). The source is always a single column on the owning table.
338
+ */
125
339
  interface TableVectorIndex {
126
340
  dimensions: number;
127
341
  embed: VectorEmbedder;
@@ -132,74 +346,110 @@ interface TableVectorIndex {
132
346
  }
133
347
  interface TableDefinition<Shape extends Record<string, Validator> = Record<string, Validator>> {
134
348
  /**
135
- * Aggregate indexes declared via `.aggregateIndex(name, opts)`. The runtime
136
- * maintains a counter row per `by` group via the trigger seam, so reads
137
- * whose `where` keys all participate in the index's `by` set are answered
138
- * without scanning the underlying table.
139
- */
349
+ * Aggregate indexes declared via `.aggregateIndex(name, opts)`. The runtime
350
+ * maintains a counter row per `by` group via the trigger seam, so reads
351
+ * whose `where` keys all participate in the index's `by` set are answered
352
+ * without scanning the underlying table.
353
+ */
140
354
  aggregateIndexes: ReadonlyArray<AggregateIndexDefinition>;
355
+ /**
356
+ * Set by `.source(...)` (named `externalSource`, not `source`, so the data
357
+ * field doesn't collide with the fluent `.source()` builder method — same
358
+ * convention as `shardBy()`/`shardMode`). When present, the table is
359
+ * materialized from an external Hyperdrive-backed database by a system poll
360
+ * loop rather than user mutations. Implies `isExternallyManaged`.
361
+ */
362
+ externalSource?: ExternalSourceDefinition;
363
+ /**
364
+ * Geospatial indexes declared via `.geoIndex(name, { field })`. The runtime
365
+ * maintains a geohash companion over the named `v.geoPoint()` column so
366
+ * `withGeoIndex(name, q => q.near(point, radius) | q.within(bbox))` resolves
367
+ * a proximity/bounding-box read as a geohash-prefix range scan plus a
368
+ * Haversine refine/sort. Empty unless `.geoIndex()` was called.
369
+ */
370
+ geoIndexes: ReadonlyArray<GeoIndexDefinition>;
141
371
  indexes: ReadonlyArray<IndexDefinition>;
142
372
  /**
143
- * `true` when `.externallyManaged()` was called — the table's rows are
144
- * written outside Lunora's discoverable insert path (an adapter, a
145
- * migration, or framework middleware), e.g. `@lunora/auth`'s better-auth
146
- * tables or `@lunora/ratelimit`'s store. Advisor insert-path lints
147
- * (`table_without_insert`) skip such tables instead of flagging the absent
148
- * `ctx.db.insert(...)`.
149
- */
373
+ * `true` when `.externallyManaged()` was called — the table's rows are
374
+ * written outside Lunora's discoverable insert path (an adapter, a
375
+ * migration, or framework middleware), e.g. `@lunora/auth`'s better-auth
376
+ * tables or `@lunora/ratelimit`'s store. Advisor insert-path lints
377
+ * (`table_without_insert`) skip such tables instead of flagging the absent
378
+ * `ctx.db.insert(...)`.
379
+ */
150
380
  isExternallyManaged?: boolean;
151
381
  /**
152
- * `true` when `.public()` was called — the table opts OUT of secure-by-default
153
- * RLS. Under a schema marked `.rls("required")`, every table is protected (the
154
- * DO/D1 write path denies raw, non-RLS `ctx.db` access) UNLESS it is `isPublic`.
155
- * Has no effect when the schema does not require RLS.
156
- */
382
+ * `true` when `.public()` was called — the table opts OUT of secure-by-default
383
+ * RLS. Under a schema marked `.rls("required")`, every table is protected (the
384
+ * DO/D1 write path denies raw, non-RLS `ctx.db` access) UNLESS it is `isPublic`.
385
+ * Has no effect when the schema does not require RLS.
386
+ */
157
387
  isPublic?: boolean;
158
388
  /**
159
- * Rank indexes declared via `.rankIndex(name, opts)`. The runtime maintains
160
- * a sorted companion table per declared rank with a btree on
161
- * `(partition, sortBy)` so `rank(row)` returns the row's 1-based position
162
- * within its partition in O(log n), and `rankPage()` walks the index for
163
- * sorted pagination.
164
- */
389
+ * Set by `.ownedBy(field)` the column holding the owning user's id (named
390
+ * `ownerField`, a data field, rather than colliding with the fluent
391
+ * `.ownedBy()` builder method same convention as `shardBy()`/`shardMode`).
392
+ *
393
+ * A shape over this table with `owner: true` derives its predicate from this
394
+ * field, so "only the owner may replicate these rows" is declared once on the
395
+ * table instead of being restated in every shape's `where`. Absent ⇒ the table
396
+ * has no single owning column and a shape must spell its predicate out.
397
+ */
398
+ ownerField?: string;
399
+ /**
400
+ * Rank indexes declared via `.rankIndex(name, opts)`. The runtime maintains
401
+ * a sorted companion table per declared rank with a btree on
402
+ * `(partition, sortBy)` so `rank(row)` returns the row's 1-based position
403
+ * within its partition in O(log n), and `rankPage()` walks the index for
404
+ * sorted pagination.
405
+ */
165
406
  rankIndexes: ReadonlyArray<RankIndexDefinition>;
166
407
  /**
167
- * Declared relations keyed by accessor name; empty unless `.relations()`
168
- * was called. Named `relationMap` (not `relations`) so the fluent
169
- * `.relations((r) => …)` builder method doesn't collide with this field.
170
- */
408
+ * Declared relations keyed by accessor name; empty unless `.relations()`
409
+ * was called. Named `relationMap` (not `relations`) so the fluent
410
+ * `.relations((r) => …)` builder method doesn't collide with this field.
411
+ */
171
412
  relationMap: Record<string, RelationDefinition>;
172
413
  searchIndexes: ReadonlyArray<SearchIndexDefinition>;
173
414
  shape: Shape;
174
415
  shardMode: ShardMode;
175
416
  /**
176
- * Set by `.softDelete()` (named `softDeleteMode`, not `softDelete`, so the
177
- * data field doesn't collide with the fluent `.softDelete()` builder method —
178
- * same convention as `shardBy()`/`shardMode`). When present, the table carries
179
- * a nullable timestamp column (`field`, default `deletedAt`):
180
- * `ctx.db.&lt;table>.delete()` flips it instead of physically removing the row,
181
- * and **list reads** (`findMany`/`findFirst`/`query()`/`count`/`aggregate`/
182
- * relation loads) hide rows whose `field` is set unless
183
- * `includeDeleted: true` is passed. By-id `get`/`patch`/`replace` and
184
- * `restore` are unaffected. Absent ⇒ deletes are physical, as before.
185
- */
417
+ * Set by `.softDelete()` (named `softDeleteMode`, not `softDelete`, so the
418
+ * data field doesn't collide with the fluent `.softDelete()` builder method —
419
+ * same convention as `shardBy()`/`shardMode`). When present, the table carries
420
+ * a nullable timestamp column (`field`, default `deletedAt`):
421
+ * `ctx.db.<table>.delete()` flips it instead of physically removing the row,
422
+ * and **list reads** (`findMany`/`findFirst`/`query()`/`count`/`aggregate`/
423
+ * relation loads) hide rows whose `field` is set unless
424
+ * `includeDeleted: true` is passed. By-id `get`/`patch`/`replace` and
425
+ * `restore` are unaffected. Absent ⇒ deletes are physical, as before.
426
+ */
186
427
  softDeleteMode?: {
187
428
  field: string;
188
429
  };
189
430
  /**
190
- * Declared lifecycle triggers keyed by accessor name; empty unless
191
- * `.triggers()` was called. Named `triggerMap` (not `triggers`) so the
192
- * fluent `.triggers((t) => …)` builder method doesn't collide with this
193
- * field — same reasoning as {@link TableDefinition.relationMap}.
194
- */
431
+ * Declared lifecycle triggers keyed by accessor name; empty unless
432
+ * `.triggers()` was called. Named `triggerMap` (not `triggers`) so the
433
+ * fluent `.triggers((t) => …)` builder method doesn't collide with this
434
+ * field — same reasoning as {@link TableDefinition.relationMap}.
435
+ */
195
436
  triggerMap: Record<string, TriggerDefinition>;
437
+ /**
438
+ * Set by `.ttl(field, { after })` — the declarative auto-expiry policy. A DO
439
+ * alarm-driven sweep deletes rows whose expiry timestamp has passed (or
440
+ * soft-deletes them when the table also `.softDelete()`s). Named `ttlPolicy`
441
+ * (a data field) rather than colliding with the fluent `.ttl()` builder
442
+ * method — same convention as `shardBy()`/`shardMode`. Absent ⇒ rows never
443
+ * auto-expire.
444
+ */
445
+ ttlPolicy?: TtlDefinition;
196
446
  vectorIndexes: ReadonlyArray<TableVectorIndex>;
197
447
  }
198
448
  /**
199
- * Standalone vector index declared via `defineVectorIndex(...)` (DSL Shape B).
200
- * Unlike {@link TableVectorIndex}, the source is a `select` function so it can
201
- * derive the embedded text from any computation (e.g. `title + body`).
202
- */
449
+ * Standalone vector index declared via `defineVectorIndex(...)` (DSL Shape B).
450
+ * Unlike {@link TableVectorIndex}, the source is a `select` function so it can
451
+ * derive the embedded text from any computation (e.g. `title + body`).
452
+ */
203
453
  interface VectorIndexDefinition {
204
454
  readonly dimensions: number;
205
455
  readonly embed: VectorEmbedder;
@@ -211,50 +461,164 @@ interface VectorIndexDefinition {
211
461
  }
212
462
  interface Schema<T extends Record<string, TableDefinition> = Record<string, TableDefinition>> {
213
463
  /**
214
- * Secure-by-default RLS mode declared via `.rls("required")`. When
215
- * `"required"`, every table is protected: the DO/D1 write path denies raw
216
- * (non-RLS-wrapped) `ctx.db` access at runtime, so a procedure that forgets
217
- * `.use(rls(...))` fails closed instead of silently exposing the table. A
218
- * table opts out with `.public()` (→ {@link TableDefinition.isPublic}).
219
- * Absent ⇒ legacy opt-in behavior (RLS only where a policy is applied).
220
- */
464
+ * Secure-by-default RLS mode declared via `.rls("required")`. When
465
+ * `"required"`, every table is protected: the DO/D1 write path denies raw
466
+ * (non-RLS-wrapped) `ctx.db` access at runtime, so a procedure that forgets
467
+ * `.use(rls(...))` fails closed instead of silently exposing the table. A
468
+ * table opts out with `.public()` (→ {@link TableDefinition.isPublic}).
469
+ * Absent ⇒ legacy opt-in behavior (RLS only where a policy is applied).
470
+ */
221
471
  readonly rlsMode?: "required";
222
472
  readonly tables: T;
223
473
  readonly vectorIndexes: Record<string, VectorIndexDefinition>;
224
474
  }
225
475
  type FunctionKind = "action" | "mutation" | "query" | "stream";
226
476
  /**
227
- * Call surface a function is exposed on. `public` functions are reachable from
228
- * clients via the generated `api`; `internal` functions are reachable only
229
- * server-to-server (`ctx.runQuery`/`runMutation`/`runAction`) and are rejected
230
- * by the DO's external RPC path. Absence is treated as `public` for
231
- * back-compat with functions registered before visibility existed.
232
- */
477
+ * Call surface a function is exposed on. `public` functions are reachable from
478
+ * clients via the generated `api`; `internal` functions are reachable only
479
+ * server-to-server (`ctx.runQuery`/`runMutation`/`runAction`) and are rejected
480
+ * by the DO's external RPC path. Absence is treated as `public` for
481
+ * back-compat with functions registered before visibility existed.
482
+ */
233
483
  type FunctionVisibility = "internal" | "public";
484
+ /**
485
+ * x402 payment tag attached by the `.x402({ price })` builder modifier. Marks a
486
+ * public procedure as paid: the origin worker answers an unpaid client RPC with
487
+ * HTTP 402, verifies + settles the payment, and only then dispatches to the
488
+ * shard. The runtime reads only `price` from here — the network, recipient, and
489
+ * facilitator live in the worker-level x402 charge config, so `@lunora/runtime`
490
+ * never has to import `@lunora/x402` (and its viem/solana deps).
491
+ */
492
+ interface X402ProcedureConfig {
493
+ /**
494
+ * USD-denominated price: a number of dollars (`0.01`) or a decimal string
495
+ * (`"0.01"`, or the `"$0.01"` shorthand). Resolved to the network
496
+ * stablecoin's base units (USDC has 6 decimals) at challenge time.
497
+ */
498
+ readonly price: number | string;
499
+ }
500
+ /**
501
+ * HTTP caching for an exposed REST endpoint, declared as
502
+ * `.expose({ rest: true, cache: { scope: "public", maxAge: 60 } })`. The runtime
503
+ * turns this into `Cache-Control` / `Cache-Tag` / `Vary` response headers, and a
504
+ * `cache.tag` is purgeable through `ctx.cache.purge({ tags: [...] })`.
505
+ *
506
+ * This is NOT equivalent to `httpRoute(...).cacheControl()`, which writes whatever
507
+ * value the author passes with no credential downgrade. That is the unguarded
508
+ * escape hatch; this is the guarded surface.
509
+ *
510
+ * Caching a procedure whose result depends on the caller is how a REST cache
511
+ * turns into a data leak, so `scope` is enforced at runtime rather than trusted:
512
+ * a request that carries credentials (an `Authorization` header or any `Cookie`)
513
+ * is ALWAYS answered `private`, even under `scope: "public"` — see
514
+ * {@link https://developer.mozilla.org/docs/Web/HTTP/Headers/Cache-Control MDN}
515
+ * for what `private` forbids. So a per-user response can never be stored in a
516
+ * shared/edge cache, and the worst a mis-declared `scope` can cost is a missed
517
+ * cache hit. `scope: "public"` additionally emits `Vary: authorization, cookie`
518
+ * so an intermediary can't hand a stored anonymous variant to a signed-in caller.
519
+ *
520
+ * Only ever applied to a cacheable exchange: a `GET` (so `query` procedures — a
521
+ * `mutation` / `action` is `POST`-only) that produced a 2xx.
522
+ */
523
+ type RestCacheConfig = RestCachePolicy;
524
+ /**
525
+ * Opt-in public-surface tag attached by the `.expose({ rest: true })` builder
526
+ * modifier (plan 167). Marks a procedure as deliberately published over the
527
+ * public REST surface: the runtime mints a `/_lunora/rest/<namespace>/<fn>` route
528
+ * that dispatches THROUGH the procedure (so `ctx.auth` / RLS / validators are
529
+ * enforced), and the generated OpenAPI describes it. Everything is default-closed
530
+ * — a procedure without this tag is unreachable over REST.
531
+ */
532
+ interface ExposeConfig {
533
+ /** Opt this endpoint's responses into HTTP caching. Omit to leave them uncached. */
534
+ readonly cache?: RestCacheConfig;
535
+ /** Publish this procedure over the public REST surface. */
536
+ readonly rest?: boolean;
537
+ }
234
538
  interface RegisteredFunction<A extends ArgsValidator, R, Kind extends FunctionKind> {
235
539
  readonly args: A;
540
+ /**
541
+ * Set by the `.expose({ rest: true })` builder modifier. Marks the procedure
542
+ * as published on the public REST surface (plan 167). Absent on procedures that
543
+ * are reachable only via typed RPC (the default).
544
+ */
545
+ readonly expose?: ExposeConfig;
236
546
  readonly handler: (context: unknown, args: InferArgs<A>) => Promise<R> | R;
237
547
  readonly kind: Kind;
238
548
  /**
239
- * Set on connection-lifecycle hooks (`onConnect` / `onDisconnect`).
240
- * Marks the function for the generated `LUNORA_LIFECYCLE_HOOKS` manifest so the
241
- * DO dispatches it on socket connect/disconnect rather than via a client RPC.
242
- * Absent on ordinary registrations.
243
- */
549
+ * Set on connection-lifecycle hooks (`onConnect` / `onDisconnect`).
550
+ * Marks the function for the generated `LUNORA_LIFECYCLE_HOOKS` manifest so the
551
+ * DO dispatches it on socket connect/disconnect rather than via a client RPC.
552
+ * Absent on ordinary registrations.
553
+ */
244
554
  readonly lifecycle?: LifecycleEventKind;
555
+ /**
556
+ * Static per-procedure metadata declared with `.meta(...)`. Present so
557
+ * middleware (via `ctx.meta`) and tooling can read the same object; absent
558
+ * when the chain never called `.meta()`.
559
+ */
560
+ readonly meta?: Record<string, unknown>;
245
561
  readonly visibility?: FunctionVisibility;
562
+ /**
563
+ * Set by the `.x402({ price })` builder modifier. Marks the procedure as paid
564
+ * so the origin worker gates it behind an x402 402-challenge before dispatch.
565
+ * Absent on unpaid functions.
566
+ */
567
+ readonly x402?: X402ProcedureConfig;
246
568
  }
247
569
  type RegisteredQuery<A extends ArgsValidator, R> = RegisteredFunction<A, R, "query">;
248
570
  type RegisteredMutation<A extends ArgsValidator, R> = RegisteredFunction<A, R, "mutation">;
249
571
  type RegisteredAction<A extends ArgsValidator, R> = RegisteredFunction<A, R, "action">;
572
+ /**
573
+ * Structural mirror of `@lunora/client`'s `FunctionReference` — the handle the
574
+ * generated `api` / `internal` objects hand you, carrying `<file>:<function>`
575
+ * in `__lunoraRef`. Redeclared here so `@lunora/server` needs no dependency on
576
+ * the client package, exactly as {@link Scheduler} avoids one on
577
+ * `@lunora/scheduler`. `RegisteredFunction` has no `__lunoraRef`, so the two
578
+ * shapes never overlap.
579
+ */
580
+ interface FunctionHandle<Kind extends "action" | "mutation" | "query" | "stream", Args, Return> {
581
+ /** Phantom marker carrying the type parameters; never present at runtime. */
582
+ readonly __lunoraPhantom?: {
583
+ args: Args;
584
+ kind: Kind;
585
+ returns: Return;
586
+ };
587
+ readonly __lunoraRef: string;
588
+ }
589
+ /**
590
+ * `ctx.runQuery` — overloaded, see the note above.
591
+ *
592
+ * A single generic signature over the reference would be nicer (TS will not
593
+ * contextually type a parameter against a multi-signature type, so a hand-built
594
+ * ctx object must annotate its `(reference, args)` explicitly — see
595
+ * `@lunora/testing`'s harness). It does not work: a concrete
596
+ * `RegisteredQuery<{…}, number>` is not assignable to a
597
+ * `RegisteredFunction<ArgsValidator, …>` constraint, because `handler`'s args
598
+ * are in a contravariant position. Two inference sites it is.
599
+ */
600
+ interface RunQuery {
601
+ <A extends ArgsValidator, R>(reference: RegisteredQuery<A, R>, args: InferArgs<A>): Promise<R>;
602
+ <Args, R>(reference: FunctionHandle<"query", Args, R>, args: Args): Promise<R>;
603
+ }
604
+ /** `ctx.runMutation` — overloaded for the same reason as {@link RunQuery}. */
605
+ interface RunMutation {
606
+ <A extends ArgsValidator, R>(reference: RegisteredMutation<A, R>, args: InferArgs<A>): Promise<R>;
607
+ <Args, R>(reference: FunctionHandle<"mutation", Args, R>, args: Args): Promise<R>;
608
+ }
609
+ /** `ctx.runAction` — overloaded for the same reason as {@link RunQuery}. */
610
+ interface RunAction {
611
+ <A extends ArgsValidator, R>(reference: RegisteredAction<A, R>, args: InferArgs<A>): Promise<R>;
612
+ <Args, R>(reference: FunctionHandle<"action", Args, R>, args: Args): Promise<R>;
613
+ }
250
614
  /** Which side of the WebSocket lifecycle a hook fires on. */
251
615
  type LifecycleEventKind = "connect" | "disconnect";
252
616
  /**
253
- * The event a connection-lifecycle hook receives as its second argument. It is
254
- * the JSON-serializable payload the DO forwards on socket connect/disconnect;
255
- * the verified caller identity is also reflected on `ctx.auth` (the hook runs
256
- * under the connecting user via `resolveIdentity`).
257
- */
617
+ * The event a connection-lifecycle hook receives as its second argument. It is
618
+ * the JSON-serializable payload the DO forwards on socket connect/disconnect;
619
+ * the verified caller identity is also reflected on `ctx.auth` (the hook runs
620
+ * under the connecting user via `resolveIdentity`).
621
+ */
258
622
  interface LifecycleEvent {
259
623
  /** Stable per-socket id, minted at upgrade and replayed verbatim on disconnect. */
260
624
  readonly connectionId: string;
@@ -266,33 +630,69 @@ interface LifecycleEvent {
266
630
  readonly userId: string | null;
267
631
  }
268
632
  /**
269
- * A registered connection-lifecycle hook — an internal mutation tagged with the
270
- * lifecycle side it fires on. Produced by `onConnect` / `onDisconnect`.
271
- */
633
+ * A registered connection-lifecycle hook — an internal mutation tagged with the
634
+ * lifecycle side it fires on. Produced by `onConnect` / `onDisconnect`.
635
+ */
272
636
  type RegisteredLifecycleHook = RegisteredFunction<Record<string, never>, void, "mutation"> & {
273
637
  readonly lifecycle: LifecycleEventKind;
274
638
  };
275
639
  /**
276
- * A streaming query registration. Unlike {@link RegisteredFunction} the handler
277
- * returns an `AsyncIterable&lt;R>` synchronously (it does NOT `Promise&lt;R>`); the
278
- * runtime drives it frame by frame and forwards each chunk to the caller. The
279
- * third `signal` argument is wired to the caller's cancel signal so the handler
280
- * can stop early — break out of the loop or check `signal.aborted` between
281
- * yields.
282
- */
640
+ * A streaming query registration. Unlike {@link RegisteredFunction} the handler
641
+ * returns an `AsyncIterable<R>` synchronously (it does NOT `Promise<R>`); the
642
+ * runtime drives it frame by frame and forwards each chunk to the caller. The
643
+ * third `signal` argument is wired to the caller's cancel signal so the handler
644
+ * can stop early — break out of the loop or check `signal.aborted` between
645
+ * yields.
646
+ */
283
647
  interface RegisteredStream<A extends ArgsValidator, R> {
284
648
  readonly args: A;
649
+ /**
650
+ * Present when the stream was declared `durable`. Chunks are persisted per
651
+ * run before they reach a socket, the producer outlives the socket that
652
+ * opened it, and a reconnecting (or second) client attaches to the same run
653
+ * and replays what it missed. See {@link DurableStreamOptions}.
654
+ */
655
+ readonly durable?: DurableStreamOptions;
285
656
  readonly handler: (context: unknown, args: InferArgs<A>, signal: AbortSignal) => AsyncIterable<R>;
286
657
  readonly kind: "stream";
287
658
  readonly visibility?: FunctionVisibility;
288
659
  }
660
+ /**
661
+ * Durability settings for a `.stream()` procedure.
662
+ *
663
+ * A run is identified by the socket's verified identity plus the function path
664
+ * and arguments, so two clients of the SAME user calling the same stream with
665
+ * the same arguments observe one producer and one transcript. That identity is
666
+ * the feature: it is what makes a reload resume rather than re-generate, and
667
+ * what lets a second tab watch the same answer. A different identity always gets
668
+ * its own run.
669
+ *
670
+ * Sharing applies to a run still in flight. Once a run finishes, a later caller
671
+ * asking the same question gets a fresh one — a transcript is the record of one
672
+ * execution, not a cached response.
673
+ *
674
+ * **What an attach does not do:** it replays a transcript, it does not re-run the
675
+ * handler — so the procedure's middleware chain (`.use(rls(...))`, rate limits,
676
+ * any custom guard) runs once, for the caller that started the run. That is why
677
+ * the run key is identity-scoped: a guard that has already passed for one user
678
+ * must never be skipped for another.
679
+ */
680
+ interface DurableStreamOptions {
681
+ /**
682
+ * How long a finished transcript is kept before it is trimmed, in
683
+ * milliseconds. Defaults to 24 hours — long enough to survive a reload and
684
+ * a commute, short enough that a chatty shard doesn't accumulate
685
+ * transcripts forever.
686
+ */
687
+ readonly ttlMs?: number;
688
+ }
289
689
  /** The system tables `ctx.db.system` can read. */
290
690
  type SystemTableName = "_scheduled_functions" | "_storage";
291
691
  /**
292
- * A pending scheduled invocation as surfaced by the `_scheduled_functions`
293
- * system table. Mirrors {@link ScheduledJob} (the `ctx.scheduler` view); the
294
- * separate name keeps the system-table read surface self-describing.
295
- */
692
+ * A pending scheduled invocation as surfaced by the `_scheduled_functions`
693
+ * system table. Mirrors {@link ScheduledJob} (the `ctx.scheduler` view); the
694
+ * separate name keeps the system-table read surface self-describing.
695
+ */
296
696
  interface ScheduledFunctionDoc {
297
697
  /** Function arguments the job will be dispatched with. */
298
698
  args: Record<string, unknown>;
@@ -322,55 +722,79 @@ interface SystemQuery<T extends SystemTableName> {
322
722
  collect: () => Promise<SystemDoc<T>[]>;
323
723
  }
324
724
  /**
325
- * Read-only reader over Lunora's system tables (`_scheduled_functions`,
326
- * `_storage`), exposed as `ctx.db.system`. Mirrors Convex's `ctx.db.system`.
327
- *
328
- * **Best-effort and eventually consistent.** Unlike `ctx.db.&lt;table>` — which
329
- * reads the shard's transactional SQLite snapshot — the data behind these tables
330
- * lives OUTSIDE the shard (scheduled functions in the `SchedulerDO`, storage
331
- * objects in R2). Every `collect()` / `get()` reaches across to that source.
332
- *
333
- * It is **not part of the mutation transaction snapshot** (no OCC guard, no
334
- * subscription dependency recorded — reading it inside a mutation does not pin
335
- * it), and results are **eventually consistent** with writes a mutation just
336
- * made (e.g. a freshly scheduled job may not appear yet).
337
- *
338
- * Read-only by design: mutate scheduled jobs via `ctx.scheduler`, storage
339
- * objects via `ctx.storage`.
340
- */
725
+ * Read-only reader over Lunora's system tables (`_scheduled_functions`,
726
+ * `_storage`), exposed as `ctx.db.system`. Mirrors Convex's `ctx.db.system`.
727
+ *
728
+ * **Best-effort and eventually consistent.** Unlike `ctx.db.<table>` — which
729
+ * reads the shard's transactional SQLite snapshot — the data behind these tables
730
+ * lives OUTSIDE the shard (scheduled functions in the `SchedulerDO`, storage
731
+ * objects in R2). Every `collect()` / `get()` reaches across to that source.
732
+ *
733
+ * It is **not part of the mutation transaction snapshot** (no OCC guard, no
734
+ * subscription dependency recorded — reading it inside a mutation does not pin
735
+ * it), and results are **eventually consistent** with writes a mutation just
736
+ * made (e.g. a freshly scheduled job may not appear yet).
737
+ *
738
+ * Read-only by design: mutate scheduled jobs via `ctx.scheduler`, storage
739
+ * objects via `ctx.storage`.
740
+ */
341
741
  interface SystemDatabaseReader {
342
742
  /**
343
- * Resolve a single system-table row by id, or `null` when absent.
344
- * (`_scheduled_functions` → job id; `_storage` → object key.)
345
- */
743
+ * Resolve a single system-table row by id, or `null` when absent.
744
+ * (`_scheduled_functions` → job id; `_storage` → object key.)
745
+ */
346
746
  get: <T extends SystemTableName>(table: T, id: string) => Promise<SystemDoc<T> | null>;
347
747
  /**
348
- * Begin a read over a system table; call `.collect()` to resolve the full
349
- * list. No filtering, indexing, or pagination — the backing source is remote
350
- * and the surface stays deliberately minimal.
351
- */
748
+ * Begin a read over a system table; call `.collect()` to resolve the full
749
+ * list. No filtering, indexing, or pagination — the backing source is remote
750
+ * and the surface stays deliberately minimal.
751
+ */
352
752
  query: <T extends SystemTableName>(table: T) => SystemQuery<T>;
353
753
  }
354
754
  /**
355
- * Read-only handle bound to a table. Used by `query`/`mutation`/`action`. The
356
- * actual SQL implementation lives in `@lunora/do`; these are signatures only.
357
- */
755
+ * Read-only handle bound to a table. Used by `query`/`mutation`/`action`. The
756
+ * actual SQL implementation lives in `@lunora/do`; these are signatures only.
757
+ */
358
758
  interface DatabaseReader {
759
+ /**
760
+ * The throwing sibling of {@link DatabaseReader.normalizeId}: brand `id` as an
761
+ * {@link Id} for `tableName`, or throw `BAD_REQUEST` when it is not structurally
762
+ * an id. Pure — it never reads the database, so a valid id for a row that
763
+ * doesn't exist still returns.
764
+ *
765
+ * This is the **parse boundary** for an id that arrived as a plain `string`: a
766
+ * wire payload, a mutator's args, a change plan computed on the client. The
767
+ * alternative is `value as Id<"table">` at every such call site — an assertion,
768
+ * not a check, and one that has to be repeated for every table a helper is
769
+ * generic over:
770
+ *
771
+ * ```ts
772
+ * for (const patch of plan.patches) {
773
+ * await ctx.db.patch(ctx.db.asId("nodes", patch.id), patch.fields);
774
+ * }
775
+ * ```
776
+ *
777
+ * Ids are opaque strings, so the check is exactly `normalizeId`'s: it rejects
778
+ * empty, whitespace-bearing, and NUL-bearing values, not "an id that isn't in
779
+ * this table". Use it to get the brand honestly, and `get()` to learn whether
780
+ * the row exists.
781
+ */
782
+ asId: <T extends string>(tableName: T, id: string) => Id<T>;
359
783
  get: <T extends string>(id: Id<T>) => Promise<Record<string, unknown> | null>;
360
784
  /**
361
- * Validate an untrusted `id` string against the structural shape of an id
362
- * for `tableName`, returning the branded {@link Id} when it is well-formed
363
- * and `null` otherwise. Pure structural validation — it never reads the
364
- * database, so a structurally valid id for a row that doesn't exist still
365
- * returns the branded id (mirrors Convex's `db.normalizeId`).
366
- */
785
+ * Validate an untrusted `id` string against the structural shape of an id
786
+ * for `tableName`, returning the branded {@link Id} when it is well-formed
787
+ * and `null` otherwise. Pure structural validation — it never reads the
788
+ * database, so a structurally valid id for a row that doesn't exist still
789
+ * returns the branded id (mirrors Convex's `db.normalizeId`).
790
+ */
367
791
  normalizeId: <T extends string>(tableName: T, id: string) => Id<T> | null;
368
792
  query: (tableName: string) => TableReader;
369
793
  /**
370
- * Best-effort, read-only reader over Lunora's system tables
371
- * (`_scheduled_functions`, `_storage`). Eventually consistent and **not**
372
- * part of the transaction snapshot — see {@link SystemDatabaseReader}.
373
- */
794
+ * Best-effort, read-only reader over Lunora's system tables
795
+ * (`_scheduled_functions`, `_storage`). Eventually consistent and **not**
796
+ * part of the transaction snapshot — see {@link SystemDatabaseReader}.
797
+ */
374
798
  readonly system: SystemDatabaseReader;
375
799
  }
376
800
  /** Options for {@link TableReader.paginate} — Convex-compatible page request. */
@@ -378,14 +802,14 @@ interface PaginationOptions {
378
802
  /** Opaque cursor from the prior page's `continueCursor`; `null`/omitted starts at the first page. */
379
803
  cursor?: null | string;
380
804
  /**
381
- * Optional inclusive upper bound for reactive pagination. When supplied the
382
- * page covers the fixed half-open range `(cursor, endCursor]` (ignoring
383
- * `numItems`): every row strictly after `cursor` up to and including the
384
- * boundary row `endCursor` encodes. The page's `isDone` is `true` and its
385
- * `continueCursor` echoes `endCursor`, so the next page keeps starting where
386
- * this one ends even as rows are inserted/deleted inside the range. Omit (or
387
- * pass `null`) for the legacy "first `numItems` after `cursor`" behaviour.
388
- */
805
+ * Optional inclusive upper bound for reactive pagination. When supplied the
806
+ * page covers the fixed half-open range `(cursor, endCursor]` (ignoring
807
+ * `numItems`): every row strictly after `cursor` up to and including the
808
+ * boundary row `endCursor` encodes. The page's `isDone` is `true` and its
809
+ * `continueCursor` echoes `endCursor`, so the next page keeps starting where
810
+ * this one ends even as rows are inserted/deleted inside the range. Omit (or
811
+ * pass `null`) for the legacy "first `numItems` after `cursor`" behaviour.
812
+ */
389
813
  endCursor?: null | string;
390
814
  /** Maximum rows to return for this page. */
391
815
  numItems: number;
@@ -398,47 +822,106 @@ interface PaginationResult<T = Record<string, unknown>> {
398
822
  isDone: boolean;
399
823
  page: T[];
400
824
  /**
401
- * Reactive-pagination only: the midpoint cursor of a bounded
402
- * `(cursor, endCursor]` page, used by the client to split an over-grown page
403
- * into two adjacent ranges. Absent on legacy (open-ended) pages.
404
- */
825
+ * Reactive-pagination only: the midpoint cursor of a bounded
826
+ * `(cursor, endCursor]` page, used by the client to split an over-grown page
827
+ * into two adjacent ranges. Absent on legacy (open-ended) pages.
828
+ */
405
829
  splitCursor?: null | string;
406
830
  }
407
831
  /**
408
- * The fluent `ctx.db.query(table)` reader. Generic over the document type
409
- * `Row` so the generated `ctx.db` can bind it to `Doc&lt;table>` (the chain and
410
- * every terminal then resolve typed rows — no `as unknown as Doc&lt;...>` casts).
411
- * Defaults to the untyped `Record&lt;string, unknown>` shape for the base
412
- * (schema-agnostic) `@lunora/server` reader.
413
- */
414
- interface TableReader<Row = Record<string, unknown>> {
832
+ * The fluent `ctx.db.query(table)` reader. Generic over the document type
833
+ * `Row` so the generated `ctx.db` can bind it to `Doc<table>` (the chain and
834
+ * every terminal then resolve typed rows — no `as unknown as Doc<...>` casts),
835
+ * and over the table's declared index names so `.withIndex()` / `.withSearchIndex()`
836
+ * / `.withGeoIndex()` reject a name the table does not declare.
837
+ *
838
+ * All four default to the untyped shape for the base (schema-agnostic)
839
+ * `@lunora/server` reader, which is also the wide `(table: string) => TableReader`
840
+ * overload the generated `ctx.db` intersects in — so a caller holding a runtime
841
+ * string (e.g. `@lunora/ratelimit`'s `createDbStore`) is unaffected.
842
+ *
843
+ * The index-name parameters are what make a stale index name a compile error.
844
+ * They resolve to `never` for a table that declares none of that kind, so the
845
+ * only way to satisfy the call is to declare the index. Before this, a renamed
846
+ * or dropped index left its call sites typechecking, and the query either threw
847
+ * at runtime or silently degraded to a full table scan — the second being the
848
+ * worse outcome, since it stays green in tests and surfaces months later as a
849
+ * latency regression.
850
+ *
851
+ * **Why `with*` are method signatures and everything else is a property.**
852
+ * Narrowing a parameter makes the enclosing type contravariant in it, so as
853
+ * function properties these would make a BOUND reader
854
+ * (`TableReader<Doc, "by_x">`, what `ctx.db.query(t)` returns) unassignable to
855
+ * the unbound `TableReader<Doc>` — quietly breaking every helper factored as
856
+ * `(reader: TableReader<Doc<"users">>) => …`, which is the obvious way to share
857
+ * query logic. A method signature is bivariant in its parameters, which keeps
858
+ * that direction working while the narrow parameter still rejects an undeclared
859
+ * name at the call site. Both directions are pinned in `types.test-d.ts`.
860
+ */
861
+ interface TableReader<Row = Record<string, unknown>, Indexes extends string = string, SearchIndexes extends string = string, GeoIndexes extends string = string> {
862
+ /**
863
+ * Iterate rows lazily: `for await (const row of ctx.db.query("t").withIndex(…))`.
864
+ *
865
+ * Pages through the result set behind the scenes and yields row by row, so
866
+ * a consumer that stops early stops the reads too. `.collect()` is still the
867
+ * right terminal when you want the whole set; this exists for the cases
868
+ * where you cannot know up front how far you need to read.
869
+ *
870
+ * That is what merged/ordered index streams need. Reimplementing Convex's
871
+ * `convex-helpers/server/stream` in userland previously meant materialising
872
+ * each branch with a bounded `.take(1024)` before merging, so asking a
873
+ * merged stream for ONE row read up to 1,024 rows per branch. The k-way merge itself is application code and stays there — only
874
+ * the laziness had to come from the database layer.
875
+ *
876
+ * Iteration pages through `.paginate()`, so it follows the same order —
877
+ * which is `.collect()`'s order whenever the sort key is unique. Under a
878
+ * TIED sort key (an unindexed read whose rows share `_creationTime`) the
879
+ * two can disagree, because the tie-break is left to SQLite. Read through
880
+ * an index when order matters, exactly as you would for `.paginate()`.
881
+ */
882
+ [Symbol.asyncIterator]: () => AsyncIterator<Row>;
415
883
  collect: () => Promise<Row[]>;
416
- filter: (predicate: (document: Row) => boolean) => TableReader<Row>;
884
+ filter: (predicate: (document: Row) => boolean) => TableReader<Row, Indexes, SearchIndexes, GeoIndexes>;
417
885
  first: () => Promise<Row | null>;
418
886
  /**
419
- * Set the result order. Orders by the active `.withIndex()` (or by
420
- * `_creationTime` when none is staged), `"asc"` by default; `"desc"`
421
- * reverses it. Composes with `.withIndex()`, `.filter()`, and every
422
- * terminal (`collect`/`first`/`take`/`paginate`/`unique`). Mirrors Convex's
423
- * `.order("asc" | "desc")`.
424
- */
425
- order: (direction: "asc" | "desc") => TableReader<Row>;
887
+ * Set the result order. Orders by the active `.withIndex()` (or by
888
+ * `_creationTime` when none is staged), `"asc"` by default; `"desc"`
889
+ * reverses it. Composes with `.withIndex()`, `.filter()`, and every
890
+ * terminal (`collect`/`first`/`take`/`paginate`/`unique`). Mirrors Convex's
891
+ * `.order("asc" | "desc")`.
892
+ */
893
+ order: (direction: "asc" | "desc") => TableReader<Row, Indexes, SearchIndexes, GeoIndexes>;
426
894
  paginate: (options: PaginationOptions) => Promise<PaginationResult<Row>>;
427
895
  take: (limit: number) => Promise<Row[]>;
428
896
  /**
429
- * Return the single matching document. Returns `null` when nothing matches
430
- * and throws when more than one row matches. Mirrors Convex's `.unique()`.
431
- */
897
+ * Return the single matching document. Returns `null` when nothing matches
898
+ * and throws when more than one row matches. Mirrors Convex's `.unique()`.
899
+ */
432
900
  unique: () => Promise<Row | null>;
433
- withIndex: (indexName: string, range?: (q: IndexRangeBuilder) => IndexRangeBuilder) => TableReader<Row>;
434
901
  /**
435
- * Restrict the query to a declared `.searchIndex()`. The builder's
436
- * `.search(field, query)` runs a full-text match against the index's
437
- * searchable field; `.eq(field, value)` narrows by a declared filter
438
- * field. Results come back ordered by relevance pair with `.take(n)`
439
- * (`.paginate()` is not supported on a search query).
440
- */
441
- withSearchIndex: (indexName: string, search: (q: SearchFilterBuilder) => SearchFilterBuilder) => TableReader<Row>;
902
+ * Restrict the query to a declared `.geoIndex()`. The builder's
903
+ * `.near(point, radiusMeters)` returns rows within `radiusMeters` of `point`,
904
+ * ordered nearest-first; `.within(bbox)` returns rows inside the
905
+ * latitude/longitude bounding box. Both resolve as a geohash-prefix range
906
+ * scan over the index's companion followed by a Haversine refine. Pair with
907
+ * `.take(n)` to cap results (`.paginate()` is not supported on a geo query).
908
+ */
909
+ withGeoIndex(indexName: GeoIndexes, build: (q: GeoFilterBuilder) => GeoFilterBuilder): TableReader<Row, Indexes, SearchIndexes, GeoIndexes>;
910
+ /**
911
+ * Restrict the query to a declared `.index()`. `indexName` is constrained to
912
+ * this table's declared index names (`never` when it declares none), so a
913
+ * renamed, dropped, or mistyped index is a compile error rather than a
914
+ * runtime throw or a silent full-table scan.
915
+ */
916
+ withIndex(indexName: Indexes, range?: (q: IndexRangeBuilder) => IndexRangeBuilder): TableReader<Row, Indexes, SearchIndexes, GeoIndexes>;
917
+ /**
918
+ * Restrict the query to a declared `.searchIndex()`. The builder's
919
+ * `.search(field, query)` runs a full-text match against the index's
920
+ * searchable field; `.eq(field, value)` narrows by a declared filter
921
+ * field. Results come back ordered by relevance — pair with `.take(n)`
922
+ * (`.paginate()` is not supported on a search query).
923
+ */
924
+ withSearchIndex(indexName: SearchIndexes, search: (q: SearchFilterBuilder) => SearchFilterBuilder): TableReader<Row, Indexes, SearchIndexes, GeoIndexes>;
442
925
  }
443
926
  interface IndexRangeBuilder {
444
927
  eq: (field: string, value: unknown) => IndexRangeBuilder;
@@ -454,87 +937,201 @@ interface SearchFilterBuilder {
454
937
  /** Full-text match `query` against the index's searchable `field`. Call exactly once. */
455
938
  search: (field: string, query: string) => SearchFilterBuilder;
456
939
  }
940
+ /** A latitude/longitude point (WGS84 decimal degrees) accepted by geo queries. */
941
+ interface GeoPointInput {
942
+ lat: number;
943
+ lng: number;
944
+ }
457
945
  /**
458
- * Options shared by the batch-write methods (`insertMany`/`deleteMany`/
459
- * `patchMany`) a per-call payload cap. The default cap (500) rejects an
460
- * oversized call up front so an accidental O(n²) or a payload past the Durable
461
- * Object request limit fails loudly instead of degrading the mutation. Callers
462
- * with larger sets should chunk their own loop or raise `limit`.
463
- */
946
+ * An axis-aligned latitude/longitude bounding box: `sw` is the south-west
947
+ * (min lat, min lng) corner, `ne` the north-east (max lat, max lng) corner.
948
+ */
949
+ interface GeoBoundingBox {
950
+ ne: GeoPointInput;
951
+ sw: GeoPointInput;
952
+ }
953
+ /**
954
+ * Builder passed to {@link TableReader.withGeoIndex}. Call exactly one of
955
+ * `.near(...)` / `.within(...)` — the two are mutually exclusive proximity vs
956
+ * bounding-box modes.
957
+ */
958
+ interface GeoFilterBuilder {
959
+ /** Rows within `radiusMeters` of `point`, resolved nearest-first. Call exactly once. */
960
+ near: (point: GeoPointInput, radiusMeters: number) => GeoFilterBuilder;
961
+ /** Rows whose point falls inside the bounding `box`. Call exactly once. */
962
+ within: (box: GeoBoundingBox) => GeoFilterBuilder;
963
+ }
964
+ /**
965
+ * Options shared by the batch-write methods (`insertMany`/`deleteMany`/
966
+ * `patchMany`) — a per-call payload cap. The default cap (500) rejects an
967
+ * oversized call up front so an accidental O(n²) or a payload past the Durable
968
+ * Object request limit fails loudly instead of degrading the mutation. Callers
969
+ * with larger sets should chunk their own loop or raise `limit`.
970
+ */
464
971
  interface BatchWriteOptions {
465
972
  /** Reject the call when the batch size exceeds this value (default 500). */
466
973
  limit?: number;
467
974
  }
975
+ /** Options accepted by {@link DatabaseWriter.insertMany} and the per-table facade. */
976
+ interface InsertManyOptions extends BatchWriteOptions {
977
+ /**
978
+ * When `true`, a UNIQUE-constraint breach for a row resolves to `null`
979
+ * instead of throwing — the rest of the batch is still inserted. Skipped rows
980
+ * keep their input-order slot with `null` in the returned array. Mirrors
981
+ * better-drizzle's `createMany({ skipDuplicates: true })`.
982
+ */
983
+ skipDuplicates?: boolean;
984
+ }
468
985
  interface DatabaseWriter extends DatabaseReader {
469
986
  delete: <T extends string>(id: Id<T>) => Promise<void>;
470
987
  /**
471
- * Delete many rows by id in one call. Each id is deleted through the full
472
- * single-row pipeline (triggers + per-row RLS). The returned `deleted` is the
473
- * number of ids **requested**, not the rows actually removed — an unknown or
474
- * duplicated id is a silent no-op.
475
- *
476
- * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
477
- * BEGIN/COMMIT span, so a mid-batch failure (a later RLS denial or handler
478
- * error) rolls back the whole mutation. (In an action there is no transaction
479
- * span, so the prior deletes persist; the in-memory test harness mirrors the span.)
480
- */
988
+ * Delete EVERY row in `tableName`, chunking internally until the table is
989
+ * empty the erasure primitive.
990
+ *
991
+ * Unlike `deleteWhere(tableName, {})` there is **no batch cap**: a
992
+ * `BATCH_LIMIT_EXCEEDED` at row 501 of an account deletion is a bug, not a
993
+ * safety rail. Rows still go through the single-row delete pipeline, so
994
+ * triggers, cascades, companions, CDC, and live subscriptions stay correct.
995
+ *
996
+ * On a `.softDelete()` table the default flips the marker column; pass
997
+ * `{ hard: true }` to remove the rows physically (what GDPR erasure means).
998
+ */
999
+ deleteAll: (tableName: string, options?: {
1000
+ chunkSize?: number;
1001
+ hard?: boolean;
1002
+ }) => Promise<{
1003
+ deleted: number;
1004
+ }>;
1005
+ /**
1006
+ * Delete many rows by id in one call. Each id is deleted through the full
1007
+ * single-row pipeline (triggers + per-row RLS). The returned `deleted` is the
1008
+ * number of ids **requested**, not the rows actually removed — an unknown or
1009
+ * duplicated id is a silent no-op.
1010
+ *
1011
+ * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
1012
+ * BEGIN/COMMIT span, so a mid-batch failure (a later RLS denial or handler
1013
+ * error) rolls back the whole mutation. (In an action there is no transaction
1014
+ * span, so the prior deletes persist; the in-memory test harness mirrors the span.)
1015
+ */
481
1016
  deleteMany: <T extends string>(ids: ReadonlyArray<Id<T>>, options?: BatchWriteOptions) => Promise<{
482
1017
  deleted: number;
483
1018
  }>;
484
1019
  /**
485
- * Insert a document, returning its server id.
486
- *
487
- * Pass `options.clientId` (a UUID) to key the row yourself for an
488
- * optimistic client that needs the persisted row to match the key it
489
- * already rendered. It's validated for shape and still subject to the
490
- * primary-key uniqueness constraint; omit it and the server mints the id.
491
- */
1020
+ * Delete every row matching `where` in one call. Matching rows are resolved
1021
+ * first, then each row is deleted through the single-row delete pipeline
1022
+ * (triggers, companion sync, CDC, broadcast) so reactive subscriptions and
1023
+ * search/aggregate companions stay correct.
1024
+ *
1025
+ * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
1026
+ * BEGIN/COMMIT span, so a mid-batch failure rolls back the whole mutation.
1027
+ */
1028
+ deleteWhere: (tableName: string, where: Record<string, unknown>, options?: BatchWriteOptions) => Promise<{
1029
+ deleted: number;
1030
+ }>;
1031
+ /**
1032
+ * Insert a document, returning its server id.
1033
+ *
1034
+ * Pass `options.clientId` (a UUID) to key the row yourself — for an
1035
+ * optimistic client that needs the persisted row to match the key it
1036
+ * already rendered. It's validated for shape and still subject to the
1037
+ * primary-key uniqueness constraint; omit it and the server mints the id.
1038
+ */
492
1039
  insert: <T extends string>(tableName: T, document: Record<string, unknown>, options?: {
493
1040
  clientId?: string;
494
1041
  }) => Promise<Id<T>>;
495
1042
  /**
496
- * Insert many documents into one table in a single call, returning the
497
- * minted ids in input order. Equivalent to a per-row `insert()` loop — each
498
- * row gets defaults, validators, triggers, and a per-row RLS check — but the
499
- * caller pays one round-trip instead of N.
500
- *
501
- * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
502
- * BEGIN/COMMIT span, so a mid-batch failure (an invalid or RLS-denied row)
503
- * rolls back the whole mutation. (In an action there is no transaction span,
504
- * so the prior inserts persist; the in-memory test harness mirrors the span.)
505
- */
506
- insertMany: <T extends string>(tableName: T, documents: ReadonlyArray<Record<string, unknown>>, options?: BatchWriteOptions) => Promise<Id<T>[]>;
507
- /**
508
- * **Trusted** bulk insert: one multi-row `INSERT` that **skips per-row
509
- * `.check()` validators and before/after triggers** for throughput on data you
510
- * control (seed, migration, admin import). Defaults, ids, and every companion
511
- * (search/aggregate/rank/CDC + live subscriptions) are still applied, so reads
512
- * stay correct.
513
- *
514
- * It is **"unsafe" only in that it bypasses the validation/trigger pipeline** —
515
- * RLS is **not** bypassed: secure-by-default and the table's insert policy still
516
- * apply (the framework ships no RLS-bypassing writer). Pass `allowExplicitId` to
517
- * preserve a supplied `_id` (import). Use only for data you trust; prefer
518
- * `insertMany` for anything user-supplied.
519
- */
1043
+ * Insert many documents into one table in a single call, returning the
1044
+ * minted ids in input order. Equivalent to a per-row `insert()` loop — each
1045
+ * row gets defaults, validators, triggers, and a per-row RLS check — but the
1046
+ * caller pays one round-trip instead of N.
1047
+ *
1048
+ * Pass `{ skipDuplicates: true }` to turn UNIQUE-constraint breaches into
1049
+ * `null` results for that row instead of failing the whole batch; the rest of
1050
+ * the batch is still inserted and order is preserved.
1051
+ *
1052
+ * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
1053
+ * BEGIN/COMMIT span, so a mid-batch failure (an invalid or RLS-denied row)
1054
+ * rolls back the whole mutation. (In an action there is no transaction span,
1055
+ * so the prior inserts persist; the in-memory test harness mirrors the span.)
1056
+ */
1057
+ insertMany: {
1058
+ <T extends string>(tableName: T, documents: ReadonlyArray<Record<string, unknown>>, options: BatchWriteOptions & {
1059
+ skipDuplicates: true;
1060
+ }): Promise<(Id<T> | null)[]>;
1061
+ <T extends string>(tableName: T, documents: ReadonlyArray<Record<string, unknown>>, options?: InsertManyOptions): Promise<Id<T>[]>;
1062
+ };
1063
+ /**
1064
+ * **Trusted** bulk insert: one multi-row `INSERT` that **skips per-row
1065
+ * `.check()` validators and before/after triggers** for throughput on data you
1066
+ * control (seed, migration, admin import). Defaults, ids, and every companion
1067
+ * (search/aggregate/rank/CDC + live subscriptions) are still applied, so reads
1068
+ * stay correct.
1069
+ *
1070
+ * It is **"unsafe" only in that it bypasses the validation/trigger pipeline** —
1071
+ * RLS is **not** bypassed: secure-by-default and the table's insert policy still
1072
+ * apply (the framework ships no RLS-bypassing writer). Pass `allowExplicitId` to
1073
+ * preserve a supplied `_id` (import). Use only for data you trust; prefer
1074
+ * `insertMany` for anything user-supplied.
1075
+ */
520
1076
  insertManyUnsafe: <T extends string>(tableName: T, documents: ReadonlyArray<Record<string, unknown>>, options?: BatchWriteOptions & {
521
1077
  allowExplicitId?: boolean;
522
1078
  }) => Promise<Id<T>[]>;
523
1079
  patch: <T extends string>(id: Id<T>, patch: Record<string, unknown>) => Promise<void>;
524
1080
  /**
525
- * Patch many rows by id in one call. Each `{ id, patch }` is applied like a
526
- * single `patch()` (per-row triggers + RLS).
527
- *
528
- * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
529
- * BEGIN/COMMIT span, so a mid-batch failure rolls back the whole mutation.
530
- * (In an action there is no transaction span, so the prior patches persist;
531
- * the in-memory test harness mirrors the span.)
532
- */
1081
+ * Patch many rows by id in one call. Each `{ id, patch }` is applied like a
1082
+ * single `patch()` (per-row triggers + RLS). Returns the number of rows
1083
+ * actually patched.
1084
+ *
1085
+ * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
1086
+ * BEGIN/COMMIT span, so a mid-batch failure rolls back the whole mutation.
1087
+ * (In an action there is no transaction span, so the prior patches persist;
1088
+ * the in-memory test harness mirrors the span.)
1089
+ */
533
1090
  patchMany: <T extends string>(patches: ReadonlyArray<{
534
1091
  id: Id<T>;
535
1092
  patch: Record<string, unknown>;
536
- }>, options?: BatchWriteOptions) => Promise<void>;
1093
+ }>, options?: BatchWriteOptions) => Promise<{
1094
+ patched: number;
1095
+ }>;
1096
+ /**
1097
+ * Patch every row matching `where` with the same `patch` in one call. The
1098
+ * matching rows are resolved first, then each row is updated through the
1099
+ * single-row patch pipeline (OCC, triggers, companion sync, CDC, broadcast)
1100
+ * so reactive subscriptions and search/aggregate companions stay correct.
1101
+ *
1102
+ * **Atomic within a mutation:** the DO wraps a mutation's dispatch in a
1103
+ * BEGIN/COMMIT span, so a mid-batch failure rolls back the whole mutation.
1104
+ */
1105
+ patchWhere: (tableName: string, args: {
1106
+ patch: Record<string, unknown>;
1107
+ where: Record<string, unknown>;
1108
+ }, options?: BatchWriteOptions) => Promise<{
1109
+ patched: number;
1110
+ }>;
537
1111
  replace: <T extends string>(id: Id<T>, document: Record<string, unknown>) => Promise<void>;
1112
+ /**
1113
+ * Erase every shard-local table — the account-deletion / tenant-teardown
1114
+ * primitive. Sweeps the schema's non-`.global()` tables with
1115
+ * {@link DatabaseWriter.deleteAll}`({ hard: true })` and returns the per-table
1116
+ * counts.
1117
+ *
1118
+ * `.global()` tables are skipped by design: their rows live in D1 and are shared
1119
+ * across shards, so "wipe this shard" must not reach them. Restrict the sweep
1120
+ * with `options.tables`, or spare one with `options.exclude` (e.g. an audit log
1121
+ * that must outlive the data).
1122
+ *
1123
+ * ```ts
1124
+ * export const deleteAccount = internalMutation({ handler: async ({ ctx }) => ctx.db.wipeShard() });
1125
+ * ```
1126
+ */
1127
+ wipeShard: (options?: {
1128
+ chunkSize?: number;
1129
+ exclude?: ReadonlyArray<string>;
1130
+ tables?: ReadonlyArray<string>;
1131
+ }) => Promise<{
1132
+ deleted: number;
1133
+ tables: Record<string, number>;
1134
+ }>;
538
1135
  }
539
1136
  /** Authenticated identity surfaced into every context. */
540
1137
  interface AuthState {
@@ -542,11 +1139,11 @@ interface AuthState {
542
1139
  readonly userId: string | null;
543
1140
  }
544
1141
  /**
545
- * A pending scheduled invocation as surfaced by {@link Scheduler.list} /
546
- * {@link Scheduler.get}. A clean public mirror of `@lunora/scheduler`'s internal
547
- * `ScheduleRecord` — re-declared here so the public ctx surface carries no
548
- * dependency on the scheduler package's internal types.
549
- */
1142
+ * A pending scheduled invocation as surfaced by {@link Scheduler.list} /
1143
+ * {@link Scheduler.get}. A clean public mirror of `@lunora/scheduler`'s internal
1144
+ * `ScheduleRecord` — re-declared here so the public ctx surface carries no
1145
+ * dependency on the scheduler package's internal types.
1146
+ */
550
1147
  interface ScheduledJob {
551
1148
  args: Record<string, unknown>;
552
1149
  /** Number of dispatch attempts already made (absent until the first retry). */
@@ -560,6 +1157,21 @@ interface ScheduledJob {
560
1157
  /** Routing hint forwarded so dispatch lands on the right shard. */
561
1158
  shardKey?: string;
562
1159
  }
1160
+ /**
1161
+ * A schedulable durable-workflow reference — the generated `workflows.<name>` /
1162
+ * `agents.<name>` object, which carries its `WORKFLOW_*`/`AGENT_*` binding and
1163
+ * stable name. Structural mirror of `@lunora/scheduler`'s `WorkflowReference` so
1164
+ * `ctx.scheduler` can target a workflow/agent without a dependency on
1165
+ * `@lunora/scheduler` / `@lunora/workflow`. A scheduled workflow target starts a
1166
+ * fresh instance on fire (the args become its `params`).
1167
+ */
1168
+ interface SchedulableWorkflowReference {
1169
+ /** The `WORKFLOW_*`/`AGENT_*` binding name (present on a generated ref). */
1170
+ readonly binding?: string;
1171
+ readonly isLunoraWorkflow: true;
1172
+ /** The workflow/agent export/stable name (present on a generated ref). */
1173
+ readonly name?: string;
1174
+ }
563
1175
  interface Scheduler {
564
1176
  /** Cancel a pending job by id. `cancelled` is `false` when no such job exists. */
565
1177
  cancel: (id: string) => Promise<{
@@ -569,16 +1181,23 @@ interface Scheduler {
569
1181
  get: (id: string) => Promise<ScheduledJob | null>;
570
1182
  /** List all pending scheduled jobs. */
571
1183
  list: () => Promise<ScheduledJob[]>;
572
- runAfter: (delayMs: number, functionPath: string, args?: Record<string, unknown>) => Promise<string>;
573
- runAt: (timestampMs: number, functionPath: string, args?: Record<string, unknown>) => Promise<string>;
1184
+ /**
1185
+ * Schedule a one-shot run `delayMs` from now. `target` is a function path
1186
+ * (`"ns:fn"`) dispatched as a one-shot, or a generated `workflows.<name>` /
1187
+ * `agents.<name>` reference which starts a fresh durable instance on fire
1188
+ * (the args become its `params`).
1189
+ */
1190
+ runAfter: (delayMs: number, target: SchedulableWorkflowReference | string, args?: Record<string, unknown>) => Promise<string>;
1191
+ /** Like {@link Scheduler.runAfter} but fires at an absolute epoch-ms timestamp. */
1192
+ runAt: (timestampMs: number, target: SchedulableWorkflowReference | string, args?: Record<string, unknown>) => Promise<string>;
574
1193
  }
575
1194
  /**
576
- * A workflow instance's lifecycle status. Clean public mirror of
577
- * `@lunora/workflow`'s `WorkflowInstanceStatus` (itself a mirror of Cloudflare's
578
- * `WorkflowInstanceStatus`) — re-declared here so the ctx surface carries no
579
- * dependency on the workflow package, exactly as {@link Scheduler} avoids a
580
- * dependency on `@lunora/scheduler`.
581
- */
1195
+ * A workflow instance's lifecycle status. Clean public mirror of
1196
+ * `@lunora/workflow`'s `WorkflowInstanceStatus` (itself a mirror of Cloudflare's
1197
+ * `WorkflowInstanceStatus`) — re-declared here so the ctx surface carries no
1198
+ * dependency on the workflow package, exactly as {@link Scheduler} avoids a
1199
+ * dependency on `@lunora/scheduler`.
1200
+ */
582
1201
  type WorkflowInstanceStatus = "complete" | "errored" | "paused" | "queued" | "running" | "terminated" | "unknown" | "waiting" | "waitingForPause";
583
1202
  /** Result of {@link WorkflowInstance.status}. Mirrors `@lunora/workflow`'s `WorkflowStatusResult`. */
584
1203
  interface WorkflowStatusResult {
@@ -615,9 +1234,9 @@ interface WorkflowInstance {
615
1234
  terminate: () => Promise<void>;
616
1235
  }
617
1236
  /**
618
- * A typed handle to one declared workflow, addressable from `ctx.workflows`.
619
- * Mirrors `@lunora/workflow`'s `WorkflowHandle`.
620
- */
1237
+ * A typed handle to one declared workflow, addressable from `ctx.workflows`.
1238
+ * Mirrors `@lunora/workflow`'s `WorkflowHandle`.
1239
+ */
621
1240
  interface WorkflowHandle<Params = Record<string, unknown>> {
622
1241
  /** Start a new instance (optionally with an id + params). */
623
1242
  create: (options?: WorkflowCreateOptions<Params>) => Promise<WorkflowInstance>;
@@ -627,36 +1246,51 @@ interface WorkflowHandle<Params = Record<string, unknown>> {
627
1246
  get: (id: string) => Promise<WorkflowInstance>;
628
1247
  }
629
1248
  /**
630
- * The `ctx.workflows` surface on {@link MutationCtx} / {@link ActionCtx}. Each
631
- * workflow declared in `lunora/workflows.ts` is reachable by its export name;
632
- * codegen narrows the `get(name)` overloads to the known workflow names + their
633
- * inferred param types. Mirrors `@lunora/workflow`'s `Workflows`.
634
- */
1249
+ * The `ctx.workflows` surface on {@link MutationCtx} / {@link ActionCtx}. Each
1250
+ * workflow declared in `lunora/workflows.ts` is reachable by its export name;
1251
+ * codegen narrows the `get(name)` overloads to the known workflow names + their
1252
+ * inferred param types. Mirrors `@lunora/workflow`'s `Workflows`.
1253
+ */
635
1254
  interface Workflows {
636
1255
  /** Resolve the handle for a declared workflow by export name. */
637
1256
  get: <Params = Record<string, unknown>>(name: string) => WorkflowHandle<Params>;
638
1257
  }
639
1258
  /**
640
- * Structural projection of workers-types' `SecretsStoreSecret` binding the
641
- * per-secret `secrets_store_secrets[]` binding whose `.get()` resolves the
642
- * secret value (or throws if it does not exist). Mirrored structurally so the
643
- * runtime resolves it without a workerd type dependency.
644
- */
1259
+ * Programmatic cache purge surface exposed on {@link ActionCtx}. Actions run
1260
+ * in the Worker (not the DO), so they can reach the Worker's `ctx.cache.purge`.
1261
+ * Queries and mutations do not expose this they run inside the Durable Object.
1262
+ */
1263
+ interface CachePurge {
1264
+ /**
1265
+ * Purge cached responses matching the given tags, or everything when
1266
+ * `purgeEverything` is true. Only available in action handlers.
1267
+ */
1268
+ purge: (options: {
1269
+ purgeEverything?: boolean;
1270
+ tags?: string[];
1271
+ }) => Promise<unknown>;
1272
+ }
1273
+ /**
1274
+ * Structural projection of workers-types' `SecretsStoreSecret` binding — the
1275
+ * per-secret `secrets_store_secrets[]` binding whose `.get()` resolves the
1276
+ * secret value (or throws if it does not exist). Mirrored structurally so the
1277
+ * runtime resolves it without a workerd type dependency.
1278
+ */
645
1279
  interface SecretsStoreSecretLike {
646
1280
  get: () => Promise<string>;
647
1281
  }
648
1282
  /**
649
- * `ctx.secrets` — read account-level secrets bound via Cloudflare Secrets Store.
650
- * A core built-in (always present on every context, like `ctx.log`): a binding
651
- * named in wrangler's `secrets_store_secrets[]` is read by its binding name.
652
- *
653
- * ```ts
654
- * const apiKey = await ctx.secrets.get("STRIPE_KEY");
655
- * ```
656
- *
657
- * The lookup is async (the platform fetches and decrypts on first read);
658
- * reading an undeclared name throws a directed error naming the bound secrets.
659
- */
1283
+ * `ctx.secrets` — read account-level secrets bound via Cloudflare Secrets Store.
1284
+ * A core built-in (always present on every context, like `ctx.log`): a binding
1285
+ * named in wrangler's `secrets_store_secrets[]` is read by its binding name.
1286
+ *
1287
+ * ```ts
1288
+ * const apiKey = await ctx.secrets.get("STRIPE_KEY");
1289
+ * ```
1290
+ *
1291
+ * The lookup is async (the platform fetches and decrypts on first read);
1292
+ * reading an undeclared name throws a directed error naming the bound secrets.
1293
+ */
660
1294
  interface Secrets {
661
1295
  /** Resolve a Secrets Store secret by its wrangler binding name. */
662
1296
  get: (name: string) => Promise<string>;
@@ -666,11 +1300,11 @@ type TriggerTiming = "after" | "before";
666
1300
  /** The CRUD operation a trigger reacts to. `patch` and `replace` both map to `update`. */
667
1301
  type TriggerOp = "delete" | "insert" | "update";
668
1302
  /**
669
- * A row as observed by a trigger handler: the table's `Shape` (with the same
670
- * optionality rules as {@link InferArgs}) plus the system columns every stored
671
- * doc carries.
672
- */
673
- type TriggerRow<Shape extends Record<string, Validator>> = { [K in keyof Shape as undefined extends Infer<Shape[K]> ? K : never]?: Infer<Shape[K]> } & { [K in keyof Shape as undefined extends Infer<Shape[K]> ? never : K]: Infer<Shape[K]> } & {
1303
+ * A row as observed by a trigger handler: the table's `Shape` (with the same
1304
+ * optionality rules as {@link InferArgs}) plus the system columns every stored
1305
+ * doc carries.
1306
+ */
1307
+ type TriggerRow<Shape extends Record<string, Validator>> = { [K in keyof Shape as undefined extends Infer<Shape[K]> ? K : never]?: Infer<Shape[K]>; } & { [K in keyof Shape as undefined extends Infer<Shape[K]> ? never : K]: Infer<Shape[K]>; } & {
674
1308
  readonly _creationTime: number;
675
1309
  readonly _id: string;
676
1310
  };
@@ -682,11 +1316,11 @@ interface TriggerInsertEvent<Shape extends Record<string, Validator> = Record<st
682
1316
  readonly table: string;
683
1317
  }
684
1318
  /**
685
- * What an `update` trigger observes: the merged row plus the pre-write row.
686
- * `previous` is typed as always present (the row must exist to be updated); the
687
- * runtime supplies it best-effort and only omits it in the unreachable
688
- * row-vanished-mid-write case.
689
- */
1319
+ * What an `update` trigger observes: the merged row plus the pre-write row.
1320
+ * `previous` is typed as always present (the row must exist to be updated); the
1321
+ * runtime supplies it best-effort and only omits it in the unreachable
1322
+ * row-vanished-mid-write case.
1323
+ */
690
1324
  interface TriggerUpdateEvent<Shape extends Record<string, Validator> = Record<string, Validator>> {
691
1325
  readonly doc: TriggerRow<Shape>;
692
1326
  readonly id: string;
@@ -695,10 +1329,10 @@ interface TriggerUpdateEvent<Shape extends Record<string, Validator> = Record<st
695
1329
  readonly table: string;
696
1330
  }
697
1331
  /**
698
- * What a `delete` trigger observes: the row about to be (or just) removed.
699
- * `previous` is typed as always present; the runtime supplies it best-effort
700
- * and only omits it in the unreachable row-vanished-mid-write case.
701
- */
1332
+ * What a `delete` trigger observes: the row about to be (or just) removed.
1333
+ * `previous` is typed as always present; the runtime supplies it best-effort
1334
+ * and only omits it in the unreachable row-vanished-mid-write case.
1335
+ */
702
1336
  interface TriggerDeleteEvent<Shape extends Record<string, Validator> = Record<string, Validator>> {
703
1337
  readonly id: string;
704
1338
  readonly op: "delete";
@@ -722,10 +1356,10 @@ interface TriggerQueryArgs {
722
1356
  with?: Record<string, unknown>;
723
1357
  }
724
1358
  /**
725
- * Args accepted by {@link TriggerDatabase.aggregate} — structural mirror of
726
- * `@lunora/do`'s `AggregateOptions`, kept local so trigger handlers in
727
- * `@lunora/server` don't take a hard dep on the DO runtime.
728
- */
1359
+ * Args accepted by {@link TriggerDatabase.aggregate} — structural mirror of
1360
+ * `@lunora/do`'s `AggregateOptions`, kept local so trigger handlers in
1361
+ * `@lunora/server` don't take a hard dep on the DO runtime.
1362
+ */
729
1363
  interface TriggerAggregateOptions {
730
1364
  baseWhere?: Record<string, unknown>;
731
1365
  field?: string;
@@ -770,17 +1404,17 @@ interface TriggerRankPageOptions {
770
1404
  where?: Record<string, unknown>;
771
1405
  }
772
1406
  /**
773
- * Portable, table/id-addressed ORM writer handed to trigger handlers via
774
- * `ctx.db`. Mirrors `@lunora/do`'s runtime `DatabaseWriterLike` surface — it is
775
- * **not** the generated per-table `ctx.db.&lt;table>` facade (which can't be typed
776
- * from inside `defineTable`, where the full schema isn't known).
777
- *
778
- * `aggregate`/`groupBy`/`count`/`rank`/`rankPage` route through the same
779
- * trigger-maintained counter and rank tables the user-facing reader uses, so
780
- * a handler's `ctx.db.&lt;table>.aggregate(...)` observes the just-staged write
781
- * within the same DO transaction (the counter step happens before the trigger
782
- * fires).
783
- */
1407
+ * Portable, table/id-addressed ORM writer handed to trigger handlers via
1408
+ * `ctx.db`. Mirrors `@lunora/do`'s runtime `DatabaseWriterLike` surface — it is
1409
+ * **not** the generated per-table `ctx.db.<table>` facade (which can't be typed
1410
+ * from inside `defineTable`, where the full schema isn't known).
1411
+ *
1412
+ * `aggregate`/`groupBy`/`count`/`rank`/`rankPage` route through the same
1413
+ * trigger-maintained counter and rank tables the user-facing reader uses, so
1414
+ * a handler's `ctx.db.<table>.aggregate(...)` observes the just-staged write
1415
+ * within the same DO transaction (the counter step happens before the trigger
1416
+ * fires).
1417
+ */
784
1418
  interface TriggerDatabase {
785
1419
  aggregate: (tableName: string, options: TriggerAggregateOptions) => Promise<null | number>;
786
1420
  count: (tableName: string, where?: Record<string, unknown>) => Promise<number>;
@@ -796,10 +1430,10 @@ interface TriggerDatabase {
796
1430
  replace: (id: string, document: Record<string, unknown>) => Promise<void>;
797
1431
  }
798
1432
  /**
799
- * Handle injected into every trigger handler. `db` is the portable ORM writer;
800
- * `scheduler` enqueues async / cross-shard follow-up work (cross-shard work is
801
- * **not** transactional with the firing write).
802
- */
1433
+ * Handle injected into every trigger handler. `db` is the portable ORM writer;
1434
+ * `scheduler` enqueues async / cross-shard follow-up work (cross-shard work is
1435
+ * **not** transactional with the firing write).
1436
+ */
803
1437
  interface TriggerCtx {
804
1438
  readonly db: TriggerDatabase;
805
1439
  readonly scheduler: Scheduler;
@@ -807,20 +1441,20 @@ interface TriggerCtx {
807
1441
  /** A user-declared trigger handler. Throwing from a `before*` handler aborts the write. */
808
1442
  type TriggerHandler<Event> = (context: TriggerCtx, event: Event) => Promise<void> | void;
809
1443
  /**
810
- * A single declared trigger, as stored in {@link TableDefinition.triggerMap}.
811
- * The handler's event type is erased to the {@link TriggerEvent} union here; the
812
- * per-op {@link TriggerBuilder} methods recover the precise event type for
813
- * authors.
814
- */
1444
+ * A single declared trigger, as stored in {@link TableDefinition.triggerMap}.
1445
+ * The handler's event type is erased to the {@link TriggerEvent} union here; the
1446
+ * per-op {@link TriggerBuilder} methods recover the precise event type for
1447
+ * authors.
1448
+ */
815
1449
  interface TriggerDefinition {
816
1450
  readonly handler: TriggerHandler<TriggerEvent>;
817
1451
  readonly op: TriggerOp;
818
1452
  readonly timing: TriggerTiming;
819
1453
  }
820
1454
  /**
821
- * The `t` argument passed to `.triggers((t) => …)`. Each method binds a handler
822
- * to one `timing`+`op` pair, typing the event against the table's `Shape`.
823
- */
1455
+ * The `t` argument passed to `.triggers((t) => …)`. Each method binds a handler
1456
+ * to one `timing`+`op` pair, typing the event against the table's `Shape`.
1457
+ */
824
1458
  interface TriggerBuilder<Shape extends Record<string, Validator> = Record<string, Validator>> {
825
1459
  afterDelete: (handler: TriggerHandler<TriggerDeleteEvent<Shape>>) => TriggerDefinition;
826
1460
  afterInsert: (handler: TriggerHandler<TriggerInsertEvent<Shape>>) => TriggerDefinition;
@@ -830,11 +1464,11 @@ interface TriggerBuilder<Shape extends Record<string, Validator> = Record<string
830
1464
  beforeUpdate: (handler: TriggerHandler<TriggerUpdateEvent<Shape>>) => TriggerDefinition;
831
1465
  }
832
1466
  /**
833
- * Per-file metadata returned by {@link ReadOnlyStorage.getMetadata}. A clean
834
- * public mirror of `@lunora/storage`'s `ObjectMetadata` — re-declared here so
835
- * the ctx surface carries no dependency on the storage package's types. Matches
836
- * the columns Convex surfaces for `ctx.storage.getMetadata` / `_storage`.
837
- */
1467
+ * Per-file metadata returned by {@link ReadOnlyStorage.getMetadata}. A clean
1468
+ * public mirror of `@lunora/storage`'s `ObjectMetadata` — re-declared here so
1469
+ * the ctx surface carries no dependency on the storage package's types. Matches
1470
+ * the columns Convex surfaces for `ctx.storage.getMetadata` / `_storage`.
1471
+ */
838
1472
  interface StorageMetadata {
839
1473
  /** The object's `Content-Type`, when recorded. */
840
1474
  contentType?: string;
@@ -850,31 +1484,31 @@ interface StorageMetadata {
850
1484
  uploaded?: number;
851
1485
  }
852
1486
  /**
853
- * Read-only projection of `Storage` exposed on `QueryCtx` / `MutationCtx`.
854
- *
855
- * Queries are pure reads, and mutations run inside a transactional scope —
856
- * neither is allowed to perform side-effectful R2 writes (`upload`) or
857
- * deletes (`delete`). They can, however, **read** existing objects and
858
- * resolve signed URLs (the URL signing itself is HMAC-only — no R2 round
859
- * trip), so the read-only surface keeps `download` and `getSignedUrl`. The
860
- * full {@link Storage} surface stays on `ActionCtx`.
861
- */
1487
+ * Read-only projection of `Storage` exposed on `QueryCtx` / `MutationCtx`.
1488
+ *
1489
+ * Queries are pure reads, and mutations run inside a transactional scope —
1490
+ * neither is allowed to perform side-effectful R2 writes (`upload`) or
1491
+ * deletes (`delete`). They can, however, **read** existing objects and
1492
+ * resolve signed URLs (the URL signing itself is HMAC-only — no R2 round
1493
+ * trip), so the read-only surface keeps `download` and `getSignedUrl`. The
1494
+ * full {@link Storage} surface stays on `ActionCtx`.
1495
+ */
862
1496
  interface ReadOnlyStorage<Buckets extends string = string> {
863
1497
  /**
864
- * Select a named bucket (declared via `v.storage("name")`). The returned
865
- * accessor's operations target that bucket — `ctx.storage.bucket("avatars")
866
- * .download(key)`. The bare `ctx.storage` targets the default bucket.
867
- */
1498
+ * Select a named bucket (declared via `v.storage("name")`). The returned
1499
+ * accessor's operations target that bucket — `ctx.storage.bucket("avatars")
1500
+ * .download(key)`. The bare `ctx.storage` targets the default bucket.
1501
+ */
868
1502
  bucket: (name: Buckets) => ReadOnlyStorage<Buckets>;
869
1503
  /** The bucket this accessor's operations target (the default for the bare `ctx.storage`). */
870
1504
  readonly bucketName: string;
871
1505
  /** Fetch the body of an existing object. Returns `null` when absent. */
872
1506
  download: (key: string) => Promise<ReadableStream | null>;
873
1507
  /**
874
- * Read a file's metadata (size, content-type, sha256, upload time, custom
875
- * metadata) without fetching its body. Returns `null` when the object is
876
- * absent. Mirrors Convex's `ctx.storage.getMetadata`.
877
- */
1508
+ * Read a file's metadata (size, content-type, sha256, upload time, custom
1509
+ * metadata) without fetching its body. Returns `null` when the object is
1510
+ * absent. Mirrors Convex's `ctx.storage.getMetadata`.
1511
+ */
878
1512
  getMetadata: (key: string) => Promise<StorageMetadata | null>;
879
1513
  /** Resolve a short-lived signed URL for an existing object. */
880
1514
  getSignedUrl: (key: string, options?: {
@@ -888,20 +1522,20 @@ interface Storage<Buckets extends string = string> extends ReadOnlyStorage<Bucke
888
1522
  bucket: (name: Buckets) => Storage<Buckets>;
889
1523
  delete: (key: string) => Promise<void>;
890
1524
  /**
891
- * Mint a short-lived signed `PUT` URL a client can upload directly to,
892
- * optionally pinning the `Content-Type` the uploader must send. Mirrors
893
- * Convex's `storage.generateUploadUrl`.
894
- */
1525
+ * Mint a short-lived signed `PUT` URL a client can upload directly to,
1526
+ * optionally pinning the `Content-Type` the uploader must send. Mirrors
1527
+ * Convex's `storage.generateUploadUrl`.
1528
+ */
895
1529
  generateUploadUrl: (key: string, options?: {
896
1530
  contentType?: string;
897
1531
  expiresInSeconds?: number;
898
1532
  }) => Promise<string>;
899
1533
  /**
900
- * Upload `body` to `key` from the server, returning the stored object's key
901
- * and etag. Mirrors Convex's `storage.store`. Accepts the same guard fields
902
- * as `@lunora/storage`'s `UploadOptions` so `maxSize` /
903
- * `allowedContentTypes` enforcement isn't lost behind the Convex-style alias.
904
- */
1534
+ * Upload `body` to `key` from the server, returning the stored object's key
1535
+ * and etag. Mirrors Convex's `storage.store`. Accepts the same guard fields
1536
+ * as `@lunora/storage`'s `UploadOptions` so `maxSize` /
1537
+ * `allowedContentTypes` enforcement isn't lost behind the Convex-style alias.
1538
+ */
905
1539
  store: (key: string, body: ReadableStream | ArrayBuffer | Blob, options?: {
906
1540
  allowedContentTypes?: ReadonlyArray<string>;
907
1541
  contentType?: string;
@@ -945,160 +1579,525 @@ interface VectorRecord {
945
1579
  values: ReadonlyArray<number>;
946
1580
  }
947
1581
  /**
948
- * Read-only vector surface exposed on {@link QueryCtx}. Mirrors the read half
949
- * of `@lunora/bindings/vectors`' `LunoraVectors` so the live adapter is assignable.
950
- */
951
- interface VectorSearchReader {
952
- getByIds: (indexName: string, ids: ReadonlyArray<string>) => Promise<ReadonlyArray<VectorRecord>>;
953
- query: (indexName: string, input: VectorQueryInput) => Promise<VectorMatches>;
954
- }
955
- /**
956
- * Mutating vector surface on {@link MutationCtx} / {@link ActionCtx}. `upsert`
957
- * is queued post-commit by default; `upsertNow` forces a synchronous write.
958
- * `db.delete` on a vectorized table auto-propagates the matching `deleteByIds`.
959
- */
960
- interface VectorSearch extends VectorSearchReader {
961
- deleteByIds: (indexName: string, ids: ReadonlyArray<string>) => Promise<void>;
962
- upsert: (indexName: string, input: VectorUpsertInput) => Promise<void>;
963
- upsertNow: (indexName: string, input: VectorUpsertInput) => Promise<void>;
964
- }
965
- /**
966
- * Structured logger on every function `ctx`. Each call emits one attributed log
967
- * line tagged with the function path on the server that flows to an
968
- * `ObservabilitySink`'s `onLog` (where you route it in production) and, in
969
- * development, to the dev server terminal via the CLI / Vite plugin formatter.
970
- * Mirrors the `console` method names so it's a drop-in for `console.log` inside a
971
- * handler, but with attribution and a routable transport.
972
- *
973
- * Accepts any number of values per call, exactly like `console`; objects are
974
- * rendered into the human-readable message. The raw, un-rendered arguments are
975
- * preserved ONLY on the in-process `onLog` sink (which you opt into and control);
976
- * the rendered message — not the structured args — is what reaches the dev
977
- * terminal and the platform's Workers Logs.
978
- *
979
- * Attribution follows the dispatched function: a log emitted inside an internal
980
- * function invoked via `ctx.runQuery`/`runMutation`/`runAction` is attributed to
981
- * the outer request entrypoint, since the composed call reuses its context.
982
- */
1582
+ * Read-only vector surface exposed on {@link QueryCtx}. Mirrors the read half
1583
+ * of `@lunora/bindings/vectors`' `LunoraVectors` so the live adapter is assignable.
1584
+ */
1585
+ interface VectorSearchReader<IndexName extends string = string> {
1586
+ getByIds: (indexName: IndexName, ids: ReadonlyArray<string>) => Promise<ReadonlyArray<VectorRecord>>;
1587
+ query: (indexName: IndexName, input: VectorQueryInput) => Promise<VectorMatches>;
1588
+ }
1589
+ /**
1590
+ * Mutating vector surface on {@link MutationCtx} / {@link ActionCtx}. `upsert`
1591
+ * is queued post-commit by default; `upsertNow` forces a synchronous write.
1592
+ * `db.delete` on a vectorized table auto-propagates the matching `deleteByIds`.
1593
+ */
1594
+ interface VectorSearch<IndexName extends string = string> extends VectorSearchReader<IndexName> {
1595
+ deleteByIds: (indexName: IndexName, ids: ReadonlyArray<string>) => Promise<void>;
1596
+ upsert: (indexName: IndexName, input: VectorUpsertInput) => Promise<void>;
1597
+ upsertNow: (indexName: IndexName, input: VectorUpsertInput) => Promise<void>;
1598
+ }
1599
+ /**
1600
+ * Structured, filterable key/value fields attached to a log line the second
1601
+ * argument of a `ctx.log.<level>(message, fields)` call, or the fields bound by
1602
+ * `ctx.log.with(fields)`. They travel to an `ObservabilitySink`'s `onLog` and,
1603
+ * for a network sink, become OTLP log-record attributes a log pipeline (or the
1604
+ * Cloud log viewer) can filter and index on. Primitive values pass through;
1605
+ * objects/arrays are JSON-encoded at the sink boundary.
1606
+ */
1607
+ type LogFields = Record<string, unknown>;
1608
+ /**
1609
+ * One `ctx.log` severity method. Two call forms:
1610
+ *
1611
+ * - **Structured** `ctx.log.info("order placed", { orderId, total })`: a message string plus a `fields` object. The fields are indexed as attributes.
1612
+ * - **Console-style** — `ctx.log.info("state", value, other)`: any number of values, joined into the display message exactly like `console.log`.
1613
+ *
1614
+ * The structured form is matched when the second argument is a plain object;
1615
+ * otherwise the call is treated as console-style, so existing `console`-shaped
1616
+ * calls keep working unchanged.
1617
+ */
1618
+ interface LunoraLogMethod {
1619
+ (message: string, fields?: LogFields): void;
1620
+ (...args: unknown[]): void;
1621
+ }
1622
+ /**
1623
+ * Structured logger on every function `ctx`. Each call emits one attributed log
1624
+ * line — tagged with the function path on the server — that flows to an
1625
+ * `ObservabilitySink`'s `onLog` (where you route it in production) and, in
1626
+ * development, to the dev server terminal via the CLI / Vite plugin formatter.
1627
+ * Mirrors the `console` method names so it's a drop-in for `console.log` inside a
1628
+ * handler, but with attribution, structured fields, and a routable transport.
1629
+ *
1630
+ * Six severities spanning the OpenTelemetry ramp: `trace`, `debug`, `info` (and
1631
+ * its `log` alias), `warn`, `error`, `fatal`.
1632
+ *
1633
+ * Two ways to attach structured {@link LogFields}: pass them per call
1634
+ * (`ctx.log.info(message, fields)`) or bind them once with {@link with} for a
1635
+ * child logger that stamps every line. The rendered `message` and the structured
1636
+ * `fields` reach the dev terminal and the platform's Workers Logs; the raw,
1637
+ * un-rendered console-style arguments are preserved ONLY on the in-process
1638
+ * `onLog` sink (which you opt into and control).
1639
+ *
1640
+ * Attribution follows the dispatched function: a log emitted inside an internal
1641
+ * function invoked via `ctx.runQuery`/`runMutation`/`runAction` is attributed to
1642
+ * the outer request entrypoint, since the composed call reuses its context.
1643
+ */
983
1644
  interface LunoraLogger {
984
- readonly debug: (...args: unknown[]) => void;
985
- readonly error: (...args: unknown[]) => void;
986
- readonly info: (...args: unknown[]) => void;
987
- readonly log: (...args: unknown[]) => void;
988
- readonly warn: (...args: unknown[]) => void;
1645
+ readonly debug: LunoraLogMethod;
1646
+ readonly error: LunoraLogMethod;
1647
+ /**
1648
+ * Emit a **structured event** instead of a log line OpenTelemetry's Events
1649
+ * API, on the wire as `LogRecord.eventName` (plus an `event.name` attribute
1650
+ * for collectors predating that field).
1651
+ *
1652
+ * ```ts
1653
+ * ctx.log.event("checkout.completed", { plan: user.plan, total, currency });
1654
+ * ```
1655
+ *
1656
+ * The difference from `ctx.log.info("checkout completed", { … })` is what a
1657
+ * backend can do with it. A log line's payload is its message: prose, written
1658
+ * for a human, free to be reworded next sprint — so "how many checkouts
1659
+ * completed, by plan, this hour" degrades into a substring search over
1660
+ * English. An event's payload is its `fields` under a **stable name**, which a
1661
+ * collector can index, group, and alert on directly.
1662
+ *
1663
+ * Rule of thumb: `log.*` for narration you'd read while debugging, `event` for
1664
+ * anything you'd ever put on a dashboard. And for facts about the request as a
1665
+ * whole, prefer `ctx.span` — one wide event beats a dozen
1666
+ * events, however well named.
1667
+ */
1668
+ readonly event: (name: string, fields?: LogFields) => void;
1669
+ readonly fatal: LunoraLogMethod;
1670
+ readonly info: LunoraLogMethod;
1671
+ readonly log: LunoraLogMethod;
1672
+ readonly trace: LunoraLogMethod;
1673
+ readonly warn: LunoraLogMethod;
1674
+ /**
1675
+ * Return a child logger that stamps `fields` onto every line it emits,
1676
+ * merged under any per-call fields (per-call wins on a key clash). Chainable
1677
+ * — `ctx.log.with({ requestId }).with({ step })` accumulates both. Use it to
1678
+ * bind request-scoped context once instead of repeating it per call.
1679
+ */
1680
+ readonly with: (fields: LogFields) => LunoraLogger;
1681
+ }
1682
+ /**
1683
+ * Handle the enclosing `ctx.trace` span hands its body, so the body can attach
1684
+ * attributes only known *after* it resolves (an AI call's token usage / dollar
1685
+ * cost, a downstream status, a computed count). Declared structurally here to
1686
+ * mirror `shared/span-event.ts`'s `SpanHandle` and `@lunora/do`'s implementation;
1687
+ * a cross-package assignability guard in `@lunora/testing` fails the build if the
1688
+ * three drift apart. Start attributes are snapshotted before the body runs;
1689
+ * handle writes are merged over them at record time, post-hoc winning on a clash.
1690
+ */
1691
+ /**
1692
+ * One AI **evaluation** verdict — a scorer's `{name, score, label?}` — to attach
1693
+ * to a generation span via {@link SpanHandle.recordEvaluation}. Declared
1694
+ * structurally to mirror `shared/evaluation-attributes.ts`'s `EvaluationInput`
1695
+ * without a dependency edge.
1696
+ */
1697
+ interface SpanEvaluation {
1698
+ /** Optional categorical label (e.g. `"pass"` / `"fail"`), emitted as `.label`. */
1699
+ label?: string;
1700
+ /** Scorer name — the key's name segment; non-`[A-Za-z0-9._-]` chars become `_`. */
1701
+ name: string;
1702
+ /** Numeric score (typically `[0, 1]`), emitted as `.score`. */
1703
+ score: number;
1704
+ }
1705
+ interface SpanHandle {
1706
+ /**
1707
+ * Record a timestamped event on the enclosing span — a retry, a cache miss, a
1708
+ * state transition. Prefer this over an extra `ctx.log` line for anything that
1709
+ * only makes sense *relative to this span*: it rides the span's own export, so
1710
+ * it costs no additional record and can never be separated from its context.
1711
+ */
1712
+ addEvent: (name: string, attributes?: LogFields) => void;
1713
+ /**
1714
+ * Link this span to one in another trace — how a queue consumer points back at
1715
+ * the request that enqueued its message without collapsing every producer into
1716
+ * one giant trace.
1717
+ */
1718
+ addLink: (link: SpanLink) => void;
1719
+ /**
1720
+ * Attach an AI **evaluation** verdict to this (generation) span as the
1721
+ * `gen_ai.evaluation.<name>.score` / `.label` OpenTelemetry attributes, so a
1722
+ * scorer's grade rides the same trace as the generation it graded. Convenience
1723
+ * over {@link SpanHandle.setAttributes} that owns the key format; privacy-safe —
1724
+ * only the name, score, and optional label are emitted. Throws on an empty name
1725
+ * or a non-finite score.
1726
+ */
1727
+ recordEvaluation: (evaluation: SpanEvaluation) => void;
1728
+ /**
1729
+ * Record a **handled** exception as the OTel-conventional `exception` span
1730
+ * event (`exception.type` / `exception.message` / `exception.stacktrace`),
1731
+ * without marking the span failed.
1732
+ *
1733
+ * For an error you swallowed — a retried request, a fallback that worked. An
1734
+ * error that escapes the span body is recorded automatically and *does* set
1735
+ * the error status, so don't call this for one you're re-throwing.
1736
+ */
1737
+ recordException: (error: unknown) => void;
1738
+ /** Set one attribute on the enclosing span (merged at record time; post-hoc wins on key clash). */
1739
+ setAttribute: (key: string, value: LogFields[string]) => void;
1740
+ /** Merge attributes onto the enclosing span (post-hoc wins on key clash). */
1741
+ setAttributes: (fields: LogFields) => void;
1742
+ /**
1743
+ * The W3C ids of the span this handle refers to (32-hex trace, 16-hex span).
1744
+ *
1745
+ * On `ctx.span` these are the DISPATCH's ids — the trace the whole request
1746
+ * belongs to. Use it to echo a trace id back to a caller so a user can quote
1747
+ * it in a bug report, to build a `traceparent` for a hand-rolled outbound
1748
+ * call, or to parent a third-party library's spans onto this request.
1749
+ */
1750
+ spanContext: () => {
1751
+ spanId: string;
1752
+ traceId: string;
1753
+ };
1754
+ }
1755
+ /**
1756
+ * A causal reference to a span in another trace (OTel `Span.links`). Ids are
1757
+ * lowercase hex — 32 chars for `traceId`, 16 for `spanId`.
1758
+ */
1759
+ interface SpanLink {
1760
+ /** Attributes describing the relationship, e.g. `{ "link.kind": "enqueued_by" }`. */
1761
+ attributes?: LogFields;
1762
+ spanId: string;
1763
+ traceId: string;
1764
+ }
1765
+ /** OTel `SpanKind`. Drives a collector's service map — see {@link SpanOptions.kind}. */
1766
+ type SpanKind = "client" | "consumer" | "internal" | "producer" | "server";
1767
+ /** Options accepted by `ctx.trace(name, fn, options)` beyond a plain attribute bag. */
1768
+ interface SpanOptions {
1769
+ /** Start attributes, snapshotted before the body runs. */
1770
+ attributes?: LogFields;
1771
+ /**
1772
+ * OTel `SpanKind`, default `"internal"`. Set `"client"` for a call OUT to
1773
+ * another service and `"producer"`/`"consumer"` for queue hops: a collector
1774
+ * builds its service map from this, so leaving everything `"internal"` yields
1775
+ * a trace with no topology.
1776
+ */
1777
+ kind?: SpanKind;
1778
+ /** Links to spans in other traces, known at span start. */
1779
+ links?: SpanLink[];
1780
+ }
1781
+ /**
1782
+ * Span factory on every function `ctx`. Wraps a sub-operation so it becomes its
1783
+ * own **span** nested under the dispatch's RPC span, giving a trace real shape:
1784
+ * without it a slow request is one opaque bar, with it you see which part was
1785
+ * slow.
1786
+ *
1787
+ * ```ts
1788
+ * const charge = await ctx.trace("stripe.charge", () => stripe.charges.create(…), { orderId });
1789
+ * ```
1790
+ *
1791
+ * **Nesting is explicit.** The body receives a tracer bound to its own span;
1792
+ * calling *that* is what makes a child:
1793
+ *
1794
+ * ```ts
1795
+ * await ctx.trace("fulfil", async (trace) => {
1796
+ * // Children of "fulfil" — including under Promise.all, where an ambient
1797
+ * // "currently open span" would mis-record these as nested inside each other.
1798
+ * await Promise.all([trace("reserve.stock", …), trace("email.receipt", …)]);
1799
+ * });
1800
+ * ```
1801
+ *
1802
+ * Calling `ctx.trace` again inside a body (rather than the passed tracer) is not
1803
+ * an error — that span is simply parented to the dispatch instead of to the
1804
+ * enclosing span, which is flatter but never wrong.
1805
+ *
1806
+ * Spans share the dispatch's trace id with its `ctx.log` lines and any container
1807
+ * the handler calls (the same `traceparent` is propagated), so one trace spans
1808
+ * worker, shard, and container.
1809
+ *
1810
+ * The span is recorded when the body settles, and the body's value is returned
1811
+ * unchanged. A throw is recorded as an error span and then **re-thrown** — this
1812
+ * is instrumentation, never flow control. Recording is best-effort: a failing
1813
+ * sink can't turn a working handler into a broken one.
1814
+ *
1815
+ * **Post-hoc attributes.** The body also receives a {@link SpanHandle} as its
1816
+ * second argument. The `attributes` passed here are stamped at span start (and
1817
+ * snapshotted, so a later mutation can't rewrite them); anything the body sets
1818
+ * through the handle — `span.setAttribute(k, v)` / `span.setAttributes({…})` — is
1819
+ * merged over that snapshot when the span is recorded, so a value known only once
1820
+ * the body has resolved (an AI call's token usage / dollar cost, a computed
1821
+ * count) still lands on the span. Post-hoc wins on a key clash. The handle is a
1822
+ * trailing parameter, so every existing `(trace) => …` body keeps working
1823
+ * unchanged.
1824
+ * @param name Span name, e.g. `"stripe.charge"`. Prefer a low-cardinality name
1825
+ * and put the varying part in `attributes` — a name built from an id makes every
1826
+ * span its own group in a collector.
1827
+ * @param fn The body to time, receiving a tracer bound to this span for any
1828
+ * nested spans and the enclosing span's {@link SpanHandle} for post-hoc
1829
+ * attributes. May be sync or async; the result is awaited.
1830
+ * @param attributes Either a plain attribute bag to stamp on the span at start
1831
+ * (normalized like a log line's `fields`), or a {@link SpanOptions} object when
1832
+ * you need `kind` or `links`. It is read as options only when *every* key is one
1833
+ * of `attributes`/`kind`/`links`; `{ attributes: { kind: "premium" } }` is the
1834
+ * explicit form if your own attributes happen to be named that.
1835
+ */
1836
+ type LunoraTracer = <T>(name: string, function_: (trace: LunoraTracer, span: SpanHandle) => Promise<T> | T, attributes?: LogFields | SpanOptions) => Promise<T>;
1837
+ /**
1838
+ * `ctx.span` — a handle onto **this request's own span**, and with it the
1839
+ * wide-event API.
1840
+ *
1841
+ * ```ts
1842
+ * export const checkout = mutation({ handler: async (ctx, args) => {
1843
+ * ctx.span.setAttributes({ "user.plan": user.plan, "cart.items": cart.length });
1844
+ * const payment = await charge(cart);
1845
+ * ctx.span.setAttributes({ "payment.provider": payment.provider, "payment.total": payment.total });
1846
+ * if (payment.retried) ctx.span.addEvent("payment.retried", { attempts: payment.attempts });
1847
+ * return payment;
1848
+ * }});
1849
+ * ```
1850
+ *
1851
+ * **Why this instead of more log lines.** The usual way to make a handler
1852
+ * observable is to sprinkle `ctx.log.info` through it, which costs one record per
1853
+ * call, scatters one request's facts across a dozen rows, and forces every
1854
+ * question to be answered by correlating them back together. The wide-event
1855
+ * pattern inverts that: accumulate the facts as you learn them, and emit **one**
1856
+ * richly-attributed record per unit of work. Cost is flat — one span per request
1857
+ * no matter how much you attach — and every question ("p99 checkout latency for
1858
+ * pro-plan users with >10 items") becomes a single filter over one table instead
1859
+ * of a join across log lines.
1860
+ *
1861
+ * **This is plain OpenTelemetry, not a Lunora convention.** The attributes land
1862
+ * on the span the dispatch already emits, and are additionally exported as an
1863
+ * OTel Event record named `lunora.dispatch`, correlated by `trace_id`/`span_id`.
1864
+ * Any OTLP backend groups and aggregates them with no special configuration.
1865
+ *
1866
+ * **`span` vs `trace`.** `ctx.trace(name, fn)` creates a NEW child span to time a
1867
+ * sub-operation; `ctx.span` attaches to the one that already exists for the
1868
+ * request. Use `trace` for "how long did this part take", `span` for "what was
1869
+ * true about this request". Inside a `ctx.trace` body, the handle passed as the
1870
+ * body's second argument is that child span's equivalent of this.
1871
+ *
1872
+ * Attributes are normalized exactly like `ctx.log` fields, and recording is
1873
+ * best-effort — a telemetry failure never breaks the handler.
1874
+ */
1875
+ type LunoraWideEvent = SpanHandle;
1876
+ /**
1877
+ * Application metrics on every function `ctx` — the third signal alongside
1878
+ * `ctx.log` and `ctx.trace`. Each call records one measurement that flows to an
1879
+ * `ObservabilitySink`'s `onMetric`, and from `otlpSink` to a collector's
1880
+ * `/v1/metrics`.
1881
+ *
1882
+ * ```ts
1883
+ * ctx.metrics.count("orders.placed", 1, { plan: user.plan });
1884
+ * ctx.metrics.record("checkout.latency_ms", Date.now() - started);
1885
+ * ctx.metrics.gauge("cart.items", cart.items.length);
1886
+ * ```
1887
+ *
1888
+ * Pick the instrument by the question you want to answer: `count` for "how many"
1889
+ * (summed over time), `gauge` for "how many right now" (replaces the last
1890
+ * reading), `record` for "what's the distribution" (percentiles, not just a
1891
+ * mean).
1892
+ *
1893
+ * `attributes` are the metric's dimensions. Keep them **low-cardinality** — an
1894
+ * attribute valued by user id or order id creates a distinct time series per id,
1895
+ * which is how a metrics backend gets expensive. Put identifiers on a log line or
1896
+ * a span instead.
1897
+ *
1898
+ * No pre-aggregation happens: one call is one exported measurement, with counter
1899
+ * deltas for the collector to sum. In a hot loop, sum locally and record once
1900
+ * rather than calling per iteration.
1901
+ */
1902
+ interface LunoraMetrics {
1903
+ /**
1904
+ * Add to a monotonic counter (default `1`) — requests served, retries,
1905
+ * bytes sent. The collector sums successive deltas.
1906
+ */
1907
+ readonly count: (name: string, value?: number, attributes?: LogFields) => void;
1908
+ /**
1909
+ * Report a point-in-time reading that replaces the previous one — queue
1910
+ * depth, cache size, connections open.
1911
+ */
1912
+ readonly gauge: (name: string, value: number, attributes?: LogFields) => void;
1913
+ /**
1914
+ * Observe one sample of a distribution — latency, payload size. Use this,
1915
+ * not a counter, when percentiles matter.
1916
+ */
1917
+ readonly record: (name: string, value: number, attributes?: LogFields) => void;
989
1918
  }
990
1919
  interface QueryCtx {
991
1920
  readonly auth: AuthState;
992
1921
  readonly db: DatabaseReader;
993
1922
  /**
994
- * The caller's IP for this request Cloudflare's trusted `CF-Connecting-IP`,
995
- * forwarded server-side (never read from a client header). `undefined` when
996
- * unknown: a live-subscription re-run, a server-initiated dispatch, or
997
- * non-Cloudflare hosting. A convenient rate-limit key for anonymous traffic.
998
- */
1923
+ * The validated, typed environment. Populated only when the project declares
1924
+ * a `defineEnv(...)` contract in `lunora/env.ts`; codegen then narrows this to
1925
+ * the validated `InferEnv` shape so `ctx.env.STRIPE_KEY` is parsed and
1926
+ * coercion-aware. Absent (optional) without a contract declare
1927
+ * `lunora/env.ts` to populate and type it.
1928
+ */
1929
+ readonly env?: Record<string, unknown>;
1930
+ /**
1931
+ * The caller's IP for this request — Cloudflare's trusted `CF-Connecting-IP`,
1932
+ * forwarded server-side (never read from a client header). `undefined` when
1933
+ * unknown: a live-subscription re-run, a server-initiated dispatch, or
1934
+ * non-Cloudflare hosting. A convenient rate-limit key for anonymous traffic.
1935
+ */
999
1936
  readonly ip?: string;
1000
1937
  /** Structured, function-attributed logger; see {@link LunoraLogger}. */
1001
1938
  readonly log: LunoraLogger;
1939
+ /** Application counters, gauges, and histograms; see {@link LunoraMetrics}. */
1002
1940
  /**
1003
- * Wall-clock time (epoch ms) the function began, captured once so the whole
1004
- * handler sees a single stable value. Query/mutation handlers must be
1005
- * deterministic they may be re-run on OCC retry / subscription re-eval so
1006
- * read time through `ctx.now` instead of `Date.now()` (the latter is flagged
1007
- * by the `nondeterministic_query_mutation` advisor). Actions may use `Date.now()`.
1008
- */
1941
+ * Static metadata declared on this procedure with `.meta(...)`, merged
1942
+ * across calls. Present so middleware can read the policy it is meant to
1943
+ * enforce (`ctx.meta.rateLimit`, …) instead of having it hard-wired at each
1944
+ * `.use()` site; absent when the procedure never called `.meta()`.
1945
+ */
1946
+ readonly meta?: Record<string, unknown>;
1947
+ readonly metrics: LunoraMetrics;
1948
+ /**
1949
+ * Wall-clock time (epoch ms) the function began, captured once so the whole
1950
+ * handler sees a single stable value. Query/mutation handlers must be
1951
+ * deterministic — they may be re-run on OCC retry / subscription re-eval — so
1952
+ * read time through `ctx.now` instead of `Date.now()` (the latter is flagged
1953
+ * by the `nondeterministic_query_mutation` advisor). Actions may use `Date.now()`.
1954
+ */
1009
1955
  readonly now: number;
1010
1956
  /**
1011
- * Compose a read-only subquery in-process, reusing this query's read
1012
- * context (same transaction, same `db`). Executes the referenced query's
1013
- * handler directly — no fresh DO RPC round-trip — so it observes the exact
1014
- * same snapshot. A query may only call other queries; there is no
1015
- * `runMutation` on a `QueryCtx` (writes are not allowed from a query).
1016
- * Mirrors Convex's `ctx.runQuery`.
1017
- */
1018
- readonly runQuery: <A extends ArgsValidator, R>(reference: RegisteredQuery<A, R>, args: InferArgs<A>) => Promise<R>;
1957
+ * Compose a read-only subquery in-process, reusing this query's read
1958
+ * context (same transaction, same `db`). Executes the referenced query's
1959
+ * handler directly — no fresh DO RPC round-trip — so it observes the exact
1960
+ * same snapshot. A query may only call other queries; there is no
1961
+ * `runMutation` on a `QueryCtx` (writes are not allowed from a query).
1962
+ * Mirrors Convex's `ctx.runQuery`.
1963
+ */
1964
+ readonly runQuery: RunQuery;
1019
1965
  /** Read account-level secrets from Cloudflare Secrets Store; see {@link Secrets}. */
1020
1966
  readonly secrets: Secrets;
1967
+ /** Attach facts to THIS request's span — the wide event; see {@link LunoraWideEvent}. */
1968
+ readonly span: LunoraWideEvent;
1021
1969
  readonly storage: ReadOnlyStorage;
1970
+ /** Wrap a sub-operation in its own nested span; see {@link LunoraTracer}. */
1971
+ readonly trace: LunoraTracer;
1022
1972
  readonly vectors: VectorSearchReader;
1023
1973
  }
1024
1974
  interface MutationCtx {
1025
1975
  readonly auth: AuthState;
1026
1976
  readonly db: DatabaseWriter;
1027
1977
  /**
1028
- * The caller's IP for this request Cloudflare's trusted `CF-Connecting-IP`,
1029
- * forwarded server-side (never read from a client header). `undefined` when
1030
- * unknown: a live-subscription re-run, a server-initiated dispatch, or
1031
- * non-Cloudflare hosting. A convenient rate-limit key for anonymous traffic.
1032
- */
1978
+ * The validated, typed environment. Populated only when the project declares
1979
+ * a `defineEnv(...)` contract in `lunora/env.ts`; codegen then narrows this to
1980
+ * the validated `InferEnv` shape so `ctx.env.STRIPE_KEY` is parsed and
1981
+ * coercion-aware. Absent (optional) without a contract declare
1982
+ * `lunora/env.ts` to populate and type it.
1983
+ */
1984
+ readonly env?: Record<string, unknown>;
1985
+ /**
1986
+ * The caller's IP for this request — Cloudflare's trusted `CF-Connecting-IP`,
1987
+ * forwarded server-side (never read from a client header). `undefined` when
1988
+ * unknown: a live-subscription re-run, a server-initiated dispatch, or
1989
+ * non-Cloudflare hosting. A convenient rate-limit key for anonymous traffic.
1990
+ */
1033
1991
  readonly ip?: string;
1034
1992
  /** Structured, function-attributed logger; see {@link LunoraLogger}. */
1035
1993
  readonly log: LunoraLogger;
1994
+ /** Application counters, gauges, and histograms; see {@link LunoraMetrics}. */
1036
1995
  /**
1037
- * Wall-clock time (epoch ms) the function began, captured once so the whole
1038
- * handler sees a single stable value. Mutation handlers must be deterministic
1039
- * they may be re-run on OCC retry so read time through `ctx.now` instead
1040
- * of `Date.now()` (the latter is flagged by the `nondeterministic_query_mutation`
1041
- * advisor). Actions may use `Date.now()`.
1042
- */
1996
+ * Static metadata declared on this procedure with `.meta(...)`, merged
1997
+ * across calls. Present so middleware can read the policy it is meant to
1998
+ * enforce (`ctx.meta.rateLimit`, …) instead of having it hard-wired at each
1999
+ * `.use()` site; absent when the procedure never called `.meta()`.
2000
+ */
2001
+ readonly meta?: Record<string, unknown>;
2002
+ readonly metrics: LunoraMetrics;
2003
+ /**
2004
+ * Wall-clock time (epoch ms) the function began, captured once so the whole
2005
+ * handler sees a single stable value. Mutation handlers must be deterministic
2006
+ * — they may be re-run on OCC retry — so read time through `ctx.now` instead
2007
+ * of `Date.now()` (the latter is flagged by the `nondeterministic_query_mutation`
2008
+ * advisor). Actions may use `Date.now()`.
2009
+ */
1043
2010
  readonly now: number;
1044
2011
  /**
1045
- * Compose a submutation in-process, reusing this mutation's `db` writer.
1046
- * Executes the referenced mutation's handler directly — no fresh DO RPC —
1047
- * so its writes apply through the same shard invocation as the enclosing
1048
- * mutation. Note: writes are not wrapped in a SQL transaction, so a partial
1049
- * failure does not roll back earlier writes (the same as a top-level
1050
- * mutation). Mirrors Convex's `ctx.runMutation`.
1051
- */
1052
- readonly runMutation: <A extends ArgsValidator, R>(reference: RegisteredMutation<A, R>, args: InferArgs<A>) => Promise<R>;
1053
- /**
1054
- * Compose a read-only subquery in-process, reusing this mutation's `db`.
1055
- * Executes the referenced query's handler directly — no fresh DO RPC — so
1056
- * it observes this mutation's in-flight writes. Mirrors Convex's
1057
- * `ctx.runQuery`.
1058
- */
1059
- readonly runQuery: <A extends ArgsValidator, R>(reference: RegisteredQuery<A, R>, args: InferArgs<A>) => Promise<R>;
2012
+ * Compose a submutation in-process, reusing this mutation's `db` writer.
2013
+ * Executes the referenced mutation's handler directly — no fresh DO RPC —
2014
+ * so its writes apply through the same shard invocation as the enclosing
2015
+ * mutation. Note: writes are not wrapped in a SQL transaction, so a partial
2016
+ * failure does not roll back earlier writes (the same as a top-level
2017
+ * mutation). Mirrors Convex's `ctx.runMutation`.
2018
+ */
2019
+ readonly runMutation: RunMutation;
2020
+ /**
2021
+ * Compose a read-only subquery in-process, reusing this mutation's `db`.
2022
+ * Executes the referenced query's handler directly — no fresh DO RPC — so
2023
+ * it observes this mutation's in-flight writes. Mirrors Convex's
2024
+ * `ctx.runQuery`.
2025
+ */
2026
+ readonly runQuery: RunQuery;
1060
2027
  readonly scheduler: Scheduler;
1061
2028
  /** Read account-level secrets from Cloudflare Secrets Store; see {@link Secrets}. */
1062
2029
  readonly secrets: Secrets;
2030
+ /** Attach facts to THIS request's span — the wide event; see {@link LunoraWideEvent}. */
2031
+ readonly span: LunoraWideEvent;
1063
2032
  readonly storage: ReadOnlyStorage;
2033
+ /** Wrap a sub-operation in its own nested span; see {@link LunoraTracer}. */
2034
+ readonly trace: LunoraTracer;
1064
2035
  readonly vectors: VectorSearch;
1065
2036
  /** Start / resume / inspect durable workflows; see {@link Workflows}. */
1066
2037
  readonly workflows: Workflows;
1067
2038
  }
1068
2039
  interface ActionCtx {
1069
2040
  readonly auth: AuthState;
2041
+ /**
2042
+ * Programmatic Workers Cache purge; see {@link CachePurge}.
2043
+ * **Action-only** — actions run in the Worker, which has a `cache` binding.
2044
+ * Queries and mutations run inside the Durable Object and do not expose this.
2045
+ * Optional at runtime because Workers Cache is only present when enabled.
2046
+ */
2047
+ readonly cache?: CachePurge;
1070
2048
  readonly db: DatabaseWriter;
2049
+ /**
2050
+ * The validated, typed environment. Populated only when the project declares
2051
+ * a `defineEnv(...)` contract in `lunora/env.ts`; codegen then narrows this to
2052
+ * the validated `InferEnv` shape so `ctx.env.STRIPE_KEY` is parsed and
2053
+ * coercion-aware. Absent (optional) without a contract — declare
2054
+ * `lunora/env.ts` to populate and type it.
2055
+ */
2056
+ readonly env?: Record<string, unknown>;
1071
2057
  readonly fetch: typeof globalThis.fetch;
1072
2058
  /**
1073
- * The caller's IP for this request — Cloudflare's trusted `CF-Connecting-IP`,
1074
- * forwarded server-side (never read from a client header). `undefined` when
1075
- * unknown: a live-subscription re-run, a server-initiated dispatch, or
1076
- * non-Cloudflare hosting. A convenient rate-limit key for anonymous traffic.
1077
- */
2059
+ * The caller's IP for this request — Cloudflare's trusted `CF-Connecting-IP`,
2060
+ * forwarded server-side (never read from a client header). `undefined` when
2061
+ * unknown: a live-subscription re-run, a server-initiated dispatch, or
2062
+ * non-Cloudflare hosting. A convenient rate-limit key for anonymous traffic.
2063
+ */
1078
2064
  readonly ip?: string;
1079
2065
  /** Structured, function-attributed logger; see {@link LunoraLogger}. */
1080
2066
  readonly log: LunoraLogger;
2067
+ /** Application counters, gauges, and histograms; see {@link LunoraMetrics}. */
2068
+ /**
2069
+ * Static metadata declared on this procedure with `.meta(...)`, merged
2070
+ * across calls. Present so middleware can read the policy it is meant to
2071
+ * enforce (`ctx.meta.rateLimit`, …) instead of having it hard-wired at each
2072
+ * `.use()` site; absent when the procedure never called `.meta()`.
2073
+ */
2074
+ readonly meta?: Record<string, unknown>;
2075
+ readonly metrics: LunoraMetrics;
1081
2076
  /**
1082
- * Wall-clock time (epoch ms) the action began, captured once for convenience
1083
- * and parity with query/mutation `ctx.now`. Actions run exactly once, so they
1084
- * may also use ambient `Date.now()` freely.
1085
- */
2077
+ * Wall-clock time (epoch ms) the action began, captured once for convenience
2078
+ * and parity with query/mutation `ctx.now`. Actions run exactly once, so they
2079
+ * may also use ambient `Date.now()` freely.
2080
+ */
1086
2081
  readonly now: number;
1087
- readonly runAction: <A extends ArgsValidator, R>(reference: RegisteredAction<A, R>, args: InferArgs<A>) => Promise<R>;
1088
- readonly runMutation: <A extends ArgsValidator, R>(reference: RegisteredMutation<A, R>, args: InferArgs<A>) => Promise<R>;
1089
- readonly runQuery: <A extends ArgsValidator, R>(reference: RegisteredQuery<A, R>, args: InferArgs<A>) => Promise<R>;
2082
+ readonly runAction: RunAction;
2083
+ readonly runMutation: RunMutation;
2084
+ readonly runQuery: RunQuery;
1090
2085
  readonly scheduler: Scheduler;
1091
2086
  /** Read account-level secrets from Cloudflare Secrets Store; see {@link Secrets}. */
1092
2087
  readonly secrets: Secrets;
2088
+ /** Attach facts to THIS request's span — the wide event; see {@link LunoraWideEvent}. */
2089
+ readonly span: LunoraWideEvent;
1093
2090
  readonly storage: Storage;
2091
+ /** Wrap a sub-operation in its own nested span; see {@link LunoraTracer}. */
2092
+ readonly trace: LunoraTracer;
1094
2093
  readonly vectors: VectorSearch;
1095
2094
  /** Start / resume / inspect durable workflows; see {@link Workflows}. */
1096
2095
  readonly workflows: Workflows;
1097
2096
  }
1098
2097
  /**
1099
- * Stand-in returned by codegen so projects can `import { api } from "./_generated/api"`.
1100
- * The runtime value is opaque; the types are filled in by generated declarations.
1101
- */
2098
+ * Stand-in returned by codegen so projects can `import { api } from "./_generated/api"`.
2099
+ * The runtime value is opaque; the types are filled in by generated declarations.
2100
+ */
1102
2101
  type AnyApi = Record<string, Record<string, RegisteredFunction<ArgsValidator, unknown, FunctionKind>>>;
1103
2102
  declare const anyApi: AnyApi;
1104
- export { type ActionCtx, type AggregateIndexDefinition, type AggregateOp, type AnyApi, type ArgsValidator, type AuthState, type DatabaseReader, type DatabaseWriter, type DurableObjectJurisdiction, type FunctionKind, type FunctionVisibility, type GlobalBackend, type IndexDefinition, type IndexRangeBuilder, type InferArgs, type LifecycleEvent, type LifecycleEventKind, type LunoraLogger, type MutationCtx, type OnDeleteAction, type PaginationOptions, type PaginationResult, type QueryCtx, type RankIndexDefinition, type RankSortKey, type ReadOnlyStorage, type RegisteredAction, type RegisteredFunction, type RegisteredLifecycleHook, type RegisteredMutation, type RegisteredQuery, type RegisteredStream, type RelationDefinition, type ScheduledFunctionDoc, type ScheduledJob, type Scheduler, type Schema, type SearchFilterBuilder, type SearchIndexDefinition, type Secrets, type SecretsStoreSecretLike, type ShardMode, type Storage, type StorageMetadata, type SystemDatabaseReader, type SystemDoc, type SystemQuery, type SystemTableName, type TableDefinition, type TableReader, type TableVectorIndex, type TriggerAggregateOptions, type TriggerBuilder, type TriggerCtx, type TriggerDatabase, type TriggerDefinition, type TriggerDeleteEvent, type TriggerEvent, type TriggerGroupByEntry, type TriggerGroupByOptions, type TriggerHandler, type TriggerInsertEvent, type TriggerOp, type TriggerQueryArgs, type TriggerQueryPage, type TriggerRankOptions, type TriggerRankPageOptions, type TriggerRankResult, type TriggerRow, type TriggerTiming, type TriggerUpdateEvent, type VectorEmbedder, type VectorIndexDefinition, type VectorMatch, type VectorMatches, type VectorMetric, type VectorQueryInput, type VectorRecord, type VectorSearch, type VectorSearchReader, type VectorUpsertInput, type WorkflowCreateOptions, type WorkflowHandle, type WorkflowInstance, type WorkflowInstanceStatus, type WorkflowStatusResult, type Workflows, anyApi };
2103
+ export { type ActionCtx, type AggregateIndexDefinition, type AggregateOp, type AnyApi, type ArgsValidator, type AuthState, type CachePurge, type DatabaseReader, type DatabaseWriter, type DurableObjectJurisdiction, type DurableStreamOptions, type ExposeConfig, type ExternalSourceCursor, type ExternalSourceDefinition, type ExternalSourceMode, type ExternalSourceRefresh, type FunctionKind, type FunctionVisibility, type GeoBoundingBox, type GeoFilterBuilder, type GeoIndexDefinition, type GeoPointInput, type GlobalBackend, type IndexDefinition, type IndexRangeBuilder, type InferArgs, type LifecycleEvent, type LifecycleEventKind, type LogFields, type LunoraLogMethod, type LunoraLogger, type LunoraMetrics, type LunoraTracer, type LunoraWideEvent, type MutationCtx, type OnDeleteAction, type PaginationOptions, type PaginationResult, type QueryCtx, type RankIndexDefinition, type RankSortKey, type ReadOnlyStorage, type RegisteredAction, type RegisteredFunction, type RegisteredLifecycleHook, type RegisteredMutation, type RegisteredQuery, type RegisteredStream, type RelationDefinition, type RestCacheConfig, type ScheduledFunctionDoc, type ScheduledJob, type Scheduler, type Schema, type SearchFilterBuilder, type SearchIndexDefinition, type SearchLanguage, type SearchStrategy, type Secrets, type SecretsStoreSecretLike, type ShardMode, type SpanEvaluation, type SpanHandle, type SpanKind, type SpanLink, type SpanOptions, type Storage, type StorageMetadata, type SystemDatabaseReader, type SystemDoc, type SystemQuery, type SystemTableName, type TableDefinition, type TableReader, type TableVectorIndex, type TriggerAggregateOptions, type TriggerBuilder, type TriggerCtx, type TriggerDatabase, type TriggerDefinition, type TriggerDeleteEvent, type TriggerEvent, type TriggerGroupByEntry, type TriggerGroupByOptions, type TriggerHandler, type TriggerInsertEvent, type TriggerOp, type TriggerQueryArgs, type TriggerQueryPage, type TriggerRankOptions, type TriggerRankPageOptions, type TriggerRankResult, type TriggerRow, type TriggerTiming, type TriggerUpdateEvent, type TtlDefinition, type VectorEmbedder, type VectorIndexDefinition, type VectorMatch, type VectorMatches, type VectorMetric, type VectorQueryInput, type VectorRecord, type VectorSearch, type VectorSearchReader, type VectorUpsertInput, type WorkflowCreateOptions, type WorkflowHandle, type WorkflowInstance, type WorkflowInstanceStatus, type WorkflowStatusResult, type Workflows, type X402ProcedureConfig, anyApi };