@confect/core 10.0.0-next.17 → 10.0.0-next.19

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 (68) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/dist/FunctionProvenance.d.ts +81 -60
  3. package/dist/FunctionProvenance.d.ts.map +1 -1
  4. package/dist/FunctionProvenance.js +9 -9
  5. package/dist/FunctionProvenance.js.map +1 -1
  6. package/dist/FunctionSpec.d.ts +256 -144
  7. package/dist/FunctionSpec.d.ts.map +1 -1
  8. package/dist/FunctionSpec.js +25 -20
  9. package/dist/FunctionSpec.js.map +1 -1
  10. package/dist/GroupPath.d.ts +1 -4
  11. package/dist/GroupPath.d.ts.map +1 -1
  12. package/dist/GroupPath.js.map +1 -1
  13. package/dist/GroupSpec.d.ts +5 -3
  14. package/dist/GroupSpec.d.ts.map +1 -1
  15. package/dist/GroupSpec.js +1 -1
  16. package/dist/GroupSpec.js.map +1 -1
  17. package/dist/Lazy.d.ts +2 -3
  18. package/dist/Lazy.d.ts.map +1 -1
  19. package/dist/Lazy.js +2 -3
  20. package/dist/Lazy.js.map +1 -1
  21. package/dist/MiddlewareSpec.d.ts +2 -2
  22. package/dist/MiddlewareSpec.d.ts.map +1 -1
  23. package/dist/MiddlewareSpec.js +1 -1
  24. package/dist/MiddlewareSpec.js.map +1 -1
  25. package/dist/PaginationError.d.ts +14 -0
  26. package/dist/PaginationError.d.ts.map +1 -0
  27. package/dist/PaginationError.js +25 -0
  28. package/dist/PaginationError.js.map +1 -0
  29. package/dist/Ref.d.ts +66 -20
  30. package/dist/Ref.d.ts.map +1 -1
  31. package/dist/Ref.js +3 -3
  32. package/dist/Ref.js.map +1 -1
  33. package/dist/Refs.d.ts +1 -1
  34. package/dist/Refs.d.ts.map +1 -1
  35. package/dist/Refs.js.map +1 -1
  36. package/dist/SchemaToValidator.d.ts +99 -0
  37. package/dist/SchemaToValidator.d.ts.map +1 -0
  38. package/dist/SchemaToValidator.js +156 -0
  39. package/dist/SchemaToValidator.js.map +1 -0
  40. package/dist/Spec.d.ts +3 -2
  41. package/dist/Spec.d.ts.map +1 -1
  42. package/dist/Spec.js +1 -1
  43. package/dist/Spec.js.map +1 -1
  44. package/dist/Table.d.ts +74 -0
  45. package/dist/Table.d.ts.map +1 -0
  46. package/dist/Table.js +77 -0
  47. package/dist/Table.js.map +1 -0
  48. package/dist/Types.d.ts +8 -2
  49. package/dist/Types.d.ts.map +1 -1
  50. package/dist/Types.js.map +1 -1
  51. package/dist/index.d.ts +3 -0
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +4 -1
  54. package/package.json +4 -4
  55. package/src/FunctionProvenance.ts +84 -71
  56. package/src/FunctionSpec.ts +193 -110
  57. package/src/GroupPath.ts +1 -6
  58. package/src/GroupSpec.ts +6 -6
  59. package/src/Lazy.ts +2 -3
  60. package/src/MiddlewareSpec.ts +2 -2
  61. package/src/PaginationError.ts +72 -0
  62. package/src/Ref.ts +164 -153
  63. package/src/Refs.ts +1 -0
  64. package/src/SchemaToValidator.ts +601 -0
  65. package/src/Spec.ts +4 -4
  66. package/src/Table.ts +426 -0
  67. package/src/Types.ts +9 -3
  68. package/src/index.ts +3 -0
