cvx-kit 0.1.15 → 0.1.16

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 (45) hide show
  1. package/README.md +59 -38
  2. package/dist/relations.mjs +1 -1
  3. package/dist/relations.mjs.map +1 -1
  4. package/dist/zod-table.d.mts +59 -2
  5. package/dist/zod-table.mjs +1 -1
  6. package/dist/zod-table.mjs.map +1 -1
  7. package/package.json +10 -3
  8. package/AGENTS.md +0 -47
  9. package/CHANGELOG.md +0 -337
  10. package/ROADMAP.md +0 -126
  11. package/docs/agent-tools.md +0 -41
  12. package/docs/approvals.md +0 -201
  13. package/docs/architecture.md +0 -124
  14. package/docs/auth.md +0 -443
  15. package/docs/command-context.md +0 -150
  16. package/docs/commands-legacy.md +0 -513
  17. package/docs/commands.md +0 -173
  18. package/docs/conventions.md +0 -345
  19. package/docs/crud.md +0 -66
  20. package/docs/domain-contracts.md +0 -148
  21. package/docs/domain-operations.md +0 -615
  22. package/docs/effect.md +0 -403
  23. package/docs/llms.md +0 -324
  24. package/docs/maintainability.md +0 -124
  25. package/docs/migration.md +0 -351
  26. package/docs/oxlint.md +0 -260
  27. package/docs/querying.md +0 -329
  28. package/docs/tenancy.md +0 -176
  29. package/docs/triggers.md +0 -77
  30. package/docs/upgrading.md +0 -287
  31. package/docs/webhooks.md +0 -58
  32. package/docs/zod-table.md +0 -278
  33. package/scripts/convex-config-bridge.mjs +0 -44
  34. package/scripts/convex-smoke-env.mjs +0 -33
  35. package/scripts/extract-changelog.mjs +0 -29
  36. package/scripts/owned-process.mjs +0 -140
  37. package/scripts/owned-process.test.mjs +0 -107
  38. package/scripts/smoke-packed-components.mjs +0 -341
  39. package/scripts/smoke-packed-effect.mjs +0 -439
  40. package/scripts/smoke-packed-querying.mjs +0 -298
  41. package/scripts/test-packed-auth.mjs +0 -72
  42. package/scripts/test-packed-effect.mjs +0 -482
  43. package/scripts/test-packed-ids.mjs +0 -150
  44. package/scripts/test-packed-oxlint.mjs +0 -167
  45. package/scripts/test-packed-querying.mjs +0 -185
package/README.md CHANGED
@@ -28,26 +28,47 @@ Supported versions: Convex `^1.46.0`, Zod `^4.5.4`, Bun `1.3+`, and Node.js
28
28
 
29
29
  ## Documentation
30
30
 
