@confect/server 9.0.1 → 9.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,11 +1,21 @@
1
1
  # @confect/server
2
2
 
3
+ ## 9.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - dd33006: Fix a crash when `get` by index missed on an index over non-string fields.
8
+
9
+ `GetByIndexFailure` declared its `indexFieldValues` as `Schema.Array(Schema.String)` while the actual values can be any Convex value (numbers, bigints, booleans, ids, etc.). Because `Schema.TaggedError` constructors validate at runtime, a `get` by such an index would die with a `ParseError` defect whenever the lookup found no document — instead of failing with the catchable `GetByIndexFailure`. Lookups that found a document were unaffected, so the crash only surfaced on a miss.
10
+
11
+ `indexFieldValues` is now typed as an array of unknown values and carries the actual values used in the failed lookup. The error `message` renders them as JSON, handling every Convex value type including `bigint` values from `int64` index fields.
12
+ - @confect/core@9.0.2
13
+
3
14
  ## 9.0.1
4
15
 
5
16
  ### Patch Changes
6
17
 
7
18
  - 445ea9b: Loosen and align dependency ranges across all packages:
8
-
9
19
  - The `convex` peer dependency is now `^1.32.0` in every package (previously pinned exactly to `1.39.1`, or `^1.30.0` in `@confect/react`). The range is validated against convex 1.32.0 through 1.40.0.
10
20
  - `@confect/server`'s `@effect/platform-node` peer dependency is now optional — it is only needed when using the `@confect/server/node` entrypoint.
11
21
  - `@confect/cli` now uses caret ranges for its `@effect/platform` and `@effect/platform-node` dependencies so they can deduplicate with the versions resolved for `@confect/server`, and no longer declares an unused direct dependency on `@effect/platform-node-shared`.
@@ -25,7 +35,6 @@
25
35
  ### Filesystem-driven groups
26
36
 
27
37
  Your API is now authored as colocated `*.spec.ts`/`*.impl.ts` pairs, one pair per group, and **the file's path within `confect/` is the group's name** (its stem for top-level groups, the dot-joined directory path for nested groups). `GroupSpec.make()` and `GroupSpec.makeNode()` no longer take a name argument.
28
-
29
38
  - Each `*.spec.ts` `export default`s its `GroupSpec` (named co-exports like error classes are still allowed).
30
39
  - Each `*.impl.ts` default-imports its sibling spec, passes it to `FunctionImpl.make` / `GroupImpl.make`, and ends the layer pipeline with `GroupImpl.finalize`—a compile-time completeness check that only typechecks once every function the spec declares has a `FunctionImpl` provided.
31
40
  - The root `confect/spec.ts`, `confect/impl.ts`, `confect/nodeSpec.ts`, and `confect/nodeImpl.ts` files are gone, along with `Impl.make` and `Impl.finalize`. `confect codegen` deletes any of these (and the stale aggregate `_generated/registeredFunctions.ts` / `_generated/nodeRegisteredFunctions.ts`) on upgrade.
@@ -45,12 +54,11 @@
45
54
  Schema.Struct({
46
55
  userId: Schema.optional(Id("users")),
47
56
  text: Schema.String,
48
- })
57
+ }),
49
58
  );
50
59
  ```
51
60
 
52
61
  Codegen emits, alongside it:
53
-
54
62
  - `_generated/schema.ts`—the runtime `DatabaseSchema`. Never imports `convex/server`, so a runtime cold start no longer evaluates `defineSchema(...)`.
55
63
  - `_generated/convexSchema.ts`—the Convex deploy `SchemaDefinition`, re-exported from `convex/schema.ts`.
56
64
  - `_generated/id.ts`—a type-safe `Id` constructor whose argument is constrained to your table names. Use `Id("notes")` everywhere you previously wrote `GenericId.GenericId("notes")`; cross-table `_id` typos are now caught at compile time.
@@ -70,7 +78,7 @@
70
78
  name: "list",
71
79
  args: Schema.Struct({}),
72
80
  returns: Schema.Array(Notes.Doc),
73
- })
81
+ }),
74
82
  );
75
83
  ```
76
84
 
@@ -89,7 +97,7 @@
89
97
  name: "list",
90
98
  args: () => Schema.Struct({}),
91
99
  returns: () => Schema.Array(notes.Doc),
92
- })
100
+ }),
93
101
  );
94
102
  ```
95
103
 
@@ -100,14 +108,13 @@
100
108
  const list = FunctionImpl.make(databaseSchema, notes, "list", handler);
101
109
  export default GroupImpl.make(databaseSchema, notes).pipe(
102
110
  Layer.provide(list),
103
- GroupImpl.finalize
111
+ GroupImpl.finalize,
104
112
  );
105
113
  ```
106
114
 
107
115
  ### Node functions are first-class
108
116
 
109
117
  A group's runtime is now declared solely by its spec—`GroupSpec.makeNode()` for a Node action group, `GroupSpec.make()` otherwise—mirroring vanilla Convex's per-file `"use node"` directive. The separate `node` namespace is gone: Node specs/impls are ordinary colocated pairs that can live anywhere in `confect/`, and codegen emits the `"use node"` directive based on the spec.
110
-
111
118
  - A Node group at `confect/email.spec.ts` is now reached at `refs.public.email.send` instead of `refs.public.node.email.send`.
112
119
  - `@confect/core` removes `Spec.makeNode`, `Spec.merge`, and `Spec.isConvexSpec` / `Spec.isNodeSpec`; `Spec.make()` is a single mixed-runtime container and `Refs.make(spec)` takes one argument. `GroupSpec.makeNode()`, `FunctionSpec.publicNodeAction()` / `internalNodeAction()` are unchanged.
113
120
 