package/src/Table.ts ADDED
@@ -0,0 +1,426 @@
1
+ import * as Lazy from "./Lazy";
2
+ import * as SystemFields from "./SystemFields";
3
+ import type { TableSchemaToTableValidator } from "./SchemaToValidator";
4
+ import type {
5
+ SystemFields as ConvexSystemFields,
6
+ Expand,
7
+ GenericTableIndexes,
8
+ GenericTableSearchIndexes,
9
+ GenericTableVectorIndexes,
10
+ IndexTiebreakerField,
11
+ SearchIndexConfig,
12
+ VectorIndexConfig,
13
+ } from "convex/server";
14
+ import type { GenericValidator, Validator } from "convex/values";
15
+ import * as Predicate from "effect/Predicate";
16
+ import type * as Schema from "effect/Schema";
17
+
18
+ export const TypeId = "~@confect/core/Table";
19
+ export type TypeId = typeof TypeId;
20
+
21
+ // -----------------------------------------------------------------------------
22
+ // Predicates
23
+ // -----------------------------------------------------------------------------
24
+ //
25
+ // Both bound `Table`s and `UnnamedTable` callables share the same `[TypeId]`
26
+ // brand. They disambiguate by whether a `tableName` property is set: bound
27
+ // tables have one, unnamed callables do not.
28
+ //
29
+ // The discriminator is `tableName` (not `name`) so it does not collide with
30
+ // the built-in `Function.prototype.name` that every JS function carries.
31
+
32
+ export const isTable = (u: unknown): u is Any =>
33
+ Predicate.hasProperty(u, TypeId) && Predicate.hasProperty(u, "tableName");
34
+
35
+ export const isUnnamedTable = (u: unknown): u is UnnamedAny =>
36
+ Predicate.hasProperty(u, TypeId) && !Predicate.hasProperty(u, "tableName");
37
+
38
+ // -----------------------------------------------------------------------------
39
+ // Bound Table
40
+ // -----------------------------------------------------------------------------
41
+ //
42
+ // A bound `Table` is the client-safe document schema: `Fields`, `Doc`, and
43
+ // the index metadata records. The Convex deploy `tableDefinition` (which
44
+ // value-imports `convex/server`) is produced separately by
45
+ // `@confect/server/Table.tableDefinition`.
46
+
47
+ export interface Table<
48
+ Name_ extends string,
49
+ TableSchema_ extends Schema.Codec<any, any>,
50
+ TableValidator_ extends GenericValidator =
51
+ TableSchemaToTableValidator<TableSchema_>,
52
+ Indexes_ extends GenericTableIndexes = {},
53
+ SearchIndexes_ extends GenericTableSearchIndexes = {},
54
+ VectorIndexes_ extends GenericTableVectorIndexes = {},
55
+ > {
56
+ readonly [TypeId]: TypeId;
57
+ readonly tableName: Name_;
58
+ readonly Fields: TableSchema_;
59
+ readonly Doc: SystemFields.ExtendWithSystemFields<Name_, TableSchema_>;
60
+ readonly indexes: Indexes_;
61
+ readonly searchIndexes: SearchIndexes_;
62
+ readonly vectorIndexes: VectorIndexes_;
63
+ readonly "~TableValidator": TableValidator_;
64
+ }
65
+
66
+ export interface Any {
67
+ readonly [TypeId]: TypeId;
68
+ readonly tableName: string;
69
+ }
70
+
71
+ export type AnyWithProps = Table<
72
+ any,
73
+ Schema.Codec<any, any>,
74
+ GenericValidator,
75
+ GenericTableIndexes,
76
+ GenericTableSearchIndexes,
77
+ GenericTableVectorIndexes
78
+ >;
79
+
80
+ // -----------------------------------------------------------------------------
81
+ // UnnamedTable (callable)
82
+ // -----------------------------------------------------------------------------
83
+ //
84
+ // `Table.make(lazyFields)` returns an `UnnamedTable`: a callable that
85
+ // produces a fully bound `Table` when invoked with a name. Chaining methods
86
+ // (`.index`, `.searchIndex`, `.vectorIndex`) live here and return new
87
+ // `UnnamedTable`s, accumulating plain index metadata records. The
88
+ // user-supplied `lazyFields` callback is just carried through until first
89
+ // access of `Fields` / `Doc` on a bound table. The codegen pipeline emits a
90
+ // wrapper file per user-authored table that simply invokes the unnamed
91
+ // callable with the filename basename.
92
+
93
+ export interface UnnamedTable<
94
+ TableSchema_ extends Schema.Codec<any, any>,
95
+ TableValidator_ extends GenericValidator =
96
+ TableSchemaToTableValidator<TableSchema_>,
97
+ Indexes_ extends GenericTableIndexes = {},
98
+ SearchIndexes_ extends GenericTableSearchIndexes = {},
99
+ VectorIndexes_ extends GenericTableVectorIndexes = {},
100
+ > {
101
+ <const Name_ extends string>(
102
+ tableName: Name_,
103
+ ): Table<
104
+ Name_,
105
+ TableSchema_,
106
+ TableValidator_,
107
+ Indexes_,
108
+ SearchIndexes_,
109
+ VectorIndexes_
110
+ >;
111
+
112
+ readonly [TypeId]: TypeId;
113
+ readonly indexes: Indexes_;
114
+ readonly searchIndexes: SearchIndexes_;
115
+ readonly vectorIndexes: VectorIndexes_;
116
+ readonly "~TableValidator": TableValidator_;
117
+
118
+ index<
119
+ IndexName extends string,
120
+ FirstFieldPath extends ExtractFieldPaths<TableValidator_>,
121
+ RestFieldPaths extends ExtractFieldPaths<TableValidator_>[],
122
+ >(
123
+ name: IndexName,
124
+ fields: [FirstFieldPath, ...RestFieldPaths],
125
+ ): UnnamedTable<
126
+ TableSchema_,
127
+ TableValidator_,
128
+ Expand<
129
+ Indexes_ &
130
+ Record<
131
+ IndexName,
132
+ [FirstFieldPath, ...RestFieldPaths, IndexTiebreakerField]
133
+ >
134
+ >,
135
+ SearchIndexes_,
136
+ VectorIndexes_
137
+ >;
138
+
139
+ searchIndex<
140
+ IndexName extends string,
141
+ SearchField extends ExtractFieldPaths<TableValidator_>,
142
+ FilterFields extends ExtractFieldPaths<TableValidator_> = never,
143
+ >(
144
+ name: IndexName,
145
+ indexConfig: Expand<SearchIndexConfig<SearchField, FilterFields>>,
146
+ ): UnnamedTable<
147
+ TableSchema_,
148
+ TableValidator_,
149
+ Indexes_,
150
+ Expand<
151
+ SearchIndexes_ &
152
+ Record<
153
+ IndexName,
154
+ {
155
+ searchField: SearchField;
156
+ filterFields: FilterFields;
157
+ }
158
+ >
159
+ >,
160
+ VectorIndexes_
161
+ >;
162
+
163
+ vectorIndex<
164
+ IndexName extends string,
165
+ VectorField extends ExtractFieldPaths<TableValidator_>,
166
+ FilterFields extends ExtractFieldPaths<TableValidator_> = never,
167
+ >(
168
+ name: IndexName,
169
+ indexConfig: Expand<VectorIndexConfig<VectorField, FilterFields>>,
170
+ ): UnnamedTable<
171
+ TableSchema_,
172
+ TableValidator_,
173
+ Indexes_,
174
+ SearchIndexes_,
175
+ Expand<
176
+ VectorIndexes_ &
177
+ Record<
178
+ IndexName,
179
+ {
180
+ vectorField: VectorField;
181
+ dimensions: number;
182
+ filterFields: FilterFields;
183
+ }
184
+ >
185
+ >
186
+ >;
187
+ }
188
+
189
+ export interface UnnamedAny {
190
+ readonly [TypeId]: TypeId;
191
+ }
192
+
193
+ export type UnnamedAnyWithProps = UnnamedTable<
194
+ Schema.Codec<any, any>,
195
+ GenericValidator,
196
+ GenericTableIndexes,
197
+ GenericTableSearchIndexes,
198
+ GenericTableVectorIndexes
199
+ >;
200
+
201
+ // -----------------------------------------------------------------------------
202
+ // Type extractors
203
+ // -----------------------------------------------------------------------------
204
+
205
+ export type Name<TableDef extends AnyWithProps> = TableDef["tableName"] &
206
+ string;
207
+
208
+ export type TableSchema<TableDef extends AnyWithProps> = TableDef["Fields"];
209
+
210
+ export type TableValidator<TableDef extends AnyWithProps> =
211
+ TableDef["~TableValidator"];
212
+
213
+ export type Indexes<TableDef extends AnyWithProps> = TableDef["indexes"];
214
+
215
+ export type SearchIndexes<TableDef extends AnyWithProps> =
216
+ TableDef["searchIndexes"];
217
+
218
+ export type VectorIndexes<TableDef extends AnyWithProps> =
219
+ TableDef["vectorIndexes"];
220
+
221
+ export type Doc<TableDef extends AnyWithProps> = TableDef["Doc"];
222
+
223
+ export type Fields<TableDef extends AnyWithProps> = TableDef["Fields"];
224
+
225
+ export type WithName<
226
+ TableDef extends AnyWithProps,
227
+ Name_ extends string,
228
+ > = TableDef extends { readonly tableName: Name_ } ? TableDef : never;
229
+
230
+ export type TablesRecord<Tables extends AnyWithProps> = {
231
+ readonly [TableName_ in Name<Tables>]: WithName<Tables, TableName_>;
232
+ };
233
+
234
+ // -----------------------------------------------------------------------------
235
+ // Construction
236
+ // -----------------------------------------------------------------------------
237
+ //
238
+ // `make` only stores the user-supplied `lazyFields` callback alongside any
239
+ // chained index metadata. Neither `Fields` nor `Doc` is constructed until
240
+ // first access on a bound `Table`. Each chain step is O(1) (plain object
241
+ // spread of the metadata records) and never invokes the callback. Binding
242
+ // via `unnamed(tableName)` installs lazy memoised getters for `Fields` and
243
+ // `Doc` via `Lazy.defineProperty`, so the first access materialises the
244
+ // value and replaces the getter with a plain data property — second-and-
245
+ // subsequent accesses are observably indistinguishable from a plain
246
+ // property and avoid all function-call overhead.
247
+
248
+ interface UnnamedState<
249
+ TableSchema_ extends Schema.Codec<any, any>,
250
+ Indexes_ extends GenericTableIndexes,
251
+ SearchIndexes_ extends GenericTableSearchIndexes,
252
+ VectorIndexes_ extends GenericTableVectorIndexes,
253
+ > {
254
+ readonly lazyFields: () => TableSchema_;
255
+ readonly indexes: Indexes_;
256
+ readonly searchIndexes: SearchIndexes_;
257
+ readonly vectorIndexes: VectorIndexes_;
258
+ }
259
+
260
+ const makeBound = <
261
+ Name_ extends string,
262
+ TableSchema_ extends Schema.Codec<any, any>,
263
+ TableValidator_ extends Validator<any, any, any>,
264
+ Indexes_ extends GenericTableIndexes,
265
+ SearchIndexes_ extends GenericTableSearchIndexes,
266
+ VectorIndexes_ extends GenericTableVectorIndexes,
267
+ >(
268
+ tableName: Name_,
269
+ state: UnnamedState<TableSchema_, Indexes_, SearchIndexes_, VectorIndexes_>,
270
+ ): Table<
271
+ Name_,
272
+ TableSchema_,
273
+ TableValidator_,
274
+ Indexes_,
275
+ SearchIndexes_,
276
+ VectorIndexes_
277
+ > => {
278
+ const bound = {
279
+ [TypeId]: TypeId as TypeId,
280
+ tableName,
281
+ indexes: state.indexes,
282
+ searchIndexes: state.searchIndexes,
283
+ vectorIndexes: state.vectorIndexes,
284
+ } as Table<
285
+ Name_,
286
+ TableSchema_,
287
+ TableValidator_,
288
+ Indexes_,
289
+ SearchIndexes_,
290
+ VectorIndexes_
291
+ >;
292
+
293
+ Lazy.defineProperty(bound, "Fields", () => state.lazyFields());
294
+
295
+ Lazy.defineProperty(bound, "Doc", () =>
296
+ SystemFields.extendWithSystemFields(
297
+ tableName,
298
+ (bound as { Fields: TableSchema_ }).Fields,
299
+ ),
300
+ );
301
+
302
+ return bound;
303
+ };
304
+
305
+ const makeUnnamed = <
306
+ TableSchema_ extends Schema.Codec<any, any>,
307
+ TableValidator_ extends Validator<any, any, any>,
308
+ Indexes_ extends GenericTableIndexes,
309
+ SearchIndexes_ extends GenericTableSearchIndexes,
310
+ VectorIndexes_ extends GenericTableVectorIndexes,
311
+ >(
312
+ state: UnnamedState<TableSchema_, Indexes_, SearchIndexes_, VectorIndexes_>,
313
+ ): UnnamedTable<
314
+ TableSchema_,
315
+ TableValidator_,
316
+ Indexes_,
317
+ SearchIndexes_,
318
+ VectorIndexes_
319
+ > => {
320
+ type UnnamedTable_ = UnnamedTable<
321
+ TableSchema_,
322
+ TableValidator_,
323
+ Indexes_,
324
+ SearchIndexes_,
325
+ VectorIndexes_
326
+ >;
327
+
328
+ type UnnamedTableFunction<FunctionName extends keyof UnnamedTable_> =
329
+ UnnamedTable_[FunctionName];
330
+
331
+ const bind = <const Name_ extends string>(
332
+ tableName: Name_,
333
+ ): Table<
334
+ Name_,
335
+ TableSchema_,
336
+ TableValidator_,
337
+ Indexes_,
338
+ SearchIndexes_,
339
+ VectorIndexes_
340
+ > =>
341
+ makeBound<
342
+ Name_,
343
+ TableSchema_,
344
+ TableValidator_,
345
+ Indexes_,
346
+ SearchIndexes_,
347
+ VectorIndexes_
348
+ >(tableName, state);
349
+
350
+ const index: UnnamedTableFunction<"index"> = (name, fields) =>
351
+ makeUnnamed({
352
+ lazyFields: state.lazyFields,
353
+ indexes: {
354
+ ...state.indexes,
355
+ [name]: fields,
356
+ } as any,
357
+ searchIndexes: state.searchIndexes,
358
+ vectorIndexes: state.vectorIndexes,
359
+ });
360
+
361
+ const searchIndex: UnnamedTableFunction<"searchIndex"> = (
362
+ name,
363
+ indexConfig,
364
+ ) =>
365
+ makeUnnamed({
366
+ lazyFields: state.lazyFields,
367
+ indexes: state.indexes,
368
+ searchIndexes: {
369
+ ...state.searchIndexes,
370
+ [name]: indexConfig,
371
+ } as any,
372
+ vectorIndexes: state.vectorIndexes,
373
+ });
374
+
375
+ const vectorIndex: UnnamedTableFunction<"vectorIndex"> = (
376
+ name,
377
+ indexConfig,
378
+ ) =>
379
+ makeUnnamed({
380
+ lazyFields: state.lazyFields,
381
+ indexes: state.indexes,
382
+ searchIndexes: state.searchIndexes,
383
+ vectorIndexes: {
384
+ ...state.vectorIndexes,
385
+ [name]: indexConfig,
386
+ } as any,
387
+ });
388
+
389
+ return Object.assign(bind, {
390
+ [TypeId]: TypeId as TypeId,
391
+ indexes: state.indexes,
392
+ searchIndexes: state.searchIndexes,
393
+ vectorIndexes: state.vectorIndexes,
394
+ index,
395
+ searchIndex,
396
+ vectorIndex,
397
+ }) as UnnamedTable_;
398
+ };
399
+
400
+ export const make = <const TableSchema_ extends Schema.Codec<any, any>>(
401
+ lazyFields: () => TableSchema_,
402
+ ): UnnamedTable<TableSchema_, TableSchemaToTableValidator<TableSchema_>> => {
403
+ type TableValidator_ = TableSchemaToTableValidator<TableSchema_>;
404
+ type UnnamedTable_ = UnnamedTable<TableSchema_, TableValidator_>;
405
+
406
+ return makeUnnamed<TableSchema_, TableValidator_, {}, {}, {}>({
407
+ lazyFields,
408
+ indexes: {},
409
+ searchIndexes: {},
410
+ vectorIndexes: {},
411
+ }) as UnnamedTable_;
412
+ };
413
+
414
+ // Vendored types from convex-js, partially modified. Ideally we could use these directly. See https://github.com/get-convex/convex-js/pull/14
415
+
416
+ /**
417
+ * Extract all of the index field paths within a Convex `Validator`.
418
+ *
419
+ * This is used when declaring table indexes.
420
+ * @public
421
+ */
422
+ type ExtractFieldPaths<T extends Validator<any, any, any>> =
423
+ // Add in the system fields available in index definitions.
424
+ // This should be everything except for `_id` because thats added to indexes
425
+ // automatically.
426
+ T["fieldPaths"] | keyof ConvexSystemFields;
package/src/Types.ts CHANGED
@@ -72,9 +72,15 @@ export type DeepMutable<T> =
72
72
  : DeepMutable<T[K]>;