31
- Full docs ship with the package — `node_modules/cvx-kit/docs/` when installed,
32
- authored in [`src/docs/`](./src/docs/):
33
-
34
- - [`architecture.md`](./src/docs/architecture.md) — the layered application shape the kit encodes
35
- - [`conventions.md`](./src/docs/conventions.md) — the doctrine: folder structure, file anatomy, naming grammar, import rules
36
- - [`zod-table.md`](./src/docs/zod-table.md) — one zod shape per entity, masked into every boundary
37
- - [`auth.md`](./src/docs/auth.md) — auth-aware function constructors from injected policy
38
- - [`commands.md`](./src/docs/commands.md) — Effect command declarations, lifecycle, and migration
39
- - [`effect.md`](./src/docs/effect.md) — opt-in Effect v4 registries, injected services, and shared API builders
40
- - [`domain-operations.md`](./src/docs/domain-operations.md) — server domain/API examples, contracts, errors, receipts, workflow steps, and selected tools
41
- - [`triggers.md`](./src/docs/triggers.md) — trigger registry, timestamps/append-only/no-delete
42
- - [`approvals.md`](./src/docs/approvals.md) — declarative approval workflows
43
- - [`tenancy.md`](./src/docs/tenancy.md) — row-level security (roles) with opt-in multi-tenant isolation
44
- - [`crud.md`](./src/docs/crud.md) — the CRUD command factory
45
- - [`webhooks.md`](./src/docs/webhooks.md) — the sanctioned webhook boundary
46
- - [`agent-tools.md`](./src/docs/agent-tools.md) — agent tools that are audited commands
47
- - [`upgrading.md`](./src/docs/upgrading.md) — 0.0.x → 0.1.0, old way → new way
48
- - [`maintainability.md`](./src/docs/maintainability.md) — keeping a Convex app maintainable long-term
49
- - [`migration.md`](./src/docs/migration.md) — restructuring an existing raw Convex project into this architecture
50
- - [`llms.md`](./src/docs/llms.md) — condensed reference to paste into an LLM/agent context
31
+ Full docs live on [GitHub](https://github.com/ImRLopezAG/cvx-kit/tree/main/skills/cvx-kit/references)
32
+ and are bundled with the repository's [agent skill](https://github.com/ImRLopezAG/cvx-kit/tree/main/skills/cvx-kit).
33
+ The npm package includes this README; full docs and skills are installed separately.
34
+
35
+ If you use a coding agent, install the cvx-kit skill from your application directory:
36
+
37
+ ```sh
38
+ npx skills add ImRLopezAG/cvx-kit --skill cvx-kit
39
+ ```
40
+
41
+ The [skills CLI](https://www.skills.sh/docs) lets you choose your agent. Installation
42
+ is project-scoped by default; add `--global` only for a user-wide install. Public
43
+ GitHub installation needs no login. Preview available skills with
44
+ `npx skills add ImRLopezAG/cvx-kit --list`.
45
+ The skill reads the guides needed for each task instead of loading all docs.
46
+ Check your installed package version against the skill's source revision before
47
+ using newer APIs. For a release containing the skill, use its GitHub tag URL
48
+ (`https://github.com/ImRLopezAG/cvx-kit/tree/<tag>/skills/cvx-kit`) as the install source.
49
+
50
+ Maintainers edit `skills/cvx-kit/references/` directly. CI verifies that relative
51
+ links remain inside the installed skill with `bun run skills:check`.
52
+
53
+ Guides:
54
+
55
+ - [`architecture.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/architecture.md) — the layered application shape the kit encodes
56
+ - [`conventions.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/conventions.md) — the doctrine: folder structure, file anatomy, naming grammar, import rules
57
+ - [`zod-table.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/zod-table.md) — one zod shape per entity, masked into every boundary
58
+ - [`auth.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/auth.md) — auth-aware function constructors from injected policy
59
+ - [`commands.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/commands.md) — Effect command declarations, lifecycle, and migration
60
+ - [`effect.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/effect.md) — opt-in Effect v4 registries, injected services, and shared API builders
61
+ - [`domain-operations.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/domain-operations.md) — server domain/API examples, contracts, errors, receipts, workflow steps, and selected tools
62
+ - [`triggers.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/triggers.md) — trigger registry, timestamps/append-only/no-delete
63
+ - [`approvals.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/approvals.md) — declarative approval workflows
64
+ - [`tenancy.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/tenancy.md) — row-level security (roles) with opt-in multi-tenant isolation
65
+ - [`crud.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/crud.md) — the CRUD command factory
66
+ - [`webhooks.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/webhooks.md) — the sanctioned webhook boundary
67
+ - [`agent-tools.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/agent-tools.md) — agent tools that are audited commands
68
+ - [`upgrading.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/upgrading.md) — 0.0.x → 0.1.0, old way → new way
69
+ - [`maintainability.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/maintainability.md) — keeping a Convex app maintainable long-term
70
+ - [`migration.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/migration.md) — restructuring an existing raw Convex project into this architecture
71
+ - [`llms.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/llms.md) — optional condensed reference for coding agents
51
72
 
52
73
  ## Modules
53
74
 
@@ -85,9 +106,9 @@ LLM-tool JSON schemas primitive while preserving the `Id<...>` type.
85
106
 
86
107
  ### `cvx-kit/relations` and `cvx-kit/querying`
87
108
 
88
- See the [querying guide](./src/docs/querying.md),
89
- [DTO parameters](./src/docs/zod-table.md#public-dto-parameters), and
90
- [consumer rule reference](./src/docs/oxlint.md#dto-and-pagination-diagnostics).
109
+ See the [querying guide](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/querying.md),
110
+ [DTO parameters](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/zod-table.md#public-dto-parameters), and
111
+ [consumer rule reference](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/oxlint.md#dto-and-pagination-diagnostics).
91
112
 
92
113
  Declare relationships once in `convex/relations.ts`, then configure the engine
93
114
  once in `convex/querying.ts`. Both factories are also available from the package
@@ -131,7 +152,7 @@ One engine serves every registered table. Cursor keys load internally on first
131
152
  pagination from server environment variables `CVX_QUERY_CURSOR_KEYS_JSON`
132
153
  (key IDs mapped to 128-character hex secrets) and `CVX_QUERY_CURSOR_CURRENT_KEY_ID`.
133
154
  Keep key material out of arguments, client code, and logs. Optional overrides
134
- belong in a separate second argument; see [querying](src/docs/querying.md). The default codec
155
+ belong in a separate second argument; see [querying](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/querying.md). The default codec
135
156
  uses authenticated AES-SIV encryption without a plaintext fallback; identical
136
157
  payloads under the same binding reveal equality. Tokens bind filters, selected
137
158
  output, index, order, relation ancestry, principal/tenant/security scope, and
@@ -324,12 +345,12 @@ export const executeRename = (input: z.input<typeof renameInput>, host: Host) =>
324
345
  services and execute it; domain code can compose `Effect.gen` and `Effect.fn`
325
346
  without starting another runner. Commands validate inputs and results, enforce
326
347
  permissions and guards, and write audit entries in the mutation transaction.
327
- See [commands](./src/docs/commands.md), [complete Effect wiring](./src/docs/effect.md),
328
- and [authenticated builders](./src/docs/auth.md).
348
+ See [commands](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/commands.md), [complete Effect wiring](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/effect.md),
349
+ and [authenticated builders](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/auth.md).
329
350
 
330
351
  The old `Foundation.Command` Promise facade is **deprecated** and retained for
331
352
  existing consumers. Its separate executor handler options are covered only in
332
- the [compatibility reference](./src/docs/commands-legacy.md). The Foundation
353
+ the [compatibility reference](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/commands-legacy.md). The Foundation
333
354
  component and its transaction result helpers remain available.
334
355
 
335
356
  ### `cvx-kit/components/approvals` (component)
@@ -349,7 +370,7 @@ app.use(approvals)
349
370
  ## Application lint rules
350
371
 
351
372
  The TypeScript plugin `cvx-kit/oxlint` checks applications using the kit against
352
- [`conventions.md`](./src/docs/conventions.md). It targets `convex/`, including
373
+ [`conventions.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/conventions.md). It targets `convex/`, including
353
374
  public adapters, domain modules, root facades, and locally authored components.
354
375
  Load it in your application's `.oxlintrc.json` and select rules:
355
376
 
@@ -390,11 +411,11 @@ module cycles. Run Oxlint on the whole backend, including `convex.config.ts`;
390
411
  changed-file-only lint cannot guarantee a project scan. No application-owned
391
412
  architecture test framework is required. The plugin also exports `checkArchitecture()`
392
413
  for tools that need project diagnostics directly, named `rules`, and `RuleName`. Verified with
393
- Oxlint 1.87.0 and Vite+ 1.1.0. See [rule coverage and limitations](./src/docs/oxlint.md)
394
- and the [migration from the initial 0.1.4 plugin](./src/docs/upgrading.md).
414
+ Oxlint 1.87.0 and Vite+ 1.1.0. See [rule coverage and limitations](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/oxlint.md)
415
+ and the [migration from the initial 0.1.4 plugin](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/upgrading.md).
395
416
 
396
417
  Repository checks and the enforced library rules are documented in
397
- [`tools/oxlint/README.md`](./tools/oxlint/README.md). Run `bun run lint` for
418
+ [`tools/oxlint/README.md`](https://github.com/ImRLopezAG/cvx-kit/blob/main/tools/oxlint/README.md). Run `bun run lint` for
398
419
  anti-slop and architecture checks, `bun run test:lint` to test the custom
399
420
  rules, and `bun run check` for the full Vite+ lint and formatting check.
400
421
 
@@ -425,7 +446,7 @@ registerApprovals(t)
425
446
  ## Releasing
426
447
 
427
448
  Every release also creates a GitHub Release whose notes are the matching
428
- [CHANGELOG](./CHANGELOG.md) section (extracted by
449
+ [CHANGELOG](https://github.com/ImRLopezAG/cvx-kit/blob/main/CHANGELOG.md) section (extracted by
429
450
  `scripts/extract-changelog.mjs` in the release workflow).
430
451
 
431
452
  ### OIDC setup
@@ -434,10 +455,10 @@ Publishing uses [npm Trusted Publishing](https://docs.npmjs.com/trusted-publishe
434
455
  (OIDC) — no tokens stored anywhere. The release workflow authenticates via
435
456
  GitHub's OIDC and npm generates provenance attestations automatically.
436
457
 
437
- - `bun run release` — patch version bump, tag, and push; the `v*` tag
438
- triggers `.github/workflows/release.yml`, which verifies (typecheck, test,
439
- build + publint) and publishes.
440
- - `bun run release:minor` — same, minor bump.
458
+ Merge the version bump and changelog into `main`, then wait for successful CI
459
+ on that exact commit before pushing its `v<version>` tag. The tag triggers
460
+ `.github/workflows/release.yml`, which checks the main CI result, builds and
461
+ validates the package and skill documentation, and publishes to npm.
441
462
 
442
463
  One-time setup (already-published package required first):
443
464
 
@@ -449,10 +470,10 @@ One-time setup (already-published package required first):
449
470
 
450
471
  ## Roadmap
451
472
 
452
- See [ROADMAP.md](./ROADMAP.md) — milestones from 0.1.0 through 1.0.0.
473
+ See [ROADMAP.md](https://github.com/ImRLopezAG/cvx-kit/blob/main/ROADMAP.md) — milestones from 0.1.0 through 1.0.0.
453
474
 
454
475
  Optional request context can be added through the existing Effect API adapter's
455
476
  `context` callback. Bind selected registries with `commands.withContext(value)`
456
477
  and consume `ctx.commands.exec(name, input)` through the application's existing
457
478
  function helper names. Keep dependencies local to each feature; explicit execution
458
- and input-only command handlers remain supported. See [optional context injection](./src/docs/command-context.md).
479
+ and input-only command handlers remain supported. See [optional context injection](https://github.com/ImRLopezAG/cvx-kit/blob/main/skills/cvx-kit/references/command-context.md).
@@ -1,2 +1,2 @@
1
- import{nativePagination as e,relationFilterSchema as t,scalarPredicateSchema as n}from"./querying/filters.mjs";import{paginated as r}from"./zod-table.mjs";import{zodToConvex as i}from"convex-helpers/server/zod4";import{z as a}from"zod";import{v as o}from"convex/values";const s=Symbol(`relationField`),c=Symbol(`relationEdge`),l=/* @__PURE__ */ new WeakMap,u=/* @__PURE__ */ new WeakMap,d=/* @__PURE__ */ new WeakMap,f=/* @__PURE__ */ new WeakSet,p=/* @__PURE__ */ new WeakSet,m=a.object({indexes:a.array(a.object({indexDescriptor:a.string(),fields:a.array(a.string())}))});function h(n,o){let c={},h=/* @__PURE__ */ new Set,g={},_={},v={};for(let[e,t]of Object.entries(n)){if(t.tableName!==e||[`one`,`many`,`__proto__`,`constructor`,`prototype`].includes(e))throw Error(`Invalid relation table "${e}"`);let n=t.dto(!0);if(!(n instanceof a.ZodObject))throw Error(`Expected object DTO`);let r=I(n),o=i(t.schema),l=k(t.table);M(l.documentType),N(e,o.fields,l.documentType);let u=m.parse(l).indexes.map(e=>({name:e.indexDescriptor,fields:[...e.fields]})).sort((e,t)=>e.name.localeCompare(t.name));M(A(i(n))),c[e]={fields:o.fields,indexes:u,dto:n,assertUnchanged:r};let p=Object.create(null);for(let[t,n]of Object.entries(o.fields)){let r={[s]:void 0,table:e,field:t,through(e){let t=d.get(e);if(!h.has(e)||!t||!S(n,t))throw Error(`Invalid junction field or registry`);let i=Object.freeze({base:r,junction:e});return f.add(i),i}};d.set(r,n),h.add(r),p[t]=Object.freeze(r)}g[e]=Object.freeze(p),_[e]=t=>x(e,`one`,t,h),v[e]=t=>x(e,`many`,t,h)}let C=o(Object.freeze({...g,one:Object.freeze(_),many:Object.freeze(v)})),T=Object.fromEntries(Object.entries(C).map(([e,t])=>[e,Object.freeze({...t})]));Object.freeze(T);for(let[e,t]of Object.entries(T)){if(!Object.hasOwn(n,e))throw Error(`Unknown source table "${e}"`);for(let[n,r]of Object.entries(t)){if(Object.hasOwn(c[e].fields,n)||[`and`,`or`,`not`,`fields`,`then`,`__proto__`,`constructor`,`prototype`].includes(n))throw Error(`Relation name collision "${n}"`);if(!p.has(r)||!h.has(b(r.from))||!h.has(b(r.to))||r.source!==e)throw Error(`Relation source bucket mismatch`);w(r,c)}}let P={metadata:c,declarations:T},F={};for(let i of Object.keys(n))F[i]={paginationOpts(n){return B(P),y(n),H(e.extend({filters:t(D(P,i,n)).optional(),order:a.enum([`asc`,`desc`]).optional()}),()=>B(P))},pageDto(e){return B(P),y(e),H(r(O(P,i,e)).result,()=>B(P))}};let L=JSON.stringify([1,Object.entries(c).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,Object.entries(t.fields).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,t.isOptional,A(t)]),t.indexes,j(A(i(t.dto)))]),Object.entries(T).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,Object.entries(t).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,t.cardinality,t.target,E(t.from),E(t.to)])])]),R=Object.freeze({identity:L,version:1,tables:Object.freeze(F)});l.set(R,U(P));let V=Object.freeze({...n}),W=Object.fromEntries(Object.entries(V).map(([e,t])=>{let n=z(t.schema,`stored`),r=JSON.stringify(k(t.table));return[e,()=>{if(n(),JSON.stringify(k(t.table))!==r)throw Error(`Native schema mutated after registry construction: ${e}`)}]}));return u.set(R,{internal:P,tables:V,assertStoredUnchanged:()=>{for(let e of Object.values(W))e()},assertNativeTable(e,t){if(!Object.hasOwn(V,e)||V[e].table!==t)throw Error(`Native schema/table ownership disagrees with relation registry`);W[e]()}}),R}function g(i){let o=u.get(i);if(!o)throw Error(`Unknown relation registry`);let s=()=>{B(o.internal),o.assertStoredUnchanged()},c=e=>{s();let t=Object.keys(o.tables);if(t.length!==Object.keys(e).length||t.some(t=>e[t]!==o.tables[t].table))throw Error(`Native schema/table ownership disagrees with relation registry`)};return s(),Object.freeze({assertUnchanged:s,assertNativeTable:o.assertNativeTable,assertSchema:c,pagination(r){s();let i=o.internal,c=/* @__PURE__ */ new Map;function l(e,t){let r=i.metadata[e];if(!r)throw Error(`Unknown query table`);let a=c.get(e);if(!a){a={};for(let[e,t]of Object.entries(r.fields))v(t)&&(a[e]=n(t).optional());c.set(e,a)}return{fields:a,relations:t>=4?{}:Object.fromEntries(Object.entries(i.declarations[e]??{}).map(([e,n])=>[e,{cardinality:n.cardinality,target:l(n.target,t+1)}]))}}return e.extend({filters:t(l(r,0)).optional(),order:a.enum([`asc`,`desc`]).optional()})},page(e,t){return s(),y(t),r(O(o.internal,e,t)).result},project(e,t,n,r){if(c(e),!r())return null;let i=o.internal.metadata[t]?.dto;if(!i)throw Error(`Unknown query table`);return i.parse(Object.fromEntries(Object.keys(i.shape).filter(e=>Object.hasOwn(n,e)).map(e=>[e,n[e]])))}})}function _(e){let t=l.get(e);if(!t)throw Error(`Unknown relation registry`);return t}function v(e){return[`id`,`string`,`float64`,`int64`,`boolean`,`null`,`literal`].includes(e.kind)||e.kind===`union`&&e.members.every(v)}function y(e){let t=[{value:e,depth:0,ancestors:[]}],n=0;for(;t.length;){let{value:e,depth:r,ancestors:i}=t.pop();if(++n>256||r>4||i.includes(e))throw Error(`Selection cycle or depth/node budget exceeded`);let a=Object.getPrototypeOf(e);if(a!==Object.prototype&&a!==null)throw Error(`Invalid selection`);let o=Reflect.ownKeys(e);if(o.length+n+t.length>256)throw Error(`Selection node budget exceeded`);for(let a of o){let o=Object.getOwnPropertyDescriptor(e,a);if(!o.enumerable||!(`value`in o)||String(a)!==a)throw Error(`Invalid selection property`);if(o.value!==!0)t.push({value:o.value,depth:r+1,ancestors:[...i,e]});else if(++n>256)throw Error(`Selection node budget exceeded`)}}}function b(e){return`base`in e?e.base:e}function x(e,t,n,r){for(let e of[n.from,n.to])if(!r.has(b(e))||`base`in e&&(!f.has(e)||!r.has(e.junction)))throw Error(`Foreign registry or invalid field reference`);if(b(n.to).table!==e)throw Error(`Wrong relation target table`);let i=Object.freeze({[c]:!0,source:b(n.from).table,target:e,cardinality:t,from:n.from,to:n.to});return p.add(i),i}function S(e,t){let n=C(e);return n.length>0&&JSON.stringify(n)===JSON.stringify(C(t))}function C(e){if(e.kind===`union`)return[...new Set(e.members.flatMap(C))].sort();if(e.kind===`null`)return[];if(![`id`,`string`,`float64`,`int64`,`boolean`,`literal`].includes(e.kind))throw Error(`Unsupported relation field kind`);return[JSON.stringify(A(e))]}function w(e,t){let n=e.from,r=e.to;if(`base`in n!=`base`in r)throw Error(`Both relation legs must use through`);if(`base`in n&&`base`in r){if(n.junction.table!==r.junction.table)throw Error(`Different junction tables`);T(t,n.junction.table,n.junction.field),T(t,e.target,r.base.field);return}if(!S(d.get(b(n)),d.get(b(r))))throw Error(`Incompatible relation field types`);T(t,e.target,b(r).field)}function T(e,t,n){if(n!==`_id`&&!e[t].indexes.some(e=>e.fields[0]===n))throw Error(`No active index begins with "${t}.${n}"`)}function E(e){return`base`in e?[e.base.table,e.base.field,e.junction.table,e.junction.field]:[e.table,e.field]}function D(e,t,r,i=0){let a={},o={};for(let[s,c]of Object.entries(r)){let r=e.metadata[t].fields[s],l=e.declarations[t]?.[s];if(c===!0&&r)a[s]=n(r).optional();else if(c!==!0&&l&&i<4)o[s]={cardinality:l.cardinality,target:D(e,l.target,c,i+1)};else throw Error(`Invalid relation filter selection "${t}.${s}"`)}return{fields:a,relations:o}}function O(e,t,n,i=0){let o={};for(let[a,s]of Object.entries(n)){let n=e.declarations[t]?.[a];if(!n||i>=4)throw Error(`Unknown or too deep relation "${t}.${a}"`);let c=s===!0?e.metadata[n.target].dto:O(e,n.target,s,i+1);o[a]=n.cardinality===`one`?c.nullable():r(c).result}let s=e.metadata[t].dto;if(!(s instanceof a.ZodObject))throw Error(`Expected object DTO`);return H(s.extend(o).strict(),()=>B(e))}function k(e){return e.export()}function A(e){return e.json}function j(e){if(Array.isArray(e))return`[${e.map(j).join(`,`)}]`;if(e!==null&&Object(e)===e){let t=e;return`{${Object.keys(t).sort().map(e=>`${JSON.stringify(e)}:${j(t[e])}`).join(`,`)}}`}return JSON.stringify(e)}function M(e){if(e===null||Object(e)!==e)return;let t=e;if(t.type===`any`)throw Error(`Unsupported native validation form`);for(let e of Object.values(t))M(e)}function N(e,t,n){let{_id:r,_creationTime:i,...a}=t;if(!r||!i||j(A(r))!==j(A(o.id(e)))||j(A(i))!==j(A(o.number()))||r.isOptional!==`required`||i.isOptional!==`required`||j(n)!==j(A(o.object(a))))throw Error(`Native stored document disagrees with boundary "${e}"`)}const P=a.object({field:a.string()}),F=new Set([P,P.pick({field:!0}),P.omit({field:!0}),P.extend({other:a.string()}),P.safeExtend({other:a.string()}),P.merge(a.object({other:a.string()})),P.partial(),P.required()].flatMap(e=>{let t=Object.getOwnPropertyDescriptor(e._zod.def,`shape`)?.get;return t?[Function.prototype.toString.call(t)]:[]}));function I(e){return z(e,`dto`)}const L=Function.prototype.toString.call(Object.getOwnPropertyDescriptor(a.string().default(``)._zod.def,`defaultValue`).get),R=Function.prototype.toString.call(Object.getOwnPropertyDescriptor(a.string().prefault(``)._zod.def,`defaultValue`).get);function z(e,t){let n=[],r=/* @__PURE__ */ new Set,i=[],o=/* @__PURE__ */ new Set,s=/* @__PURE__ */ new Set,c=[];function l(e,u){if(s.has(e))throw Error(`Unsupported cyclic DTO schema`);if(!o.has(e)){if(u>32||n.length>=4096)throw Error(`DTO schema graph budget exceeded`);o.add(e),s.add(e);try{if(e instanceof RegExp&&i.push({expression:e,source:e.source,flags:e.flags}),e instanceof a.core.$ZodCheck&&`check`in e._zod.def){if(t===`dto`&&e._zod.def.check===`overwrite`)throw Error(`Unsupported DTO overwrite check`);l(e._zod.def,u+1),n.push({object:e._zod,exactKeys:!1,keys:[`def`,`check`],values:[`def`,`check`].map(t=>Object.getOwnPropertyDescriptor(e._zod,t))});return}if(e instanceof a.core.$ZodType){let i=e._zod.def;if(t===`dto`&&(![`object`,`array`,`optional`,`nullable`,`nonoptional`,`union`,`intersection`,`tuple`,`record`,`string`,`number`,`boolean`,`bigint`,`null`,`never`,`literal`,`enum`].includes(i.type)||`coerce`in i&&i.coerce))throw Error(`Unsupported DTO schema "${i.type}"`);if(t===`stored`&&(i.type==="default"||i.type===`prefault`)){let e=Object.getOwnPropertyDescriptor(i,`defaultValue`);if(e?.get){if(e.set||Function.prototype.toString.call(e.get)!==(i.type==="default"?L:R))throw Error(`Unsupported stored schema accessor`);r.add(i)}}if(i.type===`object`){let t=Object.getOwnPropertyDescriptor(i,`shape`);if(t?.get){if(t.set||!F.has(Function.prototype.toString.call(t.get)))throw Error(`Unsupported DTO schema accessor`);let e=i.shape;l(e,u+1),Object.getOwnPropertyDescriptor(i,`shape`)?.get&&c.push({definition:i,fields:e})}if(e instanceof a.ZodObject){let t=Object.getOwnPropertyDescriptor(e,`shape`);if(t&&!(`value`in t))throw Error(`Unsupported DTO schema accessor`);l(e.shape,u+1)}}l(i,u+1),n.push({object:e._zod,exactKeys:!1,keys:[`def`],values:[Object.getOwnPropertyDescriptor(e._zod,`def`)]});return}let o=Reflect.ownKeys(e),s=o.map(t=>Object.getOwnPropertyDescriptor(e,t));if(s.some((t,n)=>!(`value`in t)&&!c.some(({definition:t})=>t===e&&o[n]===`shape`)&&!(r.has(e)&&o[n]===`defaultValue`)))throw Error(`Unsupported DTO schema accessor`);n.push({object:e,exactKeys:!0,keys:o,values:s});for(let e of s)e.value!==null&&Object(e.value)===e.value&&!(e.value instanceof Function)&&l(e.value,u+1)}finally{s.delete(e)}}}return l(e,0),()=>{for(let e of i)if(e.expression.source!==e.source||e.expression.flags!==e.flags)throw Error(`DTO schema mutated after registry construction`);for(let e of n){let t=e.keys;if(e.exactKeys&&Reflect.ownKeys(e.object).length!==t.length)throw Error(`DTO schema mutated after registry construction`);for(let n=0;n<t.length;n++){if(e.object instanceof RegExp&&t[n]===`lastIndex`)continue;let r=Object.getOwnPropertyDescriptor(e.object,t[n]),i=e.values[n];if(!r||r.value!==i.value||r.get!==i.get||r.set!==i.set||r.enumerable!==i.enumerable)throw Error(`DTO schema mutated after registry construction`)}}for(let{definition:e,fields:t}of c){let n=e.shape,r=Reflect.ownKeys(t);if(Reflect.ownKeys(n).length!==r.length||r.some(e=>Object.getOwnPropertyDescriptor(n,e)?.value!==Object.getOwnPropertyDescriptor(t,e)?.value))throw Error(`DTO schema mutated after registry construction`)}}}function B(e){for(let t of Object.values(e.metadata))t.assertUnchanged()}const V=a.core.$constructor(`GuardedDtoObject`,(e,t)=>{let n=t;a.ZodObject.init(e,t),e._zod.deferred??=[],e._zod.deferred.push(()=>{let t=e._zod.run;e._zod.run=(e,r)=>{try{n.assertUnchanged()}catch{return e.issues.push({code:`custom`,input:e.value,message:`DTO schema mutated after registry construction`}),e}return t(e,r)}})});function H(e,t){let n=e.shape,r={...e._zod.def,assertUnchanged:t};return Object.defineProperty(r,"shape",{enumerable:!0,configurable:!0,get:()=>(t(),n)}),new V(r)}function U(e){let t={};for(let[n,r]of Object.entries(e.metadata)){let e={};for(let[t,n]of Object.entries(r.fields))e[t]=Object.freeze({kind:n.kind,optional:n.isOptional===`optional`,nullable:W(n),idTables:Object.freeze(G(n)),native:K(A(n))});t[n]=Object.freeze({fields:Object.freeze(e),indexes:Object.freeze(r.indexes.map(e=>Object.freeze({name:e.name,fields:Object.freeze([...e.fields])})))})}return Object.freeze({tables:Object.freeze(t),declarations:e.declarations})}function W(e){return e.kind===`null`||e.kind===`union`&&e.members.some(W)}function G(e){return e.kind===`id`?[e.tableName]:e.kind===`union`?[...new Set(e.members.flatMap(G))].sort():[]}function K(e){let t=structuredClone(e),n=[t];for(;n.length;){let e=n.pop();if(Object(e)!==e)continue;let t=e;Object.freeze(t),n.push(...Object.values(t))}return t}export{h as defineRelations,g as getRelationExecutor,_ as getRelationMetadata};
1
+ import{nativePagination as e,relationFilterSchema as t,scalarPredicateSchema as n}from"./querying/filters.mjs";import{paginated as r}from"./zod-table.mjs";import{zodToConvex as i}from"convex-helpers/server/zod4";import{z as a}from"zod";import{v as o}from"convex/values";const s=Symbol(`relationField`),c=Symbol(`relationEdge`),l=/* @__PURE__ */ new WeakMap,u=/* @__PURE__ */ new WeakMap,d=/* @__PURE__ */ new WeakMap,f=/* @__PURE__ */ new WeakSet,p=/* @__PURE__ */ new WeakSet,m=a.object({indexes:a.array(a.object({indexDescriptor:a.string(),fields:a.array(a.string())}))});function h(n,o){let c={},h=/* @__PURE__ */ new Set,g={},_={},v={};for(let[e,t]of Object.entries(n)){if(t.schema instanceof a.ZodUnion)throw Error(`Variant tables are not supported by relations or the query engine`);if(t.tableName!==e||[`one`,`many`,`__proto__`,`constructor`,`prototype`].includes(e))throw Error(`Invalid relation table "${e}"`);let n=t.dto(!0);if(!(n instanceof a.ZodObject))throw Error(`Expected object DTO`);let r=I(n),o=i(t.schema),l=k(t.table);M(l.documentType),N(e,o.fields,l.documentType);let u=m.parse(l).indexes.map(e=>({name:e.indexDescriptor,fields:[...e.fields]})).sort((e,t)=>e.name.localeCompare(t.name));M(A(i(n))),c[e]={fields:o.fields,indexes:u,dto:n,assertUnchanged:r};let p=Object.create(null);for(let[t,n]of Object.entries(o.fields)){let r={[s]:void 0,table:e,field:t,through(e){let t=d.get(e);if(!h.has(e)||!t||!S(n,t))throw Error(`Invalid junction field or registry`);let i=Object.freeze({base:r,junction:e});return f.add(i),i}};d.set(r,n),h.add(r),p[t]=Object.freeze(r)}g[e]=Object.freeze(p),_[e]=t=>x(e,`one`,t,h),v[e]=t=>x(e,`many`,t,h)}let C=o(Object.freeze({...g,one:Object.freeze(_),many:Object.freeze(v)})),T=Object.fromEntries(Object.entries(C).map(([e,t])=>[e,Object.freeze({...t})]));Object.freeze(T);for(let[e,t]of Object.entries(T)){if(!Object.hasOwn(n,e))throw Error(`Unknown source table "${e}"`);for(let[n,r]of Object.entries(t)){if(Object.hasOwn(c[e].fields,n)||[`and`,`or`,`not`,`fields`,`then`,`__proto__`,`constructor`,`prototype`].includes(n))throw Error(`Relation name collision "${n}"`);if(!p.has(r)||!h.has(b(r.from))||!h.has(b(r.to))||r.source!==e)throw Error(`Relation source bucket mismatch`);w(r,c)}}let P={metadata:c,declarations:T},F={};for(let i of Object.keys(n))F[i]={paginationOpts(n){return B(P),y(n),H(e.extend({filters:t(D(P,i,n)).optional(),order:a.enum([`asc`,`desc`]).optional()}),()=>B(P))},pageDto(e){return B(P),y(e),H(r(O(P,i,e)).result,()=>B(P))}};let L=JSON.stringify([1,Object.entries(c).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,Object.entries(t.fields).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,t.isOptional,A(t)]),t.indexes,j(A(i(t.dto)))]),Object.entries(T).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,Object.entries(t).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,t.cardinality,t.target,E(t.from),E(t.to)])])]),R=Object.freeze({identity:L,version:1,tables:Object.freeze(F)});l.set(R,U(P));let V=Object.freeze({...n}),W=Object.fromEntries(Object.entries(V).map(([e,t])=>{let n=z(t.schema,`stored`),r=JSON.stringify(k(t.table));return[e,()=>{if(n(),JSON.stringify(k(t.table))!==r)throw Error(`Native schema mutated after registry construction: ${e}`)}]}));return u.set(R,{internal:P,tables:V,assertStoredUnchanged:()=>{for(let e of Object.values(W))e()},assertNativeTable(e,t){if(!Object.hasOwn(V,e)||V[e].table!==t)throw Error(`Native schema/table ownership disagrees with relation registry`);W[e]()}}),R}function g(i){let o=u.get(i);if(!o)throw Error(`Unknown relation registry`);let s=()=>{B(o.internal),o.assertStoredUnchanged()},c=e=>{s();let t=Object.keys(o.tables);if(t.length!==Object.keys(e).length||t.some(t=>e[t]!==o.tables[t].table))throw Error(`Native schema/table ownership disagrees with relation registry`)};return s(),Object.freeze({assertUnchanged:s,assertNativeTable:o.assertNativeTable,assertSchema:c,pagination(r){s();let i=o.internal,c=/* @__PURE__ */ new Map;function l(e,t){let r=i.metadata[e];if(!r)throw Error(`Unknown query table`);let a=c.get(e);if(!a){a={};for(let[e,t]of Object.entries(r.fields))v(t)&&(a[e]=n(t).optional());c.set(e,a)}return{fields:a,relations:t>=4?{}:Object.fromEntries(Object.entries(i.declarations[e]??{}).map(([e,n])=>[e,{cardinality:n.cardinality,target:l(n.target,t+1)}]))}}return e.extend({filters:t(l(r,0)).optional(),order:a.enum([`asc`,`desc`]).optional()})},page(e,t){return s(),y(t),r(O(o.internal,e,t)).result},project(e,t,n,r){if(c(e),!r())return null;let i=o.internal.metadata[t]?.dto;if(!i)throw Error(`Unknown query table`);return i.parse(Object.fromEntries(Object.keys(i.shape).filter(e=>Object.hasOwn(n,e)).map(e=>[e,n[e]])))}})}function _(e){let t=l.get(e);if(!t)throw Error(`Unknown relation registry`);return t}function v(e){return[`id`,`string`,`float64`,`int64`,`boolean`,`null`,`literal`].includes(e.kind)||e.kind===`union`&&e.members.every(v)}function y(e){let t=[{value:e,depth:0,ancestors:[]}],n=0;for(;t.length;){let{value:e,depth:r,ancestors:i}=t.pop();if(++n>256||r>4||i.includes(e))throw Error(`Selection cycle or depth/node budget exceeded`);let a=Object.getPrototypeOf(e);if(a!==Object.prototype&&a!==null)throw Error(`Invalid selection`);let o=Reflect.ownKeys(e);if(o.length+n+t.length>256)throw Error(`Selection node budget exceeded`);for(let a of o){let o=Object.getOwnPropertyDescriptor(e,a);if(!o.enumerable||!(`value`in o)||String(a)!==a)throw Error(`Invalid selection property`);if(o.value!==!0)t.push({value:o.value,depth:r+1,ancestors:[...i,e]});else if(++n>256)throw Error(`Selection node budget exceeded`)}}}function b(e){return`base`in e?e.base:e}function x(e,t,n,r){for(let e of[n.from,n.to])if(!r.has(b(e))||`base`in e&&(!f.has(e)||!r.has(e.junction)))throw Error(`Foreign registry or invalid field reference`);if(b(n.to).table!==e)throw Error(`Wrong relation target table`);let i=Object.freeze({[c]:!0,source:b(n.from).table,target:e,cardinality:t,from:n.from,to:n.to});return p.add(i),i}function S(e,t){let n=C(e);return n.length>0&&JSON.stringify(n)===JSON.stringify(C(t))}function C(e){if(e.kind===`union`)return[...new Set(e.members.flatMap(C))].sort();if(e.kind===`null`)return[];if(![`id`,`string`,`float64`,`int64`,`boolean`,`literal`].includes(e.kind))throw Error(`Unsupported relation field kind`);return[JSON.stringify(A(e))]}function w(e,t){let n=e.from,r=e.to;if(`base`in n!=`base`in r)throw Error(`Both relation legs must use through`);if(`base`in n&&`base`in r){if(n.junction.table!==r.junction.table)throw Error(`Different junction tables`);T(t,n.junction.table,n.junction.field),T(t,e.target,r.base.field);return}if(!S(d.get(b(n)),d.get(b(r))))throw Error(`Incompatible relation field types`);T(t,e.target,b(r).field)}function T(e,t,n){if(n!==`_id`&&!e[t].indexes.some(e=>e.fields[0]===n))throw Error(`No active index begins with "${t}.${n}"`)}function E(e){return`base`in e?[e.base.table,e.base.field,e.junction.table,e.junction.field]:[e.table,e.field]}function D(e,t,r,i=0){let a={},o={};for(let[s,c]of Object.entries(r)){let r=e.metadata[t].fields[s],l=e.declarations[t]?.[s];if(c===!0&&r)a[s]=n(r).optional();else if(c!==!0&&l&&i<4)o[s]={cardinality:l.cardinality,target:D(e,l.target,c,i+1)};else throw Error(`Invalid relation filter selection "${t}.${s}"`)}return{fields:a,relations:o}}function O(e,t,n,i=0){let o={};for(let[a,s]of Object.entries(n)){let n=e.declarations[t]?.[a];if(!n||i>=4)throw Error(`Unknown or too deep relation "${t}.${a}"`);let c=s===!0?e.metadata[n.target].dto:O(e,n.target,s,i+1);o[a]=n.cardinality===`one`?c.nullable():r(c).result}let s=e.metadata[t].dto;if(!(s instanceof a.ZodObject))throw Error(`Expected object DTO`);return H(s.extend(o).strict(),()=>B(e))}function k(e){return e.export()}function A(e){return e.json}function j(e){if(Array.isArray(e))return`[${e.map(j).join(`,`)}]`;if(e!==null&&Object(e)===e){let t=e;return`{${Object.keys(t).sort().map(e=>`${JSON.stringify(e)}:${j(t[e])}`).join(`,`)}}`}return JSON.stringify(e)}function M(e){if(e===null||Object(e)!==e)return;let t=e;if(t.type===`any`)throw Error(`Unsupported native validation form`);for(let e of Object.values(t))M(e)}function N(e,t,n){let{_id:r,_creationTime:i,...a}=t;if(!r||!i||j(A(r))!==j(A(o.id(e)))||j(A(i))!==j(A(o.number()))||r.isOptional!==`required`||i.isOptional!==`required`||j(n)!==j(A(o.object(a))))throw Error(`Native stored document disagrees with boundary "${e}"`)}const P=a.object({field:a.string()}),F=new Set([P,P.pick({field:!0}),P.omit({field:!0}),P.extend({other:a.string()}),P.safeExtend({other:a.string()}),P.merge(a.object({other:a.string()})),P.partial(),P.required()].flatMap(e=>{let t=Object.getOwnPropertyDescriptor(e._zod.def,`shape`)?.get;return t?[Function.prototype.toString.call(t)]:[]}));function I(e){return z(e,`dto`)}const L=Function.prototype.toString.call(Object.getOwnPropertyDescriptor(a.string().default(``)._zod.def,`defaultValue`).get),R=Function.prototype.toString.call(Object.getOwnPropertyDescriptor(a.string().prefault(``)._zod.def,`defaultValue`).get);function z(e,t){let n=[],r=/* @__PURE__ */ new Set,i=[],o=/* @__PURE__ */ new Set,s=/* @__PURE__ */ new Set,c=[];function l(e,u){if(s.has(e))throw Error(`Unsupported cyclic DTO schema`);if(!o.has(e)){if(u>32||n.length>=4096)throw Error(`DTO schema graph budget exceeded`);o.add(e),s.add(e);try{if(e instanceof RegExp&&i.push({expression:e,source:e.source,flags:e.flags}),e instanceof a.core.$ZodCheck&&`check`in e._zod.def){if(t===`dto`&&e._zod.def.check===`overwrite`)throw Error(`Unsupported DTO overwrite check`);l(e._zod.def,u+1),n.push({object:e._zod,exactKeys:!1,keys:[`def`,`check`],values:[`def`,`check`].map(t=>Object.getOwnPropertyDescriptor(e._zod,t))});return}if(e instanceof a.core.$ZodType){let i=e._zod.def;if(t===`dto`&&(![`object`,`array`,`optional`,`nullable`,`nonoptional`,`union`,`intersection`,`tuple`,`record`,`string`,`number`,`boolean`,`bigint`,`null`,`never`,`literal`,`enum`].includes(i.type)||`coerce`in i&&i.coerce))throw Error(`Unsupported DTO schema "${i.type}"`);if(t===`stored`&&(i.type==="default"||i.type===`prefault`)){let e=Object.getOwnPropertyDescriptor(i,`defaultValue`);if(e?.get){if(e.set||Function.prototype.toString.call(e.get)!==(i.type==="default"?L:R))throw Error(`Unsupported stored schema accessor`);r.add(i)}}if(i.type===`object`){let t=Object.getOwnPropertyDescriptor(i,`shape`);if(t?.get){if(t.set||!F.has(Function.prototype.toString.call(t.get)))throw Error(`Unsupported DTO schema accessor`);let e=i.shape;l(e,u+1),Object.getOwnPropertyDescriptor(i,`shape`)?.get&&c.push({definition:i,fields:e})}if(e instanceof a.ZodObject){let t=Object.getOwnPropertyDescriptor(e,`shape`);if(t&&!(`value`in t))throw Error(`Unsupported DTO schema accessor`);l(e.shape,u+1)}}l(i,u+1),n.push({object:e._zod,exactKeys:!1,keys:[`def`],values:[Object.getOwnPropertyDescriptor(e._zod,`def`)]});return}let o=Reflect.ownKeys(e),s=o.map(t=>Object.getOwnPropertyDescriptor(e,t));if(s.some((t,n)=>!(`value`in t)&&!c.some(({definition:t})=>t===e&&o[n]===`shape`)&&!(r.has(e)&&o[n]===`defaultValue`)))throw Error(`Unsupported DTO schema accessor`);n.push({object:e,exactKeys:!0,keys:o,values:s});for(let e of s)e.value!==null&&Object(e.value)===e.value&&!(e.value instanceof Function)&&l(e.value,u+1)}finally{s.delete(e)}}}return l(e,0),()=>{for(let e of i)if(e.expression.source!==e.source||e.expression.flags!==e.flags)throw Error(`DTO schema mutated after registry construction`);for(let e of n){let t=e.keys;if(e.exactKeys&&Reflect.ownKeys(e.object).length!==t.length)throw Error(`DTO schema mutated after registry construction`);for(let n=0;n<t.length;n++){if(e.object instanceof RegExp&&t[n]===`lastIndex`)continue;let r=Object.getOwnPropertyDescriptor(e.object,t[n]),i=e.values[n];if(!r||r.value!==i.value||r.get!==i.get||r.set!==i.set||r.enumerable!==i.enumerable)throw Error(`DTO schema mutated after registry construction`)}}for(let{definition:e,fields:t}of c){let n=e.shape,r=Reflect.ownKeys(t);if(Reflect.ownKeys(n).length!==r.length||r.some(e=>Object.getOwnPropertyDescriptor(n,e)?.value!==Object.getOwnPropertyDescriptor(t,e)?.value))throw Error(`DTO schema mutated after registry construction`)}}}function B(e){for(let t of Object.values(e.metadata))t.assertUnchanged()}const V=a.core.$constructor(`GuardedDtoObject`,(e,t)=>{let n=t;a.ZodObject.init(e,t),e._zod.deferred??=[],e._zod.deferred.push(()=>{let t=e._zod.run;e._zod.run=(e,r)=>{try{n.assertUnchanged()}catch{return e.issues.push({code:`custom`,input:e.value,message:`DTO schema mutated after registry construction`}),e}return t(e,r)}})});function H(e,t){let n=e.shape,r={...e._zod.def,assertUnchanged:t};return Object.defineProperty(r,"shape",{enumerable:!0,configurable:!0,get:()=>(t(),n)}),new V(r)}function U(e){let t={};for(let[n,r]of Object.entries(e.metadata)){let e={};for(let[t,n]of Object.entries(r.fields))e[t]=Object.freeze({kind:n.kind,optional:n.isOptional===`optional`,nullable:W(n),idTables:Object.freeze(G(n)),native:K(A(n))});t[n]=Object.freeze({fields:Object.freeze(e),indexes:Object.freeze(r.indexes.map(e=>Object.freeze({name:e.name,fields:Object.freeze([...e.fields])})))})}return Object.freeze({tables:Object.freeze(t),declarations:e.declarations})}function W(e){return e.kind===`null`||e.kind===`union`&&e.members.some(W)}function G(e){return e.kind===`id`?[e.tableName]:e.kind===`union`?[...new Set(e.members.flatMap(G))].sort():[]}function K(e){let t=structuredClone(e),n=[t];for(;n.length;){let e=n.pop();if(Object(e)!==e)continue;let t=e;Object.freeze(t),n.push(...Object.values(t))}return t}export{h as defineRelations,g as getRelationExecutor,_ as getRelationMetadata};
2
2
  //# sourceMappingURL=relations.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"relations.mjs","names":["fieldBrand","edgeBrand","registries","executorRegistries","refs","throughRefs","edgeRefs","indexExport","z","defineRelations","metadata","ownRefs","fields","one","many","name","table","tables","dto","assertUnchanged","dtoMutationGuard","native","zodToConvex","exported","nativeTableExport","assertNativeValidation","assertStoredDocument","indexes","index","a","b","nativeJson","tableRefs","field","validator","ref","junctionValidator","junction","compatible","result","makeEdge","spec","supplied","declare","declarations","edges","source","edge","base","validatePath","internal","helpers","assertRegistryUnchanged","assertSelection","selection","guardedDto","nativePagination","relationFilterSchema","filterGrammar","paginated","rowSchema","identity","data","canonicalNative","serializeLeg","registry","snapshotMetadata","ownedTables","storedGuards","schemaGuard","schemaMutationGuard","nativeSnapshot","guard","definition","getRelationExecutor","owned","assertSchema","names","scalarFields","grammar","scalarValidator","scalarPredicateSchema","depth","declaration","eligible","row","key","getRelationMetadata","pending","nodes","ancestors","value","prototype","keys","property","leg","own","target","leftDomain","domain","left","right","from","to","requireIndex","relations","mask","relationFields","object","container","child","stored","_id","_creationTime","v","document","objectAccessorSample","builtinObjectGetters","getter","schema","builtinDefaultGetter","builtinPrefaultGetter","fallbackDefinitions","patterns","seen","active","objectFields","visit","boundary","def","descriptor","publicFields","values","i","pattern","node","current","before","GuardedDtoObject","guarded","inst","run","payload","ctx","outputFields","hasNull","idTables","frozenNative","snapshot","next"],"sources":["../src/relations.ts"],"sourcesContent":["import { z } from 'zod'\nimport { zodToConvex, type ConvexValidatorFromZod } from 'convex-helpers/server/zod4'\nimport type { TableDefinition, PaginationResult } from 'convex/server'\nimport {\n\tv,\n\ttype GenericId,\n\ttype GenericValidator,\n\ttype ValidatorJSON,\n\ttype JSONValue,\n} from 'convex/values'\nimport { paginated } from './zod-table'\nimport {\n\tnativePagination,\n\trelationFilterSchema,\n\tscalarPredicateSchema,\n\ttype FieldSchemas,\n\ttype FilterGrammar,\n\ttype PaginationOptionsSchema,\n\ttype ScalarSelection,\n\ttype ScalarPredicate,\n} from './querying/filters'\n\n/** Object table boundaries only. Runtime metadata comes from native validators/export. */\nexport interface RelationTable {\n\ttableName: string\n\tschema: z.ZodObject\n\tpublicDto: z.ZodObject\n\ttable: TableDefinition\n\tdto(includeId: true): z.ZodType\n}\nexport type RelationTables = Record<string, RelationTable>\n/** Internal named declaration dependency; excluded from the supported relations facade. */\nexport interface SelectionMask {\n\t[key: string]: true | SelectionMask\n}\ntype Fields<T extends RelationTable> = T['schema']['shape']\ntype Native<T extends RelationTable, K extends keyof Fields<T>> = ConvexValidatorFromZod<\n\tFields<T>[K],\n\t'required'\n>['type']\nconst fieldBrand: unique symbol = Symbol('relationField')\nconst edgeBrand: unique symbol = Symbol('relationEdge')\ntype Comparable<Value> = Exclude<Value, null | undefined>\ntype Same<A, B> = [Comparable<A> | Comparable<B>] extends [never]\n\t? false\n\t: [Comparable<A>] extends [Comparable<B>]\n\t\t? [Comparable<B>] extends [Comparable<A>]\n\t\t\t? true\n\t\t\t: false\n\t\t: false\n\nexport interface FieldReference<Table extends string, Field extends string, Value> {\n\treadonly [fieldBrand]: Value\n\treadonly table: Table\n\treadonly field: Field\n\tthrough<J extends AnyField>(\n\t\tjunction: J & (Same<Value, J[typeof fieldBrand]> extends true ? {} : never),\n\t): ThroughReference<Table, Field, Value, J>\n}\ntype AnyField = FieldReference<string, string, any>\nexport interface ThroughReference<\n\tTable extends string,\n\tField extends string,\n\tValue,\n\tJunction extends AnyField,\n> {\n\treadonly base: FieldReference<Table, Field, Value>\n\treadonly junction: Junction\n}\ntype AnyThrough = ThroughReference<string, string, any, AnyField>\ntype Leg = AnyField | AnyThrough\ntype Base<L extends Leg> = L extends AnyThrough ? L['base'] : L\ntype JunctionTable<L extends Leg> = L extends AnyThrough ? L['junction']['table'] : never\ntype CompatibleLegs<F extends Leg, T extends Leg> = F extends AnyThrough\n\t? T extends AnyThrough\n\t\t? Same<JunctionTable<F>, JunctionTable<T>> extends true\n\t\t\t? {}\n\t\t\t: never\n\t\t: never\n\t: T extends AnyThrough\n\t\t? never\n\t\t: Same<Base<F>[typeof fieldBrand], Base<T>[typeof fieldBrand]> extends true\n\t\t\t? {}\n\t\t\t: never\n\nexport interface RelationEdge<\n\tSource extends string,\n\tTarget extends string,\n\tCardinality extends 'one' | 'many',\n> {\n\treadonly [edgeBrand]: true\n\treadonly source: Source\n\treadonly target: Target\n\treadonly cardinality: Cardinality\n\treadonly from: Leg\n\treadonly to: Leg\n}\ntype AnyEdge = RelationEdge<string, string, 'one' | 'many'>\ntype FieldReferences<Tables extends RelationTables> = {\n\t[T in keyof Tables]: {\n\t\t[K in Extract<keyof Fields<Tables[T]>, string>]: FieldReference<\n\t\t\tExtract<T, string>,\n\t\t\tK,\n\t\t\tNative<Tables[T], K>\n\t\t>\n\t}\n}\ntype AllReferences<Tables extends RelationTables> = {\n\t[T in keyof Tables]: FieldReferences<Tables>[T][Extract<keyof FieldReferences<Tables>[T], string>]\n}[keyof Tables]\ntype Builders<Tables extends RelationTables, C extends 'one' | 'many'> = {\n\t[T in keyof Tables]: <\n\t\tF extends AllReferences<Tables> | AnyThrough,\n\t\tTo extends\n\t\t\t| FieldReferences<Tables>[T][Extract<keyof FieldReferences<Tables>[T], string>]\n\t\t\t| AnyThrough,\n\t>(\n\t\tspec: { from: F; to: To } & CompatibleLegs<F, To> & (Base<To>['table'] extends T ? {} : never),\n\t) => RelationEdge<Base<F>['table'], Extract<T, string>, C>\n}\nexport type RelationBuilder<Tables extends RelationTables> = FieldReferences<Tables> & {\n\tone: Builders<Tables, 'one'>\n\tmany: Builders<Tables, 'many'>\n}\nexport type RelationDeclarations = Record<string, Record<string, AnyEdge>>\ntype ExactDeclarations<Tables extends RelationTables, D extends RelationDeclarations> = {\n\t[T in keyof D]: T extends keyof Tables\n\t\t? {\n\t\t\t\t[K in keyof D[T]]: K extends\n\t\t\t\t\t| keyof Fields<Tables[T]>\n\t\t\t\t\t| 'and'\n\t\t\t\t\t| 'or'\n\t\t\t\t\t| 'not'\n\t\t\t\t\t| 'fields'\n\t\t\t\t\t| 'then'\n\t\t\t\t\t| '__proto__'\n\t\t\t\t\t| 'constructor'\n\t\t\t\t\t| 'prototype'\n\t\t\t\t\t? never\n\t\t\t\t\t: D[T][K]['source'] extends T\n\t\t\t\t\t\t? D[T][K]\n\t\t\t\t\t\t: never\n\t\t\t}\n\t\t: never\n}\ntype Depth = readonly [1, 1, 1, 1]\ntype Tail<D extends readonly 1[]> = D extends readonly [1, ...infer R extends 1[]] ? R : []\ntype Edges<D, T> = T extends keyof D ? D[T] : {}\ntype Target<E> = E extends AnyEdge ? E['target'] : never\nexport type RelationSelection<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tLevel extends readonly 1[] = Depth,\n> = Level extends readonly []\n\t? Record<string, never>\n\t: {\n\t\t\t[K in keyof Edges<D, T>]?:\n\t\t\t\t| true\n\t\t\t\t| RelationSelection<Tables, D, Extract<Target<Edges<D, T>[K]>, keyof Tables>, Tail<Level>>\n\t\t}\nexport type RelationFilterSelection<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tLevel extends readonly 1[] = Depth,\n> = ScalarSelection<Fields<Tables[T]>> &\n\t(Level extends readonly []\n\t\t? {}\n\t\t: {\n\t\t\t\t[K in keyof Edges<D, T>]?: RelationFilterSelection<\n\t\t\t\t\tTables,\n\t\t\t\t\tD,\n\t\t\t\t\tExtract<Target<Edges<D, T>[K]>, keyof Tables>,\n\t\t\t\t\tTail<Level>\n\t\t\t\t>\n\t\t\t})\ntype ExactOutputMask<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tL extends readonly 1[] = Depth,\n> = S & {\n\t[K in keyof S]: L extends readonly []\n\t\t? never\n\t\t: K extends keyof Edges<D, T>\n\t\t\t? S[K] extends true\n\t\t\t\t? true\n\t\t\t\t: ExactOutputMask<Tables, D, Extract<Target<Edges<D, T>[K]>, keyof Tables>, S[K], Tail<L>>\n\t\t\t: never\n}\ntype ExactFilterMask<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tL extends readonly 1[] = Depth,\n> = S & {\n\t[K in keyof S]: K extends keyof ScalarSelection<Fields<Tables[T]>>\n\t\t? S[K] extends true\n\t\t\t? true\n\t\t\t: never\n\t\t: L extends readonly []\n\t\t\t? never\n\t\t\t: K extends keyof Edges<D, T>\n\t\t\t\t? S[K] extends true\n\t\t\t\t\t? never\n\t\t\t\t\t: ExactFilterMask<Tables, D, Extract<Target<Edges<D, T>[K]>, keyof Tables>, S[K], Tail<L>>\n\t\t\t\t: never\n}\ntype ScalarValues<T extends RelationTable, S, Side extends 'input' | 'output'> = {\n\t[K in Extract<keyof S, keyof Fields<T>>]?: ScalarPredicate<Fields<T>[K], Side>\n}\nexport type RelationFilters<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tSide extends 'input' | 'output' = 'output',\n\tLevel extends readonly 1[] = Depth,\n> = Omit<ScalarValues<Tables[T], S, Side>, 'and' | 'or' | 'not' | 'fields'> & {\n\tfields?: ScalarValues<Tables[T], S, Side>\n} & (Level extends readonly []\n\t\t? {}\n\t\t: {\n\t\t\t\t[K in Extract<keyof S, keyof Edges<D, T>>]?: Edges<D, T>[K] extends RelationEdge<\n\t\t\t\t\tstring,\n\t\t\t\t\tinfer Target,\n\t\t\t\t\tinfer C\n\t\t\t\t>\n\t\t\t\t\t? C extends 'one'\n\t\t\t\t\t\t? RelationFilters<\n\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\tS[K],\n\t\t\t\t\t\t\t\tSide,\n\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t> | null\n\t\t\t\t\t\t:\n\t\t\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\t\t\tsome: RelationFilters<\n\t\t\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\t\t\tS[K],\n\t\t\t\t\t\t\t\t\t\t\tSide,\n\t\t\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\t\t\tnone: RelationFilters<\n\t\t\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\t\t\tS[K],\n\t\t\t\t\t\t\t\t\t\t\tSide,\n\t\t\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t: never\n\t\t\t} & {\n\t\t\t\tand?: RelationFilters<Tables, D, T, S, Side, Tail<Level>>[]\n\t\t\t\tor?: RelationFilters<Tables, D, T, S, Side, Tail<Level>>[]\n\t\t\t\tnot?: RelationFilters<Tables, D, T, S, Side, Tail<Level>>\n\t\t\t})\ntype PublicRow<T extends RelationTable> = z.output<T['publicDto']> & {\n\t_id: GenericId<T['tableName']>\n}\nexport type RelationRow<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tLevel extends readonly 1[] = Depth,\n> = PublicRow<Tables[T]> &\n\t(Level extends readonly []\n\t\t? {}\n\t\t: {\n\t\t\t\t[K in keyof S]: K extends keyof Edges<D, T>\n\t\t\t\t\t? Edges<D, T>[K] extends RelationEdge<string, infer Target, infer C>\n\t\t\t\t\t\t? C extends 'one'\n\t\t\t\t\t\t\t? RelationRow<\n\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\tS[K] extends true ? {} : S[K],\n\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t> | null\n\t\t\t\t\t\t\t: PaginationResult<\n\t\t\t\t\t\t\t\t\tRelationRow<\n\t\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\t\tS[K] extends true ? {} : S[K],\n\t\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t: never\n\t\t\t\t\t: never\n\t\t\t})\nexport interface RelationTableHelpers<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n> {\n\tpaginationOpts<const S extends RelationFilterSelection<Tables, D, T>>(\n\t\tselection: ExactFilterMask<Tables, D, T, S>,\n\t): PaginationOptionsSchema<\n\t\tRelationFilters<Tables, D, T, S>,\n\t\tRelationFilters<Tables, D, T, S, 'input'>\n\t>\n\tpageDto<const S extends RelationSelection<Tables, D, T>>(\n\t\tselection: ExactOutputMask<Tables, D, T, S>,\n\t): z.ZodType<PaginationResult<RelationRow<Tables, D, T, S>>>\n}\nexport interface RelationRegistryIdentity {\n\treadonly identity: string\n\treadonly version: 1\n}\nexport interface RelationRegistry<Tables extends RelationTables, D extends RelationDeclarations> {\n\treadonly identity: string\n\treadonly version: 1\n\treadonly tables: { [T in keyof Tables]: RelationTableHelpers<Tables, D, T> }\n}\n\nexport interface RelationFieldMetadata {\n\treadonly kind: string\n\treadonly optional: boolean\n\treadonly nullable: boolean\n\treadonly idTables: readonly string[]\n\treadonly native: Readonly<ValidatorJSON>\n}\nexport interface RelationTableMetadata {\n\treadonly fields: Readonly<Record<string, RelationFieldMetadata>>\n\treadonly indexes: readonly { readonly name: string; readonly fields: readonly string[] }[]\n}\ninterface TableMetadata {\n\tfields: Record<string, GenericValidator>\n\tindexes: { name: string; fields: string[] }[]\n\tdto: z.ZodObject\n\tassertUnchanged(): void\n}\ninterface RegistryMetadata {\n\tmetadata: Record<string, TableMetadata>\n\tdeclarations: RelationDeclarations\n}\nexport interface RelationMetadata {\n\treadonly tables: Readonly<Record<string, RelationTableMetadata>>\n\treadonly declarations: Readonly<Record<string, Readonly<Record<string, AnyEdge>>>>\n}\n\nconst registries = new WeakMap<RelationRegistryIdentity, RelationMetadata>()\n// Private executor state retains validators and projection, never a database reader.\nconst executorRegistries = new WeakMap<\n\tRelationRegistryIdentity,\n\t{\n\t\tinternal: RegistryMetadata\n\t\ttables: RelationTables\n\t\tassertStoredUnchanged(): void\n\t\tassertNativeTable(table: string, definition: TableDefinition): void\n\t}\n>()\nconst refs = new WeakMap<AnyField, GenericValidator>()\nconst throughRefs = new WeakSet<AnyThrough>()\nconst edgeRefs = new WeakSet<AnyEdge>()\nconst indexExport = z.object({\n\tindexes: z.array(z.object({ indexDescriptor: z.string(), fields: z.array(z.string()) })),\n})\n\n/** No ID-field inference and no readers: this registry owns declarations/schema helpers only. */\nexport function defineRelations<\n\tconst Tables extends RelationTables,\n\tconst D extends RelationDeclarations,\n>(\n\ttables: Tables,\n\tdeclare: (r: RelationBuilder<Tables>) => D & ExactDeclarations<Tables, D>,\n): RelationRegistry<Tables, D> {\n\tconst metadata: Record<string, TableMetadata> = {}\n\tconst ownRefs = new Set<AnyField>()\n\tconst fields: Record<string, Record<string, AnyField>> = {}\n\tconst one: Record<string, (spec: { from: Leg; to: Leg }) => AnyEdge> = {}\n\tconst many: Record<string, (spec: { from: Leg; to: Leg }) => AnyEdge> = {}\n\tfor (const [name, table] of Object.entries(tables)) {\n\t\tif (\n\t\t\ttable.tableName !== name ||\n\t\t\t['one', 'many', '__proto__', 'constructor', 'prototype'].includes(name)\n\t\t)\n\t\t\tthrow new Error(`Invalid relation table \"${name}\"`)\n\t\tconst dto = table.dto(true)\n\t\tif (!(dto instanceof z.ZodObject)) throw new Error('Expected object DTO')\n\t\tconst assertUnchanged = dtoMutationGuard(dto)\n\t\tconst native = zodToConvex(table.schema)\n\t\tconst exported = nativeTableExport(table.table)\n\t\tassertNativeValidation(exported.documentType)\n\t\tassertStoredDocument(name, native.fields, exported.documentType)\n\t\tconst indexes = indexExport\n\t\t\t.parse(exported)\n\t\t\t.indexes.map((index) => ({ name: index.indexDescriptor, fields: [...index.fields] }))\n\t\t\t.sort((a, b) => a.name.localeCompare(b.name))\n\t\tassertNativeValidation(nativeJson(zodToConvex(dto)))\n\t\tmetadata[name] = { fields: native.fields, indexes, dto, assertUnchanged }\n\t\tconst tableRefs: Record<string, AnyField> = Object.create(null)\n\t\tfor (const [field, validator] of Object.entries(native.fields)) {\n\t\t\tconst ref: AnyField = {\n\t\t\t\t[fieldBrand]: undefined,\n\t\t\t\ttable: name,\n\t\t\t\tfield,\n\t\t\t\tthrough<J extends AnyField>(\n\t\t\t\t\tjunction: J & (Same<any, J[typeof fieldBrand]> extends true ? {} : never),\n\t\t\t\t): ThroughReference<string, string, any, J> {\n\t\t\t\t\tconst junctionValidator = refs.get(junction)\n\t\t\t\t\tif (\n\t\t\t\t\t\t!ownRefs.has(junction) ||\n\t\t\t\t\t\t!junctionValidator ||\n\t\t\t\t\t\t!compatible(validator, junctionValidator)\n\t\t\t\t\t)\n\t\t\t\t\t\tthrow new Error('Invalid junction field or registry')\n\t\t\t\t\tconst result = Object.freeze({ base: ref, junction })\n\t\t\t\t\tthroughRefs.add(result)\n\t\t\t\t\treturn result\n\t\t\t\t},\n\t\t\t}\n\t\t\trefs.set(ref, validator)\n\t\t\townRefs.add(ref)\n\t\t\ttableRefs[field] = Object.freeze(ref)\n\t\t}\n\t\tfields[name] = Object.freeze(tableRefs)\n\t\tone[name] = (spec) => makeEdge(name, 'one', spec, ownRefs)\n\t\tmany[name] = (spec) => makeEdge(name, 'many', spec, ownRefs)\n\t}\n\t// SAFETY: every native field has a branded reference; mapped builders retain the literal names.\n\tconst builder = Object.freeze({\n\t\t...fields,\n\t\tone: Object.freeze(one),\n\t\tmany: Object.freeze(many),\n\t}) as RelationBuilder<Tables>\n\tconst supplied = declare(builder)\n\tconst declarations: RelationDeclarations = Object.fromEntries(\n\t\tObject.entries(supplied).map(([name, edges]) => [name, Object.freeze({ ...edges })]),\n\t)\n\tObject.freeze(declarations)\n\tfor (const [source, edges] of Object.entries(declarations)) {\n\t\tif (!Object.hasOwn(tables, source)) throw new Error(`Unknown source table \"${source}\"`)\n\t\tfor (const [name, edge] of Object.entries(edges)) {\n\t\t\tif (\n\t\t\t\tObject.hasOwn(metadata[source].fields, name) ||\n\t\t\t\t['and', 'or', 'not', 'fields', 'then', '__proto__', 'constructor', 'prototype'].includes(\n\t\t\t\t\tname,\n\t\t\t\t)\n\t\t\t)\n\t\t\t\tthrow new Error(`Relation name collision \"${name}\"`)\n\t\t\tif (\n\t\t\t\t!edgeRefs.has(edge) ||\n\t\t\t\t!ownRefs.has(base(edge.from)) ||\n\t\t\t\t!ownRefs.has(base(edge.to)) ||\n\t\t\t\tedge.source !== source\n\t\t\t)\n\t\t\t\tthrow new Error('Relation source bucket mismatch')\n\t\t\tvalidatePath(edge, metadata)\n\t\t}\n\t}\n\tconst internal: RegistryMetadata = { metadata, declarations }\n\tconst helpers: Record<\n\t\tstring,\n\t\t{\n\t\t\tpaginationOpts(selection: SelectionMask): z.ZodType\n\t\t\tpageDto(selection: SelectionMask): z.ZodType\n\t\t}\n\t> = {}\n\tfor (const table of Object.keys(tables))\n\t\thelpers[table] = {\n\t\t\tpaginationOpts(selection: SelectionMask): z.ZodType {\n\t\t\t\tassertRegistryUnchanged(internal)\n\t\t\t\tassertSelection(selection)\n\t\t\t\treturn guardedDto(\n\t\t\t\t\tnativePagination.extend({\n\t\t\t\t\t\tfilters: relationFilterSchema(filterGrammar(internal, table, selection)).optional(),\n\t\t\t\t\t\torder: z.enum(['asc', 'desc']).optional(),\n\t\t\t\t\t}),\n\t\t\t\t\t() => assertRegistryUnchanged(internal),\n\t\t\t\t)\n\t\t\t},\n\t\t\tpageDto(selection: SelectionMask): z.ZodType {\n\t\t\t\tassertRegistryUnchanged(internal)\n\t\t\t\tassertSelection(selection)\n\t\t\t\treturn guardedDto(paginated(rowSchema(internal, table, selection)).result, () =>\n\t\t\t\t\tassertRegistryUnchanged(internal),\n\t\t\t\t)\n\t\t\t},\n\t\t}\n\t// Stable, collision-free canonical metadata encoding. It is an identity, not a secret/hash.\n\tconst identity = JSON.stringify([\n\t\t1,\n\t\tObject.entries(metadata)\n\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t.map(([name, data]) => [\n\t\t\t\tname,\n\t\t\t\tObject.entries(data.fields)\n\t\t\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t\t\t.map(([field, validator]) => [field, validator.isOptional, nativeJson(validator)]),\n\t\t\t\tdata.indexes,\n\t\t\t\tcanonicalNative(nativeJson(zodToConvex(data.dto))),\n\t\t\t]),\n\t\tObject.entries(declarations)\n\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t.map(([source, edges]) => [\n\t\t\t\tsource,\n\t\t\t\tObject.entries(edges)\n\t\t\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t\t\t.map(([name, edge]) => [\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tedge.cardinality,\n\t\t\t\t\t\tedge.target,\n\t\t\t\t\t\tserializeLeg(edge.from),\n\t\t\t\t\t\tserializeLeg(edge.to),\n\t\t\t\t\t]),\n\t\t\t]),\n\t])\n\t// SAFETY: helpers are assembled for every validated literal table and mask; schemas derive matching native fields/public DTOs.\n\tconst registry = Object.freeze({\n\t\tidentity,\n\t\tversion: 1,\n\t\ttables: Object.freeze(helpers),\n\t}) as RelationRegistry<Tables, D>\n\tregistries.set(registry, snapshotMetadata(internal))\n\tconst ownedTables = Object.freeze({ ...tables })\n\tconst storedGuards = Object.fromEntries(\n\t\tObject.entries(ownedTables).map(([name, table]) => {\n\t\t\tconst schemaGuard = schemaMutationGuard(table.schema, 'stored')\n\t\t\tconst nativeSnapshot = JSON.stringify(nativeTableExport(table.table))\n\t\t\treturn [\n\t\t\t\tname,\n\t\t\t\t() => {\n\t\t\t\t\tschemaGuard()\n\t\t\t\t\tif (JSON.stringify(nativeTableExport(table.table)) !== nativeSnapshot)\n\t\t\t\t\t\tthrow new Error(`Native schema mutated after registry construction: ${name}`)\n\t\t\t\t},\n\t\t\t]\n\t\t}),\n\t)\n\texecutorRegistries.set(registry, {\n\t\tinternal,\n\t\ttables: ownedTables,\n\t\tassertStoredUnchanged: () => {\n\t\t\tfor (const guard of Object.values(storedGuards)) guard()\n\t\t},\n\t\tassertNativeTable(table, definition) {\n\t\t\tif (!Object.hasOwn(ownedTables, table) || ownedTables[table].table !== definition)\n\t\t\t\tthrow new Error('Native schema/table ownership disagrees with relation registry')\n\t\t\tstoredGuards[table]()\n\t\t},\n\t})\n\treturn registry\n}\n\n/** Internal owned executor bridge. Not an advertised root API. Every operation\n * rechecks the live registry, including cached plans and individual projections.\n */\nexport function getRelationExecutor(registry: RelationRegistryIdentity) {\n\tconst owned = executorRegistries.get(registry)\n\tif (!owned) throw new Error('Unknown relation registry')\n\tconst assertUnchanged = () => {\n\t\tassertRegistryUnchanged(owned.internal)\n\t\towned.assertStoredUnchanged()\n\t}\n\tconst assertSchema = (tables: Readonly<Record<string, TableDefinition>>) => {\n\t\tassertUnchanged()\n\t\tconst names = Object.keys(owned.tables)\n\t\tif (\n\t\t\tnames.length !== Object.keys(tables).length ||\n\t\t\tnames.some((name) => tables[name] !== owned.tables[name].table)\n\t\t)\n\t\t\tthrow new Error('Native schema/table ownership disagrees with relation registry')\n\t}\n\tassertUnchanged()\n\treturn Object.freeze({\n\t\tassertUnchanged,\n\t\tassertNativeTable: owned.assertNativeTable,\n\t\tassertSchema,\n\t\tpagination(table: string) {\n\t\t\tassertUnchanged()\n\t\t\tconst internal = owned.internal\n\t\t\tconst scalarFields = new Map<string, FieldSchemas>()\n\t\t\tfunction grammar(name: string, depth: number): FilterGrammar {\n\t\t\t\tconst data = internal.metadata[name]\n\t\t\t\tif (!data) throw new Error('Unknown query table')\n\t\t\t\tlet fields = scalarFields.get(name)\n\t\t\t\tif (!fields) {\n\t\t\t\t\tfields = {}\n\t\t\t\t\tfor (const [name, validator] of Object.entries(data.fields)) {\n\t\t\t\t\t\t// Unsupported stored containers are not scalar-queryable. No validators\n\t\t\t\t\t\t// are reconstructed from JSON; these are the actual native validators.\n\t\t\t\t\t\tif (scalarValidator(validator))\n\t\t\t\t\t\t\tfields[name] = scalarPredicateSchema(validator).optional()\n\t\t\t\t\t}\n\t\t\t\t\tscalarFields.set(name, fields)\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tfields,\n\t\t\t\t\trelations:\n\t\t\t\t\t\tdepth >= 4\n\t\t\t\t\t\t\t? {}\n\t\t\t\t\t\t\t: Object.fromEntries(\n\t\t\t\t\t\t\t\t\tObject.entries(internal.declarations[name] ?? {}).map(([edge, declaration]) => [\n\t\t\t\t\t\t\t\t\t\tedge,\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tcardinality: declaration.cardinality,\n\t\t\t\t\t\t\t\t\t\t\ttarget: grammar(declaration.target, depth + 1),\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nativePagination.extend({\n\t\t\t\tfilters: relationFilterSchema(grammar(table, 0)).optional(),\n\t\t\t\torder: z.enum(['asc', 'desc']).optional(),\n\t\t\t})\n\t\t},\n\t\tpage(table: string, selection: SelectionMask) {\n\t\t\tassertUnchanged()\n\t\t\tassertSelection(selection)\n\t\t\treturn paginated(rowSchema(owned.internal, table, selection)).result\n\t\t},\n\t\tproject(\n\t\t\ttables: Readonly<Record<string, TableDefinition>>,\n\t\t\ttable: string,\n\t\t\trow: Record<string, import('convex/values').Value>,\n\t\t\teligible: () => boolean,\n\t\t) {\n\t\t\tassertSchema(tables)\n\t\t\tif (!eligible()) return null\n\t\t\tconst dto = owned.internal.metadata[table]?.dto\n\t\t\tif (!dto) throw new Error('Unknown query table')\n\t\t\t// The strict DTO remains the authority. Pick its fields before parsing,\n\t\t\t// matching table projection without spreading any storage-only fields.\n\t\t\treturn dto.parse(\n\t\t\t\tObject.fromEntries(\n\t\t\t\t\tObject.keys(dto.shape)\n\t\t\t\t\t\t.filter((key) => Object.hasOwn(row, key))\n\t\t\t\t\t\t.map((key) => [key, row[key]]),\n\t\t\t\t),\n\t\t\t)\n\t\t},\n\t})\n}\n/** Internal compiler/engine adapter. Registry identity alone cannot forge a registry. */\nexport function getRelationMetadata(registry: RelationRegistryIdentity): RelationMetadata {\n\tconst metadata = registries.get(registry)\n\tif (!metadata) throw new Error('Unknown relation registry')\n\treturn metadata\n}\n\nfunction scalarValidator(validator: GenericValidator): boolean {\n\treturn (\n\t\t['id', 'string', 'float64', 'int64', 'boolean', 'null', 'literal'].includes(validator.kind) ||\n\t\t(validator.kind === 'union' && validator.members.every(scalarValidator))\n\t)\n}\n\nfunction assertSelection(selection: SelectionMask): void {\n\tconst pending: { value: SelectionMask; depth: number; ancestors: readonly SelectionMask[] }[] = [\n\t\t{ value: selection, depth: 0, ancestors: [] },\n\t]\n\tlet nodes = 0\n\twhile (pending.length) {\n\t\tconst { value, depth, ancestors } = pending.pop()!\n\t\tif (++nodes > 256 || depth > 4 || ancestors.includes(value))\n\t\t\tthrow new Error('Selection cycle or depth/node budget exceeded')\n\t\tconst prototype = Object.getPrototypeOf(value)\n\t\tif (prototype !== Object.prototype && prototype !== null) throw new Error('Invalid selection')\n\t\tconst keys = Reflect.ownKeys(value)\n\t\tif (keys.length + nodes + pending.length > 256)\n\t\t\tthrow new Error('Selection node budget exceeded')\n\t\tfor (const key of keys) {\n\t\t\tconst property = Object.getOwnPropertyDescriptor(value, key)!\n\t\t\tif (!property.enumerable || !('value' in property) || String(key) !== key)\n\t\t\t\tthrow new Error('Invalid selection property')\n\t\t\tif (property.value !== true)\n\t\t\t\tpending.push({ value: property.value, depth: depth + 1, ancestors: [...ancestors, value] })\n\t\t\telse if (++nodes > 256) throw new Error('Selection node budget exceeded')\n\t\t}\n\t}\n}\nfunction base(leg: Leg): AnyField {\n\treturn 'base' in leg ? leg.base : leg\n}\nfunction makeEdge(\n\ttarget: string,\n\tcardinality: 'one' | 'many',\n\tspec: { from: Leg; to: Leg },\n\town: Set<AnyField>,\n): AnyEdge {\n\tfor (const leg of [spec.from, spec.to]) {\n\t\tif (!own.has(base(leg)) || ('base' in leg && (!throughRefs.has(leg) || !own.has(leg.junction))))\n\t\t\tthrow new Error('Foreign registry or invalid field reference')\n\t}\n\tif (base(spec.to).table !== target) throw new Error('Wrong relation target table')\n\tconst result: AnyEdge = Object.freeze({\n\t\t[edgeBrand]: true as const,\n\t\tsource: base(spec.from).table,\n\t\ttarget,\n\t\tcardinality,\n\t\tfrom: spec.from,\n\t\tto: spec.to,\n\t})\n\tedgeRefs.add(result)\n\treturn result\n}\nfunction compatible(left: GenericValidator, right: GenericValidator): boolean {\n\tconst leftDomain = domain(left)\n\treturn leftDomain.length > 0 && JSON.stringify(leftDomain) === JSON.stringify(domain(right))\n}\nfunction domain(validator: GenericValidator): string[] {\n\tif (validator.kind === 'union') return [...new Set(validator.members.flatMap(domain))].sort()\n\tif (validator.kind === 'null') return []\n\tif (!['id', 'string', 'float64', 'int64', 'boolean', 'literal'].includes(validator.kind))\n\t\tthrow new Error('Unsupported relation field kind')\n\treturn [JSON.stringify(nativeJson(validator))]\n}\nfunction validatePath(edge: AnyEdge, metadata: Record<string, TableMetadata>): void {\n\tconst from = edge.from\n\tconst to = edge.to\n\tif ('base' in from !== 'base' in to) throw new Error('Both relation legs must use through')\n\tif ('base' in from && 'base' in to) {\n\t\tif (from.junction.table !== to.junction.table) throw new Error('Different junction tables')\n\t\trequireIndex(metadata, from.junction.table, from.junction.field)\n\t\trequireIndex(metadata, edge.target, to.base.field)\n\t\treturn\n\t}\n\tif (!compatible(refs.get(base(from))!, refs.get(base(to))!))\n\t\tthrow new Error('Incompatible relation field types')\n\trequireIndex(metadata, edge.target, base(to).field)\n}\nfunction requireIndex(metadata: Record<string, TableMetadata>, table: string, field: string): void {\n\tif (field !== '_id' && !metadata[table].indexes.some((index) => index.fields[0] === field))\n\t\tthrow new Error(`No active index begins with \"${table}.${field}\"`)\n}\nfunction serializeLeg(leg: Leg): string[] {\n\treturn 'base' in leg\n\t\t? [leg.base.table, leg.base.field, leg.junction.table, leg.junction.field]\n\t\t: [leg.table, leg.field]\n}\nfunction filterGrammar(\n\tregistry: RegistryMetadata,\n\ttable: string,\n\tselection: SelectionMask,\n\tdepth: number = 0,\n): FilterGrammar {\n\tconst fields: FieldSchemas = {}\n\tconst relations: FilterGrammar['relations'] = {}\n\tfor (const [key, mask] of Object.entries(selection)) {\n\t\tconst validator = registry.metadata[table].fields[key]\n\t\tconst edge = registry.declarations[table]?.[key]\n\t\tif (mask === true && validator) fields[key] = scalarPredicateSchema(validator).optional()\n\t\telse if (mask !== true && edge && depth < 4)\n\t\t\trelations[key] = {\n\t\t\t\tcardinality: edge.cardinality,\n\t\t\t\ttarget: filterGrammar(registry, edge.target, mask, depth + 1),\n\t\t\t}\n\t\telse throw new Error(`Invalid relation filter selection \"${table}.${key}\"`)\n\t}\n\treturn { fields, relations }\n}\nfunction rowSchema(\n\tregistry: RegistryMetadata,\n\ttable: string,\n\tselection: SelectionMask,\n\tdepth: number = 0,\n): z.ZodType {\n\tconst relationFields: FieldSchemas = {}\n\tfor (const [name, mask] of Object.entries(selection)) {\n\t\tconst edge = registry.declarations[table]?.[name]\n\t\tif (!edge || depth >= 4) throw new Error(`Unknown or too deep relation \"${table}.${name}\"`)\n\t\tconst target =\n\t\t\tmask === true\n\t\t\t\t? registry.metadata[edge.target].dto\n\t\t\t\t: rowSchema(registry, edge.target, mask, depth + 1)\n\t\trelationFields[name] = edge.cardinality === 'one' ? target.nullable() : paginated(target).result\n\t}\n\t// Keep caller validations; guard the supported graph before parsing or native conversion.\n\tconst dto = registry.metadata[table].dto\n\tif (!(dto instanceof z.ZodObject)) throw new Error('Expected object DTO')\n\treturn guardedDto(dto.extend(relationFields).strict(), () => assertRegistryUnchanged(registry))\n}\n\ninterface NativeTableExport {\n\tindexes: { indexDescriptor: string; fields: string[] }[]\n\tdocumentType: ValidatorJSON\n}\nfunction nativeTableExport(table: TableDefinition): NativeTableExport {\n\t// SAFETY: Convex exposes export at runtime but strips its internal signature from declarations.\n\treturn (table as TableDefinition & { export(): NativeTableExport }).export()\n}\nfunction nativeJson(validator: GenericValidator): ValidatorJSON {\n\t// SAFETY: native validators expose converter metadata at runtime; Convex strips json from declarations.\n\treturn (validator as GenericValidator & { json: ValidatorJSON }).json\n}\n\nfunction canonicalNative(value: JSONValue): string {\n\tif (Array.isArray(value)) return `[${value.map(canonicalNative).join(',')}]`\n\tif (value !== null && Object(value) === value) {\n\t\t// SAFETY: native JSON contains only JSON objects, arrays and primitives.\n\t\tconst object = value as { [key: string]: JSONValue }\n\t\treturn `{${Object.keys(object)\n\t\t\t.sort()\n\t\t\t.map((key) => `${JSON.stringify(key)}:${canonicalNative(object[key])}`)\n\t\t\t.join(',')}}`\n\t}\n\treturn JSON.stringify(value)\n}\n\nfunction assertNativeValidation(value: JSONValue): void {\n\tif (value === null || Object(value) !== value) return\n\t// SAFETY: native converter metadata contains only JSON containers and primitives.\n\tconst container = value as { [key: string]: JSONValue }\n\tif (container.type === 'any') throw new Error('Unsupported native validation form')\n\tfor (const child of Object.values(container)) assertNativeValidation(child)\n}\n\nfunction assertStoredDocument(\n\ttable: string,\n\tfields: Record<string, GenericValidator>,\n\tdocument: ValidatorJSON,\n): void {\n\tconst { _id, _creationTime, ...stored } = fields\n\tif (\n\t\t!_id ||\n\t\t!_creationTime ||\n\t\tcanonicalNative(nativeJson(_id)) !== canonicalNative(nativeJson(v.id(table))) ||\n\t\tcanonicalNative(nativeJson(_creationTime)) !== canonicalNative(nativeJson(v.number())) ||\n\t\t_id.isOptional !== 'required' ||\n\t\t_creationTime.isOptional !== 'required' ||\n\t\tcanonicalNative(document) !== canonicalNative(nativeJson(v.object(stored)))\n\t)\n\t\tthrow new Error(`Native stored document disagrees with boundary \"${table}\"`)\n}\n\ninterface DtoGraphNode {\n\tobject: object\n\texactKeys: boolean\n\tkeys: readonly PropertyKey[]\n\tvalues: readonly PropertyDescriptor[]\n}\n\n// Compare against this peer's own built-in accessor, using the intrinsic rather\n// than a caller-provided toString. Older Zod clones retain this accessor even\n// after reading shape, because it caches on the original definition.\nconst objectAccessorSample = z.object({ field: z.string() })\nconst builtinObjectGetters = new Set(\n\t[\n\t\tobjectAccessorSample,\n\t\tobjectAccessorSample.pick({ field: true }),\n\t\tobjectAccessorSample.omit({ field: true }),\n\t\tobjectAccessorSample.extend({ other: z.string() }),\n\t\tobjectAccessorSample.safeExtend({ other: z.string() }),\n\t\tobjectAccessorSample.merge(z.object({ other: z.string() })),\n\t\tobjectAccessorSample.partial(),\n\t\tobjectAccessorSample.required(),\n\t].flatMap((schema) => {\n\t\tconst getter = Object.getOwnPropertyDescriptor(schema._zod.def, 'shape')?.get\n\t\treturn getter ? [Function.prototype.toString.call(getter)] : []\n\t}),\n)\n\n/** Snapshot supported value-validation definitions, not Zod's mutable parse caches.\n * Functions remain the original validation functions; their captured application state\n * is outside the schema contract. Lazy/transform/default/coercion nodes fail closed.\n */\nfunction dtoMutationGuard(dto: z.ZodObject): () => void {\n\treturn schemaMutationGuard(dto, 'dto')\n}\n\n// Stored schemas already passed the native input boundary. Track their graph\n// independently of public DTO admissibility, without running any stored parser.\nconst builtinDefaultGetter = Function.prototype.toString.call(\n\tObject.getOwnPropertyDescriptor(z.string().default('')._zod.def, 'defaultValue')!.get!,\n)\nconst builtinPrefaultGetter = Function.prototype.toString.call(\n\tObject.getOwnPropertyDescriptor(z.string().prefault('')._zod.def, 'defaultValue')!.get!,\n)\n\nfunction schemaMutationGuard(dto: z.ZodObject, boundary: 'dto' | 'stored'): () => void {\n\tconst nodes: DtoGraphNode[] = []\n\tconst fallbackDefinitions = new Set<object>()\n\tconst patterns: { expression: RegExp; source: string; flags: string }[] = []\n\tconst seen = new Set<object>()\n\tconst active = new Set<object>()\n\tconst objectFields: {\n\t\tdefinition: z.core.$ZodObjectDef\n\t\tfields: Record<string, z.core.$ZodType>\n\t}[] = []\n\tfunction visit(value: DtoGraphNode['object'], depth: number): void {\n\t\tif (active.has(value)) throw new Error('Unsupported cyclic DTO schema')\n\t\tif (seen.has(value)) return\n\t\tif (depth > 32 || nodes.length >= 4096) throw new Error('DTO schema graph budget exceeded')\n\t\tseen.add(value)\n\t\tactive.add(value)\n\t\ttry {\n\t\t\tif (value instanceof RegExp)\n\t\t\t\tpatterns.push({ expression: value, source: value.source, flags: value.flags })\n\t\t\tif (value instanceof z.core.$ZodCheck && 'check' in value._zod.def) {\n\t\t\t\tif (boundary === 'dto' && value._zod.def.check === 'overwrite')\n\t\t\t\t\tthrow new Error('Unsupported DTO overwrite check')\n\t\t\t\tvisit(value._zod.def, depth + 1)\n\t\t\t\tnodes.push({\n\t\t\t\t\tobject: value._zod,\n\t\t\t\t\texactKeys: false,\n\t\t\t\t\tkeys: ['def', 'check'],\n\t\t\t\t\tvalues: ['def', 'check'].map((key) => Object.getOwnPropertyDescriptor(value._zod, key)!),\n\t\t\t\t})\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (value instanceof z.core.$ZodType) {\n\t\t\t\t// SAFETY: built-in Zod definitions form a discriminated union.\n\t\t\t\tconst def = (value as z.core.$ZodTypes)._zod.def\n\t\t\t\tif (\n\t\t\t\t\tboundary === 'dto' &&\n\t\t\t\t\t(![\n\t\t\t\t\t\t'object',\n\t\t\t\t\t\t'array',\n\t\t\t\t\t\t'optional',\n\t\t\t\t\t\t'nullable',\n\t\t\t\t\t\t'nonoptional',\n\t\t\t\t\t\t'union',\n\t\t\t\t\t\t'intersection',\n\t\t\t\t\t\t'tuple',\n\t\t\t\t\t\t'record',\n\t\t\t\t\t\t'string',\n\t\t\t\t\t\t'number',\n\t\t\t\t\t\t'boolean',\n\t\t\t\t\t\t'bigint',\n\t\t\t\t\t\t'null',\n\t\t\t\t\t\t'never',\n\t\t\t\t\t\t'literal',\n\t\t\t\t\t\t'enum',\n\t\t\t\t\t].includes(def.type) ||\n\t\t\t\t\t\t('coerce' in def && def.coerce))\n\t\t\t\t)\n\t\t\t\t\tthrow new Error(`Unsupported DTO schema \"${def.type}\"`)\n\t\t\t\tif (boundary === 'stored' && (def.type === 'default' || def.type === 'prefault')) {\n\t\t\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(def, 'defaultValue')\n\t\t\t\t\tif (descriptor?.get) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tdescriptor.set ||\n\t\t\t\t\t\t\tFunction.prototype.toString.call(descriptor.get) !==\n\t\t\t\t\t\t\t\t(def.type === 'default' ? builtinDefaultGetter : builtinPrefaultGetter)\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\tthrow new Error('Unsupported stored schema accessor')\n\t\t\t\t\t\t// Snapshot the accessor identity below, never evaluate its fallback.\n\t\t\t\t\t\tfallbackDefinitions.add(def)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (def.type === 'object') {\n\t\t\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(def, 'shape')\n\t\t\t\t\tif (descriptor?.get) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tdescriptor.set ||\n\t\t\t\t\t\t\t!builtinObjectGetters.has(Function.prototype.toString.call(descriptor.get))\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\tthrow new Error('Unsupported DTO schema accessor')\n\t\t\t\t\t\tconst fields = def.shape\n\t\t\t\t\t\tvisit(fields, depth + 1)\n\t\t\t\t\t\t// A refined clone's getter can return fresh shallow copies.\n\t\t\t\t\t\t// Preserve that caller behavior and compare its fields separately.\n\t\t\t\t\t\tif (Object.getOwnPropertyDescriptor(def, 'shape')?.get)\n\t\t\t\t\t\t\tobjectFields.push({ definition: def, fields })\n\t\t\t\t\t}\n\t\t\t\t\tif (value instanceof z.ZodObject) {\n\t\t\t\t\t\tconst publicFields = Object.getOwnPropertyDescriptor(value, 'shape')\n\t\t\t\t\t\tif (publicFields && !('value' in publicFields))\n\t\t\t\t\t\t\tthrow new Error('Unsupported DTO schema accessor')\n\t\t\t\t\t\tvisit(value.shape, depth + 1)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tvisit(def, depth + 1)\n\t\t\t\t// Track definition replacement, without snapshotting caches initialized by parsing.\n\t\t\t\tnodes.push({\n\t\t\t\t\tobject: value._zod,\n\t\t\t\t\texactKeys: false,\n\t\t\t\t\tkeys: ['def'],\n\t\t\t\t\tvalues: [Object.getOwnPropertyDescriptor(value._zod, 'def')!],\n\t\t\t\t})\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst keys = Reflect.ownKeys(value)\n\t\t\tconst values = keys.map((key) => Object.getOwnPropertyDescriptor(value, key)!)\n\t\t\tif (\n\t\t\t\tvalues.some(\n\t\t\t\t\t(property, i) =>\n\t\t\t\t\t\t!('value' in property) &&\n\t\t\t\t\t\t!objectFields.some(({ definition }) => definition === value && keys[i] === 'shape') &&\n\t\t\t\t\t\t!(fallbackDefinitions.has(value) && keys[i] === 'defaultValue'),\n\t\t\t\t)\n\t\t\t)\n\t\t\t\tthrow new Error('Unsupported DTO schema accessor')\n\t\t\tnodes.push({ object: value, exactKeys: true, keys, values })\n\t\t\tfor (const property of values) {\n\t\t\t\tif (\n\t\t\t\t\tproperty.value !== null &&\n\t\t\t\t\tObject(property.value) === property.value &&\n\t\t\t\t\t!(property.value instanceof Function)\n\t\t\t\t)\n\t\t\t\t\tvisit(property.value, depth + 1)\n\t\t\t}\n\t\t} finally {\n\t\t\tactive.delete(value)\n\t\t}\n\t}\n\tvisit(dto, 0)\n\treturn () => {\n\t\tfor (const pattern of patterns)\n\t\t\tif (\n\t\t\t\tpattern.expression.source !== pattern.source ||\n\t\t\t\tpattern.expression.flags !== pattern.flags\n\t\t\t)\n\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\tfor (const node of nodes) {\n\t\t\tconst keys = node.keys\n\t\t\tif (node.exactKeys && Reflect.ownKeys(node.object).length !== keys.length)\n\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\t\t// Regex checks may update lastIndex while parsing; pattern identity is checked above.\n\t\t\t\tif (node.object instanceof RegExp && keys[i] === 'lastIndex') continue\n\t\t\t\tconst current = Object.getOwnPropertyDescriptor(node.object, keys[i])\n\t\t\t\tconst before = node.values[i]\n\t\t\t\tif (\n\t\t\t\t\t!current ||\n\t\t\t\t\tcurrent.value !== before.value ||\n\t\t\t\t\tcurrent.get !== before.get ||\n\t\t\t\t\tcurrent.set !== before.set ||\n\t\t\t\t\tcurrent.enumerable !== before.enumerable\n\t\t\t\t)\n\t\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\t\t}\n\t\t}\n\t\tfor (const { definition, fields } of objectFields) {\n\t\t\tconst current = definition.shape\n\t\t\tconst keys = Reflect.ownKeys(fields)\n\t\t\tif (\n\t\t\t\tReflect.ownKeys(current).length !== keys.length ||\n\t\t\t\tkeys.some(\n\t\t\t\t\t(key) =>\n\t\t\t\t\t\tObject.getOwnPropertyDescriptor(current, key)?.value !==\n\t\t\t\t\t\tObject.getOwnPropertyDescriptor(fields, key)?.value,\n\t\t\t\t)\n\t\t\t)\n\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\t}\n\t}\n}\n\nfunction assertRegistryUnchanged(registry: RegistryMetadata): void {\n\tfor (const table of Object.values(registry.metadata)) table.assertUnchanged()\n}\n\ninterface GuardedDtoDefinition extends z.core.$ZodObjectDef {\n\tassertUnchanged(): void\n}\nconst GuardedDtoObject = z.core.$constructor<z.ZodObject>('GuardedDtoObject', (inst, def) => {\n\t// SAFETY: only guardedDto creates this definition; Zod clones retain its guard.\n\tconst guarded = def as GuardedDtoDefinition\n\tz.ZodObject.init(inst, def)\n\t// Run after Zod's own deferred initializers, including its memoizer wrapper.\n\t// Guarding parse alone is too late for first-run schema introspection.\n\tinst._zod.deferred ??= []\n\tinst._zod.deferred.push(() => {\n\t\tconst run = inst._zod.run\n\t\tinst._zod.run = (payload, ctx) => {\n\t\t\ttry {\n\t\t\t\tguarded.assertUnchanged()\n\t\t\t} catch {\n\t\t\t\tpayload.issues.push({\n\t\t\t\t\tcode: 'custom',\n\t\t\t\t\tinput: payload.value,\n\t\t\t\t\tmessage: 'DTO schema mutated after registry construction',\n\t\t\t\t})\n\t\t\t\treturn payload\n\t\t\t}\n\t\t\treturn run(payload, ctx)\n\t\t}\n\t})\n})\nfunction guardedDto(dto: z.ZodObject, assertUnchanged: () => void): z.ZodObject {\n\tconst outputFields = dto.shape\n\tconst def: GuardedDtoDefinition = { ...dto._zod.def, assertUnchanged }\n\tObject.defineProperty(def, 'shape', {\n\t\tenumerable: true,\n\t\tconfigurable: true,\n\t\tget: () => {\n\t\t\tassertUnchanged()\n\t\t\treturn outputFields\n\t\t},\n\t})\n\treturn new GuardedDtoObject(def)\n}\n\nfunction snapshotMetadata(registry: RegistryMetadata): RelationMetadata {\n\tconst tables: Record<string, RelationTableMetadata> = {}\n\tfor (const [name, data] of Object.entries(registry.metadata)) {\n\t\tconst fields: Record<string, RelationFieldMetadata> = {}\n\t\tfor (const [field, validator] of Object.entries(data.fields))\n\t\t\tfields[field] = Object.freeze({\n\t\t\t\tkind: validator.kind,\n\t\t\t\toptional: validator.isOptional === 'optional',\n\t\t\t\tnullable: hasNull(validator),\n\t\t\t\tidTables: Object.freeze(idTables(validator)),\n\t\t\t\tnative: frozenNative(nativeJson(validator)),\n\t\t\t})\n\t\ttables[name] = Object.freeze({\n\t\t\tfields: Object.freeze(fields),\n\t\t\tindexes: Object.freeze(\n\t\t\t\tdata.indexes.map((index) =>\n\t\t\t\t\tObject.freeze({ name: index.name, fields: Object.freeze([...index.fields]) }),\n\t\t\t\t),\n\t\t\t),\n\t\t})\n\t}\n\treturn Object.freeze({ tables: Object.freeze(tables), declarations: registry.declarations })\n}\nfunction hasNull(validator: GenericValidator): boolean {\n\treturn (\n\t\tvalidator.kind === 'null' || (validator.kind === 'union' && validator.members.some(hasNull))\n\t)\n}\nfunction idTables(validator: GenericValidator): string[] {\n\treturn validator.kind === 'id'\n\t\t? [validator.tableName]\n\t\t: validator.kind === 'union'\n\t\t\t? [...new Set(validator.members.flatMap(idTables))].sort()\n\t\t\t: []\n}\nfunction frozenNative(native: ValidatorJSON): Readonly<ValidatorJSON> {\n\tconst snapshot = structuredClone(native)\n\tconst pending: JSONValue[] = [snapshot]\n\twhile (pending.length) {\n\t\tconst next = pending.pop()!\n\t\tif (Object(next) !== next) continue\n\t\t// SAFETY: native JSON metadata contains ordinary arrays/objects; the boxing test excludes primitives.\n\t\tconst container = next as JSONValue[] | { [key: string]: JSONValue }\n\t\tObject.freeze(container)\n\t\tpending.push(...Object.values(container))\n\t}\n\treturn snapshot\n}\n"],"mappings":"8QAwCA,MAAMA,EAA4B,OAAO,eAAe,EAClDC,EAA2B,OAAO,cAAc,EAwThDC,kBAAa,IAAI,QAEjBC,kBAAqB,IAAI,QASzBC,kBAAO,IAAI,QACXC,kBAAc,IAAI,QAClBC,kBAAW,IAAI,QACfC,EAAcC,EAAE,OAAO,CAC5B,QAASA,EAAE,MAAMA,EAAE,OAAO,CAAE,gBAAiBA,EAAE,OAAO,EAAG,OAAQA,EAAE,MAAMA,EAAE,OAAO,CAAC,CAAE,CAAC,CAAC,CACxF,CAAC,EAGD,SAAgBC,EAIf,EACA,EAC8B,CAC9B,IAAMC,EAA0C,CAAC,EAC3CC,kBAAU,IAAI,IACdC,EAAmD,CAAC,EACpDC,EAAiE,CAAC,EAClEC,EAAkE,CAAC,EACzE,IAAK,GAAM,CAACC,EAAMC,KAAU,OAAO,QAAQC,CAAM,EAAG,CACnD,GACCD,EAAM,YAAcD,GACpB,CAAC,MAAO,OAAQ,YAAa,cAAe,WAAW,CAAC,CAAC,SAASA,CAAI,EAEtE,MAAU,MAAM,2BAA2BA,EAAK,EAAE,EACnD,IAAMG,EAAMF,EAAM,IAAI,EAAI,EAC1B,GAAI,EAAEE,aAAeV,EAAE,WAAY,MAAU,MAAM,qBAAqB,EACxE,IAAMW,EAAkBC,EAAiBF,CAAG,EACtCG,EAASC,EAAYN,EAAM,MAAM,EACjCO,EAAWC,EAAkBR,EAAM,KAAK,EA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AQiH,EAAapH,EAAWG,CAAS,CAAC,CAC3C,CAAC,EACF,EAAOnB,GAAQ,OAAO,OAAO,CAC5B,OAAQ,OAAO,OAAOH,CAAM,EAC5B,QAAS,OAAO,OACfkD,EAAK,QAAQ,IAAK,GACjB,OAAO,OAAO,CAAE,KAAMlC,EAAM,KAAM,OAAQ,OAAO,OAAO,CAAC,GAAGA,EAAM,MAAM,CAAC,CAAE,CAAC,CAC7E,CACD,CACD,CAAC,CACF,CACA,OAAO,OAAO,OAAO,CAAE,OAAQ,OAAO,OAAOX,CAAM,EAAG,aAAcgD,EAAS,YAAa,CAAC,CAC5F,CACA,SAASgF,EAAQ,EAAsC,CACtD,OACC/G,EAAU,OAAS,QAAWA,EAAU,OAAS,SAAWA,EAAU,QAAQ,KAAK+G,CAAO,CAE5F,CACA,SAASC,EAAS,EAAuC,CACxD,OAAOhH,EAAU,OAAS,KACvB,CAACA,EAAU,SAAS,EACpBA,EAAU,OAAS,QAClB,CAAC,GAAG,IAAI,IAAIA,EAAU,QAAQ,QAAQgH,CAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,EACvD,CAAC,CACN,CACA,SAASC,EAAa,EAAgD,CACrE,IAAMC,EAAW,gBAAgB/H,CAAM,EACjCmE,EAAuB,CAAC4D,CAAQ,EACtC,KAAO5D,EAAQ,QAAQ,CACtB,IAAM6D,EAAO7D,EAAQ,IAAI,EACzB,GAAI,OAAO6D,CAAI,IAAMA,EAAM,SAE3B,IAAMxC,EAAYwC,EAClB,OAAO,OAAOxC,CAAS,EACvBrB,EAAQ,KAAK,GAAG,OAAO,OAAOqB,CAAS,CAAC,CACzC,CACA,OAAOuC,CACR"}