@@ -124,7 +131,6 @@
124
131
  The codegen bundler now uses [`bundle-require`](https://github.com/egoist/bundle-require), so impls may import third-party packages and use `tsconfig.json` `paths` aliases (`~/*`, `@/*`, …) for their own source. A parent `confect/{path}.spec.ts` may now declare functions alongside a sibling `confect/{path}/` subdirectory of further specs, and codegen reports a clear error on a name collision between the two.
125
132
 
126
133
  ### Migration
127
-
128
134
  1. **Tables.** Delete `confect/schema.ts`. Rename each table file to a valid JS identifier (e.g. `confect/tables/notes.ts`); the basename becomes the table name. Drop the name argument from `Table.make`, wrap the field struct in `() =>`, and replace `GenericId.GenericId("x")` with `Id("x")` from `_generated/id`. If you read `table.name` off a bound table, rename it to `table.tableName`.
129
135
  2. **Specs.** Split each group into a colocated `*.spec.ts` that `export default`s `GroupSpec.make()` (no name). Wrap every `args`/`returns`/`error` in `() =>`. Import a table's `Doc`/`Fields` from its wrapper, `import notes from "./_generated/tables/notes"`.
130
136
  3. **Impls.** In each `*.impl.ts`, default-import the sibling spec, import `databaseSchema` from `_generated/schema`, pass it to `FunctionImpl.make` / `GroupImpl.make` in place of `api`, end the pipeline with `GroupImpl.finalize`, and `export default` it. Delete the root `confect/spec.ts`, `impl.ts`, `nodeSpec.ts`, and `nodeImpl.ts` (codegen will also remove them).
@@ -167,13 +173,11 @@
167
173
  The `node` namespace existed only because the pre-v9 architecture aggregated every function's impl into one module that all generated `convex/` modules imported; Node functions had to be quarantined into a separate spec/impl/registry tree so Convex-runtime functions wouldn't transitively import Node-only code. v9's per-group isolation removed that constraint, so the namespace was no longer load-bearing — only ergonomic overhead that diverged from vanilla Convex (which identifies Node modules per-file, with no directory requirement).
168
174
 
169
175
  ### Breaking changes
170
-
171
176
  - **API namespace removed.** A Node group at `confect/email.spec.ts` is now referenced as `refs.public.email.send` instead of `refs.public.node.email.send`. Node groups are ordinary groups in the refs tree, nesting preserved like any other group.
172
177
  - **Generated layout changed.** Node modules are emitted at `convex/<path>.ts` (carrying `"use node"`) instead of `convex/node/<path>.ts`, and their registries at `confect/_generated/registeredFunctions/<path>.ts`. The single assembled `confect/_generated/spec.ts` now contains every group regardless of runtime; `confect/_generated/nodeSpec.ts` is no longer generated (codegen deletes any stale copy on upgrade).
173
178
  - **`@confect/core` API.** Removed `Spec.makeNode`, `Spec.merge`, and `Spec.isConvexSpec`/`Spec.isNodeSpec`. `Spec` is now a single mixed-runtime container (`Spec.make()` accepts groups of any runtime). `Refs.make(spec)` takes a single argument (the unified spec) instead of `(convexSpec, nodeSpec)`. `GroupSpec.makeNode()`/`makeNodeAt()` and `FunctionSpec.publicNodeAction()`/`internalNodeAction()` are unchanged; `GroupSpec` subgroups may now be of any runtime (a group is just a namespace for its children).
174
179
 
175
180
  ### Migration
176
-
177
181
  1. Move any `confect/node/<path>.spec.ts`/`.impl.ts` files to wherever you want them under `confect/` (e.g. `confect/<path>.spec.ts`); the `node/` directory has no special meaning anymore. Their specs already use `GroupSpec.makeNode()`, so no spec-body change is needed — only fix the impl's relative import of `_generated/schema` if its depth changed.
178
182
  2. Update call sites to drop the `node` segment: `refs.public.node.<group>.<fn>` → `refs.public.<group>.<fn>`.
179
183
  3. Replace `Refs.make(spec, nodeSpec)` with `Refs.make(spec)` (codegen does this for `_generated/refs.ts` automatically).
@@ -283,7 +287,6 @@
283
287
  Previously, `confect/schema.ts` was user-authored and `DatabaseSchema` carried a `convexSchemaDefinition` field that was eagerly rebuilt on every `.addTable(...)`. That field was an `O(n²)` allocation for `n` tables, and it forced both the deploy CLI (which only needs `defineSchema(...)`) and the runtime (which only needs the table codec lookup) through the same module — so any runtime function bundle dragged in `convex/server`'s `defineSchema`. Issue 1.
284
288
 
285
289
  Codegen now scans `confect/tables/*.ts` (every file must default-export a `Table`) and emits two siblings:
286
-
287
290
  - `confect/_generated/schema.ts` — the runtime `DatabaseSchema`, consumed by `_generated/api.ts`. Imports `@confect/server` but never `convex/server`.
288
291
  - `confect/_generated/convexSchema.ts` — the Convex deploy `SchemaDefinition`, re-exported one-line from `convex/schema.ts`. Imports `convex/server` but never `@confect/server`.
289
292
 
@@ -296,7 +299,6 @@
296
299
  This eliminates a class of subtle infelicities: the file basename and the table name can never drift out of sync, cross-table `_id` references are type-constrained against the actual set of declared tables (catching typos at compile time), and ESM cycle hazards for mutual cross-table `Id` references are gone because authoring files no longer transitively import each other.
297
300
 
298
301
  Codegen now emits two new sets of files alongside `_generated/schema.ts` and `_generated/convexSchema.ts`:
299
-
300
302
  - `confect/_generated/id.ts` — a single `Id` constructor whose argument is type-constrained to the union of your table names. Use `Id("notes")` everywhere you previously wrote `GenericId.GenericId("notes")`.
301
303
  - `confect/_generated/tables/<name>.ts` — one thin wrapper per table that binds the unnamed value from `confect/tables/<name>.ts` to its filename. This is what other modules (specs, impls, HTTP handlers) default-import to reach a table's `Doc`, `Fields`, and `tableName`.
302
304
 
@@ -313,7 +315,6 @@
313
315
  The bound `Table` now exposes `Fields` / `Doc` / `tableDefinition` as lazy getters that compute their value on first access, then replace themselves with a plain non-writable data property so second-and-subsequent accesses are observably indistinguishable from a plain property (and skip all function-call overhead). The result: a function bundle only pays the schema-construction cost for tables it actually touches via `db.table(name)` (which reaches `Fields` through `Document.decode`). The `UnnamedTable` callable no longer exposes `Fields` or `tableDefinition` — read these off the bound `Table` (the generated `_generated/tables/<name>.ts` wrapper already binds the name).
314
316
 
315
317
  ### Migration
316
-
317
318
  1. Delete your `confect/schema.ts`. Codegen will refuse to run while a stray copy is present.
318
319
  2. Rename each `confect/tables/<Name>.ts` to a valid JS identifier in your chosen casing convention (e.g. `confect/tables/notes.ts`). The basename becomes the table name; you no longer pass it as an argument.
319
320
  3. Convert each table file to a **default-export-only** unnamed module: drop the name argument from `Table.make`, wrap the field-schema struct in a `() => ...` callback, and switch any `GenericId.GenericId("users")` references to `Id("users")` imported from `../_generated/id`:
@@ -403,7 +404,6 @@
403
404
  Since `9.0.0-next.1`, codegen has wrapped every parent leaf that has sibling subdirectory specs in `<parent>.addGroupAt("child", <child>)`. Because `GroupSpec.addGroupAt` is immutable, that produced a fresh object in the assembled tree, while the parent's `*.impl.ts` continued to hold a reference to the original imported leaf. The runtime resolver compared by `===`, so every such impl failed `validateImpl` with "Could not resolve group path for the provided GroupSpec." Child impls happened to work only because `GroupSpec.withName` was secretly mutating its argument in place to keep the child's identity stable — an asymmetry that was load-bearing for one half of the API and broken for the other.
404
405
 
405
406
  ### What changed
406
-
407
407
  - `@confect/core/Spec` carries a new `readonly paths: ReadonlyMap<GroupSpec.AnyWithProps, string>` field and exposes a chainable `Spec#addPath(group, path)` builder. `add` / `addAt` / `merge` propagate `paths` unchanged; `merge` re-prefixes a node spec's entries with `"node."` to match the merged tree.
408
408
  - `@confect/core/GroupSpec.withName` is now pure: it returns a fresh copy when the name differs and no longer rewrites the input in place. No new identity-tracking machinery is introduced.
409
409
  - `@confect/server/FunctionImpl.make` and `GroupImpl.make` resolve their group path via `api.spec.paths.get(group)` — an O(1) map lookup instead of a tree walk — and throw a clearer error pointing at `Spec.addPath` when the spec hasn't been registered.
@@ -411,7 +411,6 @@
411
411
  - `@confect/cli` codegen emits one `.addPath(<binding>, "<dot.path>")` call per leaf in `_generated/spec.ts` (and `_generated/nodeSpec.ts`) so the imported leaves carry their full paths into the assembled spec value.
412
412
 
413
413
  ### User-facing impact
414
-
415
414
  - Spec authoring (`*.spec.ts`) and impl authoring (`*.impl.ts`) APIs are unchanged. `FunctionImpl.make(api, spec, name, handler)` and `GroupImpl.make(api, spec)` keep their exact signatures.
416
415
  - Generated `_generated/spec.ts` (and `_generated/nodeSpec.ts`) pick up one `.addPath(...)` chain entry per leaf on the next `confect codegen` run. The shape is fully immutable — no module-load mutation, no hidden side effects.
417
416
  - Hand-rolled tests that construct a `Spec` and pass it to `Api.make` must now also call `.addPath(spec, "dot.path")` for any group they intend to look up.
@@ -450,7 +449,6 @@
450
449
  Splitting impl across colocated `*.impl.ts` files is the vehicle for fixing that. With this change, `confect codegen` emits one `_generated/registeredFunctions/{path}.ts` per group, and each generated `convex/` module imports only its own group's per-group registry — which in turn imports only its own sibling `.impl.ts`. A Convex function's cold-start bundle now scales with its own group's impl rather than with the size of the whole project.
451
450
 
452
451
  ### Breaking changes
453
-
454
452
  - `GroupSpec.make()` and `GroupSpec.makeNode()` no longer take a name argument; the group name is derived from the spec file's path within `confect/`.
455
453
  - `FunctionImpl.make(api, groupSpec, fn, handler)` and `GroupImpl.make(api, groupSpec)` now take the imported sibling spec object as their second argument instead of a dot-path string.
456
454
  - Every `GroupImpl` pipeline must end with `GroupImpl.finalize`, which only typechecks once every function declared by the spec has a corresponding `FunctionImpl` provided to the group layer. `GroupImpl.finalize` snapshots the names of every registered function onto the produced `Finalized` `GroupImpl` service value, and `confect codegen` reads those names to verify per-function coverage against the spec at runtime.
@@ -459,7 +457,6 @@
459
457
  - Every module under `convex/` is re-emitted to import from `_generated/registeredFunctions/{path}` instead of the previous aggregate file. Users who commit `convex/` to source control should expect a full rewrite of that directory on first codegen.
460
458
 
461
459
  ### Migration
462
-
463
460
  1. For each existing group, create a colocated `confect/{path}.spec.ts` and `confect/{path}.impl.ts` pair (under a subdirectory for nested groups).
464
461
  - In each spec, call `GroupSpec.make()` (or `GroupSpec.makeNode()`) without a name and `export default` the result.
465
462
  - In each impl, default-import the sibling spec (e.g. `import notes from "./notes.spec"`), pass it to `FunctionImpl.make`/`GroupImpl.make` in place of the previous dot-path string, append `GroupImpl.finalize` to the pipeline, and `export default` the resulting `GroupImpl` layer:
@@ -467,7 +464,7 @@
467
464
  export default GroupImpl.make(api, notes).pipe(
468
465
  Layer.provide(list),
469
466
  Layer.provide(insert),
470
- GroupImpl.finalize
467
+ GroupImpl.finalize,
471
468
  );
472
469
  ```
473
470
  2. Delete root `confect/spec.ts`, `confect/impl.ts`, `confect/nodeSpec.ts`, `confect/nodeImpl.ts`, and any parent aggregator spec/impl files. (`confect codegen` will also delete any of these it finds, plus the stale `_generated/registeredFunctions.ts` and `_generated/nodeRegisteredFunctions.ts`, so this step can be skipped.)
@@ -525,7 +522,7 @@
525
522
 
526
523
  export class NoteNotFound extends Schema.TaggedError<NoteNotFound>()(
527
524
  "NoteNotFound",
528
- { noteId: GenericId.GenericId("notes") }
525
+ { noteId: GenericId.GenericId("notes") },
529
526
  ) {}
530
527
 
531
528
  export const notes = GroupSpec.make("notes").addFunction(
@@ -534,7 +531,7 @@
534
531
  args: Schema.Struct({ noteId: GenericId.GenericId("notes") }),
535
532
  returns: Notes.Doc,
536
533
  error: NoteNotFound,
537
- })
534
+ }),
538
535
  );
539
536
  ```
540
537
 
@@ -554,7 +551,7 @@
554
551
  .table("notes")
555
552
  .get(noteId)
556
553
  .pipe(Effect.mapError(() => new NoteNotFound({ noteId })));
557
- })
554
+ }),
558
555
  );