73
73
  };
74
74
 
75
- export type TypeError<Message extends string, T = never> = [Message, T];
76
-
77
- export type TypeDefect<Message extends string, T = never> = [Message, T];
75
+ export interface TypeError<Message extends string, T = never> {
76
+ readonly "~@confect/core/Types/TypeError": Message;
77
+ readonly "~value"?: T;
78
+ }
79
+
80
+ export interface TypeDefect<Message extends string, T = never> {
81
+ readonly "~@confect/core/Types/TypeDefect": Message;
82
+ readonly "~value"?: T;
83
+ }
78
84
 
79
85
  export type IsRecursive<T> = true extends DetectCycle<T> ? true : false;
80
86
 
package/src/index.ts CHANGED
@@ -6,12 +6,15 @@ export * as GroupSpec from "./GroupSpec";
6
6
  export * as Identifier from "./Identifier";
7
7
  export * as Lazy from "./Lazy";
8
8
  export * as MiddlewareSpec from "./MiddlewareSpec";
9
+ export * as PaginationError from "./PaginationError";
9
10
  export * as PaginationOptions from "./PaginationOptions";
10
11
  export * as PaginationResult from "./PaginationResult";
11
12
  export * as Ref from "./Ref";
12
13
  export * as Refs from "./Refs";
13
14
  export * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
15
+ export * as SchemaToValidator from "./SchemaToValidator";
14
16
  export * as Spec from "./Spec";
15
17
  export * as SystemFields from "./SystemFields";
18
+ export * as Table from "./Table";
16
19
  export * as Types from "./Types";
17
20
  export * as UserIdentity from "./UserIdentity";