1
+ {"version":3,"file":"relations.mjs","names":["fieldBrand","edgeBrand","registries","executorRegistries","refs","throughRefs","edgeRefs","indexExport","z","defineRelations","metadata","ownRefs","fields","one","many","name","table","tables","dto","assertUnchanged","dtoMutationGuard","native","zodToConvex","exported","nativeTableExport","assertNativeValidation","assertStoredDocument","indexes","index","a","b","nativeJson","tableRefs","field","validator","ref","junctionValidator","junction","compatible","result","makeEdge","spec","supplied","declare","declarations","edges","source","edge","base","validatePath","internal","helpers","assertRegistryUnchanged","assertSelection","selection","guardedDto","nativePagination","relationFilterSchema","filterGrammar","paginated","rowSchema","identity","data","canonicalNative","serializeLeg","registry","snapshotMetadata","ownedTables","storedGuards","schemaGuard","schemaMutationGuard","nativeSnapshot","guard","definition","getRelationExecutor","owned","assertSchema","names","scalarFields","grammar","scalarValidator","scalarPredicateSchema","depth","declaration","eligible","row","key","getRelationMetadata","pending","nodes","ancestors","value","prototype","keys","property","leg","own","target","leftDomain","domain","left","right","from","to","requireIndex","relations","mask","relationFields","object","container","child","stored","_id","_creationTime","v","document","objectAccessorSample","builtinObjectGetters","getter","schema","builtinDefaultGetter","builtinPrefaultGetter","fallbackDefinitions","patterns","seen","active","objectFields","visit","boundary","def","descriptor","publicFields","values","i","pattern","node","current","before","GuardedDtoObject","guarded","inst","run","payload","ctx","outputFields","hasNull","idTables","frozenNative","snapshot","next"],"sources":["../src/relations.ts"],"sourcesContent":["import { z } from 'zod'\nimport { zodToConvex, type ConvexValidatorFromZod } from 'convex-helpers/server/zod4'\nimport type { TableDefinition, PaginationResult } from 'convex/server'\nimport {\n\tv,\n\ttype GenericId,\n\ttype GenericValidator,\n\ttype ValidatorJSON,\n\ttype JSONValue,\n} from 'convex/values'\nimport { paginated } from './zod-table'\nimport {\n\tnativePagination,\n\trelationFilterSchema,\n\tscalarPredicateSchema,\n\ttype FieldSchemas,\n\ttype FilterGrammar,\n\ttype PaginationOptionsSchema,\n\ttype ScalarSelection,\n\ttype ScalarPredicate,\n} from './querying/filters'\n\n/** Object table boundaries only. Runtime metadata comes from native validators/export. */\nexport interface RelationTable {\n\ttableName: string\n\tschema: z.ZodObject\n\tpublicDto: z.ZodObject\n\ttable: TableDefinition\n\tdto(includeId: true): z.ZodType\n}\nexport type RelationTables = Record<string, RelationTable>\n/** Internal named declaration dependency; excluded from the supported relations facade. */\nexport interface SelectionMask {\n\t[key: string]: true | SelectionMask\n}\ntype Fields<T extends RelationTable> = T['schema']['shape']\ntype Native<T extends RelationTable, K extends keyof Fields<T>> = ConvexValidatorFromZod<\n\tFields<T>[K],\n\t'required'\n>['type']\nconst fieldBrand: unique symbol = Symbol('relationField')\nconst edgeBrand: unique symbol = Symbol('relationEdge')\ntype Comparable<Value> = Exclude<Value, null | undefined>\ntype Same<A, B> = [Comparable<A> | Comparable<B>] extends [never]\n\t? false\n\t: [Comparable<A>] extends [Comparable<B>]\n\t\t? [Comparable<B>] extends [Comparable<A>]\n\t\t\t? true\n\t\t\t: false\n\t\t: false\n\nexport interface FieldReference<Table extends string, Field extends string, Value> {\n\treadonly [fieldBrand]: Value\n\treadonly table: Table\n\treadonly field: Field\n\tthrough<J extends AnyField>(\n\t\tjunction: J & (Same<Value, J[typeof fieldBrand]> extends true ? {} : never),\n\t): ThroughReference<Table, Field, Value, J>\n}\ntype AnyField = FieldReference<string, string, any>\nexport interface ThroughReference<\n\tTable extends string,\n\tField extends string,\n\tValue,\n\tJunction extends AnyField,\n> {\n\treadonly base: FieldReference<Table, Field, Value>\n\treadonly junction: Junction\n}\ntype AnyThrough = ThroughReference<string, string, any, AnyField>\ntype Leg = AnyField | AnyThrough\ntype Base<L extends Leg> = L extends AnyThrough ? L['base'] : L\ntype JunctionTable<L extends Leg> = L extends AnyThrough ? L['junction']['table'] : never\ntype CompatibleLegs<F extends Leg, T extends Leg> = F extends AnyThrough\n\t? T extends AnyThrough\n\t\t? Same<JunctionTable<F>, JunctionTable<T>> extends true\n\t\t\t? {}\n\t\t\t: never\n\t\t: never\n\t: T extends AnyThrough\n\t\t? never\n\t\t: Same<Base<F>[typeof fieldBrand], Base<T>[typeof fieldBrand]> extends true\n\t\t\t? {}\n\t\t\t: never\n\nexport interface RelationEdge<\n\tSource extends string,\n\tTarget extends string,\n\tCardinality extends 'one' | 'many',\n> {\n\treadonly [edgeBrand]: true\n\treadonly source: Source\n\treadonly target: Target\n\treadonly cardinality: Cardinality\n\treadonly from: Leg\n\treadonly to: Leg\n}\ntype AnyEdge = RelationEdge<string, string, 'one' | 'many'>\ntype FieldReferences<Tables extends RelationTables> = {\n\t[T in keyof Tables]: {\n\t\t[K in Extract<keyof Fields<Tables[T]>, string>]: FieldReference<\n\t\t\tExtract<T, string>,\n\t\t\tK,\n\t\t\tNative<Tables[T], K>\n\t\t>\n\t}\n}\ntype AllReferences<Tables extends RelationTables> = {\n\t[T in keyof Tables]: FieldReferences<Tables>[T][Extract<keyof FieldReferences<Tables>[T], string>]\n}[keyof Tables]\ntype Builders<Tables extends RelationTables, C extends 'one' | 'many'> = {\n\t[T in keyof Tables]: <\n\t\tF extends AllReferences<Tables> | AnyThrough,\n\t\tTo extends\n\t\t\t| FieldReferences<Tables>[T][Extract<keyof FieldReferences<Tables>[T], string>]\n\t\t\t| AnyThrough,\n\t>(\n\t\tspec: { from: F; to: To } & CompatibleLegs<F, To> & (Base<To>['table'] extends T ? {} : never),\n\t) => RelationEdge<Base<F>['table'], Extract<T, string>, C>\n}\nexport type RelationBuilder<Tables extends RelationTables> = FieldReferences<Tables> & {\n\tone: Builders<Tables, 'one'>\n\tmany: Builders<Tables, 'many'>\n}\nexport type RelationDeclarations = Record<string, Record<string, AnyEdge>>\ntype ExactDeclarations<Tables extends RelationTables, D extends RelationDeclarations> = {\n\t[T in keyof D]: T extends keyof Tables\n\t\t? {\n\t\t\t\t[K in keyof D[T]]: K extends\n\t\t\t\t\t| keyof Fields<Tables[T]>\n\t\t\t\t\t| 'and'\n\t\t\t\t\t| 'or'\n\t\t\t\t\t| 'not'\n\t\t\t\t\t| 'fields'\n\t\t\t\t\t| 'then'\n\t\t\t\t\t| '__proto__'\n\t\t\t\t\t| 'constructor'\n\t\t\t\t\t| 'prototype'\n\t\t\t\t\t? never\n\t\t\t\t\t: D[T][K]['source'] extends T\n\t\t\t\t\t\t? D[T][K]\n\t\t\t\t\t\t: never\n\t\t\t}\n\t\t: never\n}\ntype Depth = readonly [1, 1, 1, 1]\ntype Tail<D extends readonly 1[]> = D extends readonly [1, ...infer R extends 1[]] ? R : []\ntype Edges<D, T> = T extends keyof D ? D[T] : {}\ntype Target<E> = E extends AnyEdge ? E['target'] : never\nexport type RelationSelection<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tLevel extends readonly 1[] = Depth,\n> = Level extends readonly []\n\t? Record<string, never>\n\t: {\n\t\t\t[K in keyof Edges<D, T>]?:\n\t\t\t\t| true\n\t\t\t\t| RelationSelection<Tables, D, Extract<Target<Edges<D, T>[K]>, keyof Tables>, Tail<Level>>\n\t\t}\nexport type RelationFilterSelection<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tLevel extends readonly 1[] = Depth,\n> = ScalarSelection<Fields<Tables[T]>> &\n\t(Level extends readonly []\n\t\t? {}\n\t\t: {\n\t\t\t\t[K in keyof Edges<D, T>]?: RelationFilterSelection<\n\t\t\t\t\tTables,\n\t\t\t\t\tD,\n\t\t\t\t\tExtract<Target<Edges<D, T>[K]>, keyof Tables>,\n\t\t\t\t\tTail<Level>\n\t\t\t\t>\n\t\t\t})\ntype ExactOutputMask<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tL extends readonly 1[] = Depth,\n> = S & {\n\t[K in keyof S]: L extends readonly []\n\t\t? never\n\t\t: K extends keyof Edges<D, T>\n\t\t\t? S[K] extends true\n\t\t\t\t? true\n\t\t\t\t: ExactOutputMask<Tables, D, Extract<Target<Edges<D, T>[K]>, keyof Tables>, S[K], Tail<L>>\n\t\t\t: never\n}\ntype ExactFilterMask<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tL extends readonly 1[] = Depth,\n> = S & {\n\t[K in keyof S]: K extends keyof ScalarSelection<Fields<Tables[T]>>\n\t\t? S[K] extends true\n\t\t\t? true\n\t\t\t: never\n\t\t: L extends readonly []\n\t\t\t? never\n\t\t\t: K extends keyof Edges<D, T>\n\t\t\t\t? S[K] extends true\n\t\t\t\t\t? never\n\t\t\t\t\t: ExactFilterMask<Tables, D, Extract<Target<Edges<D, T>[K]>, keyof Tables>, S[K], Tail<L>>\n\t\t\t\t: never\n}\ntype ScalarValues<T extends RelationTable, S, Side extends 'input' | 'output'> = {\n\t[K in Extract<keyof S, keyof Fields<T>>]?: ScalarPredicate<Fields<T>[K], Side>\n}\nexport type RelationFilters<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tSide extends 'input' | 'output' = 'output',\n\tLevel extends readonly 1[] = Depth,\n> = Omit<ScalarValues<Tables[T], S, Side>, 'and' | 'or' | 'not' | 'fields'> & {\n\tfields?: ScalarValues<Tables[T], S, Side>\n} & (Level extends readonly []\n\t\t? {}\n\t\t: {\n\t\t\t\t[K in Extract<keyof S, keyof Edges<D, T>>]?: Edges<D, T>[K] extends RelationEdge<\n\t\t\t\t\tstring,\n\t\t\t\t\tinfer Target,\n\t\t\t\t\tinfer C\n\t\t\t\t>\n\t\t\t\t\t? C extends 'one'\n\t\t\t\t\t\t? RelationFilters<\n\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\tS[K],\n\t\t\t\t\t\t\t\tSide,\n\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t> | null\n\t\t\t\t\t\t:\n\t\t\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\t\t\tsome: RelationFilters<\n\t\t\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\t\t\tS[K],\n\t\t\t\t\t\t\t\t\t\t\tSide,\n\t\t\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\t\t\tnone: RelationFilters<\n\t\t\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\t\t\tS[K],\n\t\t\t\t\t\t\t\t\t\t\tSide,\n\t\t\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t: never\n\t\t\t} & {\n\t\t\t\tand?: RelationFilters<Tables, D, T, S, Side, Tail<Level>>[]\n\t\t\t\tor?: RelationFilters<Tables, D, T, S, Side, Tail<Level>>[]\n\t\t\t\tnot?: RelationFilters<Tables, D, T, S, Side, Tail<Level>>\n\t\t\t})\ntype PublicRow<T extends RelationTable> = z.output<T['publicDto']> & {\n\t_id: GenericId<T['tableName']>\n}\nexport type RelationRow<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS,\n\tLevel extends readonly 1[] = Depth,\n> = PublicRow<Tables[T]> &\n\t(Level extends readonly []\n\t\t? {}\n\t\t: {\n\t\t\t\t[K in keyof S]: K extends keyof Edges<D, T>\n\t\t\t\t\t? Edges<D, T>[K] extends RelationEdge<string, infer Target, infer C>\n\t\t\t\t\t\t? C extends 'one'\n\t\t\t\t\t\t\t? RelationRow<\n\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\tS[K] extends true ? {} : S[K],\n\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t> | null\n\t\t\t\t\t\t\t: PaginationResult<\n\t\t\t\t\t\t\t\t\tRelationRow<\n\t\t\t\t\t\t\t\t\t\tTables,\n\t\t\t\t\t\t\t\t\t\tD,\n\t\t\t\t\t\t\t\t\t\tExtract<Target, keyof Tables>,\n\t\t\t\t\t\t\t\t\t\tS[K] extends true ? {} : S[K],\n\t\t\t\t\t\t\t\t\t\tTail<Level>\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t: never\n\t\t\t\t\t: never\n\t\t\t})\nexport interface RelationTableHelpers<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n> {\n\tpaginationOpts<const S extends RelationFilterSelection<Tables, D, T>>(\n\t\tselection: ExactFilterMask<Tables, D, T, S>,\n\t): PaginationOptionsSchema<\n\t\tRelationFilters<Tables, D, T, S>,\n\t\tRelationFilters<Tables, D, T, S, 'input'>\n\t>\n\tpageDto<const S extends RelationSelection<Tables, D, T>>(\n\t\tselection: ExactOutputMask<Tables, D, T, S>,\n\t): z.ZodType<PaginationResult<RelationRow<Tables, D, T, S>>>\n}\nexport interface RelationRegistryIdentity {\n\treadonly identity: string\n\treadonly version: 1\n}\nexport interface RelationRegistry<Tables extends RelationTables, D extends RelationDeclarations> {\n\treadonly identity: string\n\treadonly version: 1\n\treadonly tables: { [T in keyof Tables]: RelationTableHelpers<Tables, D, T> }\n}\n\nexport interface RelationFieldMetadata {\n\treadonly kind: string\n\treadonly optional: boolean\n\treadonly nullable: boolean\n\treadonly idTables: readonly string[]\n\treadonly native: Readonly<ValidatorJSON>\n}\nexport interface RelationTableMetadata {\n\treadonly fields: Readonly<Record<string, RelationFieldMetadata>>\n\treadonly indexes: readonly { readonly name: string; readonly fields: readonly string[] }[]\n}\ninterface TableMetadata {\n\tfields: Record<string, GenericValidator>\n\tindexes: { name: string; fields: string[] }[]\n\tdto: z.ZodObject\n\tassertUnchanged(): void\n}\ninterface RegistryMetadata {\n\tmetadata: Record<string, TableMetadata>\n\tdeclarations: RelationDeclarations\n}\nexport interface RelationMetadata {\n\treadonly tables: Readonly<Record<string, RelationTableMetadata>>\n\treadonly declarations: Readonly<Record<string, Readonly<Record<string, AnyEdge>>>>\n}\n\nconst registries = new WeakMap<RelationRegistryIdentity, RelationMetadata>()\n// Private executor state retains validators and projection, never a database reader.\nconst executorRegistries = new WeakMap<\n\tRelationRegistryIdentity,\n\t{\n\t\tinternal: RegistryMetadata\n\t\ttables: RelationTables\n\t\tassertStoredUnchanged(): void\n\t\tassertNativeTable(table: string, definition: TableDefinition): void\n\t}\n>()\nconst refs = new WeakMap<AnyField, GenericValidator>()\nconst throughRefs = new WeakSet<AnyThrough>()\nconst edgeRefs = new WeakSet<AnyEdge>()\nconst indexExport = z.object({\n\tindexes: z.array(z.object({ indexDescriptor: z.string(), fields: z.array(z.string()) })),\n})\n\n/** No ID-field inference and no readers: this registry owns declarations/schema helpers only. */\nexport function defineRelations<\n\tconst Tables extends RelationTables,\n\tconst D extends RelationDeclarations,\n>(\n\ttables: Tables,\n\tdeclare: (r: RelationBuilder<Tables>) => D & ExactDeclarations<Tables, D>,\n): RelationRegistry<Tables, D> {\n\tconst metadata: Record<string, TableMetadata> = {}\n\tconst ownRefs = new Set<AnyField>()\n\tconst fields: Record<string, Record<string, AnyField>> = {}\n\tconst one: Record<string, (spec: { from: Leg; to: Leg }) => AnyEdge> = {}\n\tconst many: Record<string, (spec: { from: Leg; to: Leg }) => AnyEdge> = {}\n\tfor (const [name, table] of Object.entries(tables)) {\n\t\tif (table.schema instanceof z.ZodUnion)\n\t\t\tthrow new Error('Variant tables are not supported by relations or the query engine')\n\t\tif (\n\t\t\ttable.tableName !== name ||\n\t\t\t['one', 'many', '__proto__', 'constructor', 'prototype'].includes(name)\n\t\t)\n\t\t\tthrow new Error(`Invalid relation table \"${name}\"`)\n\t\tconst dto = table.dto(true)\n\t\tif (!(dto instanceof z.ZodObject)) throw new Error('Expected object DTO')\n\t\tconst assertUnchanged = dtoMutationGuard(dto)\n\t\tconst native = zodToConvex(table.schema)\n\t\tconst exported = nativeTableExport(table.table)\n\t\tassertNativeValidation(exported.documentType)\n\t\tassertStoredDocument(name, native.fields, exported.documentType)\n\t\tconst indexes = indexExport\n\t\t\t.parse(exported)\n\t\t\t.indexes.map((index) => ({ name: index.indexDescriptor, fields: [...index.fields] }))\n\t\t\t.sort((a, b) => a.name.localeCompare(b.name))\n\t\tassertNativeValidation(nativeJson(zodToConvex(dto)))\n\t\tmetadata[name] = { fields: native.fields, indexes, dto, assertUnchanged }\n\t\tconst tableRefs: Record<string, AnyField> = Object.create(null)\n\t\tfor (const [field, validator] of Object.entries(native.fields)) {\n\t\t\tconst ref: AnyField = {\n\t\t\t\t[fieldBrand]: undefined,\n\t\t\t\ttable: name,\n\t\t\t\tfield,\n\t\t\t\tthrough<J extends AnyField>(\n\t\t\t\t\tjunction: J & (Same<any, J[typeof fieldBrand]> extends true ? {} : never),\n\t\t\t\t): ThroughReference<string, string, any, J> {\n\t\t\t\t\tconst junctionValidator = refs.get(junction)\n\t\t\t\t\tif (\n\t\t\t\t\t\t!ownRefs.has(junction) ||\n\t\t\t\t\t\t!junctionValidator ||\n\t\t\t\t\t\t!compatible(validator, junctionValidator)\n\t\t\t\t\t)\n\t\t\t\t\t\tthrow new Error('Invalid junction field or registry')\n\t\t\t\t\tconst result = Object.freeze({ base: ref, junction })\n\t\t\t\t\tthroughRefs.add(result)\n\t\t\t\t\treturn result\n\t\t\t\t},\n\t\t\t}\n\t\t\trefs.set(ref, validator)\n\t\t\townRefs.add(ref)\n\t\t\ttableRefs[field] = Object.freeze(ref)\n\t\t}\n\t\tfields[name] = Object.freeze(tableRefs)\n\t\tone[name] = (spec) => makeEdge(name, 'one', spec, ownRefs)\n\t\tmany[name] = (spec) => makeEdge(name, 'many', spec, ownRefs)\n\t}\n\t// SAFETY: every native field has a branded reference; mapped builders retain the literal names.\n\tconst builder = Object.freeze({\n\t\t...fields,\n\t\tone: Object.freeze(one),\n\t\tmany: Object.freeze(many),\n\t}) as RelationBuilder<Tables>\n\tconst supplied = declare(builder)\n\tconst declarations: RelationDeclarations = Object.fromEntries(\n\t\tObject.entries(supplied).map(([name, edges]) => [name, Object.freeze({ ...edges })]),\n\t)\n\tObject.freeze(declarations)\n\tfor (const [source, edges] of Object.entries(declarations)) {\n\t\tif (!Object.hasOwn(tables, source)) throw new Error(`Unknown source table \"${source}\"`)\n\t\tfor (const [name, edge] of Object.entries(edges)) {\n\t\t\tif (\n\t\t\t\tObject.hasOwn(metadata[source].fields, name) ||\n\t\t\t\t['and', 'or', 'not', 'fields', 'then', '__proto__', 'constructor', 'prototype'].includes(\n\t\t\t\t\tname,\n\t\t\t\t)\n\t\t\t)\n\t\t\t\tthrow new Error(`Relation name collision \"${name}\"`)\n\t\t\tif (\n\t\t\t\t!edgeRefs.has(edge) ||\n\t\t\t\t!ownRefs.has(base(edge.from)) ||\n\t\t\t\t!ownRefs.has(base(edge.to)) ||\n\t\t\t\tedge.source !== source\n\t\t\t)\n\t\t\t\tthrow new Error('Relation source bucket mismatch')\n\t\t\tvalidatePath(edge, metadata)\n\t\t}\n\t}\n\tconst internal: RegistryMetadata = { metadata, declarations }\n\tconst helpers: Record<\n\t\tstring,\n\t\t{\n\t\t\tpaginationOpts(selection: SelectionMask): z.ZodType\n\t\t\tpageDto(selection: SelectionMask): z.ZodType\n\t\t}\n\t> = {}\n\tfor (const table of Object.keys(tables))\n\t\thelpers[table] = {\n\t\t\tpaginationOpts(selection: SelectionMask): z.ZodType {\n\t\t\t\tassertRegistryUnchanged(internal)\n\t\t\t\tassertSelection(selection)\n\t\t\t\treturn guardedDto(\n\t\t\t\t\tnativePagination.extend({\n\t\t\t\t\t\tfilters: relationFilterSchema(filterGrammar(internal, table, selection)).optional(),\n\t\t\t\t\t\torder: z.enum(['asc', 'desc']).optional(),\n\t\t\t\t\t}),\n\t\t\t\t\t() => assertRegistryUnchanged(internal),\n\t\t\t\t)\n\t\t\t},\n\t\t\tpageDto(selection: SelectionMask): z.ZodType {\n\t\t\t\tassertRegistryUnchanged(internal)\n\t\t\t\tassertSelection(selection)\n\t\t\t\treturn guardedDto(paginated(rowSchema(internal, table, selection)).result, () =>\n\t\t\t\t\tassertRegistryUnchanged(internal),\n\t\t\t\t)\n\t\t\t},\n\t\t}\n\t// Stable, collision-free canonical metadata encoding. It is an identity, not a secret/hash.\n\tconst identity = JSON.stringify([\n\t\t1,\n\t\tObject.entries(metadata)\n\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t.map(([name, data]) => [\n\t\t\t\tname,\n\t\t\t\tObject.entries(data.fields)\n\t\t\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t\t\t.map(([field, validator]) => [field, validator.isOptional, nativeJson(validator)]),\n\t\t\t\tdata.indexes,\n\t\t\t\tcanonicalNative(nativeJson(zodToConvex(data.dto))),\n\t\t\t]),\n\t\tObject.entries(declarations)\n\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t.map(([source, edges]) => [\n\t\t\t\tsource,\n\t\t\t\tObject.entries(edges)\n\t\t\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t\t\t.map(([name, edge]) => [\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tedge.cardinality,\n\t\t\t\t\t\tedge.target,\n\t\t\t\t\t\tserializeLeg(edge.from),\n\t\t\t\t\t\tserializeLeg(edge.to),\n\t\t\t\t\t]),\n\t\t\t]),\n\t])\n\t// SAFETY: helpers are assembled for every validated literal table and mask; schemas derive matching native fields/public DTOs.\n\tconst registry = Object.freeze({\n\t\tidentity,\n\t\tversion: 1,\n\t\ttables: Object.freeze(helpers),\n\t}) as RelationRegistry<Tables, D>\n\tregistries.set(registry, snapshotMetadata(internal))\n\tconst ownedTables = Object.freeze({ ...tables })\n\tconst storedGuards = Object.fromEntries(\n\t\tObject.entries(ownedTables).map(([name, table]) => {\n\t\t\tconst schemaGuard = schemaMutationGuard(table.schema, 'stored')\n\t\t\tconst nativeSnapshot = JSON.stringify(nativeTableExport(table.table))\n\t\t\treturn [\n\t\t\t\tname,\n\t\t\t\t() => {\n\t\t\t\t\tschemaGuard()\n\t\t\t\t\tif (JSON.stringify(nativeTableExport(table.table)) !== nativeSnapshot)\n\t\t\t\t\t\tthrow new Error(`Native schema mutated after registry construction: ${name}`)\n\t\t\t\t},\n\t\t\t]\n\t\t}),\n\t)\n\texecutorRegistries.set(registry, {\n\t\tinternal,\n\t\ttables: ownedTables,\n\t\tassertStoredUnchanged: () => {\n\t\t\tfor (const guard of Object.values(storedGuards)) guard()\n\t\t},\n\t\tassertNativeTable(table, definition) {\n\t\t\tif (!Object.hasOwn(ownedTables, table) || ownedTables[table].table !== definition)\n\t\t\t\tthrow new Error('Native schema/table ownership disagrees with relation registry')\n\t\t\tstoredGuards[table]()\n\t\t},\n\t})\n\treturn registry\n}\n\n/** Internal owned executor bridge. Not an advertised root API. Every operation\n * rechecks the live registry, including cached plans and individual projections.\n */\nexport function getRelationExecutor(registry: RelationRegistryIdentity) {\n\tconst owned = executorRegistries.get(registry)\n\tif (!owned) throw new Error('Unknown relation registry')\n\tconst assertUnchanged = () => {\n\t\tassertRegistryUnchanged(owned.internal)\n\t\towned.assertStoredUnchanged()\n\t}\n\tconst assertSchema = (tables: Readonly<Record<string, TableDefinition>>) => {\n\t\tassertUnchanged()\n\t\tconst names = Object.keys(owned.tables)\n\t\tif (\n\t\t\tnames.length !== Object.keys(tables).length ||\n\t\t\tnames.some((name) => tables[name] !== owned.tables[name].table)\n\t\t)\n\t\t\tthrow new Error('Native schema/table ownership disagrees with relation registry')\n\t}\n\tassertUnchanged()\n\treturn Object.freeze({\n\t\tassertUnchanged,\n\t\tassertNativeTable: owned.assertNativeTable,\n\t\tassertSchema,\n\t\tpagination(table: string) {\n\t\t\tassertUnchanged()\n\t\t\tconst internal = owned.internal\n\t\t\tconst scalarFields = new Map<string, FieldSchemas>()\n\t\t\tfunction grammar(name: string, depth: number): FilterGrammar {\n\t\t\t\tconst data = internal.metadata[name]\n\t\t\t\tif (!data) throw new Error('Unknown query table')\n\t\t\t\tlet fields = scalarFields.get(name)\n\t\t\t\tif (!fields) {\n\t\t\t\t\tfields = {}\n\t\t\t\t\tfor (const [name, validator] of Object.entries(data.fields)) {\n\t\t\t\t\t\t// Unsupported stored containers are not scalar-queryable. No validators\n\t\t\t\t\t\t// are reconstructed from JSON; these are the actual native validators.\n\t\t\t\t\t\tif (scalarValidator(validator))\n\t\t\t\t\t\t\tfields[name] = scalarPredicateSchema(validator).optional()\n\t\t\t\t\t}\n\t\t\t\t\tscalarFields.set(name, fields)\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tfields,\n\t\t\t\t\trelations:\n\t\t\t\t\t\tdepth >= 4\n\t\t\t\t\t\t\t? {}\n\t\t\t\t\t\t\t: Object.fromEntries(\n\t\t\t\t\t\t\t\t\tObject.entries(internal.declarations[name] ?? {}).map(([edge, declaration]) => [\n\t\t\t\t\t\t\t\t\t\tedge,\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tcardinality: declaration.cardinality,\n\t\t\t\t\t\t\t\t\t\t\ttarget: grammar(declaration.target, depth + 1),\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nativePagination.extend({\n\t\t\t\tfilters: relationFilterSchema(grammar(table, 0)).optional(),\n\t\t\t\torder: z.enum(['asc', 'desc']).optional(),\n\t\t\t})\n\t\t},\n\t\tpage(table: string, selection: SelectionMask) {\n\t\t\tassertUnchanged()\n\t\t\tassertSelection(selection)\n\t\t\treturn paginated(rowSchema(owned.internal, table, selection)).result\n\t\t},\n\t\tproject(\n\t\t\ttables: Readonly<Record<string, TableDefinition>>,\n\t\t\ttable: string,\n\t\t\trow: Record<string, import('convex/values').Value>,\n\t\t\teligible: () => boolean,\n\t\t) {\n\t\t\tassertSchema(tables)\n\t\t\tif (!eligible()) return null\n\t\t\tconst dto = owned.internal.metadata[table]?.dto\n\t\t\tif (!dto) throw new Error('Unknown query table')\n\t\t\t// The strict DTO remains the authority. Pick its fields before parsing,\n\t\t\t// matching table projection without spreading any storage-only fields.\n\t\t\treturn dto.parse(\n\t\t\t\tObject.fromEntries(\n\t\t\t\t\tObject.keys(dto.shape)\n\t\t\t\t\t\t.filter((key) => Object.hasOwn(row, key))\n\t\t\t\t\t\t.map((key) => [key, row[key]]),\n\t\t\t\t),\n\t\t\t)\n\t\t},\n\t})\n}\n/** Internal compiler/engine adapter. Registry identity alone cannot forge a registry. */\nexport function getRelationMetadata(registry: RelationRegistryIdentity): RelationMetadata {\n\tconst metadata = registries.get(registry)\n\tif (!metadata) throw new Error('Unknown relation registry')\n\treturn metadata\n}\n\nfunction scalarValidator(validator: GenericValidator): boolean {\n\treturn (\n\t\t['id', 'string', 'float64', 'int64', 'boolean', 'null', 'literal'].includes(validator.kind) ||\n\t\t(validator.kind === 'union' && validator.members.every(scalarValidator))\n\t)\n}\n\nfunction assertSelection(selection: SelectionMask): void {\n\tconst pending: { value: SelectionMask; depth: number; ancestors: readonly SelectionMask[] }[] = [\n\t\t{ value: selection, depth: 0, ancestors: [] },\n\t]\n\tlet nodes = 0\n\twhile (pending.length) {\n\t\tconst { value, depth, ancestors } = pending.pop()!\n\t\tif (++nodes > 256 || depth > 4 || ancestors.includes(value))\n\t\t\tthrow new Error('Selection cycle or depth/node budget exceeded')\n\t\tconst prototype = Object.getPrototypeOf(value)\n\t\tif (prototype !== Object.prototype && prototype !== null) throw new Error('Invalid selection')\n\t\tconst keys = Reflect.ownKeys(value)\n\t\tif (keys.length + nodes + pending.length > 256)\n\t\t\tthrow new Error('Selection node budget exceeded')\n\t\tfor (const key of keys) {\n\t\t\tconst property = Object.getOwnPropertyDescriptor(value, key)!\n\t\t\tif (!property.enumerable || !('value' in property) || String(key) !== key)\n\t\t\t\tthrow new Error('Invalid selection property')\n\t\t\tif (property.value !== true)\n\t\t\t\tpending.push({ value: property.value, depth: depth + 1, ancestors: [...ancestors, value] })\n\t\t\telse if (++nodes > 256) throw new Error('Selection node budget exceeded')\n\t\t}\n\t}\n}\nfunction base(leg: Leg): AnyField {\n\treturn 'base' in leg ? leg.base : leg\n}\nfunction makeEdge(\n\ttarget: string,\n\tcardinality: 'one' | 'many',\n\tspec: { from: Leg; to: Leg },\n\town: Set<AnyField>,\n): AnyEdge {\n\tfor (const leg of [spec.from, spec.to]) {\n\t\tif (!own.has(base(leg)) || ('base' in leg && (!throughRefs.has(leg) || !own.has(leg.junction))))\n\t\t\tthrow new Error('Foreign registry or invalid field reference')\n\t}\n\tif (base(spec.to).table !== target) throw new Error('Wrong relation target table')\n\tconst result: AnyEdge = Object.freeze({\n\t\t[edgeBrand]: true as const,\n\t\tsource: base(spec.from).table,\n\t\ttarget,\n\t\tcardinality,\n\t\tfrom: spec.from,\n\t\tto: spec.to,\n\t})\n\tedgeRefs.add(result)\n\treturn result\n}\nfunction compatible(left: GenericValidator, right: GenericValidator): boolean {\n\tconst leftDomain = domain(left)\n\treturn leftDomain.length > 0 && JSON.stringify(leftDomain) === JSON.stringify(domain(right))\n}\nfunction domain(validator: GenericValidator): string[] {\n\tif (validator.kind === 'union') return [...new Set(validator.members.flatMap(domain))].sort()\n\tif (validator.kind === 'null') return []\n\tif (!['id', 'string', 'float64', 'int64', 'boolean', 'literal'].includes(validator.kind))\n\t\tthrow new Error('Unsupported relation field kind')\n\treturn [JSON.stringify(nativeJson(validator))]\n}\nfunction validatePath(edge: AnyEdge, metadata: Record<string, TableMetadata>): void {\n\tconst from = edge.from\n\tconst to = edge.to\n\tif ('base' in from !== 'base' in to) throw new Error('Both relation legs must use through')\n\tif ('base' in from && 'base' in to) {\n\t\tif (from.junction.table !== to.junction.table) throw new Error('Different junction tables')\n\t\trequireIndex(metadata, from.junction.table, from.junction.field)\n\t\trequireIndex(metadata, edge.target, to.base.field)\n\t\treturn\n\t}\n\tif (!compatible(refs.get(base(from))!, refs.get(base(to))!))\n\t\tthrow new Error('Incompatible relation field types')\n\trequireIndex(metadata, edge.target, base(to).field)\n}\nfunction requireIndex(metadata: Record<string, TableMetadata>, table: string, field: string): void {\n\tif (field !== '_id' && !metadata[table].indexes.some((index) => index.fields[0] === field))\n\t\tthrow new Error(`No active index begins with \"${table}.${field}\"`)\n}\nfunction serializeLeg(leg: Leg): string[] {\n\treturn 'base' in leg\n\t\t? [leg.base.table, leg.base.field, leg.junction.table, leg.junction.field]\n\t\t: [leg.table, leg.field]\n}\nfunction filterGrammar(\n\tregistry: RegistryMetadata,\n\ttable: string,\n\tselection: SelectionMask,\n\tdepth: number = 0,\n): FilterGrammar {\n\tconst fields: FieldSchemas = {}\n\tconst relations: FilterGrammar['relations'] = {}\n\tfor (const [key, mask] of Object.entries(selection)) {\n\t\tconst validator = registry.metadata[table].fields[key]\n\t\tconst edge = registry.declarations[table]?.[key]\n\t\tif (mask === true && validator) fields[key] = scalarPredicateSchema(validator).optional()\n\t\telse if (mask !== true && edge && depth < 4)\n\t\t\trelations[key] = {\n\t\t\t\tcardinality: edge.cardinality,\n\t\t\t\ttarget: filterGrammar(registry, edge.target, mask, depth + 1),\n\t\t\t}\n\t\telse throw new Error(`Invalid relation filter selection \"${table}.${key}\"`)\n\t}\n\treturn { fields, relations }\n}\nfunction rowSchema(\n\tregistry: RegistryMetadata,\n\ttable: string,\n\tselection: SelectionMask,\n\tdepth: number = 0,\n): z.ZodType {\n\tconst relationFields: FieldSchemas = {}\n\tfor (const [name, mask] of Object.entries(selection)) {\n\t\tconst edge = registry.declarations[table]?.[name]\n\t\tif (!edge || depth >= 4) throw new Error(`Unknown or too deep relation \"${table}.${name}\"`)\n\t\tconst target =\n\t\t\tmask === true\n\t\t\t\t? registry.metadata[edge.target].dto\n\t\t\t\t: rowSchema(registry, edge.target, mask, depth + 1)\n\t\trelationFields[name] = edge.cardinality === 'one' ? target.nullable() : paginated(target).result\n\t}\n\t// Keep caller validations; guard the supported graph before parsing or native conversion.\n\tconst dto = registry.metadata[table].dto\n\tif (!(dto instanceof z.ZodObject)) throw new Error('Expected object DTO')\n\treturn guardedDto(dto.extend(relationFields).strict(), () => assertRegistryUnchanged(registry))\n}\n\ninterface NativeTableExport {\n\tindexes: { indexDescriptor: string; fields: string[] }[]\n\tdocumentType: ValidatorJSON\n}\nfunction nativeTableExport(table: TableDefinition): NativeTableExport {\n\t// SAFETY: Convex exposes export at runtime but strips its internal signature from declarations.\n\treturn (table as TableDefinition & { export(): NativeTableExport }).export()\n}\nfunction nativeJson(validator: GenericValidator): ValidatorJSON {\n\t// SAFETY: native validators expose converter metadata at runtime; Convex strips json from declarations.\n\treturn (validator as GenericValidator & { json: ValidatorJSON }).json\n}\n\nfunction canonicalNative(value: JSONValue): string {\n\tif (Array.isArray(value)) return `[${value.map(canonicalNative).join(',')}]`\n\tif (value !== null && Object(value) === value) {\n\t\t// SAFETY: native JSON contains only JSON objects, arrays and primitives.\n\t\tconst object = value as { [key: string]: JSONValue }\n\t\treturn `{${Object.keys(object)\n\t\t\t.sort()\n\t\t\t.map((key) => `${JSON.stringify(key)}:${canonicalNative(object[key])}`)\n\t\t\t.join(',')}}`\n\t}\n\treturn JSON.stringify(value)\n}\n\nfunction assertNativeValidation(value: JSONValue): void {\n\tif (value === null || Object(value) !== value) return\n\t// SAFETY: native converter metadata contains only JSON containers and primitives.\n\tconst container = value as { [key: string]: JSONValue }\n\tif (container.type === 'any') throw new Error('Unsupported native validation form')\n\tfor (const child of Object.values(container)) assertNativeValidation(child)\n}\n\nfunction assertStoredDocument(\n\ttable: string,\n\tfields: Record<string, GenericValidator>,\n\tdocument: ValidatorJSON,\n): void {\n\tconst { _id, _creationTime, ...stored } = fields\n\tif (\n\t\t!_id ||\n\t\t!_creationTime ||\n\t\tcanonicalNative(nativeJson(_id)) !== canonicalNative(nativeJson(v.id(table))) ||\n\t\tcanonicalNative(nativeJson(_creationTime)) !== canonicalNative(nativeJson(v.number())) ||\n\t\t_id.isOptional !== 'required' ||\n\t\t_creationTime.isOptional !== 'required' ||\n\t\tcanonicalNative(document) !== canonicalNative(nativeJson(v.object(stored)))\n\t)\n\t\tthrow new Error(`Native stored document disagrees with boundary \"${table}\"`)\n}\n\ninterface DtoGraphNode {\n\tobject: object\n\texactKeys: boolean\n\tkeys: readonly PropertyKey[]\n\tvalues: readonly PropertyDescriptor[]\n}\n\n// Compare against this peer's own built-in accessor, using the intrinsic rather\n// than a caller-provided toString. Older Zod clones retain this accessor even\n// after reading shape, because it caches on the original definition.\nconst objectAccessorSample = z.object({ field: z.string() })\nconst builtinObjectGetters = new Set(\n\t[\n\t\tobjectAccessorSample,\n\t\tobjectAccessorSample.pick({ field: true }),\n\t\tobjectAccessorSample.omit({ field: true }),\n\t\tobjectAccessorSample.extend({ other: z.string() }),\n\t\tobjectAccessorSample.safeExtend({ other: z.string() }),\n\t\tobjectAccessorSample.merge(z.object({ other: z.string() })),\n\t\tobjectAccessorSample.partial(),\n\t\tobjectAccessorSample.required(),\n\t].flatMap((schema) => {\n\t\tconst getter = Object.getOwnPropertyDescriptor(schema._zod.def, 'shape')?.get\n\t\treturn getter ? [Function.prototype.toString.call(getter)] : []\n\t}),\n)\n\n/** Snapshot supported value-validation definitions, not Zod's mutable parse caches.\n * Functions remain the original validation functions; their captured application state\n * is outside the schema contract. Lazy/transform/default/coercion nodes fail closed.\n */\nfunction dtoMutationGuard(dto: z.ZodObject): () => void {\n\treturn schemaMutationGuard(dto, 'dto')\n}\n\n// Stored schemas already passed the native input boundary. Track their graph\n// independently of public DTO admissibility, without running any stored parser.\nconst builtinDefaultGetter = Function.prototype.toString.call(\n\tObject.getOwnPropertyDescriptor(z.string().default('')._zod.def, 'defaultValue')!.get!,\n)\nconst builtinPrefaultGetter = Function.prototype.toString.call(\n\tObject.getOwnPropertyDescriptor(z.string().prefault('')._zod.def, 'defaultValue')!.get!,\n)\n\nfunction schemaMutationGuard(dto: z.ZodObject, boundary: 'dto' | 'stored'): () => void {\n\tconst nodes: DtoGraphNode[] = []\n\tconst fallbackDefinitions = new Set<object>()\n\tconst patterns: { expression: RegExp; source: string; flags: string }[] = []\n\tconst seen = new Set<object>()\n\tconst active = new Set<object>()\n\tconst objectFields: {\n\t\tdefinition: z.core.$ZodObjectDef\n\t\tfields: Record<string, z.core.$ZodType>\n\t}[] = []\n\tfunction visit(value: DtoGraphNode['object'], depth: number): void {\n\t\tif (active.has(value)) throw new Error('Unsupported cyclic DTO schema')\n\t\tif (seen.has(value)) return\n\t\tif (depth > 32 || nodes.length >= 4096) throw new Error('DTO schema graph budget exceeded')\n\t\tseen.add(value)\n\t\tactive.add(value)\n\t\ttry {\n\t\t\tif (value instanceof RegExp)\n\t\t\t\tpatterns.push({ expression: value, source: value.source, flags: value.flags })\n\t\t\tif (value instanceof z.core.$ZodCheck && 'check' in value._zod.def) {\n\t\t\t\tif (boundary === 'dto' && value._zod.def.check === 'overwrite')\n\t\t\t\t\tthrow new Error('Unsupported DTO overwrite check')\n\t\t\t\tvisit(value._zod.def, depth + 1)\n\t\t\t\tnodes.push({\n\t\t\t\t\tobject: value._zod,\n\t\t\t\t\texactKeys: false,\n\t\t\t\t\tkeys: ['def', 'check'],\n\t\t\t\t\tvalues: ['def', 'check'].map((key) => Object.getOwnPropertyDescriptor(value._zod, key)!),\n\t\t\t\t})\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (value instanceof z.core.$ZodType) {\n\t\t\t\t// SAFETY: built-in Zod definitions form a discriminated union.\n\t\t\t\tconst def = (value as z.core.$ZodTypes)._zod.def\n\t\t\t\tif (\n\t\t\t\t\tboundary === 'dto' &&\n\t\t\t\t\t(![\n\t\t\t\t\t\t'object',\n\t\t\t\t\t\t'array',\n\t\t\t\t\t\t'optional',\n\t\t\t\t\t\t'nullable',\n\t\t\t\t\t\t'nonoptional',\n\t\t\t\t\t\t'union',\n\t\t\t\t\t\t'intersection',\n\t\t\t\t\t\t'tuple',\n\t\t\t\t\t\t'record',\n\t\t\t\t\t\t'string',\n\t\t\t\t\t\t'number',\n\t\t\t\t\t\t'boolean',\n\t\t\t\t\t\t'bigint',\n\t\t\t\t\t\t'null',\n\t\t\t\t\t\t'never',\n\t\t\t\t\t\t'literal',\n\t\t\t\t\t\t'enum',\n\t\t\t\t\t].includes(def.type) ||\n\t\t\t\t\t\t('coerce' in def && def.coerce))\n\t\t\t\t)\n\t\t\t\t\tthrow new Error(`Unsupported DTO schema \"${def.type}\"`)\n\t\t\t\tif (boundary === 'stored' && (def.type === 'default' || def.type === 'prefault')) {\n\t\t\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(def, 'defaultValue')\n\t\t\t\t\tif (descriptor?.get) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tdescriptor.set ||\n\t\t\t\t\t\t\tFunction.prototype.toString.call(descriptor.get) !==\n\t\t\t\t\t\t\t\t(def.type === 'default' ? builtinDefaultGetter : builtinPrefaultGetter)\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\tthrow new Error('Unsupported stored schema accessor')\n\t\t\t\t\t\t// Snapshot the accessor identity below, never evaluate its fallback.\n\t\t\t\t\t\tfallbackDefinitions.add(def)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (def.type === 'object') {\n\t\t\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(def, 'shape')\n\t\t\t\t\tif (descriptor?.get) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tdescriptor.set ||\n\t\t\t\t\t\t\t!builtinObjectGetters.has(Function.prototype.toString.call(descriptor.get))\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\tthrow new Error('Unsupported DTO schema accessor')\n\t\t\t\t\t\tconst fields = def.shape\n\t\t\t\t\t\tvisit(fields, depth + 1)\n\t\t\t\t\t\t// A refined clone's getter can return fresh shallow copies.\n\t\t\t\t\t\t// Preserve that caller behavior and compare its fields separately.\n\t\t\t\t\t\tif (Object.getOwnPropertyDescriptor(def, 'shape')?.get)\n\t\t\t\t\t\t\tobjectFields.push({ definition: def, fields })\n\t\t\t\t\t}\n\t\t\t\t\tif (value instanceof z.ZodObject) {\n\t\t\t\t\t\tconst publicFields = Object.getOwnPropertyDescriptor(value, 'shape')\n\t\t\t\t\t\tif (publicFields && !('value' in publicFields))\n\t\t\t\t\t\t\tthrow new Error('Unsupported DTO schema accessor')\n\t\t\t\t\t\tvisit(value.shape, depth + 1)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tvisit(def, depth + 1)\n\t\t\t\t// Track definition replacement, without snapshotting caches initialized by parsing.\n\t\t\t\tnodes.push({\n\t\t\t\t\tobject: value._zod,\n\t\t\t\t\texactKeys: false,\n\t\t\t\t\tkeys: ['def'],\n\t\t\t\t\tvalues: [Object.getOwnPropertyDescriptor(value._zod, 'def')!],\n\t\t\t\t})\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst keys = Reflect.ownKeys(value)\n\t\t\tconst values = keys.map((key) => Object.getOwnPropertyDescriptor(value, key)!)\n\t\t\tif (\n\t\t\t\tvalues.some(\n\t\t\t\t\t(property, i) =>\n\t\t\t\t\t\t!('value' in property) &&\n\t\t\t\t\t\t!objectFields.some(({ definition }) => definition === value && keys[i] === 'shape') &&\n\t\t\t\t\t\t!(fallbackDefinitions.has(value) && keys[i] === 'defaultValue'),\n\t\t\t\t)\n\t\t\t)\n\t\t\t\tthrow new Error('Unsupported DTO schema accessor')\n\t\t\tnodes.push({ object: value, exactKeys: true, keys, values })\n\t\t\tfor (const property of values) {\n\t\t\t\tif (\n\t\t\t\t\tproperty.value !== null &&\n\t\t\t\t\tObject(property.value) === property.value &&\n\t\t\t\t\t!(property.value instanceof Function)\n\t\t\t\t)\n\t\t\t\t\tvisit(property.value, depth + 1)\n\t\t\t}\n\t\t} finally {\n\t\t\tactive.delete(value)\n\t\t}\n\t}\n\tvisit(dto, 0)\n\treturn () => {\n\t\tfor (const pattern of patterns)\n\t\t\tif (\n\t\t\t\tpattern.expression.source !== pattern.source ||\n\t\t\t\tpattern.expression.flags !== pattern.flags\n\t\t\t)\n\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\tfor (const node of nodes) {\n\t\t\tconst keys = node.keys\n\t\t\tif (node.exactKeys && Reflect.ownKeys(node.object).length !== keys.length)\n\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\t\t// Regex checks may update lastIndex while parsing; pattern identity is checked above.\n\t\t\t\tif (node.object instanceof RegExp && keys[i] === 'lastIndex') continue\n\t\t\t\tconst current = Object.getOwnPropertyDescriptor(node.object, keys[i])\n\t\t\t\tconst before = node.values[i]\n\t\t\t\tif (\n\t\t\t\t\t!current ||\n\t\t\t\t\tcurrent.value !== before.value ||\n\t\t\t\t\tcurrent.get !== before.get ||\n\t\t\t\t\tcurrent.set !== before.set ||\n\t\t\t\t\tcurrent.enumerable !== before.enumerable\n\t\t\t\t)\n\t\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\t\t}\n\t\t}\n\t\tfor (const { definition, fields } of objectFields) {\n\t\t\tconst current = definition.shape\n\t\t\tconst keys = Reflect.ownKeys(fields)\n\t\t\tif (\n\t\t\t\tReflect.ownKeys(current).length !== keys.length ||\n\t\t\t\tkeys.some(\n\t\t\t\t\t(key) =>\n\t\t\t\t\t\tObject.getOwnPropertyDescriptor(current, key)?.value !==\n\t\t\t\t\t\tObject.getOwnPropertyDescriptor(fields, key)?.value,\n\t\t\t\t)\n\t\t\t)\n\t\t\t\tthrow new Error('DTO schema mutated after registry construction')\n\t\t}\n\t}\n}\n\nfunction assertRegistryUnchanged(registry: RegistryMetadata): void {\n\tfor (const table of Object.values(registry.metadata)) table.assertUnchanged()\n}\n\ninterface GuardedDtoDefinition extends z.core.$ZodObjectDef {\n\tassertUnchanged(): void\n}\nconst GuardedDtoObject = z.core.$constructor<z.ZodObject>('GuardedDtoObject', (inst, def) => {\n\t// SAFETY: only guardedDto creates this definition; Zod clones retain its guard.\n\tconst guarded = def as GuardedDtoDefinition\n\tz.ZodObject.init(inst, def)\n\t// Run after Zod's own deferred initializers, including its memoizer wrapper.\n\t// Guarding parse alone is too late for first-run schema introspection.\n\tinst._zod.deferred ??= []\n\tinst._zod.deferred.push(() => {\n\t\tconst run = inst._zod.run\n\t\tinst._zod.run = (payload, ctx) => {\n\t\t\ttry {\n\t\t\t\tguarded.assertUnchanged()\n\t\t\t} catch {\n\t\t\t\tpayload.issues.push({\n\t\t\t\t\tcode: 'custom',\n\t\t\t\t\tinput: payload.value,\n\t\t\t\t\tmessage: 'DTO schema mutated after registry construction',\n\t\t\t\t})\n\t\t\t\treturn payload\n\t\t\t}\n\t\t\treturn run(payload, ctx)\n\t\t}\n\t})\n})\nfunction guardedDto(dto: z.ZodObject, assertUnchanged: () => void): z.ZodObject {\n\tconst outputFields = dto.shape\n\tconst def: GuardedDtoDefinition = { ...dto._zod.def, assertUnchanged }\n\tObject.defineProperty(def, 'shape', {\n\t\tenumerable: true,\n\t\tconfigurable: true,\n\t\tget: () => {\n\t\t\tassertUnchanged()\n\t\t\treturn outputFields\n\t\t},\n\t})\n\treturn new GuardedDtoObject(def)\n}\n\nfunction snapshotMetadata(registry: RegistryMetadata): RelationMetadata {\n\tconst tables: Record<string, RelationTableMetadata> = {}\n\tfor (const [name, data] of Object.entries(registry.metadata)) {\n\t\tconst fields: Record<string, RelationFieldMetadata> = {}\n\t\tfor (const [field, validator] of Object.entries(data.fields))\n\t\t\tfields[field] = Object.freeze({\n\t\t\t\tkind: validator.kind,\n\t\t\t\toptional: validator.isOptional === 'optional',\n\t\t\t\tnullable: hasNull(validator),\n\t\t\t\tidTables: Object.freeze(idTables(validator)),\n\t\t\t\tnative: frozenNative(nativeJson(validator)),\n\t\t\t})\n\t\ttables[name] = Object.freeze({\n\t\t\tfields: Object.freeze(fields),\n\t\t\tindexes: Object.freeze(\n\t\t\t\tdata.indexes.map((index) =>\n\t\t\t\t\tObject.freeze({ name: index.name, fields: Object.freeze([...index.fields]) }),\n\t\t\t\t),\n\t\t\t),\n\t\t})\n\t}\n\treturn Object.freeze({ tables: Object.freeze(tables), declarations: registry.declarations })\n}\nfunction hasNull(validator: GenericValidator): boolean {\n\treturn (\n\t\tvalidator.kind === 'null' || (validator.kind === 'union' && validator.members.some(hasNull))\n\t)\n}\nfunction idTables(validator: GenericValidator): string[] {\n\treturn validator.kind === 'id'\n\t\t? [validator.tableName]\n\t\t: validator.kind === 'union'\n\t\t\t? [...new Set(validator.members.flatMap(idTables))].sort()\n\t\t\t: []\n}\nfunction frozenNative(native: ValidatorJSON): Readonly<ValidatorJSON> {\n\tconst snapshot = structuredClone(native)\n\tconst pending: JSONValue[] = [snapshot]\n\twhile (pending.length) {\n\t\tconst next = pending.pop()!\n\t\tif (Object(next) !== next) continue\n\t\t// SAFETY: native JSON metadata contains ordinary arrays/objects; the boxing test excludes primitives.\n\t\tconst container = next as JSONValue[] | { [key: string]: JSONValue }\n\t\tObject.freeze(container)\n\t\tpending.push(...Object.values(container))\n\t}\n\treturn snapshot\n}\n"],"mappings":"8QAwCA,MAAMA,EAA4B,OAAO,eAAe,EAClDC,EAA2B,OAAO,cAAc,EAwThDC,kBAAa,IAAI,QAEjBC,kBAAqB,IAAI,QASzBC,kBAAO,IAAI,QACXC,kBAAc,IAAI,QAClBC,kBAAW,IAAI,QACfC,EAAcC,EAAE,OAAO,CAC5B,QAASA,EAAE,MAAMA,EAAE,OAAO,CAAE,gBAAiBA,EAAE,OAAO,EAAG,OAAQA,EAAE,MAAMA,EAAE,OAAO,CAAC,CAAE,CAAC,CAAC,CACxF,CAAC,EAGD,SAAgBC,EAIf,EACA,EAC8B,CAC9B,IAAMC,EAA0C,CAAC,EAC3CC,kBAAU,IAAI,IACdC,EAAmD,CAAC,EACpDC,EAAiE,CAAC,EAClEC,EAAkE,CAAC,EACzE,IAAK,GAAM,CAACC,EAAMC,KAAU,OAAO,QAAQC,CAAM,EAAG,CACnD,GAAID,EAAM,kBAAkBR,EAAE,SAC7B,MAAU,MAAM,mEAAmE,EACpF,GACCQ,EAAM,YAAcD,GACpB,CAAC,MAAO,OAAQ,YAAa,cAAe,WAAW,CAAC,CAAC,SAASA,CAAI,EAEtE,M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