559
556
  ```
560
557
 
@@ -626,7 +623,6 @@
626
623
  Unspecified failures continue to reject the promise.
627
624
 
628
625
  ### Migration
629
-
630
626
  - For each `useQuery` call site, replace `result === undefined` checks and direct property access with `QueryResult.match` (or the lower-level `QueryResult.isLoading`/`isSuccess`/`isFailure` predicates).
631
627
  - For each `useMutation`/`useAction` call site whose ref now declares an `error` schema, unwrap the resolved `Either` (e.g. with `Either.match`); call sites against refs without an `error` schema need no change.
632
628
 
@@ -35,7 +35,7 @@ declare const GetByIndexFailure_base: Schema.TaggedErrorClass<GetByIndexFailure,
35
35
  } & {
36
36
  tableName: typeof Schema.String;
37
37
  indexName: typeof Schema.String;
38
- indexFieldValues: Schema.Array$<typeof Schema.String>;
38
+ indexFieldValues: Schema.Array$<typeof Schema.Unknown>;
39
39
  }>;
40
40
  export declare class GetByIndexFailure extends GetByIndexFailure_base {
41
41
  get message(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"QueryInitializer.d.ts","sourceRoot":"","sources":["../src/QueryInitializer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,cAAc,EAEd,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,iBAAiB,EACjB,UAAU,EACV,gBAAgB,EAGhB,YAAY,EACZ,mBAAmB,EACnB,aAAa,EACd,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAG/C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,KAAK,SAAS,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,YAAY,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,KAAK,KAAK,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,KAAK,SAAS,MAAM,aAAa,CAAC;AAE9C,KAAK,gBAAgB,CACnB,UAAU,SAAS,SAAS,CAAC,YAAY,EACzC,SAAS,SAAS,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,EAClD,gBAAgB,SAAS,gBAAgB,EACzC,UAAU,SAAS,SAAS,CAAC,YAAY,IACvC;IACF,QAAQ,CAAC,GAAG,EAAE;QACZ,CACE,EAAE,EAAE,SAAS,CAAC,SAAS,CAAC,GACvB,MAAM,CAAC,MAAM,CACd,UAAU,CAAC,UAAU,CAAC,EACtB,QAAQ,CAAC,mBAAmB,GAAG,cAAc,CAC9C,CAA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CAAC,iBAAiB,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,EACnE,SAAS,CAAC,kBAAkB,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAiLrE,CAAC;AAEF,eAAO,MAAM,OAAO,GACjB,MAAM,SAAS,KAAK,CAAC,YAAY,EAAE,SAAS,SAAS,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EACtE,WAAW,SAAS,EACpB,sBAAsB,kBAAkB,CACtC,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CACjD,EACD,OAAO,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,MAEzC,IAAI,SAAS,CAAC,SAAS,CAAC,6EAOtB,CAAC;;;;;;;AAEN,qBAAa,cAAe,SAAQ,mBAMnC;IACC,IAAa,OAAO,IAAI,MAAM,CAM7B;CACF;;;;;;;;AAED,qBAAa,iBAAkB,SAAQ,sBAOtC;IACC,IAAa,OAAO,IAAI,MAAM,CAK7B;CACF"}
@@ -75,10 +75,10 @@ var GetByIdFailure = class extends Schema.TaggedError()("GetByIdFailure", {
75
75
  var GetByIndexFailure = class extends Schema.TaggedError()("GetByIndexFailure", {
76
76
  tableName: Schema.String,
77
77
  indexName: Schema.String,
78
- indexFieldValues: Schema.Array(Schema.String)
78
+ indexFieldValues: Schema.Array(Schema.Unknown)
79
79
  }) {
80
80
  get message() {
81
- return `No documents found in table '${this.tableName}' with index '${this.indexName}' and field values '${this.indexFieldValues}'`;
81
+ return `No documents found in table '${this.tableName}' with index '${this.indexName}' and field values '${JSON.stringify(this.indexFieldValues, (_key, value) => typeof value === "bigint" ? value.toString() : value)}'`;
82
82
  }
83
83
  };
84
84
 
@@ -1 +1 @@
1
- {"version":3,"file":"QueryInitializer.js","names":["Document.decode","OrderedQuery.make","Document.documentErrorMessage"],"sources":["../src/QueryInitializer.ts"],"sourcesContent":["import type {\n OrderedQuery as ConvexOrderedQuery,\n QueryInitializer as ConvexQueryInitializer,\n DocumentByInfo,\n GenericTableIndexes,\n GenericTableInfo,\n Indexes,\n IndexRange,\n IndexRangeBuilder,\n NamedIndex,\n NamedSearchIndex,\n NamedTableInfo,\n Query,\n SearchFilter,\n SearchFilterBuilder,\n SearchIndexes,\n} from \"convex/server\";\nimport type { GenericId } from \"convex/values\";\nimport { pipe } from \"effect/Function\";\nimport * as Array from \"effect/Array\";\nimport * as Effect from \"effect/Effect\";\nimport * as Either from \"effect/Either\";\nimport * as Schema from \"effect/Schema\";\nimport type {\n BaseDatabaseReader,\n IndexFieldTypesForEq,\n} from \"@confect/core/Types\";\nimport type * as DataModel from \"./DataModel\";\nimport * as Document from \"./Document\";\nimport * as OrderedQuery from \"./OrderedQuery\";\nimport type * as Table from \"./Table\";\nimport type * as TableInfo from \"./TableInfo\";\n\ntype QueryInitializer<\n DataModel_ extends DataModel.AnyWithProps,\n TableName extends DataModel.TableNames<DataModel_>,\n _ConvexTableInfo extends GenericTableInfo,\n _TableInfo extends TableInfo.AnyWithProps,\n> = {\n readonly get: {\n (\n id: GenericId<TableName>,\n ): Effect.Effect<\n _TableInfo[\"document\"],\n Document.DocumentDecodeError | GetByIdFailure\n >;\n <IndexName extends keyof Indexes<_ConvexTableInfo>>(\n indexName: IndexName,\n ...indexFieldValues: IndexFieldTypesForEq<\n DataModel.ToConvex<DataModel_>,\n TableName,\n Indexes<_ConvexTableInfo>[IndexName]\n >\n ): Effect.Effect<\n _TableInfo[\"document\"],\n Document.DocumentDecodeError | GetByIndexFailure\n >;\n };\n readonly index: {\n <IndexName extends keyof Indexes<_ConvexTableInfo>>(\n indexName: IndexName,\n indexRange?: (\n q: IndexRangeBuilder<\n _TableInfo[\"convexDocument\"],\n NamedIndex<_ConvexTableInfo, IndexName>\n >,\n ) => IndexRange,\n order?: \"asc\" | \"desc\",\n ): OrderedQuery.OrderedQuery<_TableInfo, TableName>;\n <IndexName extends keyof Indexes<_ConvexTableInfo>>(\n indexName: IndexName,\n order?: \"asc\" | \"desc\",\n ): OrderedQuery.OrderedQuery<_TableInfo, TableName>;\n };\n readonly search: <IndexName extends keyof SearchIndexes<_ConvexTableInfo>>(\n indexName: IndexName,\n searchFilter: (\n q: SearchFilterBuilder<\n DocumentByInfo<_ConvexTableInfo>,\n NamedSearchIndex<_ConvexTableInfo, IndexName>\n >,\n ) => SearchFilter,\n ) => OrderedQuery.OrderedQuery<_TableInfo, TableName>;\n};\n\nexport const make = <\n Tables extends Table.AnyWithProps,\n TableName extends Table.Name<Tables>,\n>(\n tableName: TableName,\n convexDatabaseReader: BaseDatabaseReader<\n DataModel.ToConvex<DataModel.FromTables<Tables>>\n >,\n table: Table.WithName<Tables, TableName>,\n): QueryInitializer<\n DataModel.DataModel<Tables>,\n TableName,\n DataModel.TableInfoWithName<DataModel.DataModel<Tables>, TableName>,\n DataModel.TableInfoWithName_<DataModel.DataModel<Tables>, TableName>\n> => {\n type DataModel_ = DataModel.DataModel<Tables>;\n type ConvexDataModel_ = DataModel.ToConvex<DataModel_>;\n type ThisQueryInitializer = QueryInitializer<\n DataModel_,\n TableName,\n DataModel.TableInfoWithName<DataModel_, TableName>,\n DataModel.TableInfoWithName_<DataModel_, TableName>\n >;\n type QueryInitializerFunction<\n FunctionName extends keyof ThisQueryInitializer,\n > = ThisQueryInitializer[FunctionName];\n\n const getByIndex = <\n IndexName extends keyof Indexes<\n DataModel.TableInfoWithName<DataModel_, TableName>\n >,\n >(\n indexName: IndexName,\n indexFieldValues: IndexFieldTypesForEq<\n DataModel.ToConvex<DataModel_>,\n TableName,\n Indexes<DataModel.TableInfoWithName<DataModel_, TableName>>[IndexName]\n >,\n ): Effect.Effect<\n DataModel.DocumentWithName<DataModel_, TableName>,\n Document.DocumentDecodeError | GetByIndexFailure\n > => {\n const indexFields: GenericTableIndexes[keyof GenericTableIndexes] = (\n table.indexes as GenericTableIndexes\n )[indexName as keyof GenericTableIndexes]!;\n\n return pipe(\n Effect.promise(() =>\n convexDatabaseReader\n .query(tableName)\n .withIndex(indexName, (q) =>\n Array.reduce(\n indexFieldValues,\n q,\n (q_, v, i) => q_.eq(indexFields[i] as any, v as any) as any,\n ),\n )\n .unique(),\n ),\n Effect.andThen(\n Either.fromNullable(\n () =>\n new GetByIndexFailure({\n tableName,\n indexName: indexName as string,\n indexFieldValues: indexFieldValues as string[],\n }),\n ),\n ),\n Effect.andThen(Document.decode(tableName, table.Fields)),\n );\n };\n\n const get: QueryInitializerFunction<\"get\"> = ((\n ...args: Parameters<QueryInitializerFunction<\"get\">>\n ) => {\n if (args.length === 1) {\n const id = args[0] as GenericId<TableName>;\n\n return getById(tableName, convexDatabaseReader, table)(id);\n } else {\n const [indexName, ...indexFieldValues] = args;\n\n return getByIndex(\n indexName as keyof Indexes<\n DataModel.TableInfoWithName<DataModel_, TableName>\n >,\n indexFieldValues,\n );\n }\n }) as QueryInitializerFunction<\"get\">;\n\n const index: QueryInitializerFunction<\"index\"> = <\n IndexName extends keyof Indexes<\n DataModel.TableInfoWithName<DataModel_, TableName>\n >,\n >(\n indexName: IndexName,\n indexRangeOrOrder?:\n | ((\n q: IndexRangeBuilder<\n DataModel.TableInfoWithName_<\n DataModel_,\n TableName\n >[\"convexDocument\"],\n NamedIndex<\n DataModel.TableInfoWithName<DataModel_, TableName>,\n IndexName\n >\n >,\n ) => IndexRange)\n | \"asc\"\n | \"desc\",\n order?: \"asc\" | \"desc\",\n ) => {\n const {\n applyWithIndex,\n applyOrder,\n }: {\n applyWithIndex: (\n queryInitializer: ConvexQueryInitializer<\n NamedTableInfo<ConvexDataModel_, TableName>\n >,\n ) => Query<NamedTableInfo<ConvexDataModel_, TableName>>;\n applyOrder: (\n query: Query<NamedTableInfo<ConvexDataModel_, TableName>>,\n ) => ConvexOrderedQuery<NamedTableInfo<ConvexDataModel_, TableName>>;\n } =\n indexRangeOrOrder === undefined\n ? {\n applyWithIndex: (q) => q.withIndex(indexName),\n applyOrder: (q) => q.order(\"asc\"),\n }\n : typeof indexRangeOrOrder === \"function\"\n ? order === undefined\n ? {\n applyWithIndex: (q) =>\n q.withIndex(indexName, indexRangeOrOrder),\n applyOrder: (q) => q.order(\"asc\"),\n }\n : {\n applyWithIndex: (q) =>\n q.withIndex(indexName, indexRangeOrOrder),\n applyOrder: (q) => q.order(order),\n }\n : {\n applyWithIndex: (q) => q.withIndex(indexName),\n applyOrder: (q) => q.order(indexRangeOrOrder),\n };\n\n const orderedQuery = pipe(\n convexDatabaseReader.query(tableName),\n applyWithIndex,\n applyOrder,\n );\n\n return OrderedQuery.make<\n DataModel.TableInfoWithName_<DataModel_, TableName>,\n TableName\n >(\n orderedQuery,\n tableName,\n table.Fields as TableInfo.TableSchema<\n DataModel.TableInfoWithName_<DataModel_, TableName>\n >,\n );\n };\n\n const search: QueryInitializerFunction<\"search\"> = (\n indexName,\n searchFilter,\n ) =>\n OrderedQuery.make<\n DataModel.TableInfoWithName_<DataModel_, TableName>,\n TableName\n >(\n convexDatabaseReader\n .query(tableName)\n .withSearchIndex(indexName, searchFilter),\n tableName,\n table.Fields as TableInfo.TableSchema<\n DataModel.TableInfoWithName_<DataModel_, TableName>\n >,\n );\n\n return {\n get,\n index,\n search,\n };\n};\n\nexport const getById =\n <Tables extends Table.AnyWithProps, TableName extends Table.Name<Tables>>(\n tableName: TableName,\n convexDatabaseReader: BaseDatabaseReader<\n DataModel.ToConvex<DataModel.FromTables<Tables>>\n >,\n table: Table.WithName<Tables, TableName>,\n ) =>\n (id: GenericId<TableName>) =>\n pipe(\n Effect.promise(() => convexDatabaseReader.get(id)),\n Effect.andThen(\n Either.fromNullable(() => new GetByIdFailure({ tableName, id })),\n ),\n Effect.andThen(Document.decode(tableName, table.Fields)),\n );\n\nexport class GetByIdFailure extends Schema.TaggedError<GetByIdFailure>()(\n \"GetByIdFailure\",\n {\n id: Schema.String,\n tableName: Schema.String,\n },\n) {\n override get message(): string {\n return Document.documentErrorMessage({\n id: this.id,\n tableName: this.tableName,\n message: \"not found\",\n });\n }\n}\n\nexport class GetByIndexFailure extends Schema.TaggedError<GetByIndexFailure>()(\n \"GetByIndexFailure\",\n {\n tableName: Schema.String,\n indexName: Schema.String,\n indexFieldValues: Schema.Array(Schema.String),\n },\n) {\n override get message(): string {\n return `No documents found in table '${this.tableName}' with index '${this.indexName}' and field values '${this.indexFieldValues}'`;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAqFA,MAAa,QAIX,WACA,sBAGA,UAMG;CAaH,MAAM,cAKJ,WACA,qBAQG;EACH,MAAM,cACJ,MAAM,QACN;AAEF,SAAO,KACL,OAAO,cACL,qBACG,MAAM,UAAU,CAChB,UAAU,YAAY,MACrB,MAAM,OACJ,kBACA,IACC,IAAI,GAAG,MAAM,GAAG,GAAG,YAAY,IAAW,EAAS,CACrD,CACF,CACA,QAAQ,CACZ,EACD,OAAO,QACL,OAAO,mBAEH,IAAI,kBAAkB;GACpB;GACW;GACO;GACnB,CAAC,CACL,CACF,EACD,OAAO,QAAQA,OAAgB,WAAW,MAAM,OAAO,CAAC,CACzD;;CAGH,MAAM,QACJ,GAAG,SACA;AACH,MAAI,KAAK,WAAW,GAAG;GACrB,MAAM,KAAK,KAAK;AAEhB,UAAO,QAAQ,WAAW,sBAAsB,MAAM,CAAC,GAAG;SACrD;GACL,MAAM,CAAC,WAAW,GAAG,oBAAoB;AAEzC,UAAO,WACL,WAGA,iBACD;;;CAIL,MAAM,SAKJ,WACA,mBAeA,UACG;EACH,MAAM,EACJ,gBACA,eAWA,sBAAsB,SAClB;GACE,iBAAiB,MAAM,EAAE,UAAU,UAAU;GAC7C,aAAa,MAAM,EAAE,MAAM,MAAM;GAClC,GACD,OAAO,sBAAsB,aAC3B,UAAU,SACR;GACE,iBAAiB,MACf,EAAE,UAAU,WAAW,kBAAkB;GAC3C,aAAa,MAAM,EAAE,MAAM,MAAM;GAClC,GACD;GACE,iBAAiB,MACf,EAAE,UAAU,WAAW,kBAAkB;GAC3C,aAAa,MAAM,EAAE,MAAM,MAAM;GAClC,GACH;GACE,iBAAiB,MAAM,EAAE,UAAU,UAAU;GAC7C,aAAa,MAAM,EAAE,MAAM,kBAAkB;GAC9C;EAET,MAAM,eAAe,KACnB,qBAAqB,MAAM,UAAU,EACrC,gBACA,WACD;AAED,SAAOC,OAIL,cACA,WACA,MAAM,OAGP;;CAGH,MAAM,UACJ,WACA,iBAEAA,OAIE,qBACG,MAAM,UAAU,CAChB,gBAAgB,WAAW,aAAa,EAC3C,WACA,MAAM,OAGP;AAEH,QAAO;EACL;EACA;EACA;EACD;;AAGH,MAAa,WAET,WACA,sBAGA,WAED,OACC,KACE,OAAO,cAAc,qBAAqB,IAAI,GAAG,CAAC,EAClD,OAAO,QACL,OAAO,mBAAmB,IAAI,eAAe;CAAE;CAAW;CAAI,CAAC,CAAC,CACjE,EACD,OAAO,QAAQD,OAAgB,WAAW,MAAM,OAAO,CAAC,CACzD;AAEL,IAAa,iBAAb,cAAoC,OAAO,aAA6B,CACtE,kBACA;CACE,IAAI,OAAO;CACX,WAAW,OAAO;CACnB,CACF,CAAC;CACA,IAAa,UAAkB;AAC7B,SAAOE,qBAA8B;GACnC,IAAI,KAAK;GACT,WAAW,KAAK;GAChB,SAAS;GACV,CAAC;;;AAIN,IAAa,oBAAb,cAAuC,OAAO,aAAgC,CAC5E,qBACA;CACE,WAAW,OAAO;CAClB,WAAW,OAAO;CAClB,kBAAkB,OAAO,MAAM,OAAO,OAAO;CAC9C,CACF,CAAC;CACA,IAAa,UAAkB;AAC7B,SAAO,gCAAgC,KAAK,UAAU,gBAAgB,KAAK,UAAU,sBAAsB,KAAK,iBAAiB"}
1
+ {"version":3,"file":"QueryInitializer.js","names":["Document.decode","OrderedQuery.make","Document.documentErrorMessage"],"sources":["../src/QueryInitializer.ts"],"sourcesContent":["import type {\n OrderedQuery as ConvexOrderedQuery,\n QueryInitializer as ConvexQueryInitializer,\n DocumentByInfo,\n GenericTableIndexes,\n GenericTableInfo,\n Indexes,\n IndexRange,\n IndexRangeBuilder,\n NamedIndex,\n NamedSearchIndex,\n NamedTableInfo,\n Query,\n SearchFilter,\n SearchFilterBuilder,\n SearchIndexes,\n} from \"convex/server\";\nimport type { GenericId } from \"convex/values\";\nimport { pipe } from \"effect/Function\";\nimport * as Array from \"effect/Array\";\nimport * as Effect from \"effect/Effect\";\nimport * as Either from \"effect/Either\";\nimport * as Schema from \"effect/Schema\";\nimport type {\n BaseDatabaseReader,\n IndexFieldTypesForEq,\n} from \"@confect/core/Types\";\nimport type * as DataModel from \"./DataModel\";\nimport * as Document from \"./Document\";\nimport * as OrderedQuery from \"./OrderedQuery\";\nimport type * as Table from \"./Table\";\nimport type * as TableInfo from \"./TableInfo\";\n\ntype QueryInitializer<\n DataModel_ extends DataModel.AnyWithProps,\n TableName extends DataModel.TableNames<DataModel_>,\n _ConvexTableInfo extends GenericTableInfo,\n _TableInfo extends TableInfo.AnyWithProps,\n> = {\n readonly get: {\n (\n id: GenericId<TableName>,\n ): Effect.Effect<\n _TableInfo[\"document\"],\n Document.DocumentDecodeError | GetByIdFailure\n >;\n <IndexName extends keyof Indexes<_ConvexTableInfo>>(\n indexName: IndexName,\n ...indexFieldValues: IndexFieldTypesForEq<\n DataModel.ToConvex<DataModel_>,\n TableName,\n Indexes<_ConvexTableInfo>[IndexName]\n >\n ): Effect.Effect<\n _TableInfo[\"document\"],\n Document.DocumentDecodeError | GetByIndexFailure\n >;\n };\n readonly index: {\n <IndexName extends keyof Indexes<_ConvexTableInfo>>(\n indexName: IndexName,\n indexRange?: (\n q: IndexRangeBuilder<\n _TableInfo[\"convexDocument\"],\n NamedIndex<_ConvexTableInfo, IndexName>\n >,\n ) => IndexRange,\n order?: \"asc\" | \"desc\",\n ): OrderedQuery.OrderedQuery<_TableInfo, TableName>;\n <IndexName extends keyof Indexes<_ConvexTableInfo>>(\n indexName: IndexName,\n order?: \"asc\" | \"desc\",\n ): OrderedQuery.OrderedQuery<_TableInfo, TableName>;\n };\n readonly search: <IndexName extends keyof SearchIndexes<_ConvexTableInfo>>(\n indexName: IndexName,\n searchFilter: (\n q: SearchFilterBuilder<\n DocumentByInfo<_ConvexTableInfo>,\n NamedSearchIndex<_ConvexTableInfo, IndexName>\n >,\n ) => SearchFilter,\n ) => OrderedQuery.OrderedQuery<_TableInfo, TableName>;\n};\n\nexport const make = <\n Tables extends Table.AnyWithProps,\n TableName extends Table.Name<Tables>,\n>(\n tableName: TableName,\n convexDatabaseReader: BaseDatabaseReader<\n DataModel.ToConvex<DataModel.FromTables<Tables>>\n >,\n table: Table.WithName<Tables, TableName>,\n): QueryInitializer<\n DataModel.DataModel<Tables>,\n TableName,\n DataModel.TableInfoWithName<DataModel.DataModel<Tables>, TableName>,\n DataModel.TableInfoWithName_<DataModel.DataModel<Tables>, TableName>\n> => {\n type DataModel_ = DataModel.DataModel<Tables>;\n type ConvexDataModel_ = DataModel.ToConvex<DataModel_>;\n type ThisQueryInitializer = QueryInitializer<\n DataModel_,\n TableName,\n DataModel.TableInfoWithName<DataModel_, TableName>,\n DataModel.TableInfoWithName_<DataModel_, TableName>\n >;\n type QueryInitializerFunction<\n FunctionName extends keyof ThisQueryInitializer,\n > = ThisQueryInitializer[FunctionName];\n\n const getByIndex = <\n IndexName extends keyof Indexes<\n DataModel.TableInfoWithName<DataModel_, TableName>\n >,\n >(\n indexName: IndexName,\n indexFieldValues: IndexFieldTypesForEq<\n DataModel.ToConvex<DataModel_>,\n TableName,\n Indexes<DataModel.TableInfoWithName<DataModel_, TableName>>[IndexName]\n >,\n ): Effect.Effect<\n DataModel.DocumentWithName<DataModel_, TableName>,\n Document.DocumentDecodeError | GetByIndexFailure\n > => {\n const indexFields: GenericTableIndexes[keyof GenericTableIndexes] = (\n table.indexes as GenericTableIndexes\n )[indexName as keyof GenericTableIndexes]!;\n\n return pipe(\n Effect.promise(() =>\n convexDatabaseReader\n .query(tableName)\n .withIndex(indexName, (q) =>\n Array.reduce(\n indexFieldValues,\n q,\n (q_, v, i) => q_.eq(indexFields[i] as any, v as any) as any,\n ),\n )\n .unique(),\n ),\n Effect.andThen(\n Either.fromNullable(\n () =>\n new GetByIndexFailure({\n tableName,\n indexName: indexName as string,\n indexFieldValues,\n }),\n ),\n ),\n Effect.andThen(Document.decode(tableName, table.Fields)),\n );\n };\n\n const get: QueryInitializerFunction<\"get\"> = ((\n ...args: Parameters<QueryInitializerFunction<\"get\">>\n ) => {\n if (args.length === 1) {\n const id = args[0] as GenericId<TableName>;\n\n return getById(tableName, convexDatabaseReader, table)(id);\n } else {\n const [indexName, ...indexFieldValues] = args;\n\n return getByIndex(\n indexName as keyof Indexes<\n DataModel.TableInfoWithName<DataModel_, TableName>\n >,\n indexFieldValues,\n );\n }\n }) as QueryInitializerFunction<\"get\">;\n\n const index: QueryInitializerFunction<\"index\"> = <\n IndexName extends keyof Indexes<\n DataModel.TableInfoWithName<DataModel_, TableName>\n >,\n >(\n indexName: IndexName,\n indexRangeOrOrder?:\n | ((\n q: IndexRangeBuilder<\n DataModel.TableInfoWithName_<\n DataModel_,\n TableName\n >[\"convexDocument\"],\n NamedIndex<\n DataModel.TableInfoWithName<DataModel_, TableName>,\n IndexName\n >\n >,\n ) => IndexRange)\n | \"asc\"\n | \"desc\",\n order?: \"asc\" | \"desc\",\n ) => {\n const {\n applyWithIndex,\n applyOrder,\n }: {\n applyWithIndex: (\n queryInitializer: ConvexQueryInitializer<\n NamedTableInfo<ConvexDataModel_, TableName>\n >,\n ) => Query<NamedTableInfo<ConvexDataModel_, TableName>>;\n applyOrder: (\n query: Query<NamedTableInfo<ConvexDataModel_, TableName>>,\n ) => ConvexOrderedQuery<NamedTableInfo<ConvexDataModel_, TableName>>;\n } =\n indexRangeOrOrder === undefined\n ? {\n applyWithIndex: (q) => q.withIndex(indexName),\n applyOrder: (q) => q.order(\"asc\"),\n }\n : typeof indexRangeOrOrder === \"function\"\n ? order === undefined\n ? {\n applyWithIndex: (q) =>\n q.withIndex(indexName, indexRangeOrOrder),\n applyOrder: (q) => q.order(\"asc\"),\n }\n : {\n applyWithIndex: (q) =>\n q.withIndex(indexName, indexRangeOrOrder),\n applyOrder: (q) => q.order(order),\n }\n : {\n applyWithIndex: (q) => q.withIndex(indexName),\n applyOrder: (q) => q.order(indexRangeOrOrder),\n };\n\n const orderedQuery = pipe(\n convexDatabaseReader.query(tableName),\n applyWithIndex,\n applyOrder,\n );\n\n return OrderedQuery.make<\n DataModel.TableInfoWithName_<DataModel_, TableName>,\n TableName\n >(\n orderedQuery,\n tableName,\n table.Fields as TableInfo.TableSchema<\n DataModel.TableInfoWithName_<DataModel_, TableName>\n >,\n );\n };\n\n const search: QueryInitializerFunction<\"search\"> = (\n indexName,\n searchFilter,\n ) =>\n OrderedQuery.make<\n DataModel.TableInfoWithName_<DataModel_, TableName>,\n TableName\n >(\n convexDatabaseReader\n .query(tableName)\n .withSearchIndex(indexName, searchFilter),\n tableName,\n table.Fields as TableInfo.TableSchema<\n DataModel.TableInfoWithName_<DataModel_, TableName>\n >,\n );\n\n return {\n get,\n index,\n search,\n };\n};\n\nexport const getById =\n <Tables extends Table.AnyWithProps, TableName extends Table.Name<Tables>>(\n tableName: TableName,\n convexDatabaseReader: BaseDatabaseReader<\n DataModel.ToConvex<DataModel.FromTables<Tables>>\n >,\n table: Table.WithName<Tables, TableName>,\n ) =>\n (id: GenericId<TableName>) =>\n pipe(\n Effect.promise(() => convexDatabaseReader.get(id)),\n Effect.andThen(\n Either.fromNullable(() => new GetByIdFailure({ tableName, id })),\n ),\n Effect.andThen(Document.decode(tableName, table.Fields)),\n );\n\nexport class GetByIdFailure extends Schema.TaggedError<GetByIdFailure>()(\n \"GetByIdFailure\",\n {\n id: Schema.String,\n tableName: Schema.String,\n },\n) {\n override get message(): string {\n return Document.documentErrorMessage({\n id: this.id,\n tableName: this.tableName,\n message: \"not found\",\n });\n }\n}\n\nexport class GetByIndexFailure extends Schema.TaggedError<GetByIndexFailure>()(\n \"GetByIndexFailure\",\n {\n tableName: Schema.String,\n indexName: Schema.String,\n indexFieldValues: Schema.Array(Schema.Unknown),\n },\n) {\n override get message(): string {\n return `No documents found in table '${this.tableName}' with index '${this.indexName}' and field values '${JSON.stringify(\n this.indexFieldValues,\n (_key, value) => (typeof value === \"bigint\" ? value.toString() : value),\n )}'`;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAqFA,MAAa,QAIX,WACA,sBAGA,UAMG;CAaH,MAAM,cAKJ,WACA,qBAQG;EACH,MAAM,cACJ,MAAM,QACN;AAEF,SAAO,KACL,OAAO,cACL,qBACG,MAAM,UAAU,CAChB,UAAU,YAAY,MACrB,MAAM,OACJ,kBACA,IACC,IAAI,GAAG,MAAM,GAAG,GAAG,YAAY,IAAW,EAAS,CACrD,CACF,CACA,QAAQ,CACZ,EACD,OAAO,QACL,OAAO,mBAEH,IAAI,kBAAkB;GACpB;GACW;GACX;GACD,CAAC,CACL,CACF,EACD,OAAO,QAAQA,OAAgB,WAAW,MAAM,OAAO,CAAC,CACzD;;CAGH,MAAM,QACJ,GAAG,SACA;AACH,MAAI,KAAK,WAAW,GAAG;GACrB,MAAM,KAAK,KAAK;AAEhB,UAAO,QAAQ,WAAW,sBAAsB,MAAM,CAAC,GAAG;SACrD;GACL,MAAM,CAAC,WAAW,GAAG,oBAAoB;AAEzC,UAAO,WACL,WAGA,iBACD;;;CAIL,MAAM,SAKJ,WACA,mBAeA,UACG;EACH,MAAM,EACJ,gBACA,eAWA,sBAAsB,SAClB;GACE,iBAAiB,MAAM,EAAE,UAAU,UAAU;GAC7C,aAAa,MAAM,EAAE,MAAM,MAAM;GAClC,GACD,OAAO,sBAAsB,aAC3B,UAAU,SACR;GACE,iBAAiB,MACf,EAAE,UAAU,WAAW,kBAAkB;GAC3C,aAAa,MAAM,EAAE,MAAM,MAAM;GAClC,GACD;GACE,iBAAiB,MACf,EAAE,UAAU,WAAW,kBAAkB;GAC3C,aAAa,MAAM,EAAE,MAAM,MAAM;GAClC,GACH;GACE,iBAAiB,MAAM,EAAE,UAAU,UAAU;GAC7C,aAAa,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