kitcn 0.12.9 → 0.12.11

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.
@@ -1,3420 +0,0 @@
1
- #!/usr/bin/env node
2
- import fs from "node:fs";
3
- import path from "node:path";
4
- import { createHash } from "node:crypto";
5
- import { createJiti } from "jiti";
6
- import { v } from "convex/values";
7
- import "convex/server";
8
- import { createColors } from "picocolors";
9
-
10
- //#region src/orm/builders/column-builder.ts
11
- /**
12
- * entityKind symbol for runtime type checking
13
- * Following Drizzle's pattern for type guards
14
- */
15
- const entityKind = Symbol.for("kitcn:entityKind");
16
- /**
17
- * Base ColumnBuilder abstract class
18
- *
19
- * All column builders inherit from this class.
20
- * Implements chaining methods and stores runtime config.
21
- */
22
- var ColumnBuilder = class {
23
- static [entityKind] = "ColumnBuilder";
24
- [entityKind] = "ColumnBuilder";
25
- /**
26
- * Runtime configuration - actual mutable state
27
- */
28
- config;
29
- constructor(name, dataType, columnType) {
30
- this.config = {
31
- name,
32
- notNull: false,
33
- default: void 0,
34
- hasDefault: false,
35
- primaryKey: false,
36
- isUnique: false,
37
- uniqueName: void 0,
38
- uniqueNulls: void 0,
39
- foreignKeyConfigs: [],
40
- dataType,
41
- columnType
42
- };
43
- }
44
- /**
45
- * Mark column as NOT NULL
46
- * Returns type-branded instance with notNull: true
47
- */
48
- notNull() {
49
- this.config.notNull = true;
50
- return this;
51
- }
52
- /**
53
- * Override the TypeScript type for this column.
54
- * Mirrors Drizzle's $type() (type-only, no runtime validation changes).
55
- */
56
- $type() {
57
- return this;
58
- }
59
- /**
60
- * Set default value for column
61
- * Makes field optional on insert
62
- */
63
- default(value) {
64
- this.config.default = value;
65
- this.config.hasDefault = true;
66
- return this;
67
- }
68
- /**
69
- * Set default function for column (runtime evaluated on insert).
70
- * Mirrors Drizzle's $defaultFn() / $default().
71
- */
72
- $defaultFn(fn) {
73
- this.config.defaultFn = fn;
74
- return this;
75
- }
76
- /**
77
- * Alias of $defaultFn for Drizzle parity.
78
- */
79
- $default(fn) {
80
- return this.$defaultFn(fn);
81
- }
82
- /**
83
- * Set on-update function for column (runtime evaluated on update).
84
- * Mirrors Drizzle's $onUpdateFn() / $onUpdate().
85
- */
86
- $onUpdateFn(fn) {
87
- this.config.onUpdateFn = fn;
88
- return this;
89
- }
90
- /**
91
- * Alias of $onUpdateFn for Drizzle parity.
92
- */
93
- $onUpdate(fn) {
94
- return this.$onUpdateFn(fn);
95
- }
96
- /**
97
- * Mark column as primary key
98
- * Implies NOT NULL
99
- */
100
- primaryKey() {
101
- this.config.primaryKey = true;
102
- this.config.notNull = true;
103
- return this;
104
- }
105
- /**
106
- * Mark column as UNIQUE
107
- * Mirrors Drizzle column unique API
108
- */
109
- unique(name, config) {
110
- this.config.isUnique = true;
111
- this.config.uniqueName = name;
112
- this.config.uniqueNulls = config?.nulls;
113
- return this;
114
- }
115
- /**
116
- * Define a foreign key reference
117
- * Mirrors Drizzle column references() API
118
- */
119
- references(ref, config = {}) {
120
- this.config.foreignKeyConfigs.push({
121
- ref,
122
- config
123
- });
124
- return this;
125
- }
126
- };
127
-
128
- //#endregion
129
- //#region src/orm/builders/system-fields.ts
130
- /**
131
- * System Fields - Convex-provided fields available on all documents
132
- *
133
- * id: Document ID (string, backed by internal Convex _id)
134
- * createdAt: Creation timestamp alias (backed by internal Convex _creationTime)
135
- *
136
- * These are automatically added to every Convex table.
137
- */
138
- var ConvexSystemIdBuilder = class extends ColumnBuilder {
139
- static [entityKind] = "ConvexSystemIdBuilder";
140
- [entityKind] = "ConvexSystemIdBuilder";
141
- constructor() {
142
- super("_id", "string", "ConvexSystemId");
143
- this.config.notNull = true;
144
- }
145
- build() {
146
- return v.string();
147
- }
148
- /**
149
- * Convex validator - runtime access
150
- * System fields use v.string() for _id
151
- */
152
- get convexValidator() {
153
- return this.build();
154
- }
155
- };
156
- var ConvexSystemCreationTimeBuilder = class extends ColumnBuilder {
157
- static [entityKind] = "ConvexSystemCreationTimeBuilder";
158
- [entityKind] = "ConvexSystemCreationTimeBuilder";
159
- constructor() {
160
- super("_creationTime", "number", "ConvexSystemCreationTime");
161
- this.config.notNull = true;
162
- }
163
- build() {
164
- return v.number();
165
- }
166
- /**
167
- * Convex validator - runtime access
168
- * System fields use v.number() for _creationTime
169
- */
170
- get convexValidator() {
171
- return this.build();
172
- }
173
- };
174
- var ConvexSystemCreatedAtBuilder = class extends ColumnBuilder {
175
- static [entityKind] = "ConvexSystemCreatedAtBuilder";
176
- [entityKind] = "ConvexSystemCreatedAtBuilder";
177
- constructor() {
178
- super("_creationTime", "number", "ConvexSystemCreatedAt");
179
- this.config.notNull = true;
180
- }
181
- build() {
182
- return v.number();
183
- }
184
- get convexValidator() {
185
- return this.build();
186
- }
187
- };
188
- function createSystemFields(tableName) {
189
- const id = new ConvexSystemIdBuilder();
190
- const creationTime = new ConvexSystemCreationTimeBuilder();
191
- const createdAt = new ConvexSystemCreatedAtBuilder();
192
- id.config.tableName = tableName;
193
- creationTime.config.tableName = tableName;
194
- createdAt.config.tableName = tableName;
195
- return {
196
- id,
197
- _creationTime: creationTime,
198
- createdAt
199
- };
200
- }
201
-
202
- //#endregion
203
- //#region src/orm/symbols.ts
204
- const TableName = Symbol.for("kitcn:TableName");
205
- const Columns = Symbol.for("kitcn:Columns");
206
- const Brand = Symbol.for("kitcn:Brand");
207
- const Relations = Symbol.for("kitcn:Relations");
208
- const OrmContext = Symbol.for("kitcn:OrmContext");
209
- const RlsPolicies = Symbol.for("kitcn:RlsPolicies");
210
- const EnableRLS = Symbol.for("kitcn:EnableRLS");
211
- const TableDeleteConfig = Symbol.for("kitcn:TableDeleteConfig");
212
- const TablePolymorphic = Symbol.for("kitcn:TablePolymorphic");
213
- const OrmSchemaOptions = Symbol.for("kitcn:OrmSchemaOptions");
214
- const OrmSchemaDefinition = Symbol.for("kitcn:OrmSchemaDefinition");
215
- const OrmSchemaExtensionTables = Symbol.for("kitcn:OrmSchemaExtensionTables");
216
- const OrmSchemaExtensions = Symbol.for("kitcn:OrmSchemaExtensions");
217
- const OrmSchemaExtensionRelations = Symbol.for("kitcn:OrmSchemaExtensionRelations");
218
- const OrmSchemaExtensionTriggers = Symbol.for("kitcn:OrmSchemaExtensionTriggers");
219
- const OrmSchemaRelations = Symbol.for("kitcn:OrmSchemaRelations");
220
- const OrmSchemaTriggers = Symbol.for("kitcn:OrmSchemaTriggers");
221
-
222
- //#endregion
223
- //#region src/orm/builders/convex-column-builder.ts
224
- /**
225
- * Convex-specific column builder base class
226
- *
227
- * All Convex column builders (ConvexTextBuilder, ConvexIntegerBuilder, etc.)
228
- * inherit from this class.
229
- */
230
- var ConvexColumnBuilder = class extends ColumnBuilder {
231
- static [entityKind] = "ConvexColumnBuilder";
232
- };
233
-
234
- //#endregion
235
- //#region src/orm/builders/boolean.ts
236
- /**
237
- * Boolean column builder class
238
- * Compiles to v.boolean() or v.optional(v.boolean())
239
- */
240
- var ConvexBooleanBuilder = class extends ConvexColumnBuilder {
241
- static [entityKind] = "ConvexBooleanBuilder";
242
- constructor(name) {
243
- super(name, "boolean", "ConvexBoolean");
244
- }
245
- /**
246
- * Expose Convex validator for schema integration
247
- */
248
- get convexValidator() {
249
- if (this.config.notNull) return v.boolean();
250
- return v.optional(v.union(v.null(), v.boolean()));
251
- }
252
- /**
253
- * Compile to Convex validator
254
- * .notNull() → v.boolean()
255
- * nullable → v.optional(v.boolean())
256
- */
257
- build() {
258
- return this.convexValidator;
259
- }
260
- };
261
- function boolean$1(name) {
262
- return new ConvexBooleanBuilder(name ?? "");
263
- }
264
-
265
- //#endregion
266
- //#region src/internal/upstream/validators.ts
267
- /** @deprecated Use `v.string()` instead. Any string value. */
268
- const string = v.string();
269
- /** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
270
- const number = v.float64();
271
- /** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
272
- const float64 = v.float64();
273
- /** @deprecated Use `v.boolean()` instead. boolean value. For typing it only as true, use `l(true)` */
274
- const boolean = v.boolean();
275
- /** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
276
- const biging = v.int64();
277
- /** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
278
- const int64 = v.int64();
279
- /** @deprecated Use `v.any()` instead. Any Convex value */
280
- const any = v.any();
281
- /** @deprecated Use `v.null()` instead. Null value. Underscore is so it doesn't shadow the null builtin */
282
- const null_ = v.null();
283
- /** @deprecated Use `v.*()` instead. */
284
- const { id: id$1, object, array, bytes, literal, optional, union } = v;
285
- /** @deprecated Use `v.bytes()` instead. ArrayBuffer validator. */
286
- const arrayBuffer = v.bytes();
287
- /** Mark fields as deprecated with this permissive validator typed as null */
288
- const deprecated = v.optional(v.any());
289
- /**
290
- * Converts an optional validator to a required validator.
291
- *
292
- * This is the inverse of `v.optional()`. It takes a validator that may be optional
293
- * and returns the equivalent required validator.
294
- *
295
- * ```ts
296
- * const optionalString = v.optional(v.string());
297
- * const requiredString = vRequired(optionalString); // v.string()
298
- *
299
- * // Already required validators are returned as-is
300
- * const alreadyRequired = v.string();
301
- * const stillRequired = vRequired(alreadyRequired); // v.string()
302
- * ```
303
- *
304
- * @param validator The validator to make required.
305
- * @returns A required version of the validator.
306
- */
307
- function vRequired(validator) {
308
- const { kind, isOptional } = validator;
309
- if (isOptional === "required") return validator;
310
- switch (kind) {
311
- case "id": return v.id(validator.tableName);
312
- case "string": return v.string();
313
- case "float64": return v.float64();
314
- case "int64": return v.int64();
315
- case "boolean": return v.boolean();
316
- case "null": return v.null();
317
- case "any": return v.any();
318
- case "literal": return v.literal(validator.value);
319
- case "bytes": return v.bytes();
320
- case "object": return v.object(validator.fields);
321
- case "array": return v.array(validator.element);
322
- case "record": return v.record(validator.key, validator.value);
323
- case "union": return v.union(...validator.members);
324
- default: throw new Error("Unknown Convex validator type: " + kind);
325
- }
326
- }
327
-
328
- //#endregion
329
- //#region src/orm/builders/custom.ts
330
- function isRecord$1(value) {
331
- return typeof value === "object" && value !== null && !Array.isArray(value);
332
- }
333
- function isValidator(value) {
334
- return isRecord$1(value) && typeof value.kind === "string" && typeof value.isOptional === "string";
335
- }
336
- function isColumnBuilder$1(value) {
337
- return isRecord$1(value) && value[entityKind] === "ColumnBuilder";
338
- }
339
- function toRequiredValidator(validator) {
340
- return validator.isOptional === "optional" ? vRequired(validator) : validator;
341
- }
342
- function toRequiredBuilderValidator(validator) {
343
- const requiredValidator = toRequiredValidator(validator);
344
- if (requiredValidator.kind !== "union") return requiredValidator;
345
- const nonNullMembers = requiredValidator.members.filter((member) => member.kind !== "null");
346
- if (nonNullMembers.length !== 1) return requiredValidator;
347
- const [member] = nonNullMembers;
348
- if (member.kind === "object" || member.kind === "array") return member;
349
- return requiredValidator;
350
- }
351
- function formatInvalidInput(path, value) {
352
- return `${path} expected a column builder, Convex validator, or nested object shape. Got ${Array.isArray(value) ? "array" : value === null ? "null" : typeof value}.`;
353
- }
354
- function objectShapeToValidator(shape, path) {
355
- const fields = {};
356
- for (const [key, value] of Object.entries(shape)) fields[key] = nestedInputToValidator(value, `${path}.${key}`);
357
- return v.object(fields);
358
- }
359
- function nestedInputToValidator(input, path) {
360
- if (isColumnBuilder$1(input)) return toRequiredBuilderValidator(input.convexValidator);
361
- if (isValidator(input)) return toRequiredValidator(input);
362
- if (isRecord$1(input)) return objectShapeToValidator(input, path);
363
- throw new Error(formatInvalidInput(path, input));
364
- }
365
- var ConvexCustomBuilder = class extends ConvexColumnBuilder {
366
- static [entityKind] = "ConvexCustomBuilder";
367
- constructor(name, validator) {
368
- super(name, "any", "ConvexCustom");
369
- this.config.validator = validator;
370
- }
371
- get convexValidator() {
372
- const validator = this.config.validator;
373
- if (this.config.notNull) return validator;
374
- return v.optional(v.union(v.null(), validator));
375
- }
376
- build() {
377
- return this.convexValidator;
378
- }
379
- };
380
- function custom(a, b) {
381
- if (b !== void 0) return new ConvexCustomBuilder(a, b);
382
- return new ConvexCustomBuilder("", a);
383
- }
384
- /**
385
- * Creates an array column from a nested validator or builder.
386
- *
387
- * Values in nested arrays are always compiled as required validators.
388
- */
389
- function arrayOf(element) {
390
- return custom(v.array(nestedInputToValidator(element, "arrayOf(element)"))).$type();
391
- }
392
- /**
393
- * Creates an object column from either:
394
- * - a nested shape of validators/builders, or
395
- * - a validator/builder describing homogeneous record values
396
- *
397
- * Fields in nested objects are always compiled as required validators.
398
- */
399
- function objectOf(input) {
400
- if (isColumnBuilder$1(input) || isValidator(input)) return custom(v.record(v.string(), nestedInputToValidator(input, "objectOf(value)"))).$type();
401
- if (!isRecord$1(input)) throw new Error(formatInvalidInput("objectOf(shape)", input));
402
- return custom(objectShapeToValidator(input, "objectOf(shape)")).$type();
403
- }
404
- /**
405
- * Convenience wrapper for Convex "JSON" values.
406
- *
407
- * Note: This is Convex JSON (runtime `v.any()`), not SQL JSON/JSONB.
408
- */
409
- function json() {
410
- return custom(v.any()).$type();
411
- }
412
-
413
- //#endregion
414
- //#region src/orm/builders/id.ts
415
- /**
416
- * ID column builder class
417
- * Compiles to v.id(tableName) or v.optional(v.id(tableName))
418
- */
419
- var ConvexIdBuilder = class extends ConvexColumnBuilder {
420
- static [entityKind] = "ConvexIdBuilder";
421
- constructor(name, tableName) {
422
- super(name, "string", "ConvexId");
423
- this.tableName = tableName;
424
- this.config.referenceTable = tableName;
425
- }
426
- /**
427
- * Expose Convex validator for schema integration
428
- */
429
- get convexValidator() {
430
- if (this.config.notNull) return v.id(this.tableName);
431
- return v.optional(v.union(v.null(), v.id(this.tableName)));
432
- }
433
- /**
434
- * Compile to Convex validator
435
- * .notNull() → v.id(tableName)
436
- * nullable → v.optional(v.id(tableName))
437
- */
438
- build() {
439
- return this.convexValidator;
440
- }
441
- };
442
- function id(tableName) {
443
- return new ConvexIdBuilder("", tableName);
444
- }
445
-
446
- //#endregion
447
- //#region src/orm/builders/number.ts
448
- /**
449
- * Number column builder class
450
- * Compiles to v.number() or v.optional(v.number())
451
- */
452
- var ConvexNumberBuilder = class extends ConvexColumnBuilder {
453
- static [entityKind] = "ConvexNumberBuilder";
454
- constructor(name) {
455
- super(name, "number", "ConvexNumber");
456
- }
457
- /**
458
- * Expose Convex validator for schema integration
459
- */
460
- get convexValidator() {
461
- if (this.config.notNull) return v.number();
462
- return v.optional(v.union(v.null(), v.number()));
463
- }
464
- /**
465
- * Compile to Convex validator
466
- * .notNull() → v.number()
467
- * nullable → v.optional(v.number())
468
- */
469
- build() {
470
- return this.convexValidator;
471
- }
472
- };
473
- function integer(name) {
474
- return new ConvexNumberBuilder(name ?? "");
475
- }
476
-
477
- //#endregion
478
- //#region src/orm/builders/text.ts
479
- /**
480
- * Text column builder class
481
- * Compiles to v.string() or v.optional(v.string())
482
- */
483
- var ConvexTextBuilder = class extends ConvexColumnBuilder {
484
- static [entityKind] = "ConvexTextBuilder";
485
- constructor(name) {
486
- super(name, "string", "ConvexText");
487
- }
488
- /**
489
- * Expose Convex validator for schema integration
490
- */
491
- get convexValidator() {
492
- if (this.config.notNull) return v.string();
493
- return v.optional(v.union(v.null(), v.string()));
494
- }
495
- /**
496
- * Compile to Convex validator
497
- * .notNull() → v.string()
498
- * nullable → v.optional(v.string())
499
- */
500
- build() {
501
- return this.convexValidator;
502
- }
503
- };
504
- function text(name) {
505
- return new ConvexTextBuilder(name ?? "");
506
- }
507
-
508
- //#endregion
509
- //#region src/orm/extensions.ts
510
- function defineChainMethod(target, key, value) {
511
- Object.defineProperty(target, key, {
512
- value,
513
- enumerable: false,
514
- configurable: true
515
- });
516
- }
517
- function createSchemaExtensionChain(state, capabilities) {
518
- const extension = {
519
- key: state.key,
520
- tables: state.tables
521
- };
522
- Object.defineProperty(extension, OrmSchemaExtensionRelations, {
523
- value: state.relations,
524
- enumerable: false,
525
- configurable: true
526
- });
527
- Object.defineProperty(extension, OrmSchemaExtensionTriggers, {
528
- value: state.triggers,
529
- enumerable: false,
530
- configurable: true
531
- });
532
- if (capabilities.canRelations) defineChainMethod(extension, "relations", (relations) => createSchemaExtensionChain({
533
- ...state,
534
- relations
535
- }, {
536
- canRelations: false,
537
- canTriggers: true
538
- }));
539
- if (capabilities.canTriggers) defineChainMethod(extension, "triggers", (triggers) => createSchemaExtensionChain({
540
- ...state,
541
- triggers
542
- }, {
543
- canRelations: false,
544
- canTriggers: false
545
- }));
546
- return extension;
547
- }
548
- function defineSchemaExtension(key, tables) {
549
- return createSchemaExtensionChain({
550
- key,
551
- tables,
552
- relations: void 0,
553
- triggers: void 0
554
- }, {
555
- canRelations: true,
556
- canTriggers: true
557
- });
558
- }
559
-
560
- //#endregion
561
- //#region src/orm/indexes.ts
562
- var ConvexIndexBuilderOn = class {
563
- static [entityKind] = "ConvexIndexBuilderOn";
564
- [entityKind] = "ConvexIndexBuilderOn";
565
- constructor(name, unique) {
566
- this.name = name;
567
- this.unique = unique;
568
- }
569
- on(...columns) {
570
- return new ConvexIndexBuilder(this.name, columns, this.unique);
571
- }
572
- };
573
- var ConvexIndexBuilder = class {
574
- static [entityKind] = "ConvexIndexBuilder";
575
- [entityKind] = "ConvexIndexBuilder";
576
- config;
577
- constructor(name, columns, unique) {
578
- this.config = {
579
- name,
580
- columns,
581
- unique,
582
- where: void 0
583
- };
584
- }
585
- /**
586
- * Partial index conditions are not supported in Convex.
587
- * This method is kept for Drizzle API parity.
588
- */
589
- where(condition) {
590
- this.config.where = condition;
591
- return this;
592
- }
593
- };
594
- var ConvexSearchIndexBuilderOn = class {
595
- static [entityKind] = "ConvexSearchIndexBuilderOn";
596
- [entityKind] = "ConvexSearchIndexBuilderOn";
597
- constructor(name) {
598
- this.name = name;
599
- }
600
- on(searchField) {
601
- return new ConvexSearchIndexBuilder(this.name, searchField);
602
- }
603
- };
604
- var ConvexSearchIndexBuilder = class {
605
- static [entityKind] = "ConvexSearchIndexBuilder";
606
- [entityKind] = "ConvexSearchIndexBuilder";
607
- config;
608
- constructor(name, searchField) {
609
- this.config = {
610
- name,
611
- searchField,
612
- filterFields: [],
613
- staged: false
614
- };
615
- }
616
- filter(...fields) {
617
- this.config.filterFields = fields;
618
- return this;
619
- }
620
- staged() {
621
- this.config.staged = true;
622
- return this;
623
- }
624
- };
625
- var ConvexVectorIndexBuilderOn = class {
626
- static [entityKind] = "ConvexVectorIndexBuilderOn";
627
- [entityKind] = "ConvexVectorIndexBuilderOn";
628
- constructor(name) {
629
- this.name = name;
630
- }
631
- on(vectorField) {
632
- return new ConvexVectorIndexBuilder(this.name, vectorField);
633
- }
634
- };
635
- var ConvexAggregateIndexBuilderOn = class {
636
- static [entityKind] = "ConvexAggregateIndexBuilderOn";
637
- [entityKind] = "ConvexAggregateIndexBuilderOn";
638
- constructor(name) {
639
- this.name = name;
640
- }
641
- on(...columns) {
642
- return new ConvexAggregateIndexBuilder(this.name, columns);
643
- }
644
- all() {
645
- return new ConvexAggregateIndexBuilder(this.name, []);
646
- }
647
- };
648
- var ConvexAggregateIndexBuilder = class {
649
- static [entityKind] = "ConvexAggregateIndexBuilder";
650
- [entityKind] = "ConvexAggregateIndexBuilder";
651
- config;
652
- constructor(name, columns) {
653
- this.config = {
654
- name,
655
- columns,
656
- countFields: [],
657
- sumFields: [],
658
- avgFields: [],
659
- minFields: [],
660
- maxFields: []
661
- };
662
- }
663
- count(...fields) {
664
- this.config.countFields = [...this.config.countFields, ...fields];
665
- return this;
666
- }
667
- sum(...fields) {
668
- this.config.sumFields = [...this.config.sumFields, ...fields];
669
- return this;
670
- }
671
- avg(...fields) {
672
- this.config.avgFields = [...this.config.avgFields, ...fields];
673
- return this;
674
- }
675
- min(...fields) {
676
- this.config.minFields = [...this.config.minFields, ...fields];
677
- return this;
678
- }
679
- max(...fields) {
680
- this.config.maxFields = [...this.config.maxFields, ...fields];
681
- return this;
682
- }
683
- };
684
- var ConvexRankIndexBuilderOn = class {
685
- static [entityKind] = "ConvexRankIndexBuilderOn";
686
- [entityKind] = "ConvexRankIndexBuilderOn";
687
- constructor(name) {
688
- this.name = name;
689
- }
690
- partitionBy(...columns) {
691
- return new ConvexRankIndexBuilder(this.name, columns, []);
692
- }
693
- all() {
694
- return new ConvexRankIndexBuilder(this.name, [], []);
695
- }
696
- };
697
- var ConvexRankIndexBuilder = class {
698
- static [entityKind] = "ConvexRankIndexBuilder";
699
- [entityKind] = "ConvexRankIndexBuilder";
700
- config;
701
- constructor(name, partitionColumns, orderColumns) {
702
- this.config = {
703
- name,
704
- partitionColumns,
705
- orderColumns,
706
- sumField: void 0
707
- };
708
- }
709
- orderBy(...columns) {
710
- this.config.orderColumns = columns.map((entry) => {
711
- if (entry && typeof entry === "object" && "column" in entry && "direction" in entry) {
712
- const builder = entry.column?.builder;
713
- if (!builder) throw new Error("rankIndex orderBy() expected a column builder.");
714
- return {
715
- column: builder,
716
- direction: entry.direction
717
- };
718
- }
719
- return {
720
- column: entry,
721
- direction: "asc"
722
- };
723
- });
724
- return this;
725
- }
726
- sum(field) {
727
- this.config.sumField = field;
728
- return this;
729
- }
730
- };
731
- var ConvexVectorIndexBuilder = class {
732
- static [entityKind] = "ConvexVectorIndexBuilder";
733
- [entityKind] = "ConvexVectorIndexBuilder";
734
- config;
735
- constructor(name, vectorField) {
736
- this.config = {
737
- name,
738
- vectorField,
739
- dimensions: void 0,
740
- filterFields: [],
741
- staged: false
742
- };
743
- }
744
- dimensions(dimensions) {
745
- if (!Number.isInteger(dimensions)) throw new Error(`Vector index '${this.config.name}' dimensions must be an integer, got ${dimensions}`);
746
- if (dimensions <= 0) throw new Error(`Vector index '${this.config.name}' dimensions must be positive, got ${dimensions}`);
747
- if (dimensions > 1e4) console.warn(`Vector index '${this.config.name}' has unusually large dimensions (${dimensions}). Common values: 768, 1536, 3072`);
748
- this.config.dimensions = dimensions;
749
- return this;
750
- }
751
- filter(...fields) {
752
- this.config.filterFields = fields;
753
- return this;
754
- }
755
- staged() {
756
- this.config.staged = true;
757
- return this;
758
- }
759
- };
760
- function index(name) {
761
- return new ConvexIndexBuilderOn(name, false);
762
- }
763
-
764
- //#endregion
765
- //#region src/orm/rls/policies.ts
766
- var RlsPolicy = class {
767
- static [entityKind] = "RlsPolicy";
768
- [entityKind] = "RlsPolicy";
769
- as;
770
- for;
771
- to;
772
- using;
773
- withCheck;
774
- /** @internal */
775
- _linkedTable;
776
- constructor(name, config) {
777
- this.name = name;
778
- if (config) {
779
- this.as = config.as;
780
- this.for = config.for;
781
- this.to = config.to;
782
- this.using = config.using;
783
- this.withCheck = config.withCheck;
784
- }
785
- }
786
- link(table) {
787
- this._linkedTable = table;
788
- return this;
789
- }
790
- };
791
- function isRlsPolicy(value) {
792
- return !!value && typeof value === "object" && value[entityKind] === "RlsPolicy";
793
- }
794
-
795
- //#endregion
796
- //#region src/orm/table.ts
797
- /**
798
- * Reserved Convex system table names that cannot be used
799
- */
800
- const RESERVED_TABLES = new Set(["_storage", "_scheduled_functions"]);
801
- const RESERVED_COLUMN_NAMES = new Set([
802
- "id",
803
- "_id",
804
- "_creationTime"
805
- ]);
806
- const DEFAULT_POLYMORPHIC_ALIAS = "details";
807
- const CONVEX_TABLE_FIELD_LIMIT = 1024;
808
- /**
809
- * Valid table name pattern: starts with letter/underscore, contains only alphanumeric and underscore
810
- */
811
- const TABLE_NAME_REGEX = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
812
- /**
813
- * Validate table name against Convex constraints
814
- */
815
- function validateTableName(name) {
816
- if (RESERVED_TABLES.has(name)) throw new Error(`Table name '${name}' is reserved. System tables cannot be redefined.`);
817
- if (!TABLE_NAME_REGEX.test(name)) throw new Error(`Invalid table name '${name}'. Must start with letter, contain only alphanumeric and underscore.`);
818
- }
819
- /**
820
- * Create a Convex object validator from column builders
821
- *
822
- * Extracts .convexValidator from each column and creates v.object({...})
823
- * This is the core factory that bridges ORM columns to Convex validators.
824
- *
825
- * @param columns - Record of column name to column builder
826
- * @returns Convex object validator
827
- */
828
- function createValidatorFromColumns(columns) {
829
- const validatorFields = Object.fromEntries(Object.entries(columns).map(([key, builder]) => [key, builder.convexValidator]));
830
- return v.object(validatorFields);
831
- }
832
- var ConvexDeletionBuilder = class {
833
- static [entityKind] = "ConvexDeletionBuilder";
834
- [entityKind] = "ConvexDeletionBuilder";
835
- constructor(config) {
836
- this.config = config;
837
- }
838
- };
839
- function isConvexIndexBuilder(value) {
840
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexIndexBuilder";
841
- }
842
- function isConvexIndexBuilderOn(value) {
843
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexIndexBuilderOn";
844
- }
845
- function isConvexAggregateIndexBuilder(value) {
846
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexAggregateIndexBuilder";
847
- }
848
- function isConvexAggregateIndexBuilderOn(value) {
849
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexAggregateIndexBuilderOn";
850
- }
851
- function isConvexRankIndexBuilderOn(value) {
852
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexRankIndexBuilderOn";
853
- }
854
- function isConvexRankIndexBuilder(value) {
855
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexRankIndexBuilder";
856
- }
857
- function isConvexUniqueConstraintBuilderOn(value) {
858
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexUniqueConstraintBuilderOn";
859
- }
860
- function isConvexForeignKeyBuilder(value) {
861
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexForeignKeyBuilder";
862
- }
863
- function isConvexCheckBuilder(value) {
864
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexCheckBuilder";
865
- }
866
- function isConvexSearchIndexBuilderOn(value) {
867
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexSearchIndexBuilderOn";
868
- }
869
- function isConvexUniqueConstraintBuilder(value) {
870
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexUniqueConstraintBuilder";
871
- }
872
- function isConvexSearchIndexBuilder(value) {
873
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexSearchIndexBuilder";
874
- }
875
- function isConvexVectorIndexBuilderOn(value) {
876
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexVectorIndexBuilderOn";
877
- }
878
- function isConvexVectorIndexBuilder(value) {
879
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexVectorIndexBuilder";
880
- }
881
- function isConvexDeletionBuilder(value) {
882
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexDeletionBuilder";
883
- }
884
- function isConvexLifecycleBuilder(value) {
885
- return typeof value === "object" && value !== null && value[entityKind] === "ConvexLifecycleBuilder";
886
- }
887
- function getColumnName(column) {
888
- const config = column.config;
889
- if (!config?.name) throw new Error("Invalid index column: expected a convexTable column builder.");
890
- return config.name;
891
- }
892
- function getColumnType(column) {
893
- return column.config?.columnType;
894
- }
895
- function getColumnDimensions(column) {
896
- return column.config?.dimensions;
897
- }
898
- function getColumnTableName(column) {
899
- const config = column.config;
900
- return config?.tableName ?? config?.referenceTable;
901
- }
902
- function getColumnTable(column) {
903
- return column.config?.table;
904
- }
905
- function getUniqueIndexName(tableName, fields, explicitName) {
906
- if (explicitName) return explicitName;
907
- return `${tableName}_${fields.join("_")}_unique`;
908
- }
909
- function assertColumnInTable(column, expectedTable, context) {
910
- const tableName = getColumnTableName(column);
911
- if (tableName && tableName !== expectedTable) throw new Error(`${context} references column from '${tableName}', but belongs to '${expectedTable}'.`);
912
- return getColumnName(column);
913
- }
914
- function assertNoReservedCreatedAtIndexFields(fields, context) {
915
- if (fields.includes("createdAt")) throw new Error(`${context} cannot use 'createdAt'. 'createdAt' is reserved and maps to internal '_creationTime'.`);
916
- }
917
- function assertSearchFieldType(column, indexName) {
918
- const columnType = getColumnType(column) ?? "unknown";
919
- if (columnType !== "ConvexText") throw new Error(`Search index '${indexName}' only supports text() columns. Field '${getColumnName(column)}' is type '${columnType}'.`);
920
- }
921
- function assertVectorFieldType(column, indexName) {
922
- const columnType = getColumnType(column) ?? "unknown";
923
- if (columnType !== "ConvexVector") throw new Error(`Vector index '${indexName}' requires a vector() column. Field '${getColumnName(column)}' is type '${columnType}'.`);
924
- }
925
- function assertAggregateSumFieldType(column, indexName) {
926
- const columnType = getColumnType(column) ?? "unknown";
927
- if (!["ConvexNumber", "ConvexTimestamp"].includes(columnType)) throw new Error(`aggregateIndex '${indexName}' sum() supports integer()/timestamp() columns only. Field '${getColumnName(column)}' is type '${columnType}'.`);
928
- }
929
- function assertAggregateAvgFieldType(column, indexName) {
930
- const columnType = getColumnType(column) ?? "unknown";
931
- if (!["ConvexNumber", "ConvexTimestamp"].includes(columnType)) throw new Error(`aggregateIndex '${indexName}' avg() supports integer()/timestamp() columns only. Field '${getColumnName(column)}' is type '${columnType}'.`);
932
- }
933
- function assertAggregateComparableFieldType(column, indexName, method) {
934
- const columnType = getColumnType(column) ?? "unknown";
935
- if (![
936
- "ConvexNumber",
937
- "ConvexTimestamp",
938
- "ConvexDate",
939
- "ConvexText",
940
- "ConvexBoolean",
941
- "ConvexId"
942
- ].includes(columnType)) throw new Error(`aggregateIndex '${indexName}' ${method}() does not support column type '${columnType}' on '${getColumnName(column)}'.`);
943
- }
944
- function assertRankOrderFieldType(column, indexName) {
945
- const columnType = getColumnType(column) ?? "unknown";
946
- if (![
947
- "ConvexNumber",
948
- "ConvexTimestamp",
949
- "ConvexDate"
950
- ].includes(columnType)) throw new Error(`rankIndex '${indexName}' orderBy() supports integer()/timestamp()/date() columns only. Field '${getColumnName(column)}' is type '${columnType}'.`);
951
- }
952
- const dedupeFieldNames = (fields) => [...new Set(fields)];
953
- const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
954
- const isColumnBuilder = (value) => isRecord(value) && typeof value.build === "function";
955
- const getDiscriminatorConfig = (value) => {
956
- if (!isColumnBuilder(value)) return;
957
- const discriminator = value.config?.discriminator;
958
- if (!discriminator) return;
959
- return discriminator;
960
- };
961
- const getPolymorphicFieldSignature = (column) => {
962
- const validator = column.convexValidator ?? column.build();
963
- return JSON.stringify({
964
- columnType: column.config?.columnType,
965
- validator: validator?.json
966
- });
967
- };
968
- function resolveTableColumns(tableName, columns) {
969
- const resolvedColumns = {};
970
- const pendingPolymorphic = [];
971
- for (const [columnName, rawBuilder] of Object.entries(columns)) {
972
- if (!isColumnBuilder(rawBuilder)) throw new Error(`Column '${columnName}' on '${tableName}' must be a column builder.`);
973
- resolvedColumns[columnName] = rawBuilder;
974
- const discriminatorConfig = getDiscriminatorConfig(rawBuilder);
975
- if (!discriminatorConfig) continue;
976
- if (!isRecord(discriminatorConfig.variants) || Object.keys(discriminatorConfig.variants).length === 0) throw new Error(`discriminator('${tableName}.${columnName}') requires at least one variant.`);
977
- const alias = discriminatorConfig.as === void 0 ? DEFAULT_POLYMORPHIC_ALIAS : discriminatorConfig.as;
978
- if (typeof alias !== "string" || alias.length === 0) throw new Error(`discriminator('${tableName}.${columnName}').as must be a non-empty string.`);
979
- pendingPolymorphic.push({
980
- discriminator: columnName,
981
- alias,
982
- variants: discriminatorConfig.variants
983
- });
984
- }
985
- if (pendingPolymorphic.length > 1) throw new Error(`Only one discriminator(...) column is currently supported on '${tableName}'.`);
986
- const polymorphicConfigs = [];
987
- for (const pending of pendingPolymorphic) {
988
- if (pending.alias in resolvedColumns) throw new Error(`discriminator('${tableName}.${pending.discriminator}') alias '${pending.alias}' collides with an existing column.`);
989
- const generatedFieldMap = /* @__PURE__ */ new Map();
990
- const variantRuntime = {};
991
- for (const [variantKey, rawVariantColumns] of Object.entries(pending.variants)) {
992
- if (!isRecord(rawVariantColumns)) throw new Error(`discriminator('${tableName}.${pending.discriminator}') variant '${variantKey}' must be an object.`);
993
- const fieldNames = [];
994
- const requiredFieldNames = [];
995
- for (const [fieldName, rawFieldBuilder] of Object.entries(rawVariantColumns)) {
996
- if (!isColumnBuilder(rawFieldBuilder)) throw new Error(`discriminator('${tableName}.${pending.discriminator}').variants.${variantKey}.${fieldName} must be a column builder.`);
997
- if (fieldName in resolvedColumns) throw new Error(`discriminator('${tableName}.${pending.discriminator}').variants.${variantKey}.${fieldName} collides with an existing table column.`);
998
- const fieldBuilder = rawFieldBuilder;
999
- const fieldConfig = fieldBuilder.config;
1000
- const isRequiredForVariant = fieldConfig?.notNull === true && fieldConfig.hasDefault !== true && typeof fieldConfig.defaultFn !== "function";
1001
- const signature = getPolymorphicFieldSignature(fieldBuilder);
1002
- const existing = generatedFieldMap.get(fieldName);
1003
- if (existing && existing.signature !== signature) throw new Error(`discriminator('${tableName}.${pending.discriminator}') field '${fieldName}' has conflicting builder signatures across variants.`);
1004
- if (!existing) {
1005
- if (fieldConfig) fieldConfig.notNull = false;
1006
- generatedFieldMap.set(fieldName, {
1007
- builder: fieldBuilder,
1008
- signature
1009
- });
1010
- }
1011
- fieldNames.push(fieldName);
1012
- if (isRequiredForVariant) requiredFieldNames.push(fieldName);
1013
- }
1014
- variantRuntime[variantKey] = {
1015
- fieldNames,
1016
- requiredFieldNames
1017
- };
1018
- }
1019
- for (const [fieldName, { builder }] of generatedFieldMap.entries()) resolvedColumns[fieldName] = builder;
1020
- polymorphicConfigs.push({
1021
- discriminator: pending.discriminator,
1022
- alias: pending.alias,
1023
- generatedFieldNames: Object.freeze([...generatedFieldMap.keys()]),
1024
- variants: Object.freeze(variantRuntime)
1025
- });
1026
- }
1027
- if (Object.keys(resolvedColumns).length > CONVEX_TABLE_FIELD_LIMIT) throw new Error(`Table '${tableName}' exceeds Convex field count limit (${CONVEX_TABLE_FIELD_LIMIT}) after discriminator expansion.`);
1028
- return {
1029
- columns: resolvedColumns,
1030
- polymorphicConfigs
1031
- };
1032
- }
1033
- function applyExtraConfig(table, config) {
1034
- if (!config) return;
1035
- const entries = Array.isArray(config) ? config : Object.values(config);
1036
- for (const entry of entries) {
1037
- if (isConvexIndexBuilderOn(entry)) throw new Error(`Invalid index definition on '${table.tableName}'. Did you forget to call .on(...)?`);
1038
- if (isConvexUniqueConstraintBuilderOn(entry)) throw new Error(`Invalid unique constraint definition on '${table.tableName}'. Did you forget to call .on(...)?`);
1039
- if (isConvexAggregateIndexBuilderOn(entry)) throw new Error(`Invalid aggregate index definition on '${table.tableName}'. Did you forget to call .on(...) or .all()?`);
1040
- if (isConvexRankIndexBuilderOn(entry)) throw new Error(`Invalid rank index definition on '${table.tableName}'. Did you forget to call .partitionBy(...) or .all()?`);
1041
- if (isConvexSearchIndexBuilderOn(entry)) throw new Error(`Invalid search index definition on '${table.tableName}'. Did you forget to call .on(...)?`);
1042
- if (isConvexVectorIndexBuilderOn(entry)) throw new Error(`Invalid vector index definition on '${table.tableName}'. Did you forget to call .on(...)?`);
1043
- if (isRlsPolicy(entry)) {
1044
- const target = entry._linkedTable ?? table;
1045
- if (typeof target.addRlsPolicy === "function") target.addRlsPolicy(entry);
1046
- else {
1047
- const policies = target[RlsPolicies] ?? [];
1048
- policies.push(entry);
1049
- target[RlsPolicies] = policies;
1050
- target[EnableRLS] = true;
1051
- }
1052
- continue;
1053
- }
1054
- if (isConvexDeletionBuilder(entry)) {
1055
- if (table[TableDeleteConfig]) throw new Error(`Only one deletion(...) config can be defined for '${table.tableName}'.`);
1056
- table[TableDeleteConfig] = {
1057
- mode: entry.config.mode,
1058
- delayMs: entry.config.delayMs
1059
- };
1060
- continue;
1061
- }
1062
- if (isConvexLifecycleBuilder(entry)) throw new Error(`Lifecycle hooks are no longer supported inside convexTable('${table.tableName}', ..., extraConfig). Export schema triggers with defineTriggers(relations, { ... }) from schema.ts.`);
1063
- if (isConvexIndexBuilder(entry)) {
1064
- const { name, columns, unique, where } = entry.config;
1065
- if (where) throw new Error(`Convex does not support partial indexes. Remove .where(...) from index '${name}'.`);
1066
- if (unique) {}
1067
- const fields = columns.map((column) => assertColumnInTable(column, table.tableName, `Index '${name}'`));
1068
- assertNoReservedCreatedAtIndexFields(fields, `Index '${name}'`);
1069
- table.addIndex(name, fields);
1070
- if (unique) table.addUniqueIndex(name, fields, false);
1071
- continue;
1072
- }
1073
- if (isConvexAggregateIndexBuilder(entry)) {
1074
- const { name, columns, countFields, sumFields, avgFields, minFields, maxFields } = entry.config;
1075
- const fields = columns.map((column) => assertColumnInTable(column, table.tableName, `Aggregate index '${name}'`));
1076
- assertNoReservedCreatedAtIndexFields(fields, `Aggregate index '${name}'`);
1077
- const resolvedCountFields = dedupeFieldNames(countFields.map((column) => assertColumnInTable(column, table.tableName, `Aggregate index '${name}' count`)));
1078
- assertNoReservedCreatedAtIndexFields(resolvedCountFields, `Aggregate index '${name}' count`);
1079
- const resolvedSumFields = dedupeFieldNames(sumFields.map((column) => {
1080
- const field = assertColumnInTable(column, table.tableName, `Aggregate index '${name}' sum`);
1081
- assertAggregateSumFieldType(column, name);
1082
- return field;
1083
- }));
1084
- assertNoReservedCreatedAtIndexFields(resolvedSumFields, `Aggregate index '${name}' sum`);
1085
- const resolvedAvgFields = dedupeFieldNames(avgFields.map((column) => {
1086
- const field = assertColumnInTable(column, table.tableName, `Aggregate index '${name}' avg`);
1087
- assertAggregateAvgFieldType(column, name);
1088
- return field;
1089
- }));
1090
- assertNoReservedCreatedAtIndexFields(resolvedAvgFields, `Aggregate index '${name}' avg`);
1091
- const resolvedMinFields = dedupeFieldNames(minFields.map((column) => {
1092
- const field = assertColumnInTable(column, table.tableName, `Aggregate index '${name}' min`);
1093
- assertAggregateComparableFieldType(column, name, "min");
1094
- return field;
1095
- }));
1096
- assertNoReservedCreatedAtIndexFields(resolvedMinFields, `Aggregate index '${name}' min`);
1097
- const resolvedMaxFields = dedupeFieldNames(maxFields.map((column) => {
1098
- const field = assertColumnInTable(column, table.tableName, `Aggregate index '${name}' max`);
1099
- assertAggregateComparableFieldType(column, name, "max");
1100
- return field;
1101
- }));
1102
- assertNoReservedCreatedAtIndexFields(resolvedMaxFields, `Aggregate index '${name}' max`);
1103
- table.addAggregateIndex(name, {
1104
- fields,
1105
- countFields: resolvedCountFields,
1106
- sumFields: resolvedSumFields,
1107
- avgFields: resolvedAvgFields,
1108
- minFields: resolvedMinFields,
1109
- maxFields: resolvedMaxFields
1110
- });
1111
- continue;
1112
- }
1113
- if (isConvexRankIndexBuilder(entry)) {
1114
- const { name, partitionColumns, orderColumns, sumField } = entry.config;
1115
- if (!orderColumns.length) throw new Error(`rankIndex '${name}' on '${table.tableName}' must declare at least one orderBy(...) column.`);
1116
- const resolvedPartitionFields = dedupeFieldNames(partitionColumns.map((column) => assertColumnInTable(column, table.tableName, `rankIndex '${name}'`)));
1117
- assertNoReservedCreatedAtIndexFields(resolvedPartitionFields, `rankIndex '${name}' partitionBy`);
1118
- const resolvedOrderFields = orderColumns.map((entry) => {
1119
- const field = assertColumnInTable(entry.column, table.tableName, `rankIndex '${name}' orderBy`);
1120
- assertNoReservedCreatedAtIndexFields([field], `rankIndex '${name}'`);
1121
- assertRankOrderFieldType(entry.column, name);
1122
- return {
1123
- field,
1124
- direction: entry.direction
1125
- };
1126
- });
1127
- const resolvedSumField = sumField ? (() => {
1128
- const field = assertColumnInTable(sumField, table.tableName, `rankIndex '${name}' sum`);
1129
- assertNoReservedCreatedAtIndexFields([field], `rankIndex '${name}'`);
1130
- assertAggregateSumFieldType(sumField, name);
1131
- return field;
1132
- })() : void 0;
1133
- table.addRankIndex(name, {
1134
- partitionFields: resolvedPartitionFields,
1135
- orderFields: resolvedOrderFields,
1136
- sumField: resolvedSumField
1137
- });
1138
- continue;
1139
- }
1140
- if (isConvexUniqueConstraintBuilder(entry)) {
1141
- const { name, columns, nullsNotDistinct } = entry.config;
1142
- const fields = columns.map((column) => assertColumnInTable(column, table.tableName, "Unique constraint"));
1143
- assertNoReservedCreatedAtIndexFields(fields, "Unique constraint");
1144
- const indexName = getUniqueIndexName(table.tableName, fields, name);
1145
- table.addIndex(indexName, fields);
1146
- table.addUniqueIndex(indexName, fields, nullsNotDistinct);
1147
- continue;
1148
- }
1149
- if (isConvexForeignKeyBuilder(entry)) {
1150
- const { name, columns, foreignColumns, onDelete, onUpdate } = entry.config;
1151
- if (columns.length === 0 || foreignColumns.length === 0) throw new Error(`Foreign key on '${table.tableName}' requires at least one column.`);
1152
- if (columns.length !== foreignColumns.length) throw new Error(`Foreign key on '${table.tableName}' must specify matching columns and foreignColumns.`);
1153
- const localFields = columns.map((column) => assertColumnInTable(column, table.tableName, "Foreign key"));
1154
- const foreignTableName = getColumnTableName(foreignColumns[0]);
1155
- if (!foreignTableName) throw new Error(`Foreign key on '${table.tableName}' references a column without a table.`);
1156
- const foreignTable = getColumnTable(foreignColumns[0]);
1157
- const foreignFields = foreignColumns.map((column) => {
1158
- const tableName = getColumnTableName(column);
1159
- if (tableName && tableName !== foreignTableName) throw new Error(`Foreign key on '${table.tableName}' mixes foreign columns from '${foreignTableName}' and '${tableName}'.`);
1160
- return getColumnName(column);
1161
- });
1162
- table.addForeignKey({
1163
- name,
1164
- columns: localFields,
1165
- foreignTableName,
1166
- foreignTable,
1167
- foreignColumns: foreignFields,
1168
- onDelete,
1169
- onUpdate
1170
- });
1171
- continue;
1172
- }
1173
- if (isConvexCheckBuilder(entry)) {
1174
- const { name, expression } = entry.config;
1175
- table.addCheck(name, expression);
1176
- continue;
1177
- }
1178
- if (isConvexSearchIndexBuilder(entry)) {
1179
- const { name, searchField, filterFields, staged } = entry.config;
1180
- const searchFieldName = assertColumnInTable(searchField, table.tableName, `Search index '${name}'`);
1181
- assertNoReservedCreatedAtIndexFields([searchFieldName], `Search index '${name}'`);
1182
- assertSearchFieldType(searchField, name);
1183
- const filterFieldNames = filterFields.map((field) => assertColumnInTable(field, table.tableName, `Search index '${name}'`));
1184
- assertNoReservedCreatedAtIndexFields(filterFieldNames, `Search index '${name}'`);
1185
- table.addSearchIndex(name, {
1186
- searchField: searchFieldName,
1187
- filterFields: filterFieldNames,
1188
- staged
1189
- });
1190
- continue;
1191
- }
1192
- if (isConvexVectorIndexBuilder(entry)) {
1193
- const { name, vectorField, dimensions, filterFields, staged } = entry.config;
1194
- if (dimensions === void 0) throw new Error(`Vector index '${name}' is missing dimensions. Call .dimensions(n) before using.`);
1195
- const vectorFieldName = assertColumnInTable(vectorField, table.tableName, `Vector index '${name}'`);
1196
- assertNoReservedCreatedAtIndexFields([vectorFieldName], `Vector index '${name}'`);
1197
- assertVectorFieldType(vectorField, name);
1198
- const columnDimensions = getColumnDimensions(vectorField);
1199
- if (columnDimensions !== void 0 && columnDimensions !== dimensions) throw new Error(`Vector index '${name}' dimensions (${dimensions}) do not match vector column '${vectorFieldName}' dimensions (${columnDimensions}).`);
1200
- const filterFieldNames = filterFields.map((field) => assertColumnInTable(field, table.tableName, `Vector index '${name}'`));
1201
- assertNoReservedCreatedAtIndexFields(filterFieldNames, `Vector index '${name}'`);
1202
- table.addVectorIndex(name, {
1203
- vectorField: vectorFieldName,
1204
- dimensions,
1205
- filterFields: filterFieldNames,
1206
- staged
1207
- });
1208
- continue;
1209
- }
1210
- throw new Error(`Unsupported extra config value in convexTable('${table.tableName}').`);
1211
- }
1212
- }
1213
- /**
1214
- * ConvexTable implementation class
1215
- * Provides all properties required by Convex's TableDefinition
1216
- *
1217
- * Following convex-ents pattern:
1218
- * - Private fields for indexes (matches TableDefinition structure)
1219
- * - Duck typing (defineSchema only checks object shape)
1220
- * - Direct validator storage (no re-wrapping)
1221
- */
1222
- var ConvexTableImpl = class {
1223
- /**
1224
- * Required by TableDefinition
1225
- * Public validator property containing v.object({...}) with all column validators
1226
- */
1227
- validator;
1228
- /**
1229
- * TableDefinition private fields
1230
- * These satisfy structural typing requirements for defineSchema()
1231
- */
1232
- indexes = [];
1233
- uniqueIndexes = [];
1234
- aggregateIndexes = [];
1235
- rankIndexes = [];
1236
- foreignKeys = [];
1237
- deferredForeignKeys = [];
1238
- deferredForeignKeysResolved = false;
1239
- stagedDbIndexes = [];
1240
- searchIndexes = [];
1241
- stagedSearchIndexes = [];
1242
- vectorIndexes = [];
1243
- stagedVectorIndexes = [];
1244
- checks = [];
1245
- /**
1246
- * Symbol-based metadata storage
1247
- */
1248
- [TableName];
1249
- [Columns];
1250
- [Brand] = "ConvexTable";
1251
- [EnableRLS] = false;
1252
- [RlsPolicies] = [];
1253
- [TableDeleteConfig];
1254
- [TablePolymorphic];
1255
- /**
1256
- * Public tableName for convenience
1257
- */
1258
- tableName;
1259
- constructor(name, columns, polymorphicConfigs) {
1260
- validateTableName(name);
1261
- for (const columnName of Object.keys(columns)) if (RESERVED_COLUMN_NAMES.has(columnName)) throw new Error(`Column name '${columnName}' is reserved. System fields are managed by Convex ORM.`);
1262
- this[TableName] = name;
1263
- const namedColumns = Object.fromEntries(Object.entries(columns).map(([columnName, builder]) => {
1264
- builder.config.name = columnName;
1265
- builder.config.tableName = name;
1266
- builder.config.table = this;
1267
- return [columnName, builder];
1268
- }));
1269
- this[Columns] = namedColumns;
1270
- this.tableName = name;
1271
- if (polymorphicConfigs && polymorphicConfigs.length > 0) this[TablePolymorphic] = polymorphicConfigs;
1272
- this.validator = createValidatorFromColumns(namedColumns);
1273
- for (const [columnName, builder] of Object.entries(namedColumns)) {
1274
- const config = builder.config;
1275
- if (config?.isUnique) {
1276
- const indexName = getUniqueIndexName(name, [columnName], config.uniqueName);
1277
- const nullsNotDistinct = config.uniqueNulls === "not distinct";
1278
- this.addIndex(indexName, [columnName]);
1279
- this.addUniqueIndex(indexName, [columnName], nullsNotDistinct);
1280
- }
1281
- if (config?.referenceTable && (!config.foreignKeyConfigs || config.foreignKeyConfigs.length === 0)) this.addForeignKey({
1282
- name: void 0,
1283
- columns: [columnName],
1284
- foreignTableName: config.referenceTable,
1285
- foreignColumns: ["_id"]
1286
- });
1287
- if (config?.foreignKeyConfigs?.length) for (const foreignConfig of config.foreignKeyConfigs) this.deferredForeignKeys.push({
1288
- localColumnName: columnName,
1289
- ref: foreignConfig.ref,
1290
- config: foreignConfig.config
1291
- });
1292
- }
1293
- }
1294
- getPolymorphicConfigs() {
1295
- return this[TablePolymorphic];
1296
- }
1297
- /**
1298
- * Internal: add index to table from builder extraConfig
1299
- *
1300
- */
1301
- addIndex(name, fields) {
1302
- this.indexes.push({
1303
- indexDescriptor: name,
1304
- fields
1305
- });
1306
- }
1307
- /**
1308
- * Internal: add unique index metadata for runtime enforcement
1309
- */
1310
- addUniqueIndex(name, fields, nullsNotDistinct) {
1311
- this.uniqueIndexes.push({
1312
- name,
1313
- fields,
1314
- nullsNotDistinct
1315
- });
1316
- }
1317
- addAggregateIndex(name, config) {
1318
- if (this.aggregateIndexes.some((index) => index.name === name) || this.rankIndexes.some((index) => index.name === name)) throw new Error(`Duplicate aggregate index '${name}' on '${this.tableName}'.`);
1319
- this.aggregateIndexes.push({
1320
- name,
1321
- fields: config.fields,
1322
- countFields: config.countFields,
1323
- sumFields: config.sumFields,
1324
- avgFields: config.avgFields,
1325
- minFields: config.minFields,
1326
- maxFields: config.maxFields
1327
- });
1328
- }
1329
- addRankIndex(name, config) {
1330
- if (this.rankIndexes.some((index) => index.name === name) || this.aggregateIndexes.some((index) => index.name === name)) throw new Error(`Duplicate aggregate index '${name}' on '${this.tableName}'.`);
1331
- this.rankIndexes.push({
1332
- name,
1333
- partitionFields: config.partitionFields,
1334
- orderFields: config.orderFields,
1335
- sumField: config.sumField
1336
- });
1337
- }
1338
- getAggregateIndexes() {
1339
- return [...this.aggregateIndexes];
1340
- }
1341
- getRankIndexes() {
1342
- return [...this.rankIndexes];
1343
- }
1344
- /**
1345
- * Internal: expose unique index metadata for mutation enforcement
1346
- */
1347
- getUniqueIndexes() {
1348
- return this.uniqueIndexes;
1349
- }
1350
- /**
1351
- * Internal: expose index metadata for runtime enforcement
1352
- */
1353
- getIndexes() {
1354
- return this.indexes.map((entry) => ({
1355
- name: entry.indexDescriptor,
1356
- fields: entry.fields
1357
- }));
1358
- }
1359
- /**
1360
- * Internal: expose search index metadata for runtime query execution
1361
- */
1362
- getSearchIndexes() {
1363
- return this.searchIndexes.map((entry) => ({
1364
- name: entry.indexDescriptor,
1365
- searchField: entry.searchField,
1366
- filterFields: entry.filterFields
1367
- }));
1368
- }
1369
- /**
1370
- * Internal: expose vector index metadata for runtime query execution
1371
- */
1372
- getVectorIndexes() {
1373
- return this.vectorIndexes.map((entry) => ({
1374
- name: entry.indexDescriptor,
1375
- vectorField: entry.vectorField,
1376
- dimensions: entry.dimensions,
1377
- filterFields: entry.filterFields
1378
- }));
1379
- }
1380
- /**
1381
- * Internal: attach an RLS policy to this table
1382
- */
1383
- addRlsPolicy(policy) {
1384
- this[RlsPolicies].push(policy);
1385
- this[EnableRLS] = true;
1386
- }
1387
- /**
1388
- * Internal: return attached RLS policies
1389
- */
1390
- getRlsPolicies() {
1391
- return this[RlsPolicies];
1392
- }
1393
- /**
1394
- * Internal: check if RLS is enabled on this table
1395
- */
1396
- isRlsEnabled() {
1397
- return this[EnableRLS];
1398
- }
1399
- /**
1400
- * Internal: add foreign key metadata for runtime enforcement
1401
- */
1402
- addForeignKey(definition) {
1403
- const matches = (existing) => {
1404
- if (existing.foreignTableName !== definition.foreignTableName) return false;
1405
- if (existing.columns.length !== definition.columns.length) return false;
1406
- if (existing.foreignColumns.length !== definition.foreignColumns.length) return false;
1407
- for (let i = 0; i < existing.columns.length; i++) if (existing.columns[i] !== definition.columns[i]) return false;
1408
- for (let i = 0; i < existing.foreignColumns.length; i++) if (existing.foreignColumns[i] !== definition.foreignColumns[i]) return false;
1409
- return true;
1410
- };
1411
- this.foreignKeys = this.foreignKeys.filter((existing) => !matches(existing));
1412
- this.foreignKeys.push(definition);
1413
- }
1414
- resolveDeferredForeignKeys() {
1415
- if (this.deferredForeignKeysResolved) return;
1416
- this.deferredForeignKeysResolved = true;
1417
- for (const deferred of this.deferredForeignKeys) {
1418
- let foreignColumn;
1419
- try {
1420
- foreignColumn = deferred.ref();
1421
- } catch (error) {
1422
- const reason = error instanceof Error ? ` ${error.message}` : "";
1423
- throw new Error(`Failed to resolve foreign key reference for '${this.tableName}.${deferred.localColumnName}'. Use references(() => targetTable.column) after both tables are declared.${reason}`);
1424
- }
1425
- const foreignTableName = getColumnTableName(foreignColumn);
1426
- if (!foreignTableName) throw new Error(`Foreign key on '${this.tableName}.${deferred.localColumnName}' references a column without a table. Use references(() => targetTable.column).`);
1427
- const foreignTable = getColumnTable(foreignColumn);
1428
- if (!foreignTable) throw new Error(`Foreign key on '${this.tableName}.${deferred.localColumnName}' references a column without table metadata. Replace references(() => id('tableName')) with references(() => table.id).`);
1429
- const foreignColumnName = getColumnName(foreignColumn);
1430
- this.addForeignKey({
1431
- name: deferred.config.name,
1432
- columns: [deferred.localColumnName],
1433
- foreignTableName,
1434
- foreignTable,
1435
- foreignColumns: [foreignColumnName],
1436
- onDelete: deferred.config.onDelete,
1437
- onUpdate: deferred.config.onUpdate
1438
- });
1439
- }
1440
- this.deferredForeignKeys = [];
1441
- }
1442
- /**
1443
- * Internal: expose foreign key metadata for mutation enforcement
1444
- */
1445
- getForeignKeys() {
1446
- this.resolveDeferredForeignKeys();
1447
- return this.foreignKeys;
1448
- }
1449
- addCheck(name, expression) {
1450
- this.checks.push({
1451
- name,
1452
- expression
1453
- });
1454
- }
1455
- getChecks() {
1456
- return this.checks;
1457
- }
1458
- /**
1459
- * Internal: add search index to table from builder extraConfig
1460
- */
1461
- addSearchIndex(name, config) {
1462
- const entry = {
1463
- indexDescriptor: name,
1464
- searchField: config.searchField,
1465
- filterFields: config.filterFields ?? []
1466
- };
1467
- if (config.staged) this.stagedSearchIndexes.push(entry);
1468
- else this.searchIndexes.push(entry);
1469
- }
1470
- /**
1471
- * Internal: add vector index to table from builder extraConfig
1472
- */
1473
- addVectorIndex(name, config) {
1474
- const entry = {
1475
- indexDescriptor: name,
1476
- vectorField: config.vectorField,
1477
- dimensions: config.dimensions,
1478
- filterFields: config.filterFields ?? []
1479
- };
1480
- if (config.staged) this.stagedVectorIndexes.push(entry);
1481
- else this.vectorIndexes.push(entry);
1482
- }
1483
- /**
1484
- * Export the contents of this definition for Convex schema tooling.
1485
- * Mirrors convex/server TableDefinition.export().
1486
- */
1487
- export() {
1488
- const documentType = this.validator.json;
1489
- if (typeof documentType !== "object") throw new Error("Invalid validator: please make sure that the parameter of `defineTable` is valid (see https://docs.convex.dev/database/schemas)");
1490
- return {
1491
- indexes: this.indexes,
1492
- stagedDbIndexes: this.stagedDbIndexes,
1493
- searchIndexes: this.searchIndexes,
1494
- stagedSearchIndexes: this.stagedSearchIndexes,
1495
- vectorIndexes: this.vectorIndexes,
1496
- stagedVectorIndexes: this.stagedVectorIndexes,
1497
- documentType
1498
- };
1499
- }
1500
- };
1501
- const convexTableInternal = (name, columns, extraConfig) => {
1502
- const expanded = resolveTableColumns(name, columns);
1503
- const rawTable = new ConvexTableImpl(name, expanded.columns, expanded.polymorphicConfigs);
1504
- const systemFields = createSystemFields(name);
1505
- for (const builder of Object.values(systemFields)) builder.config.table = rawTable;
1506
- const table = Object.assign(rawTable, systemFields, rawTable[Columns]);
1507
- const internalCreationTime = systemFields._creationTime;
1508
- if (Object.hasOwn(table, "_creationTime")) table._creationTime = void 0;
1509
- Object.defineProperty(table, "_creationTime", {
1510
- value: internalCreationTime,
1511
- enumerable: false,
1512
- configurable: true,
1513
- writable: false
1514
- });
1515
- Object.defineProperty(table, "_id", {
1516
- value: systemFields.id,
1517
- enumerable: false,
1518
- configurable: true,
1519
- writable: false
1520
- });
1521
- applyExtraConfig(rawTable, extraConfig?.(table));
1522
- return table;
1523
- };
1524
- const convexTableWithRLS = (name, columns, extraConfig) => {
1525
- const table = convexTableInternal(name, columns, extraConfig);
1526
- table[EnableRLS] = true;
1527
- return table;
1528
- };
1529
- const convexTable = Object.assign(convexTableInternal, { withRLS: convexTableWithRLS });
1530
-
1531
- //#endregion
1532
- //#region src/orm/aggregate-index/schema.ts
1533
- const AGGREGATE_BUCKET_TABLE = "aggregate_bucket";
1534
- const AGGREGATE_MEMBER_TABLE = "aggregate_member";
1535
- const AGGREGATE_EXTREMA_TABLE = "aggregate_extrema";
1536
- const AGGREGATE_RANK_TREE_TABLE = "aggregate_rank_tree";
1537
- const AGGREGATE_RANK_NODE_TABLE = "aggregate_rank_node";
1538
- const AGGREGATE_STATE_TABLE = "aggregate_state";
1539
- const countBucketTable = convexTable(AGGREGATE_BUCKET_TABLE, {
1540
- tableKey: text().notNull(),
1541
- indexName: text().notNull(),
1542
- keyHash: text().notNull(),
1543
- keyParts: arrayOf(json()).notNull(),
1544
- count: integer().notNull(),
1545
- sumValues: objectOf(integer().notNull()).notNull(),
1546
- nonNullCountValues: objectOf(integer().notNull()).notNull(),
1547
- updatedAt: integer().notNull()
1548
- }, (t) => [index("by_table_index_hash").on(t.tableKey, t.indexName, t.keyHash), index("by_table_index").on(t.tableKey, t.indexName)]);
1549
- const countMemberTable = convexTable(AGGREGATE_MEMBER_TABLE, {
1550
- kind: text().notNull(),
1551
- tableKey: text().notNull(),
1552
- indexName: text().notNull(),
1553
- docId: text().notNull(),
1554
- keyHash: text().notNull(),
1555
- keyParts: arrayOf(json()).notNull(),
1556
- sumValues: objectOf(integer().notNull()).notNull(),
1557
- nonNullCountValues: objectOf(integer().notNull()).notNull(),
1558
- extremaValues: objectOf(json()).notNull(),
1559
- rankNamespace: json(),
1560
- rankKey: json(),
1561
- rankSumValue: integer(),
1562
- updatedAt: integer().notNull()
1563
- }, (t) => [index("by_kind_table_index_doc").on(t.kind, t.tableKey, t.indexName, t.docId), index("by_kind_table_index").on(t.kind, t.tableKey, t.indexName)]);
1564
- const countExtremaTable = convexTable(AGGREGATE_EXTREMA_TABLE, {
1565
- tableKey: text().notNull(),
1566
- indexName: text().notNull(),
1567
- keyHash: text().notNull(),
1568
- fieldName: text().notNull(),
1569
- valueHash: text().notNull(),
1570
- value: json().notNull(),
1571
- sortKey: text().notNull(),
1572
- count: integer().notNull(),
1573
- updatedAt: integer().notNull()
1574
- }, (t) => [
1575
- index("by_table_index").on(t.tableKey, t.indexName),
1576
- index("by_table_index_hash_field_value").on(t.tableKey, t.indexName, t.keyHash, t.fieldName, t.valueHash),
1577
- index("by_table_index_hash_field_sort").on(t.tableKey, t.indexName, t.keyHash, t.fieldName, t.sortKey)
1578
- ]);
1579
- const countStateTable = convexTable(AGGREGATE_STATE_TABLE, {
1580
- kind: text().notNull(),
1581
- tableKey: text().notNull(),
1582
- indexName: text().notNull(),
1583
- keyDefinitionHash: text().notNull(),
1584
- metricDefinitionHash: text().notNull(),
1585
- status: text().notNull(),
1586
- cursor: text(),
1587
- processed: integer().notNull(),
1588
- startedAt: integer().notNull(),
1589
- updatedAt: integer().notNull(),
1590
- completedAt: integer(),
1591
- lastError: text()
1592
- }, (t) => [index("by_kind_table_index").on(t.kind, t.tableKey, t.indexName), index("by_kind_status").on(t.kind, t.status)]);
1593
- const rankTreeTable = convexTable(AGGREGATE_RANK_TREE_TABLE, {
1594
- aggregateName: text().notNull(),
1595
- maxNodeSize: integer().notNull(),
1596
- namespace: json(),
1597
- root: id(AGGREGATE_RANK_NODE_TABLE).notNull()
1598
- }, (tree) => [index("by_namespace").on(tree.namespace), index("by_aggregate_name").on(tree.aggregateName)]);
1599
- const rankNodeTable = convexTable(AGGREGATE_RANK_NODE_TABLE, {
1600
- aggregate: objectOf({
1601
- count: integer().notNull(),
1602
- sum: integer().notNull()
1603
- }),
1604
- items: arrayOf(objectOf({
1605
- k: json(),
1606
- v: json(),
1607
- s: integer().notNull()
1608
- })).notNull(),
1609
- subtrees: arrayOf(text().notNull()).notNull()
1610
- });
1611
- const aggregateStorageTables = {
1612
- [AGGREGATE_BUCKET_TABLE]: countBucketTable,
1613
- [AGGREGATE_MEMBER_TABLE]: countMemberTable,
1614
- [AGGREGATE_EXTREMA_TABLE]: countExtremaTable,
1615
- [AGGREGATE_RANK_TREE_TABLE]: rankTreeTable,
1616
- [AGGREGATE_RANK_NODE_TABLE]: rankNodeTable,
1617
- [AGGREGATE_STATE_TABLE]: countStateTable
1618
- };
1619
- function aggregateExtension() {
1620
- return defineSchemaExtension("aggregate", aggregateStorageTables);
1621
- }
1622
-
1623
- //#endregion
1624
- //#region src/orm/migrations/schema.ts
1625
- const MIGRATION_STATE_TABLE = "migration_state";
1626
- const MIGRATION_RUN_TABLE = "migration_run";
1627
- const migrationStateTable = convexTable(MIGRATION_STATE_TABLE, {
1628
- migrationId: text().notNull(),
1629
- checksum: text().notNull(),
1630
- applied: boolean$1().notNull(),
1631
- status: text().notNull(),
1632
- direction: text(),
1633
- runId: text(),
1634
- cursor: text(),
1635
- processed: integer().notNull(),
1636
- startedAt: integer(),
1637
- updatedAt: integer().notNull(),
1638
- completedAt: integer(),
1639
- lastError: text(),
1640
- writeMode: text().notNull()
1641
- }, (t) => [index("by_migration_id").on(t.migrationId), index("by_status").on(t.status)]);
1642
- const migrationRunTable = convexTable(MIGRATION_RUN_TABLE, {
1643
- runId: text().notNull(),
1644
- direction: text().notNull(),
1645
- status: text().notNull(),
1646
- dryRun: boolean$1().notNull(),
1647
- allowDrift: boolean$1().notNull(),
1648
- migrationIds: custom(v.array(v.string())).notNull(),
1649
- currentIndex: integer().notNull(),
1650
- startedAt: integer().notNull(),
1651
- updatedAt: integer().notNull(),
1652
- completedAt: integer(),
1653
- cancelRequested: boolean$1().notNull(),
1654
- lastError: text()
1655
- }, (t) => [index("by_run_id").on(t.runId), index("by_status").on(t.status)]);
1656
- const migrationStorageTables = {
1657
- [MIGRATION_STATE_TABLE]: migrationStateTable,
1658
- [MIGRATION_RUN_TABLE]: migrationRunTable
1659
- };
1660
- function migrationExtension() {
1661
- return defineSchemaExtension("migration", migrationStorageTables);
1662
- }
1663
-
1664
- //#endregion
1665
- //#region src/orm/relations.ts
1666
- var RelationsBuilderTable = class {
1667
- static [entityKind] = "RelationsBuilderTable";
1668
- _;
1669
- constructor(table, name) {
1670
- this._ = {
1671
- name,
1672
- table
1673
- };
1674
- }
1675
- };
1676
- var RelationsBuilderColumn = class {
1677
- static [entityKind] = "RelationsBuilderColumn";
1678
- _;
1679
- constructor(column, tableName, key) {
1680
- this._ = {
1681
- tableName,
1682
- column,
1683
- key
1684
- };
1685
- }
1686
- through(column) {
1687
- return new RelationsBuilderJunctionColumn(this._.column, this._.tableName, this._.key, column);
1688
- }
1689
- };
1690
- var RelationsBuilderJunctionColumn = class {
1691
- static [entityKind] = "RelationsBuilderColumn";
1692
- _;
1693
- constructor(column, tableName, key, through) {
1694
- this._ = {
1695
- tableName,
1696
- column,
1697
- through,
1698
- key
1699
- };
1700
- }
1701
- };
1702
- var RelationsHelperStatic = class {
1703
- static [entityKind] = "RelationsHelperStatic";
1704
- constructor(tables) {
1705
- const one = {};
1706
- const many = {};
1707
- for (const [tableName, table] of Object.entries(tables)) {
1708
- one[tableName] = (config) => new One(tables, table, tableName, config);
1709
- many[tableName] = (config) => new Many(tables, table, tableName, config);
1710
- }
1711
- this.one = one;
1712
- this.many = many;
1713
- }
1714
- one;
1715
- many;
1716
- };
1717
- function createRelationsHelper(tables) {
1718
- const helperStatic = new RelationsHelperStatic(tables);
1719
- const relationsTables = Object.entries(tables).reduce((acc, [tKey, value]) => {
1720
- const rTable = new RelationsBuilderTable(value, tKey);
1721
- const columns = Object.entries(getTableColumns(value)).reduce((colsAcc, [cKey, column]) => {
1722
- colsAcc[cKey] = new RelationsBuilderColumn(column, tKey, cKey);
1723
- return colsAcc;
1724
- }, {});
1725
- const relationsTable = Object.assign(rTable, columns);
1726
- Object.defineProperty(relationsTable, "_id", {
1727
- get() {
1728
- throw new Error("`_id` is no longer public in relations. Use `id`.");
1729
- },
1730
- enumerable: false,
1731
- configurable: false
1732
- });
1733
- acc[tKey] = relationsTable;
1734
- return acc;
1735
- }, {});
1736
- return Object.assign(helperStatic, relationsTables);
1737
- }
1738
- function extractTablesFromSchema(schema) {
1739
- const schemaTables = schema && typeof schema === "object" && !Array.isArray(schema) && "tables" in schema && schema.tables && typeof schema.tables === "object" && !Array.isArray(schema.tables) ? schema.tables : schema;
1740
- return Object.fromEntries(Object.entries(schemaTables).filter(([_, e]) => isConvexTable(e)));
1741
- }
1742
- var Relation = class {
1743
- static [entityKind] = "RelationV2";
1744
- fieldName;
1745
- sourceColumns;
1746
- targetColumns;
1747
- alias;
1748
- where;
1749
- sourceTable;
1750
- targetTable;
1751
- through;
1752
- throughTable;
1753
- isReversed;
1754
- /** @internal */
1755
- sourceColumnTableNames = [];
1756
- /** @internal */
1757
- targetColumnTableNames = [];
1758
- constructor(targetTable, targetTableName) {
1759
- this.targetTableName = targetTableName;
1760
- this.targetTable = targetTable;
1761
- }
1762
- };
1763
- var One = class extends Relation {
1764
- static [entityKind] = "OneV2";
1765
- relationType = "one";
1766
- optional;
1767
- constructor(tables, targetTable, targetTableName, config) {
1768
- super(targetTable, targetTableName);
1769
- this.alias = config?.alias;
1770
- this.where = config?.where;
1771
- if (config?.from) this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
1772
- this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
1773
- this.sourceColumnTableNames.push(it._.tableName);
1774
- return it._.column;
1775
- });
1776
- if (config?.to) this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
1777
- this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
1778
- this.targetColumnTableNames.push(it._.tableName);
1779
- return it._.column;
1780
- });
1781
- if (this.throughTable) this.through = {
1782
- source: (Array.isArray(config?.from) ? config.from : config?.from ? [config.from] : []).map((c) => c._.through),
1783
- target: (Array.isArray(config?.to) ? config.to : config?.to ? [config.to] : []).map((c) => c._.through)
1784
- };
1785
- this.optional = config?.optional ?? true;
1786
- }
1787
- };
1788
- var Many = class extends Relation {
1789
- static [entityKind] = "ManyV2";
1790
- relationType = "many";
1791
- constructor(tables, targetTable, targetTableName, config) {
1792
- super(targetTable, targetTableName);
1793
- this.config = config;
1794
- this.alias = config?.alias;
1795
- this.where = config?.where;
1796
- if (config?.from) this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
1797
- this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
1798
- this.sourceColumnTableNames.push(it._.tableName);
1799
- return it._.column;
1800
- });
1801
- if (config?.to) this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
1802
- this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
1803
- this.targetColumnTableNames.push(it._.tableName);
1804
- return it._.column;
1805
- });
1806
- if (this.throughTable) this.through = {
1807
- source: (Array.isArray(config?.from) ? config.from : config?.from ? [config.from] : []).map((c) => c._.through),
1808
- target: (Array.isArray(config?.to) ? config.to : config?.to ? [config.to] : []).map((c) => c._.through)
1809
- };
1810
- }
1811
- };
1812
- function buildRelations(tables, config, strict, defaults) {
1813
- const tablesConfig = {};
1814
- for (const [tsName, table] of Object.entries(tables)) tablesConfig[tsName] = {
1815
- table,
1816
- name: tsName,
1817
- polymorphic: table[TablePolymorphic],
1818
- relations: config[tsName] ?? {},
1819
- strict,
1820
- defaults
1821
- };
1822
- return processRelations(tablesConfig, tables);
1823
- }
1824
- function defineRelations(schema, relations) {
1825
- const tables = extractTablesFromSchema(schema);
1826
- const schemaOptions = schema[OrmSchemaOptions];
1827
- const strict = schemaOptions?.strict ?? true;
1828
- const defaults = schemaOptions?.defaults;
1829
- const schemaDefinition = schema[OrmSchemaDefinition];
1830
- const pluginTableNames = schema[OrmSchemaExtensionTables];
1831
- const plugins = schema[OrmSchemaExtensions];
1832
- const tablesConfig = buildRelations(tables, relations ? relations(createRelationsHelper(tables)) : {}, strict, defaults);
1833
- Object.defineProperty(tablesConfig, OrmSchemaOptions, {
1834
- value: {
1835
- strict,
1836
- defaults
1837
- },
1838
- enumerable: false
1839
- });
1840
- if (schemaDefinition) Object.defineProperty(tablesConfig, OrmSchemaDefinition, {
1841
- value: schemaDefinition,
1842
- enumerable: false
1843
- });
1844
- if (pluginTableNames) Object.defineProperty(tablesConfig, OrmSchemaExtensionTables, {
1845
- value: pluginTableNames,
1846
- enumerable: false
1847
- });
1848
- if (plugins) Object.defineProperty(tablesConfig, OrmSchemaExtensions, {
1849
- value: plugins,
1850
- enumerable: false
1851
- });
1852
- return tablesConfig;
1853
- }
1854
- function processRelations(tablesConfig, tables) {
1855
- for (const tableConfig of Object.values(tablesConfig)) for (const [relationFieldName, relation] of Object.entries(tableConfig.relations)) {
1856
- if (!isRelation(relation)) continue;
1857
- relation.sourceTable = tableConfig.table;
1858
- relation.fieldName = relationFieldName;
1859
- }
1860
- for (const [sourceTableName, tableConfig] of Object.entries(tablesConfig)) for (const [relationFieldName, relation] of Object.entries(tableConfig.relations)) {
1861
- if (!isRelation(relation)) continue;
1862
- let reverseRelation;
1863
- const { targetTableName, alias, sourceColumns, targetColumns, throughTable, sourceTable, through, where, sourceColumnTableNames, targetColumnTableNames } = relation;
1864
- const relationPrintName = `relations -> ${tableConfig.name}: { ${relationFieldName}: r.${relation.relationType === "one" ? "one" : "many"}.${targetTableName}(...) }`;
1865
- if (relationFieldName in getTableColumns(tableConfig.table)) throw new Error(`${relationPrintName}: relation name collides with column "${relationFieldName}" of table "${tableConfig.name}"`);
1866
- if (typeof alias === "string" && !alias) throw new Error(`${relationPrintName}: "alias" cannot be empty`);
1867
- if (sourceColumns?.length === 0) throw new Error(`${relationPrintName}: "from" cannot be empty`);
1868
- if (targetColumns?.length === 0) throw new Error(`${relationPrintName}: "to" cannot be empty`);
1869
- if (sourceColumns && targetColumns) {
1870
- if (sourceColumns.length !== targetColumns.length && !throughTable) throw new Error(`${relationPrintName}: "from" and "to" must have same length`);
1871
- for (const sName of sourceColumnTableNames) if (sName !== sourceTableName) throw new Error(`${relationPrintName}: all "from" columns must belong to table "${sourceTableName}", found "${sName}"`);
1872
- for (const tName of targetColumnTableNames) if (tName !== targetTableName) throw new Error(`${relationPrintName}: all "to" columns must belong to table "${targetTableName}", found "${tName}"`);
1873
- if (through) {
1874
- if (through.source.length !== sourceColumns.length || through.target.length !== targetColumns.length) throw new Error(`${relationPrintName}: .through() must be used on all columns in "from" and "to" or none`);
1875
- for (const column of through.source) if (tables[column._.tableName] !== throughTable) throw new Error(`${relationPrintName}: .through() must use same table for all columns`);
1876
- for (const column of through.target) if (tables[column._.tableName] !== throughTable) throw new Error(`${relationPrintName}: .through() must use same table for all columns`);
1877
- }
1878
- continue;
1879
- }
1880
- if (sourceColumns || targetColumns) throw new Error(`${relationPrintName}: relation must have both "from" and "to" or none`);
1881
- const reverseTableConfig = tablesConfig[targetTableName];
1882
- if (!reverseTableConfig) throw new Error(`${relationPrintName}: missing "from"/"to" and no reverse relation found for "${targetTableName}"`);
1883
- if (alias) {
1884
- const reverseRelations = Object.values(reverseTableConfig.relations).filter((it) => isRelation(it) && it.alias === alias && it !== relation);
1885
- if (reverseRelations.length > 1) throw new Error(`${relationPrintName}: multiple reverse relations with alias "${alias}" found`);
1886
- reverseRelation = reverseRelations[0];
1887
- if (!reverseRelation) throw new Error(`${relationPrintName}: no reverse relation with alias "${alias}" found in "${targetTableName}"`);
1888
- } else {
1889
- const reverseRelations = Object.values(reverseTableConfig.relations).filter((it) => isRelation(it) && it.targetTable === sourceTable && !it.alias && it !== relation);
1890
- if (reverseRelations.length > 1) throw new Error(`${relationPrintName}: multiple relations between "${targetTableName}" and "${sourceTableName}"; use alias`);
1891
- reverseRelation = reverseRelations[0];
1892
- if (!reverseRelation) throw new Error(`${relationPrintName}: no reverse relation between "${targetTableName}" and "${sourceTableName}"`);
1893
- }
1894
- if (!reverseRelation.sourceColumns || !reverseRelation.targetColumns) throw new Error(`${relationPrintName}: reverse relation "${targetTableName}.${reverseRelation.fieldName}" missing "from"/"to"`);
1895
- relation.sourceColumns = reverseRelation.targetColumns;
1896
- relation.targetColumns = reverseRelation.sourceColumns;
1897
- relation.through = reverseRelation.through ? {
1898
- source: reverseRelation.through.target,
1899
- target: reverseRelation.through.source
1900
- } : void 0;
1901
- relation.throughTable = reverseRelation.throughTable;
1902
- relation.isReversed = !where;
1903
- relation.where = where ?? reverseRelation.where;
1904
- }
1905
- return tablesConfig;
1906
- }
1907
- function isConvexTable(value) {
1908
- return typeof value === "object" && value !== null && "tableName" in value && Columns in value;
1909
- }
1910
- function isRelation(value) {
1911
- return value instanceof Relation;
1912
- }
1913
- function getTableColumns(table) {
1914
- const columns = table[Columns];
1915
- const system = {};
1916
- if (table.id) system.id = table.id;
1917
- if (table.createdAt || table._creationTime) system.createdAt = table._creationTime ?? table.createdAt;
1918
- return {
1919
- ...columns,
1920
- ...system
1921
- };
1922
- }
1923
-
1924
- //#endregion
1925
- //#region src/orm/triggers.ts
1926
- function defineTriggers(schema, triggers) {
1927
- return triggers;
1928
- }
1929
-
1930
- //#endregion
1931
- //#region src/orm/schema.ts
1932
- const BUILTIN_SCHEMA_EXTENSIONS = [aggregateExtension(), migrationExtension()];
1933
- function resolveSchemaExtensions(extensions) {
1934
- const resolved = [...BUILTIN_SCHEMA_EXTENSIONS, ...extensions ?? []];
1935
- const seen = /* @__PURE__ */ new Set();
1936
- for (const extension of resolved) {
1937
- if (seen.has(extension.key)) throw new Error(`defineSchema received duplicate extension '${extension.key}'. Remove duplicate extension registrations.`);
1938
- seen.add(extension.key);
1939
- }
1940
- return resolved;
1941
- }
1942
- function mergeRelationConfigs(sources) {
1943
- const merged = {};
1944
- const relationOrigins = /* @__PURE__ */ new Map();
1945
- for (const { source, config } of sources) for (const [tableName, relationConfig] of Object.entries(config)) {
1946
- if (!relationConfig) continue;
1947
- let tableRelations = merged[tableName];
1948
- if (!tableRelations) {
1949
- tableRelations = Object.create(null);
1950
- merged[tableName] = tableRelations;
1951
- }
1952
- for (const [fieldName, relation] of Object.entries(relationConfig)) {
1953
- const relationKey = `${tableName}.${fieldName}`;
1954
- const existingSource = relationOrigins.get(relationKey);
1955
- if (existingSource) throw new Error(`defineSchema relation field '${relationKey}' is defined more than once (${existingSource} and ${source}).`);
1956
- tableRelations[fieldName] = relation;
1957
- relationOrigins.set(relationKey, source);
1958
- }
1959
- }
1960
- return merged;
1961
- }
1962
- function mergeTriggerConfigs(sources) {
1963
- const merged = {};
1964
- const triggerOrigins = /* @__PURE__ */ new Map();
1965
- for (const { source, config } of sources) for (const [tableName, tableConfig] of Object.entries(config)) {
1966
- if (!tableConfig || typeof tableConfig !== "object" || Array.isArray(tableConfig)) {
1967
- const triggerKey = `${tableName}`;
1968
- const existingSource = triggerOrigins.get(triggerKey);
1969
- if (existingSource) throw new Error(`defineSchema trigger '${triggerKey}' is defined more than once (${existingSource} and ${source}).`);
1970
- merged[tableName] = tableConfig;
1971
- triggerOrigins.set(triggerKey, source);
1972
- continue;
1973
- }
1974
- let mergedTable = merged[tableName];
1975
- if (!mergedTable) {
1976
- mergedTable = Object.create(null);
1977
- merged[tableName] = mergedTable;
1978
- }
1979
- for (const [hookKey, hookValue] of Object.entries(tableConfig)) {
1980
- if ((hookKey === "create" || hookKey === "update" || hookKey === "delete") && hookValue && typeof hookValue === "object" && !Array.isArray(hookValue)) {
1981
- let mergedOperation = mergedTable[hookKey];
1982
- if (!mergedOperation || typeof mergedOperation !== "object") {
1983
- mergedOperation = Object.create(null);
1984
- mergedTable[hookKey] = mergedOperation;
1985
- }
1986
- for (const [operationHookKey, operationHookValue] of Object.entries(hookValue)) {
1987
- const triggerKey = `${tableName}.${hookKey}.${operationHookKey}`;
1988
- const existingSource = triggerOrigins.get(triggerKey);
1989
- if (existingSource) throw new Error(`defineSchema trigger '${triggerKey}' is defined more than once (${existingSource} and ${source}).`);
1990
- mergedOperation[operationHookKey] = operationHookValue;
1991
- triggerOrigins.set(triggerKey, source);
1992
- }
1993
- continue;
1994
- }
1995
- const triggerKey = `${tableName}.${hookKey}`;
1996
- const existingSource = triggerOrigins.get(triggerKey);
1997
- if (existingSource) throw new Error(`defineSchema trigger '${triggerKey}' is defined more than once (${existingSource} and ${source}).`);
1998
- mergedTable[hookKey] = hookValue;
1999
- triggerOrigins.set(triggerKey, source);
2000
- }
2001
- }
2002
- return merged;
2003
- }
2004
- function defineMetadata(target, key, value) {
2005
- const existing = Object.getOwnPropertyDescriptor(target, key);
2006
- if (existing && !existing.configurable) return;
2007
- Object.defineProperty(target, key, {
2008
- value,
2009
- enumerable: false,
2010
- configurable: true
2011
- });
2012
- }
2013
- const OrmSchemaComposerState = Symbol("kitcn:OrmSchemaComposerState");
2014
- function getSchemaComposerState(schema) {
2015
- if (!schema || typeof schema !== "object") return;
2016
- return schema[OrmSchemaComposerState];
2017
- }
2018
- function finalizeSchemaMetadata(schema) {
2019
- if (!schema || typeof schema !== "object") return;
2020
- const composerState = getSchemaComposerState(schema);
2021
- if (!composerState) return;
2022
- const schemaObject = schema;
2023
- const resolvedExtensions = schemaObject[OrmSchemaExtensions] ?? resolveSchemaExtensions(composerState.extensions);
2024
- const extensionTableNames = schemaObject[OrmSchemaExtensionTables] ?? Object.freeze([]);
2025
- const options = schemaObject[OrmSchemaOptions];
2026
- const extensionRelationFactories = resolvedExtensions.map((extension) => ({
2027
- key: extension.key,
2028
- relations: extension[OrmSchemaExtensionRelations]
2029
- })).filter((entry) => entry.relations !== void 0);
2030
- const extensionTriggerFactories = resolvedExtensions.map((extension) => ({
2031
- key: extension.key,
2032
- triggers: extension[OrmSchemaExtensionTriggers]
2033
- })).filter((entry) => entry.triggers !== void 0);
2034
- const hasExtensionRelations = extensionRelationFactories.length > 0;
2035
- const hasExtensionTriggers = extensionTriggerFactories.length > 0;
2036
- const shouldBuildRelations = hasExtensionRelations || hasExtensionTriggers || Boolean(composerState.relations) || Boolean(composerState.triggers);
2037
- let relations = schemaObject[OrmSchemaRelations];
2038
- if (!relations && shouldBuildRelations) {
2039
- relations = hasExtensionRelations || composerState.relations ? defineRelations(schema, (helpers) => mergeRelationConfigs([...extensionRelationFactories.map((extension) => ({
2040
- source: `extension '${extension.key}'`,
2041
- config: extension.relations(helpers)
2042
- })), ...composerState.relations ? [{
2043
- source: "schema.relations()",
2044
- config: composerState.relations(helpers)
2045
- }] : []])) : defineRelations(schema);
2046
- defineMetadata(relations, OrmSchemaDefinition, schema);
2047
- if (options) defineMetadata(relations, OrmSchemaOptions, options);
2048
- defineMetadata(relations, OrmSchemaExtensionTables, extensionTableNames);
2049
- defineMetadata(relations, OrmSchemaExtensions, resolvedExtensions);
2050
- defineMetadata(schema, OrmSchemaRelations, relations);
2051
- }
2052
- if (!relations || schemaObject[OrmSchemaTriggers]) return;
2053
- const triggerSources = hasExtensionTriggers || composerState.triggers ? [...extensionTriggerFactories.map((extension) => ({
2054
- source: `extension '${extension.key}'`,
2055
- config: typeof extension.triggers === "function" ? extension.triggers(relations) : extension.triggers
2056
- })), ...composerState.triggers ? [{
2057
- source: "schema.triggers()",
2058
- config: composerState.triggers
2059
- }] : []] : [];
2060
- if (triggerSources.length === 0) return;
2061
- defineMetadata(schema, OrmSchemaTriggers, defineTriggers(relations, mergeTriggerConfigs(triggerSources)));
2062
- }
2063
- function getSchemaRelations(schema) {
2064
- if (!schema || typeof schema !== "object") return;
2065
- finalizeSchemaMetadata(schema);
2066
- return schema[OrmSchemaRelations];
2067
- }
2068
- function getSchemaTriggers(schema) {
2069
- if (!schema || typeof schema !== "object") return;
2070
- finalizeSchemaMetadata(schema);
2071
- return schema[OrmSchemaTriggers];
2072
- }
2073
-
2074
- //#endregion
2075
- //#region src/cli/utils/highlighter.ts
2076
- const isTruthyEnvFlag = (value) => value !== void 0 && value !== "" && value !== "0";
2077
- const isColorEnabled = () => {
2078
- if (process.env.FORCE_COLOR === "0") return false;
2079
- if (isTruthyEnvFlag(process.env.FORCE_COLOR)) return true;
2080
- if (isTruthyEnvFlag(process.env.NO_COLOR)) return false;
2081
- return Boolean(process.stdout.isTTY && process.env.TERM !== "dumb");
2082
- };
2083
- const withColors = (callback) => callback(createColors(isColorEnabled()));
2084
- const highlighter = {
2085
- bold(value) {
2086
- return withColors((colors) => colors.bold(value));
2087
- },
2088
- dim(value) {
2089
- return withColors((colors) => colors.dim(value));
2090
- },
2091
- info(value) {
2092
- return withColors((colors) => colors.cyan(value));
2093
- },
2094
- success(value) {
2095
- return withColors((colors) => colors.green(value));
2096
- },
2097
- warn(value) {
2098
- return withColors((colors) => colors.yellow(value));
2099
- },
2100
- error(value) {
2101
- return withColors((colors) => colors.red(value));
2102
- },
2103
- path(value) {
2104
- return withColors((colors) => colors.bold(value));
2105
- }
2106
- };
2107
-
2108
- //#endregion
2109
- //#region src/shared/meta-utils.ts
2110
- /** Files to exclude from meta generation */
2111
- const EXCLUDED_FILES = new Set([
2112
- "schema.ts",
2113
- "auth.ts",
2114
- "generated.ts",
2115
- "convex.config.ts",
2116
- "auth.config.ts"
2117
- ]);
2118
- /**
2119
- * Check if a file path should be included in meta generation.
2120
- * Filters out private files/directories (prefixed with _) and config files.
2121
- */
2122
- function isValidConvexFile(file) {
2123
- if (file.endsWith(".runtime.ts")) return false;
2124
- if (file.endsWith(".test.ts")) return false;
2125
- if (file.endsWith(".spec.ts")) return false;
2126
- if (file.endsWith(".testing.ts")) return false;
2127
- if (file.endsWith(".typecheck.ts")) return false;
2128
- if (file.startsWith("generated/")) return false;
2129
- if (file.startsWith("_") || file.includes("/_")) return false;
2130
- const basename = file.split("/").pop() ?? "";
2131
- if (EXCLUDED_FILES.has(basename)) return false;
2132
- return true;
2133
- }
2134
-
2135
- //#endregion
2136
- //#region src/cli/utils/logger.ts
2137
- const joinArgs = (args) => args.map(String).join(" ");
2138
- const logger = {
2139
- error(...args) {
2140
- console.error(highlighter.error(joinArgs(args)));
2141
- },
2142
- warn(...args) {
2143
- console.warn(highlighter.warn(joinArgs(args)));
2144
- },
2145
- info(...args) {
2146
- console.info(highlighter.info(joinArgs(args)));
2147
- },
2148
- success(...args) {
2149
- console.info(highlighter.success(joinArgs(args)));
2150
- },
2151
- log(...args) {
2152
- console.info(joinArgs(args));
2153
- },
2154
- write(value) {
2155
- console.info(value);
2156
- },
2157
- break() {
2158
- console.info("");
2159
- }
2160
- };
2161
-
2162
- //#endregion
2163
- //#region src/cli/codegen.ts
2164
- /** Valid JS identifier pattern for object keys */
2165
- const VALID_IDENTIFIER_RE = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
2166
- /** Valid JS identifier start pattern */
2167
- const IDENTIFIER_START_RE = /^[a-zA-Z_$]/;
2168
- /** Pattern to strip .ts extension */
2169
- const TS_EXTENSION_RE = /\.ts$/;
2170
- /** Pattern to detect default exports in auth contract files. */
2171
- const DEFAULT_EXPORT_RE = /\bexport\s+default\b/;
2172
- const MISSING_KITCN_IMPORT_RE = /Cannot find (?:module|package) ['"]kitcn(?:\/[^'"]+)?['"]/;
2173
- const RUNTIME_CALLER_RESERVED_EXPORTS = new Set(["actions", "schedule"]);
2174
- const DEFAULT_TRIM_SEGMENTS = ["plugins", "generated"];
2175
- const CODEGEN_SCOPES = new Set([
2176
- "all",
2177
- "auth",
2178
- "orm"
2179
- ]);
2180
- function normalizeCodegenScope(scope) {
2181
- const normalized = scope ?? "all";
2182
- if (CODEGEN_SCOPES.has(normalized)) return normalized;
2183
- throw new Error(`Invalid codegen scope "${normalized}". Expected one of: all, auth, orm.`);
2184
- }
2185
- function shouldGenerateApi(scope) {
2186
- return scope === "all";
2187
- }
2188
- function shouldGenerateAuth(scope) {
2189
- return scope !== "orm";
2190
- }
2191
- function resolveGenerationMode(options) {
2192
- const scope = normalizeCodegenScope(options?.scope);
2193
- return {
2194
- generateApi: shouldGenerateApi(scope),
2195
- generateAuth: shouldGenerateAuth(scope),
2196
- modeLabel: scope
2197
- };
2198
- }
2199
- const AUTH_RUNTIME_PROCEDURES = [
2200
- {
2201
- exportName: "create",
2202
- internal: true,
2203
- type: "mutation"
2204
- },
2205
- {
2206
- exportName: "deleteMany",
2207
- internal: true,
2208
- type: "mutation"
2209
- },
2210
- {
2211
- exportName: "deleteOne",
2212
- internal: true,
2213
- type: "mutation"
2214
- },
2215
- {
2216
- exportName: "findMany",
2217
- internal: true,
2218
- type: "query"
2219
- },
2220
- {
2221
- exportName: "findOne",
2222
- internal: true,
2223
- type: "query"
2224
- },
2225
- {
2226
- exportName: "getLatestJwks",
2227
- internal: true,
2228
- type: "action"
2229
- },
2230
- {
2231
- exportName: "rotateKeys",
2232
- internal: true,
2233
- type: "action"
2234
- },
2235
- {
2236
- exportName: "updateMany",
2237
- internal: true,
2238
- type: "mutation"
2239
- },
2240
- {
2241
- exportName: "updateOne",
2242
- internal: true,
2243
- type: "mutation"
2244
- }
2245
- ];
2246
- const GENERATED_ORM_RUNTIME_PROCEDURES = [
2247
- {
2248
- exportName: "scheduledMutationBatch",
2249
- internal: true,
2250
- type: "mutation"
2251
- },
2252
- {
2253
- exportName: "scheduledDelete",
2254
- internal: true,
2255
- type: "mutation"
2256
- },
2257
- {
2258
- exportName: "aggregateBackfill",
2259
- internal: true,
2260
- type: "mutation"
2261
- },
2262
- {
2263
- exportName: "aggregateBackfillChunk",
2264
- internal: true,
2265
- type: "mutation"
2266
- },
2267
- {
2268
- exportName: "aggregateBackfillStatus",
2269
- internal: true,
2270
- type: "mutation"
2271
- },
2272
- {
2273
- exportName: "migrationRun",
2274
- internal: true,
2275
- type: "mutation"
2276
- },
2277
- {
2278
- exportName: "migrationRunChunk",
2279
- internal: true,
2280
- type: "mutation"
2281
- },
2282
- {
2283
- exportName: "migrationStatus",
2284
- internal: true,
2285
- type: "mutation"
2286
- },
2287
- {
2288
- exportName: "migrationCancel",
2289
- internal: true,
2290
- type: "mutation"
2291
- },
2292
- {
2293
- exportName: "resetChunk",
2294
- internal: true,
2295
- type: "mutation"
2296
- },
2297
- {
2298
- exportName: "reset",
2299
- internal: true,
2300
- type: "action"
2301
- }
2302
- ];
2303
- function listFilesRecursive(cwd, relDir = "") {
2304
- const absDir = path.join(cwd, relDir);
2305
- const entries = fs.readdirSync(absDir, { withFileTypes: true });
2306
- const files = [];
2307
- for (const entry of entries) {
2308
- const relPath = relDir ? `${relDir}/${entry.name}` : entry.name;
2309
- if (entry.isDirectory()) {
2310
- files.push(...listFilesRecursive(cwd, relPath));
2311
- continue;
2312
- }
2313
- if (entry.isFile()) files.push(relPath);
2314
- }
2315
- return files;
2316
- }
2317
- function ensureRelativeImportPath(value) {
2318
- if (value.startsWith(".") || value.startsWith("/")) return value;
2319
- return `./${value}`;
2320
- }
2321
- function normalizeImportPath(value) {
2322
- return value.replace(/\\/g, "/");
2323
- }
2324
- function escapeRegex(value) {
2325
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2326
- }
2327
- function formatKey(key) {
2328
- return VALID_IDENTIFIER_RE.test(key) ? key : `'${key}'`;
2329
- }
2330
- function toPascalCaseToken(token) {
2331
- if (token.length === 0) return "";
2332
- return `${token[0]?.toUpperCase() ?? ""}${token.slice(1)}`;
2333
- }
2334
- function getRuntimeNameHash(moduleName) {
2335
- return createHash("sha1").update(moduleName).digest("hex").slice(0, 6);
2336
- }
2337
- function normalizeTrimSegments(trimSegments) {
2338
- const source = [...DEFAULT_TRIM_SEGMENTS, ...trimSegments ?? []];
2339
- return [...new Set(source.map((segment) => segment.trim()).filter(Boolean))];
2340
- }
2341
- function toRuntimeExportBase(moduleSegments, fallbackSegments) {
2342
- const base = moduleSegments.filter((segment) => segment.length > 0).flatMap((segment) => segment.split(/[^a-zA-Z0-9]+/g).filter(Boolean).map((token) => toPascalCaseToken(token))).join("");
2343
- if (base.length === 0) {
2344
- if (fallbackSegments && fallbackSegments.length > 0) return toRuntimeExportBase(fallbackSegments);
2345
- return "Module";
2346
- }
2347
- return IDENTIFIER_START_RE.test(base) ? base : `M${base}`;
2348
- }
2349
- function getModuleRuntimeExportBase(moduleName, trimSegments) {
2350
- const moduleSegments = moduleName.split("/").filter(Boolean);
2351
- const trimSet = new Set(trimSegments);
2352
- const keptSegments = moduleSegments.filter((segment) => !trimSet.has(segment));
2353
- const removedSegments = moduleSegments.filter((segment) => trimSet.has(segment));
2354
- const primaryBase = toRuntimeExportBase(keptSegments, moduleSegments);
2355
- const removedBase = toRuntimeExportBase(removedSegments);
2356
- return {
2357
- primaryBase,
2358
- collisionBase: removedSegments.length > 0 ? `${primaryBase}${removedBase}` : primaryBase
2359
- };
2360
- }
2361
- function resolveModuleRuntimeExportNames(moduleNames, trimSegments) {
2362
- const resolvedNames = /* @__PURE__ */ new Map();
2363
- const byPrimaryBase = /* @__PURE__ */ new Map();
2364
- for (const moduleName of [...new Set(moduleNames)].sort((a, b) => a.localeCompare(b))) {
2365
- if (moduleName === "generated/server") {
2366
- resolvedNames.set(moduleName, {
2367
- callerExportName: "createServerCaller",
2368
- handlerExportName: "createServerHandler"
2369
- });
2370
- continue;
2371
- }
2372
- const { primaryBase, collisionBase } = getModuleRuntimeExportBase(moduleName, trimSegments);
2373
- const entries = byPrimaryBase.get(primaryBase);
2374
- if (entries) {
2375
- entries.push({
2376
- moduleName,
2377
- collisionBase
2378
- });
2379
- continue;
2380
- }
2381
- byPrimaryBase.set(primaryBase, [{
2382
- moduleName,
2383
- collisionBase
2384
- }]);
2385
- }
2386
- for (const [primaryBase, entries] of byPrimaryBase) {
2387
- if (entries.length === 1) {
2388
- const entry = entries[0];
2389
- if (!entry) continue;
2390
- resolvedNames.set(entry.moduleName, {
2391
- callerExportName: `create${primaryBase}Caller`,
2392
- handlerExportName: `create${primaryBase}Handler`
2393
- });
2394
- continue;
2395
- }
2396
- const usedBases = /* @__PURE__ */ new Set();
2397
- for (const entry of entries) {
2398
- let exportBase = entry.collisionBase;
2399
- if (usedBases.has(exportBase)) exportBase = `${exportBase}_${getRuntimeNameHash(entry.moduleName)}`;
2400
- usedBases.add(exportBase);
2401
- resolvedNames.set(entry.moduleName, {
2402
- callerExportName: `create${exportBase}Caller`,
2403
- handlerExportName: `create${exportBase}Handler`
2404
- });
2405
- }
2406
- }
2407
- return resolvedNames;
2408
- }
2409
- function getModuleRuntimeExportNames(moduleName) {
2410
- const base = toRuntimeExportBase(moduleName.split("/").filter(Boolean));
2411
- return {
2412
- callerExportName: `create${base}Caller`,
2413
- handlerExportName: `create${base}Handler`
2414
- };
2415
- }
2416
- function getModuleImportPath(outputFile, functionsDir, moduleName) {
2417
- const moduleFile = path.join(functionsDir, moduleName);
2418
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), moduleFile)));
2419
- }
2420
- function getRuntimeApiImportPath(outputFile, functionsDir) {
2421
- const runtimeApiFile = path.join(functionsDir, "_generated", "api.js");
2422
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), runtimeApiFile)));
2423
- }
2424
- function getRuntimeApiTypesImportPath(outputFile, functionsDir) {
2425
- const runtimeApiFile = path.join(functionsDir, "_generated", "api");
2426
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), runtimeApiFile)));
2427
- }
2428
- function moduleUsesOwnGeneratedRuntime(functionsDir, moduleName) {
2429
- if (moduleName === "generated/server") return true;
2430
- const moduleFilePath = path.join(functionsDir, `${moduleName}.ts`);
2431
- if (!fs.existsSync(moduleFilePath)) return false;
2432
- const source = fs.readFileSync(moduleFilePath, "utf8");
2433
- const escapedRuntimeImportPath = escapeRegex(ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(moduleFilePath), path.join(functionsDir, "generated", `${moduleName}.runtime`)))));
2434
- return [new RegExp(`from\\s+['"]${escapedRuntimeImportPath}(?:\\.[jt]s)?['"]`), new RegExp(`require\\(\\s*['"]${escapedRuntimeImportPath}(?:\\.[jt]s)?['"]\\s*\\)`)].some((pattern) => pattern.test(source));
2435
- }
2436
- function getBracketAccessPath(rootIdentifier, pathSegments) {
2437
- return pathSegments.reduce((accessPath, segment) => `${accessPath}[${JSON.stringify(segment)}]`, rootIdentifier);
2438
- }
2439
- function getSchemaImportPath(outputFile, functionsDir) {
2440
- const schemaFile = path.join(functionsDir, "schema");
2441
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), schemaFile)));
2442
- }
2443
- function getServerTypesImportPath(outputFile, functionsDir) {
2444
- const serverTypesFile = path.join(functionsDir, "_generated", "server");
2445
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), serverTypesFile)));
2446
- }
2447
- function getDataModelImportPath(outputFile, functionsDir) {
2448
- const dataModelFile = path.join(functionsDir, "_generated", "dataModel");
2449
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), dataModelFile)));
2450
- }
2451
- function getHttpImportPath(outputFile, functionsDir) {
2452
- const httpFile = path.join(functionsDir, "http");
2453
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), httpFile)));
2454
- }
2455
- const GENERATED_DIR = "generated";
2456
- function getGeneratedServerOutputFile(functionsDir) {
2457
- return path.join(functionsDir, GENERATED_DIR, "server.ts");
2458
- }
2459
- function getGeneratedOrmOutputFile(functionsDir) {
2460
- return path.join(functionsDir, GENERATED_DIR, "orm.ts");
2461
- }
2462
- function getGeneratedCrpcOutputFile(functionsDir) {
2463
- return path.join(functionsDir, GENERATED_DIR, "crpc.ts");
2464
- }
2465
- function getGeneratedAuthOutputFile(functionsDir) {
2466
- return path.join(functionsDir, GENERATED_DIR, "auth.ts");
2467
- }
2468
- function getGeneratedMigrationsHelperOutputFile(functionsDir) {
2469
- return path.join(functionsDir, GENERATED_DIR, "migrations.gen.ts");
2470
- }
2471
- function getLegacyGeneratedOutputFile(functionsDir) {
2472
- return path.join(functionsDir, "generated.ts");
2473
- }
2474
- function getModuleNameFromOutputFile(outputFile, functionsDir) {
2475
- return normalizeImportPath(path.relative(functionsDir, outputFile)).replace(TS_EXTENSION_RE, "");
2476
- }
2477
- function getGeneratedServerImportPath(outputFile, functionsDir) {
2478
- const generatedServerFile = getGeneratedServerOutputFile(functionsDir);
2479
- return ensureRelativeImportPath(normalizeImportPath(path.relative(path.dirname(outputFile), generatedServerFile)).replace(TS_EXTENSION_RE, ""));
2480
- }
2481
- function getGeneratedRuntimeOutputFile(functionsDir, moduleName) {
2482
- const runtimeModuleName = moduleName.startsWith(`${GENERATED_DIR}/`) ? moduleName.slice(10) : moduleName;
2483
- return path.join(functionsDir, GENERATED_DIR, `${runtimeModuleName}.runtime.ts`);
2484
- }
2485
- function emitGeneratedServerPlaceholderFile() {
2486
- return `// biome-ignore-all format: generated
2487
- /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-vars */
2488
- // This file is auto-generated by kitcn
2489
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2490
-
2491
- import { initCRPC as baseInitCRPC } from 'kitcn/server';
2492
- import type {
2493
- ActionCtx as ServerActionCtx,
2494
- MutationCtx as ServerMutationCtx,
2495
- QueryCtx as ServerQueryCtx,
2496
- } from '../_generated/server';
2497
-
2498
- export type QueryCtx = ServerQueryCtx;
2499
- export type MutationCtx = ServerMutationCtx;
2500
- export type ActionCtx = ServerActionCtx;
2501
- export type GenericCtx = QueryCtx | MutationCtx | ActionCtx;
2502
- export const initCRPC = baseInitCRPC;
2503
- export const httpAction = undefined as unknown;
2504
-
2505
- export function withOrm<Ctx>(ctx: Ctx): Ctx {
2506
- return ctx;
2507
- }
2508
- `;
2509
- }
2510
- function emitGeneratedAuthPlaceholderFile() {
2511
- return `// biome-ignore-all format: generated
2512
- /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-vars */
2513
- // This file is auto-generated by kitcn
2514
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2515
-
2516
- export function defineAuth<TDefinition>(definition: TDefinition): TDefinition {
2517
- return definition;
2518
- }
2519
-
2520
- export const authEnabled = false;
2521
- export const authClient = {} as Record<string, unknown>;
2522
- export const getAuth = () => ({} as Record<string, unknown>);
2523
- export const auth = {} as Record<string, unknown>;
2524
- `;
2525
- }
2526
- function emitGeneratedMigrationsPlaceholderFile() {
2527
- return `// biome-ignore-all format: generated
2528
- /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-vars */
2529
- // This file is auto-generated by kitcn
2530
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2531
-
2532
- export { defineMigration } from 'kitcn/orm';
2533
- `;
2534
- }
2535
- function writeFileIfChanged(filePath, content) {
2536
- if (fs.existsSync(filePath)) {
2537
- if (fs.readFileSync(filePath, "utf8") === content) return false;
2538
- }
2539
- fs.writeFileSync(filePath, content);
2540
- return true;
2541
- }
2542
- function ensureGeneratedSupportPlaceholders(functionsDir, options) {
2543
- const createdPlaceholderFiles = [];
2544
- const serverOutputFile = getGeneratedServerOutputFile(functionsDir);
2545
- const authOutputFile = getGeneratedAuthOutputFile(functionsDir);
2546
- const migrationsHelperOutputFile = getGeneratedMigrationsHelperOutputFile(functionsDir);
2547
- const generatedDir = path.dirname(serverOutputFile);
2548
- fs.mkdirSync(generatedDir, { recursive: true });
2549
- const includeAuth = options?.includeAuth ?? true;
2550
- if (!fs.existsSync(serverOutputFile)) {
2551
- writeFileIfChanged(serverOutputFile, emitGeneratedServerPlaceholderFile());
2552
- createdPlaceholderFiles.push(serverOutputFile);
2553
- }
2554
- if (includeAuth && !fs.existsSync(authOutputFile)) {
2555
- writeFileIfChanged(authOutputFile, emitGeneratedAuthPlaceholderFile());
2556
- createdPlaceholderFiles.push(authOutputFile);
2557
- }
2558
- if (!fs.existsSync(migrationsHelperOutputFile)) {
2559
- writeFileIfChanged(migrationsHelperOutputFile, emitGeneratedMigrationsPlaceholderFile());
2560
- createdPlaceholderFiles.push(migrationsHelperOutputFile);
2561
- }
2562
- return createdPlaceholderFiles;
2563
- }
2564
- function emitGeneratedRuntimePlaceholderFile(exportNames) {
2565
- const { callerExportName, handlerExportName } = exportNames;
2566
- return `// biome-ignore-all format: generated
2567
- // This file is auto-generated by kitcn
2568
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2569
-
2570
- export function ${callerExportName}(_ctx: unknown) {
2571
- throw new Error('[kitcn] Runtime caller is not generated yet. Run kitcn codegen.');
2572
- }
2573
-
2574
- export function ${handlerExportName}(_ctx: unknown) {
2575
- throw new Error('[kitcn] Runtime handler is not generated yet. Run kitcn codegen.');
2576
- }
2577
- `;
2578
- }
2579
- function ensureGeneratedRuntimePlaceholders(functionsDir, moduleNames, runtimeExportNames) {
2580
- const createdPlaceholderFiles = [];
2581
- for (const moduleName of moduleNames) {
2582
- const runtimeOutputFile = getGeneratedRuntimeOutputFile(functionsDir, moduleName);
2583
- if (fs.existsSync(runtimeOutputFile)) continue;
2584
- const exportNames = runtimeExportNames.get(moduleName) ?? getModuleRuntimeExportNames(moduleName);
2585
- fs.mkdirSync(path.dirname(runtimeOutputFile), { recursive: true });
2586
- writeFileIfChanged(runtimeOutputFile, emitGeneratedRuntimePlaceholderFile(exportNames));
2587
- createdPlaceholderFiles.push(runtimeOutputFile);
2588
- }
2589
- return createdPlaceholderFiles;
2590
- }
2591
- function listGeneratedRuntimeFiles(functionsDir) {
2592
- const generatedDir = path.join(functionsDir, "generated");
2593
- if (!fs.existsSync(generatedDir)) return [];
2594
- return listFilesRecursive(generatedDir).filter((file) => file.endsWith(".runtime.ts")).map((file) => path.join(generatedDir, file));
2595
- }
2596
- async function resolveSchemaMetadataForCodegen(functionsDir, debug) {
2597
- const schemaPath = path.join(functionsDir, "schema.ts");
2598
- if (!fs.existsSync(schemaPath)) return {
2599
- hasOrmSchema: false,
2600
- hasRelations: false,
2601
- hasTriggers: false
2602
- };
2603
- const jitiInstance = createJiti(process.cwd(), {
2604
- interopDefault: true,
2605
- moduleCache: false
2606
- });
2607
- try {
2608
- const schemaModule = await jitiInstance.import(schemaPath);
2609
- const schemaValue = schemaModule && typeof schemaModule === "object" ? schemaModule.default ?? schemaModule : null;
2610
- if (!schemaValue || typeof schemaValue !== "object") return {
2611
- hasOrmSchema: false,
2612
- hasRelations: false,
2613
- hasTriggers: false
2614
- };
2615
- return {
2616
- hasOrmSchema: OrmSchemaOptions in schemaValue,
2617
- hasRelations: Boolean(getSchemaRelations(schemaValue)),
2618
- hasTriggers: Boolean(getSchemaTriggers(schemaValue))
2619
- };
2620
- } catch (error) {
2621
- if (debug) logger.warn(`⚠️ Failed to load schema extensions from ${schemaPath}: ${error.message}`);
2622
- return {
2623
- hasOrmSchema: false,
2624
- hasRelations: false,
2625
- hasTriggers: false
2626
- };
2627
- }
2628
- }
2629
- function cleanupGeneratedPluginArtifacts(functionsDir) {
2630
- fs.rmSync(path.join(functionsDir, GENERATED_DIR, "plugins"), {
2631
- recursive: true,
2632
- force: true
2633
- });
2634
- }
2635
- function emitGeneratedServerFile(outputFile, functionsDir, hasOrmSchema, hasMigrationsManifest) {
2636
- const asSingleQuotedImport = (importPath) => `'${importPath.replaceAll("'", "\\'")}'`;
2637
- const serverTypesImportPath = getServerTypesImportPath(outputFile, functionsDir);
2638
- const dataModelImportPath = getDataModelImportPath(outputFile, functionsDir);
2639
- const schemaImportPath = getSchemaImportPath(outputFile, functionsDir);
2640
- const migrationsManifestImportPath = getModuleImportPath(outputFile, functionsDir, "migrations/manifest");
2641
- const serverTypesImportLiteral = asSingleQuotedImport(serverTypesImportPath);
2642
- const dataModelImportLiteral = asSingleQuotedImport(dataModelImportPath);
2643
- const schemaImportLiteral = asSingleQuotedImport(schemaImportPath);
2644
- const migrationsManifestImportLiteral = asSingleQuotedImport(migrationsManifestImportPath);
2645
- const migrationsImportLine = hasMigrationsManifest ? `import { migrations } from ${migrationsManifestImportLiteral};\n` : "";
2646
- const migrationsConfigLine = hasMigrationsManifest ? " migrations,\n" : "";
2647
- if (!hasOrmSchema) return `// biome-ignore-all format: generated
2648
- // This file is auto-generated by kitcn
2649
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2650
-
2651
- import { initCRPC as baseInitCRPC } from 'kitcn/server';
2652
- import type { DataModel } from ${dataModelImportLiteral};
2653
- import type {
2654
- ActionCtx as ServerActionCtx,
2655
- MutationCtx as ServerMutationCtx,
2656
- QueryCtx as ServerQueryCtx,
2657
- } from ${serverTypesImportLiteral};
2658
- import { httpAction, internalMutation } from ${serverTypesImportLiteral};
2659
-
2660
- export type QueryCtx = ServerQueryCtx;
2661
- export type MutationCtx = ServerMutationCtx;
2662
- export type ActionCtx = ServerActionCtx;
2663
- export type GenericCtx = QueryCtx | MutationCtx | ActionCtx;
2664
- export type OrmCtx<Ctx = QueryCtx> = Ctx;
2665
-
2666
- export function withOrm<Ctx extends ServerQueryCtx | ServerMutationCtx>(ctx: Ctx): OrmCtx<Ctx> {
2667
- return ctx as OrmCtx<Ctx>;
2668
- }
2669
-
2670
- export const initCRPC = baseInitCRPC.dataModel<DataModel>().context({
2671
- query: (ctx) => ctx,
2672
- mutation: (ctx) => ctx,
2673
- action: (ctx) => ctx,
2674
- });
2675
- export { httpAction, internalMutation };
2676
- `;
2677
- const moduleNamespace = getModuleNameFromOutputFile(outputFile, functionsDir);
2678
- return `// biome-ignore-all format: generated
2679
- // This file is auto-generated by kitcn
2680
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2681
-
2682
- import {
2683
- createOrm,
2684
- type GenericOrmCtx,
2685
- type OrmFunctions,
2686
- } from 'kitcn/orm';
2687
- import {
2688
- createGeneratedFunctionReference,
2689
- initCRPC as baseInitCRPC,
2690
- } from 'kitcn/server';
2691
- import type { DataModel } from ${dataModelImportLiteral};
2692
- import type {
2693
- ActionCtx as ServerActionCtx,
2694
- MutationCtx as ServerMutationCtx,
2695
- QueryCtx as ServerQueryCtx,
2696
- } from ${serverTypesImportLiteral};
2697
- import { httpAction, internalMutation } from ${serverTypesImportLiteral};
2698
- import schema from ${schemaImportLiteral};
2699
- ${migrationsImportLine}
2700
-
2701
- ${`const ormFunctions: OrmFunctions = {
2702
- scheduledMutationBatch: createGeneratedFunctionReference<"mutation", "internal", unknown>(${JSON.stringify(`${moduleNamespace}:scheduledMutationBatch`)}),
2703
- scheduledDelete: createGeneratedFunctionReference<"mutation", "internal", unknown>(${JSON.stringify(`${moduleNamespace}:scheduledDelete`)}),
2704
- aggregateBackfillChunk: createGeneratedFunctionReference<"mutation", "internal", unknown>(${JSON.stringify(`${moduleNamespace}:aggregateBackfillChunk`)}),
2705
- migrationRunChunk: createGeneratedFunctionReference<"mutation", "internal", unknown>(${JSON.stringify(`${moduleNamespace}:migrationRunChunk`)}),
2706
- resetChunk: createGeneratedFunctionReference<"mutation", "internal", unknown>(${JSON.stringify(`${moduleNamespace}:resetChunk`)}),
2707
- };`}
2708
- const ormSchema = schema;
2709
-
2710
- export const orm = createOrm({
2711
- schema: ormSchema,
2712
- ormFunctions,
2713
- ${migrationsConfigLine} internalMutation,
2714
- });
2715
-
2716
- export type OrmCtx<Ctx extends ServerQueryCtx | ServerMutationCtx = ServerQueryCtx> = GenericOrmCtx<Ctx, typeof ormSchema>;
2717
- export type QueryCtx = OrmCtx<ServerQueryCtx>;
2718
- export type MutationCtx = OrmCtx<ServerMutationCtx>;
2719
- export type ActionCtx = ServerActionCtx;
2720
- export type GenericCtx = QueryCtx | MutationCtx | ActionCtx;
2721
-
2722
- export function withOrm<Ctx extends ServerQueryCtx | ServerMutationCtx>(ctx: Ctx) {
2723
- return orm.with(ctx) as OrmCtx<Ctx>;
2724
- }
2725
-
2726
- export const initCRPC = baseInitCRPC.dataModel<DataModel>().context({
2727
- query: (ctx) => withOrm(ctx),
2728
- mutation: (ctx) => withOrm(ctx),
2729
- action: (ctx) => ctx,
2730
- });
2731
- export { httpAction, internalMutation };
2732
-
2733
- export const {
2734
- scheduledMutationBatch,
2735
- scheduledDelete,
2736
- aggregateBackfill,
2737
- aggregateBackfillChunk,
2738
- aggregateBackfillStatus,
2739
- migrationRun,
2740
- migrationRunChunk,
2741
- migrationStatus,
2742
- migrationCancel,
2743
- resetChunk,
2744
- reset,
2745
- } = orm.api();
2746
- `;
2747
- }
2748
- function emitGeneratedAuthFile(outputFile, functionsDir, hasOrmSchema, authContract) {
2749
- const asSingleQuotedImport = (importPath) => `'${importPath.replaceAll("'", "\\'")}'`;
2750
- const runtimeApiImportPath = getRuntimeApiImportPath(outputFile, functionsDir);
2751
- const dataModelImportPath = getDataModelImportPath(outputFile, functionsDir);
2752
- const schemaImportPath = getSchemaImportPath(outputFile, functionsDir);
2753
- const serverImportPath = getGeneratedServerImportPath(outputFile, functionsDir);
2754
- const moduleNamespace = getModuleNameFromOutputFile(outputFile, functionsDir);
2755
- const authDefinitionImportPath = getModuleImportPath(outputFile, functionsDir, "auth");
2756
- const runtimeApiImportLiteral = asSingleQuotedImport(runtimeApiImportPath);
2757
- const dataModelImportLiteral = asSingleQuotedImport(dataModelImportPath);
2758
- const schemaImportLiteral = asSingleQuotedImport(schemaImportPath);
2759
- const serverImportLiteral = asSingleQuotedImport(serverImportPath);
2760
- const authDefinitionImportLiteral = asSingleQuotedImport(authDefinitionImportPath);
2761
- const authDefinitionFilePath = normalizeImportPath(path.relative(process.cwd(), path.join(functionsDir, "auth.ts")));
2762
- const hasAuthFile = authContract.hasAuthFile;
2763
- const hasAuthDefaultExport = authContract.hasAuthDefaultExport;
2764
- const authRuntimeModule = hasAuthDefaultExport ? "kitcn/auth" : "kitcn/auth/generated";
2765
- const disabledAuthReasonKind = hasAuthFile ? hasAuthDefaultExport ? "default_export_unavailable" : "missing_default_export" : "missing_auth_file";
2766
- const authRuntimeImports = `import {
2767
- ${(hasAuthDefaultExport ? [
2768
- "type BetterAuthOptionsWithoutDatabase",
2769
- "type AuthRuntime",
2770
- "defineAuth as baseDefineAuth",
2771
- "createAuthRuntime",
2772
- "type GenericAuthDefinition",
2773
- "getInvalidAuthDefinitionExportReason",
2774
- "resolveGeneratedAuthDefinition"
2775
- ] : [
2776
- "type BetterAuthOptionsWithoutDatabase",
2777
- "type AuthRuntime",
2778
- "defineAuth as baseDefineAuth",
2779
- "type GenericAuthDefinition",
2780
- "getGeneratedAuthDisabledReason",
2781
- "createDisabledAuthRuntime"
2782
- ]).join(",\n ")},
2783
- } from '${authRuntimeModule}';`;
2784
- const authDefinitionImport = hasAuthDefaultExport ? `import * as authDefinitionModule from ${authDefinitionImportLiteral};` : "";
2785
- const runtimeApiImport = hasAuthDefaultExport ? `import { internal } from ${runtimeApiImportLiteral};` : "";
2786
- const withOrmImport = hasOrmSchema && hasAuthDefaultExport ? `import { withOrm } from ${serverImportLiteral};` : "";
2787
- const usesSchemaFallback = !hasOrmSchema && !hasAuthDefaultExport;
2788
- const schemaTypeImports = usesSchemaFallback ? `import type { GenericSchema, SchemaDefinition } from 'convex/server';` : "";
2789
- const generatedSchemaType = usesSchemaFallback ? "GeneratedSchema" : "typeof schema";
2790
- return `// biome-ignore-all format: generated
2791
- // This file is auto-generated by kitcn
2792
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2793
-
2794
- ${authRuntimeImports}
2795
- ${runtimeApiImport}
2796
- import type { DataModel } from ${dataModelImportLiteral};
2797
- import type { GenericCtx, MutationCtx } from ${serverImportLiteral};
2798
- ${schemaTypeImports}
2799
- ${withOrmImport}
2800
- ${usesSchemaFallback ? "" : `import schema from ${schemaImportLiteral};`}
2801
- ${authDefinitionImport}
2802
-
2803
- ${usesSchemaFallback ? "type GeneratedSchema = SchemaDefinition<GenericSchema, true>;" : ""}
2804
-
2805
- export function defineAuth<
2806
- AuthOptions extends BetterAuthOptionsWithoutDatabase = BetterAuthOptionsWithoutDatabase,
2807
- >(
2808
- definition: GenericAuthDefinition<GenericCtx, DataModel, ${generatedSchemaType}, AuthOptions>
2809
- ) {
2810
- return baseDefineAuth(definition);
2811
- }
2812
-
2813
- ${hasAuthDefaultExport ? `type AuthDefinitionFromFile = typeof authDefinitionModule.default;
2814
-
2815
- const authDefinition = resolveGeneratedAuthDefinition<AuthDefinitionFromFile>(
2816
- authDefinitionModule,
2817
- getInvalidAuthDefinitionExportReason(${JSON.stringify(authDefinitionFilePath)})
2818
- );
2819
- ` : ""}
2820
- const authRuntime: ${hasAuthDefaultExport ? `AuthRuntime<
2821
- DataModel,
2822
- ${generatedSchemaType},
2823
- MutationCtx,
2824
- GenericCtx,
2825
- ReturnType<AuthDefinitionFromFile>
2826
- > = createAuthRuntime<
2827
- DataModel,
2828
- ${generatedSchemaType},
2829
- MutationCtx,
2830
- GenericCtx,
2831
- ReturnType<AuthDefinitionFromFile>
2832
- >({
2833
- internal,
2834
- moduleName: ${JSON.stringify(moduleNamespace)},
2835
- schema,
2836
- auth: authDefinition,${hasOrmSchema ? "\n context: withOrm," : ""}
2837
- })` : `AuthRuntime<
2838
- DataModel,
2839
- ${generatedSchemaType},
2840
- MutationCtx,
2841
- GenericCtx
2842
- > = createDisabledAuthRuntime<DataModel, ${generatedSchemaType}, MutationCtx, GenericCtx>({
2843
- reason: getGeneratedAuthDisabledReason(
2844
- ${JSON.stringify(disabledAuthReasonKind)},
2845
- ${JSON.stringify(authDefinitionFilePath)}
2846
- ),
2847
- })`};
2848
-
2849
- export const {
2850
- authEnabled,
2851
- authClient,
2852
- getAuth,
2853
- auth,
2854
- create,
2855
- deleteMany,
2856
- deleteOne,
2857
- findMany,
2858
- findOne,
2859
- updateMany,
2860
- updateOne,
2861
- getLatestJwks,
2862
- rotateKeys,
2863
- } = authRuntime;
2864
- `;
2865
- }
2866
- function emitGeneratedMigrationsFile(outputFile, functionsDir, hasRelationsMetadata) {
2867
- if (!hasRelationsMetadata) return emitGeneratedMigrationsPlaceholderFile();
2868
- const asSingleQuotedImport = (importPath) => `'${importPath.replaceAll("'", "\\'")}'`;
2869
- const schemaImportLiteral = asSingleQuotedImport(getSchemaImportPath(outputFile, functionsDir));
2870
- const migrationSchemaImport = "schema";
2871
- const migrationSchemaType = "typeof schema";
2872
- return `// biome-ignore-all format: generated
2873
- // This file is auto-generated by kitcn
2874
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2875
-
2876
- import {
2877
- defineMigration as baseDefineMigration,
2878
- type MigrationDefinition,
2879
- } from 'kitcn/orm';
2880
- import ${migrationSchemaImport} from ${schemaImportLiteral};
2881
-
2882
- export function defineMigration(
2883
- migration: MigrationDefinition<${migrationSchemaType}>
2884
- ): MigrationDefinition<${migrationSchemaType}> {
2885
- return baseDefineMigration<${migrationSchemaType}>(migration);
2886
- }
2887
- `;
2888
- }
2889
- function emitGeneratedModuleRuntimeFile(outputFile, functionsDir, moduleName, procedureEntries, runtimeExportNames) {
2890
- const { callerExportName, handlerExportName } = runtimeExportNames?.get(moduleName) ?? getModuleRuntimeExportNames(moduleName);
2891
- const useGeneratedApiTypes = moduleUsesOwnGeneratedRuntime(functionsDir, moduleName);
2892
- const runtimeApiTypesImportPath = useGeneratedApiTypes ? getRuntimeApiTypesImportPath(outputFile, functionsDir) : null;
2893
- const generatedServerImportPath = getGeneratedServerImportPath(outputFile, functionsDir);
2894
- const { callerEntries, handlerEntries } = partitionRuntimeEntriesForEmission(procedureEntries);
2895
- const callerRegistryLines = emitProcedureRegistryEntries(callerEntries, outputFile, functionsDir, moduleName, useGeneratedApiTypes);
2896
- const callerRegistryBody = callerRegistryLines.length > 0 ? `\n${callerRegistryLines.join("\n")}\n` : "\n";
2897
- const hasHandlerRegistry = handlerEntries.length > 0;
2898
- const handlerRegistryLines = hasHandlerRegistry ? emitProcedureRegistryEntries(handlerEntries, outputFile, functionsDir, moduleName, useGeneratedApiTypes) : [];
2899
- const handlerRegistryBody = handlerRegistryLines.length > 0 ? `\n${handlerRegistryLines.join("\n")}\n` : "\n";
2900
- const allEntriesAreCrpc = callerEntries.length > 0 && callerEntries.length === handlerEntries.length;
2901
- const handlerRegistryDeclaration = hasHandlerRegistry ? allEntriesAreCrpc ? "\n const handlerRegistry = procedureRegistry;\n" : `\n const handlerRegistry = {${handlerRegistryBody}} as const;\n` : "";
2902
- const handlerTypeDeclarations = hasHandlerRegistry ? `
2903
- type ProcedureHandlerContext = QueryCtx | MutationCtx;
2904
- type GeneratedProcedureHandler<
2905
- TCtx extends ProcedureHandlerContext = ProcedureHandlerContext,
2906
- > = GeneratedRegistryHandlerForContext<
2907
- ProcedureHandlerRegistry,
2908
- TCtx,
2909
- QueryCtx,
2910
- MutationCtx
2911
- >;
2912
- ` : "";
2913
- const generatedRuntimeTypeArgs = hasHandlerRegistry ? `
2914
- QueryCtx,
2915
- MutationCtx,
2916
- ProcedureCallerRegistry,
2917
- ActionCtx,
2918
- ProcedureHandlerRegistry
2919
- ` : `
2920
- QueryCtx,
2921
- MutationCtx,
2922
- ProcedureCallerRegistry,
2923
- ActionCtx
2924
- `;
2925
- const handlerExport = hasHandlerRegistry ? `
2926
- export function ${handlerExportName}<TCtx extends ProcedureHandlerContext>(
2927
- ctx: TCtx
2928
- ): GeneratedProcedureHandler<TCtx> {
2929
- return generatedRuntime.getHandlerFactory()(ctx) as GeneratedProcedureHandler<TCtx>;
2930
- }
2931
- ` : "";
2932
- return `// biome-ignore-all format: generated
2933
- /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-vars */
2934
- // This file is auto-generated by kitcn
2935
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
2936
-
2937
- import {
2938
- createGeneratedFunctionReference,
2939
- createGeneratedRegistryRuntime,
2940
- typedProcedureResolver,
2941
- type GeneratedRegistryCallerForContext,${hasHandlerRegistry ? "\n type GeneratedRegistryHandlerForContext," : ""}
2942
- } from 'kitcn/server';
2943
- ${runtimeApiTypesImportPath ? `import type {
2944
- api as generatedApi,
2945
- internal as generatedInternal,
2946
- } from '${runtimeApiTypesImportPath}';
2947
- ` : ""}import type { ActionCtx, MutationCtx, QueryCtx } from '${generatedServerImportPath}';
2948
- import type { OrmTriggerContext } from 'kitcn/orm';
2949
-
2950
- const procedureRegistry = {${callerRegistryBody}} as const;
2951
- ${handlerRegistryDeclaration}
2952
- type ProcedureCallerRegistry = typeof procedureRegistry;
2953
- ${hasHandlerRegistry ? `type ProcedureHandlerRegistry = typeof handlerRegistry;
2954
- ` : ""}
2955
-
2956
- const generatedRuntime = createGeneratedRegistryRuntime<${generatedRuntimeTypeArgs}>({
2957
- procedureRegistry,${hasHandlerRegistry ? "\n handlerRegistry," : ""}
2958
- });
2959
-
2960
- type MutationCallerContext = MutationCtx | OrmTriggerContext<any, MutationCtx>;
2961
- type ProcedureCallerContext = QueryCtx | MutationCallerContext | ActionCtx;
2962
- type GeneratedProcedureCaller<
2963
- TCtx extends ProcedureCallerContext = ProcedureCallerContext,
2964
- > = GeneratedRegistryCallerForContext<
2965
- ProcedureCallerRegistry,
2966
- TCtx,
2967
- QueryCtx,
2968
- MutationCallerContext,
2969
- ActionCtx
2970
- >;
2971
- ${handlerTypeDeclarations}
2972
-
2973
- export function ${callerExportName}<TCtx extends ProcedureCallerContext>(
2974
- ctx: TCtx
2975
- ): GeneratedProcedureCaller<TCtx> {
2976
- return generatedRuntime.getCallerFactory()(
2977
- ctx as any
2978
- ) as GeneratedProcedureCaller<TCtx>;
2979
- }
2980
- ${handlerExport}
2981
- `;
2982
- }
2983
- function hasNamedExport(filePath, exportName) {
2984
- if (!fs.existsSync(filePath)) return false;
2985
- const source = fs.readFileSync(filePath, "utf-8");
2986
- if (new RegExp(`\\bexport\\s+(?:const|let|var|function|class|type|interface)\\s+${exportName}\\b`).test(source)) return true;
2987
- for (const match of source.matchAll(/\bexport\s*{([^}]*)}/g)) {
2988
- const exportList = match[1] ?? "";
2989
- if (new RegExp(`\\b${exportName}\\b`).test(exportList)) return true;
2990
- }
2991
- return false;
2992
- }
2993
- function hasDefaultExport(filePath) {
2994
- if (!fs.existsSync(filePath)) return false;
2995
- const source = fs.readFileSync(filePath, "utf-8");
2996
- return DEFAULT_EXPORT_RE.test(source);
2997
- }
2998
- function createApiTree(meta) {
2999
- const root = {
3000
- children: {},
3001
- functions: []
3002
- };
3003
- for (const [moduleName, fns] of Object.entries(meta)) {
3004
- const pathSegments = moduleName.split("/").filter(Boolean);
3005
- let node = root;
3006
- for (const segment of pathSegments) {
3007
- node.children[segment] ??= {
3008
- children: {},
3009
- functions: []
3010
- };
3011
- node = node.children[segment];
3012
- }
3013
- for (const fnName of Object.keys(fns).sort()) {
3014
- const fnMeta = fns[fnName] ?? {};
3015
- const type = fnMeta.type;
3016
- const fnType = type === "query" || type === "mutation" || type === "action" ? type : "query";
3017
- node.functions.push({
3018
- fnName,
3019
- fnType,
3020
- moduleName,
3021
- fnMeta: {
3022
- ...fnMeta,
3023
- type: fnType
3024
- }
3025
- });
3026
- }
3027
- }
3028
- return root;
3029
- }
3030
- function formatMetaValue(value) {
3031
- if (typeof value === "string") return JSON.stringify(value);
3032
- if (typeof value === "boolean" || typeof value === "number") return String(value);
3033
- return null;
3034
- }
3035
- function emitFnMetaLiteral(fnMeta) {
3036
- const metaProps = [];
3037
- for (const [key, value] of Object.entries(fnMeta).sort(([a], [b]) => a.localeCompare(b))) {
3038
- if (value === void 0) continue;
3039
- const formatted = formatMetaValue(value);
3040
- if (formatted !== null) metaProps.push(`${key}: ${formatted}`);
3041
- }
3042
- return `{ ${metaProps.join(", ")} }`;
3043
- }
3044
- function emitHttpRoutes(dedupedRoutes, indentLevel) {
3045
- const indent = " ".repeat(indentLevel);
3046
- const lines = [];
3047
- for (const [routeKey, route] of Object.entries(dedupedRoutes).sort(([a], [b]) => a.localeCompare(b))) lines.push(`${indent}${formatKey(routeKey)}: { path: ${JSON.stringify(route.path)}, method: ${JSON.stringify(route.method)} },`);
3048
- return lines;
3049
- }
3050
- function emitApiObject(tree, pathSegments, outputFile, functionsDir, indentLevel, dedupedRoutes, hasHttpRouterExport) {
3051
- const indent = " ".repeat(indentLevel);
3052
- const lines = [];
3053
- const childKeys = Object.keys(tree.children).sort((a, b) => a.localeCompare(b));
3054
- const functionEntries = [...tree.functions].sort((a, b) => a.fnName.localeCompare(b.fnName));
3055
- const childSet = new Set(childKeys);
3056
- for (const entry of functionEntries) if (childSet.has(entry.fnName)) throw new Error(`Codegen conflict at ${pathSegments.join("/")}: export "${entry.fnName}" conflicts with directory of same name.`);
3057
- const mergedKeys = [...childKeys, ...functionEntries.map((entry) => entry.fnName)].sort((a, b) => a.localeCompare(b));
3058
- for (const key of mergedKeys) {
3059
- if (childSet.has(key)) {
3060
- const childNode = tree.children[key];
3061
- lines.push(`${indent}${formatKey(key)}: {`);
3062
- lines.push(...emitApiObject(childNode, [...pathSegments, key], outputFile, functionsDir, indentLevel + 1, dedupedRoutes, hasHttpRouterExport));
3063
- lines.push(`${indent}},`);
3064
- continue;
3065
- }
3066
- const fnEntry = functionEntries.find((entry) => entry.fnName === key);
3067
- if (!fnEntry) continue;
3068
- const moduleImportPath = getModuleImportPath(outputFile, functionsDir, fnEntry.moduleName);
3069
- const fnMetaLiteral = emitFnMetaLiteral(fnEntry.fnMeta);
3070
- const functionRef = `createGeneratedFunctionReference<${JSON.stringify(fnEntry.fnType)}, "public", typeof import(${JSON.stringify(moduleImportPath)}).${key}>(${JSON.stringify(getGeneratedFunctionName(fnEntry.moduleName, key))})`;
3071
- lines.push(`${indent}${formatKey(key)}: createApiLeaf<${JSON.stringify(fnEntry.fnType)}, typeof import(${JSON.stringify(moduleImportPath)}).${key}>(${functionRef}, ${fnMetaLiteral}),`);
3072
- }
3073
- if (pathSegments.length === 0) {
3074
- if (hasHttpRouterExport) lines.push(`${indent}http: undefined as unknown as typeof httpRouter,`);
3075
- lines.push(`${indent}_http: {`);
3076
- lines.push(...emitHttpRoutes(dedupedRoutes, indentLevel + 1));
3077
- lines.push(`${indent}},`);
3078
- }
3079
- return lines;
3080
- }
3081
- function emitProcedureRegistryEntries(entries, outputFile, functionsDir, moduleName, useGeneratedApiTypes) {
3082
- return entries.map((entry) => {
3083
- const pathKey = entry.moduleName === moduleName ? entry.exportName : [...entry.moduleName.split("/"), entry.exportName].join(".");
3084
- const moduleImportPath = getModuleImportPath(outputFile, functionsDir, entry.moduleName);
3085
- const functionRefTypeAccess = useGeneratedApiTypes ? getBracketAccessPath(entry.internal ? "generatedInternal" : "generatedApi", entry.exportName === "default" ? entry.moduleName.split("/") : [...entry.moduleName.split("/"), entry.exportName]) : `import(${JSON.stringify(moduleImportPath)}).${entry.exportName}`;
3086
- const functionRefAccess = `createGeneratedFunctionReference<${JSON.stringify(entry.type)}, ${JSON.stringify(entry.internal ? "internal" : "public")}, typeof ${functionRefTypeAccess}>(${JSON.stringify(getGeneratedFunctionName(entry.moduleName, entry.exportName))})`;
3087
- const resolver = `(require(${JSON.stringify(moduleImportPath)}) as Record<string, unknown>)[${JSON.stringify(entry.exportName)}]`;
3088
- return ` ${JSON.stringify(pathKey)}: [${JSON.stringify(entry.type)}, typedProcedureResolver(${functionRefAccess}, () => ${resolver})],`;
3089
- }).sort((a, b) => a.localeCompare(b));
3090
- }
3091
- function getGeneratedFunctionName(moduleName, exportName) {
3092
- return exportName === "default" ? moduleName : `${moduleName}:${exportName}`;
3093
- }
3094
- function buildAuthRuntimeProcedureEntries(moduleName) {
3095
- return AUTH_RUNTIME_PROCEDURES.map((entry) => ({
3096
- ...entry,
3097
- moduleName,
3098
- kind: "crpc"
3099
- }));
3100
- }
3101
- function buildGeneratedOrmRuntimeProcedureEntries(moduleName) {
3102
- return GENERATED_ORM_RUNTIME_PROCEDURES.map((entry) => ({
3103
- ...entry,
3104
- moduleName,
3105
- kind: "dispatch"
3106
- }));
3107
- }
3108
- function partitionRuntimeEntriesForEmission(entries) {
3109
- return {
3110
- callerEntries: entries,
3111
- handlerEntries: entries.filter((entry) => entry.kind === "crpc")
3112
- };
3113
- }
3114
- function dedupeProcedureEntries(entries) {
3115
- const seen = /* @__PURE__ */ new Set();
3116
- const deduped = [];
3117
- for (const entry of entries) {
3118
- const key = `${entry.moduleName}.${entry.exportName}`;
3119
- if (seen.has(key)) continue;
3120
- seen.add(key);
3121
- deduped.push(entry);
3122
- }
3123
- return deduped;
3124
- }
3125
- function shouldSuppressHttpParseWarning(error) {
3126
- return MISSING_KITCN_IMPORT_RE.test(String(error));
3127
- }
3128
- function getConvexConfig(sharedDir) {
3129
- const convexConfigPath = path.join(process.cwd(), "convex.json");
3130
- const functionsDir = (fs.existsSync(convexConfigPath) ? JSON.parse(fs.readFileSync(convexConfigPath, "utf-8")) : {}).functions || "convex";
3131
- return {
3132
- functionsDir: path.join(process.cwd(), functionsDir),
3133
- outputFile: path.join(process.cwd(), sharedDir || "convex/shared", "api.ts")
3134
- };
3135
- }
3136
- /**
3137
- * Check if a value is a CRPCHttpRouter (has _def.router === true)
3138
- */
3139
- function isCRPCHttpRouter(value) {
3140
- return typeof value === "object" && value !== null && "_def" in value && value._def?.router === true;
3141
- }
3142
- /**
3143
- * Import a module using jiti and extract cRPC metadata from exports.
3144
- */
3145
- async function parseModuleRuntime(filePath, jitiInstance) {
3146
- const result = {};
3147
- const httpRoutes = {};
3148
- const procedures = [];
3149
- const isHttp = filePath.endsWith("http.ts");
3150
- const module = await jitiInstance.import(filePath);
3151
- if (!module || typeof module !== "object") {
3152
- if (isHttp) logger.error(" http.ts: module is empty or not an object");
3153
- return {
3154
- meta: null,
3155
- httpRoutes: {},
3156
- procedures: []
3157
- };
3158
- }
3159
- for (const [name, value] of Object.entries(module)) {
3160
- if (name.startsWith("_")) continue;
3161
- const meta = value?._crpcMeta;
3162
- if (meta?.type) {
3163
- procedures.push({
3164
- exportName: name,
3165
- internal: Boolean(meta.internal),
3166
- type: meta.type
3167
- });
3168
- if (meta.internal) continue;
3169
- const fnMeta = { type: meta.type };
3170
- if (meta.auth) fnMeta.auth = meta.auth;
3171
- for (const [key, val] of Object.entries(meta)) if (key !== "type" && key !== "internal" && val !== void 0) fnMeta[key] = val;
3172
- result[name] = fnMeta;
3173
- }
3174
- const httpRoute = value?._crpcHttpRoute;
3175
- if (httpRoute?.path && httpRoute?.method) httpRoutes[name] = {
3176
- path: httpRoute.path,
3177
- method: httpRoute.method
3178
- };
3179
- if (isCRPCHttpRouter(value)) for (const [procPath, procedure] of Object.entries(value._def.procedures)) {
3180
- const route = procedure._crpcHttpRoute;
3181
- if (route?.path && route?.method) httpRoutes[procPath] = {
3182
- path: route.path,
3183
- method: route.method
3184
- };
3185
- }
3186
- }
3187
- return {
3188
- meta: Object.keys(result).length > 0 ? result : null,
3189
- httpRoutes,
3190
- procedures
3191
- };
3192
- }
3193
- async function generateMeta(sharedDir, options) {
3194
- const startTime = Date.now();
3195
- const { functionsDir, outputFile } = getConvexConfig(sharedDir);
3196
- const serverOutputFile = getGeneratedServerOutputFile(functionsDir);
3197
- const ormOutputFile = getGeneratedOrmOutputFile(functionsDir);
3198
- const crpcOutputFile = getGeneratedCrpcOutputFile(functionsDir);
3199
- const authOutputFile = getGeneratedAuthOutputFile(functionsDir);
3200
- const migrationsHelperOutputFile = getGeneratedMigrationsHelperOutputFile(functionsDir);
3201
- const legacyGeneratedMigrationsOutputFile = path.join(functionsDir, GENERATED_DIR, "migrations.ts");
3202
- const legacyGeneratedMigrationsRuntimeOutputFile = path.join(functionsDir, GENERATED_DIR, "migrations.runtime.ts");
3203
- const legacyGeneratedMigrationsUnderscoreOutputFile = path.join(functionsDir, GENERATED_DIR, "_migrations.ts");
3204
- const generatedAuthModuleName = getModuleNameFromOutputFile(authOutputFile, functionsDir);
3205
- const debug = options?.debug ?? false;
3206
- const silent = options?.silent ?? false;
3207
- const { generateApi, generateAuth, modeLabel } = resolveGenerationMode(options);
3208
- const normalizedTrimSegments = normalizeTrimSegments(options?.trimSegments);
3209
- if (debug) if (generateApi) logger.info("Scanning Convex functions for cRPC metadata...\n");
3210
- else logger.info(`Running kitcn codegen (mode=${modeLabel})...\n`);
3211
- const meta = {};
3212
- const allHttpRoutes = {};
3213
- const procedureEntries = [];
3214
- const fatalParseFailures = [];
3215
- let createdRuntimePlaceholders = [];
3216
- let createdSupportPlaceholders = [];
3217
- const runtimeFilesPreservedFromParseFailures = /* @__PURE__ */ new Set();
3218
- let totalFunctions = 0;
3219
- const authFilePath = path.join(functionsDir, "auth.ts");
3220
- const hasAuthFile = fs.existsSync(authFilePath);
3221
- const hasAuthDefaultExport = hasDefaultExport(authFilePath);
3222
- const authContract = {
3223
- hasAuthFile,
3224
- hasAuthDefaultExport
3225
- };
3226
- const schemaMetadata = await resolveSchemaMetadataForCodegen(functionsDir, debug);
3227
- const hasOrmSchemaMetadata = schemaMetadata.hasOrmSchema;
3228
- const hasRelationsMetadata = schemaMetadata.hasRelations;
3229
- const hasRelationsExport = hasNamedExport(path.join(functionsDir, "schema.ts"), "relations");
3230
- const hasSchemaTriggersExport = hasNamedExport(path.join(functionsDir, "schema.ts"), "triggers");
3231
- const hasDedicatedTriggersExport = hasNamedExport(path.join(functionsDir, "triggers.ts"), "triggers");
3232
- const hasMigrationsManifest = fs.existsSync(path.join(functionsDir, "migrations", "manifest.ts"));
3233
- if (hasRelationsExport) throw new Error("Codegen error: do not export `relations` from schema.ts. Chain relations on the default schema export with `defineSchema(...).relations(...)`.");
3234
- if (hasSchemaTriggersExport || hasDedicatedTriggersExport) throw new Error("Codegen error: do not export `triggers` from schema.ts or triggers.ts. Chain triggers on the default schema export with `defineSchema(...).relations(...).triggers(...)`.");
3235
- const hasOrmSchema = hasOrmSchemaMetadata;
3236
- createdSupportPlaceholders = ensureGeneratedSupportPlaceholders(functionsDir, { includeAuth: generateAuth });
3237
- if (generateApi) {
3238
- globalThis.__KITCN_CODEGEN__ = true;
3239
- try {
3240
- const jitiInstance = createJiti(process.cwd(), {
3241
- interopDefault: true,
3242
- moduleCache: false
3243
- });
3244
- const files = listFilesRecursive(functionsDir).filter((file) => file.endsWith(".ts") && isValidConvexFile(file));
3245
- const existingRuntimeFilesBeforeParse = new Set(listGeneratedRuntimeFiles(functionsDir));
3246
- const runtimePlaceholderModules = [...new Set([
3247
- ...files.map((file) => file.replace(TS_EXTENSION_RE, "")),
3248
- ...hasOrmSchema ? ["generated/server"] : [],
3249
- ...generateAuth ? [generatedAuthModuleName] : []
3250
- ])];
3251
- createdRuntimePlaceholders = ensureGeneratedRuntimePlaceholders(functionsDir, runtimePlaceholderModules, resolveModuleRuntimeExportNames(runtimePlaceholderModules, normalizedTrimSegments));
3252
- for (const file of files) {
3253
- const filePath = path.join(functionsDir, file);
3254
- const moduleName = file.replace(TS_EXTENSION_RE, "");
3255
- try {
3256
- const { meta: moduleMeta, httpRoutes, procedures } = await parseModuleRuntime(filePath, jitiInstance);
3257
- if (moduleMeta) {
3258
- meta[moduleName] = moduleMeta;
3259
- const fnCount = Object.keys(moduleMeta).length;
3260
- totalFunctions += fnCount;
3261
- if (debug) logger.info(` ✓ ${moduleName}: ${fnCount} functions`);
3262
- }
3263
- if (Object.keys(httpRoutes).length > 0 && debug) logger.info(` ✓ ${moduleName}: ${Object.keys(httpRoutes).length} HTTP routes`);
3264
- Object.assign(allHttpRoutes, httpRoutes);
3265
- for (const procedure of procedures) procedureEntries.push({
3266
- moduleName,
3267
- exportName: procedure.exportName,
3268
- internal: procedure.internal,
3269
- type: procedure.type,
3270
- kind: "crpc"
3271
- });
3272
- } catch (error) {
3273
- const runtimeFile = getGeneratedRuntimeOutputFile(functionsDir, moduleName);
3274
- if (existingRuntimeFilesBeforeParse.has(runtimeFile)) runtimeFilesPreservedFromParseFailures.add(runtimeFile);
3275
- const shouldLogParseFailure = debug || file === "http.ts" && !shouldSuppressHttpParseWarning(error);
3276
- const shouldTreatParseFailureAsFatal = !(file === "http.ts" && shouldSuppressHttpParseWarning(error));
3277
- if (shouldLogParseFailure) logger.error(` ⚠ Failed to parse ${file}:`, error);
3278
- if (shouldTreatParseFailureAsFatal) fatalParseFailures.push({
3279
- file,
3280
- error
3281
- });
3282
- }
3283
- }
3284
- } finally {
3285
- delete globalThis.__KITCN_CODEGEN__;
3286
- }
3287
- }
3288
- if (fatalParseFailures.length > 0) {
3289
- for (const createdRuntimePlaceholder of createdRuntimePlaceholders) fs.rmSync(createdRuntimePlaceholder, { force: true });
3290
- for (const createdSupportPlaceholder of createdSupportPlaceholders) fs.rmSync(createdSupportPlaceholder, { force: true });
3291
- const failureSummary = fatalParseFailures.map(({ file, error }) => `- ${file}: ${error instanceof Error ? error.message : String(error)}`).join("\n");
3292
- throw new Error(`kitcn codegen aborted because module parsing failed:\n${failureSummary}`);
3293
- }
3294
- cleanupGeneratedPluginArtifacts(functionsDir);
3295
- if (generateApi) {
3296
- const routesByPath = /* @__PURE__ */ new Map();
3297
- for (const [key, route] of Object.entries(allHttpRoutes)) {
3298
- const pathKey = `${route.path}:${route.method}`;
3299
- const existing = routesByPath.get(pathKey) || [];
3300
- existing.push({
3301
- key,
3302
- route
3303
- });
3304
- routesByPath.set(pathKey, existing);
3305
- }
3306
- const dedupedRoutes = {};
3307
- for (const entries of routesByPath.values()) {
3308
- const best = entries.reduce((a, b) => a.key.length >= b.key.length ? a : b);
3309
- dedupedRoutes[best.key] = best.route;
3310
- }
3311
- const schemaImportPath = getSchemaImportPath(outputFile, functionsDir);
3312
- const httpImportPath = getHttpImportPath(outputFile, functionsDir);
3313
- const hasTablesExport = hasNamedExport(path.join(functionsDir, "schema.ts"), "tables");
3314
- const needsInferSelectModelImport = hasTablesExport;
3315
- const needsInferInsertModelImport = hasTablesExport;
3316
- const hasHttpRouterExport = hasNamedExport(path.join(functionsDir, "http.ts"), "httpRouter");
3317
- const apiTree = createApiTree(meta);
3318
- if (Object.hasOwn(apiTree.children, "http") || apiTree.functions.some((entry) => entry.fnName === "http")) throw new Error("Codegen conflict: root \"http\" namespace is reserved for generated HTTP router types. Rename your Convex module/function.");
3319
- const apiObjectLines = emitApiObject(apiTree, [], outputFile, functionsDir, 1, dedupedRoutes, hasHttpRouterExport);
3320
- const apiObjectBody = apiObjectLines.length > 0 ? `\n${apiObjectLines.join("\n")}\n` : "\n";
3321
- const serverTypeImports = "import type { inferApiInputs, inferApiOutputs } from \"kitcn/server\";";
3322
- const ormTypeImports = [needsInferInsertModelImport ? "InferInsertModel" : null, needsInferSelectModelImport ? "InferSelectModel" : null].filter((item) => !!item);
3323
- const optionalImports = [
3324
- ormTypeImports.length > 0 ? `import type { ${ormTypeImports.join(", ")} } from "kitcn/orm";` : null,
3325
- hasHttpRouterExport ? `import type { httpRouter } from ${JSON.stringify(httpImportPath)};` : null,
3326
- hasTablesExport ? `import type { tables } from ${JSON.stringify(schemaImportPath)};` : null
3327
- ].filter((line) => !!line).join("\n");
3328
- const apiTypeLine = "export type Api = typeof api;";
3329
- const optionalTypeExports = [hasTablesExport ? `
3330
- export type TableName = keyof typeof tables;
3331
- export type Select<T extends TableName> = InferSelectModel<(typeof tables)[T]>;
3332
- export type Insert<T extends TableName> = InferInsertModel<(typeof tables)[T]>;` : null].filter((entry) => !!entry).join("\n");
3333
- const output = `// biome-ignore-all format: generated
3334
- // This file is auto-generated by kitcn
3335
- // Do not edit manually. Run \`kitcn codegen\` to regenerate.
3336
-
3337
- import { createApiLeaf, createGeneratedFunctionReference } from "kitcn/server";
3338
- ${serverTypeImports}
3339
- ${optionalImports ? `\n${optionalImports}` : ""}
3340
-
3341
- export const api = {${apiObjectBody}} as const;
3342
-
3343
- ${apiTypeLine}
3344
- export type ApiInputs = inferApiInputs<Api>;
3345
- export type ApiOutputs = inferApiOutputs<Api>;
3346
- ${optionalTypeExports}
3347
- `;
3348
- const outputDirname = path.dirname(outputFile);
3349
- if (!fs.existsSync(outputDirname)) fs.mkdirSync(outputDirname, { recursive: true });
3350
- writeFileIfChanged(outputFile, output);
3351
- } else fs.rmSync(outputFile, { force: true });
3352
- const serverOutput = emitGeneratedServerFile(serverOutputFile, functionsDir, hasOrmSchema, hasMigrationsManifest);
3353
- const generatedOutputDirname = path.dirname(serverOutputFile);
3354
- if (!fs.existsSync(generatedOutputDirname)) fs.mkdirSync(generatedOutputDirname, { recursive: true });
3355
- writeFileIfChanged(serverOutputFile, serverOutput);
3356
- fs.rmSync(ormOutputFile, { force: true });
3357
- fs.rmSync(crpcOutputFile, { force: true });
3358
- writeFileIfChanged(migrationsHelperOutputFile, emitGeneratedMigrationsFile(migrationsHelperOutputFile, functionsDir, hasRelationsMetadata));
3359
- fs.rmSync(legacyGeneratedMigrationsOutputFile, { force: true });
3360
- fs.rmSync(legacyGeneratedMigrationsRuntimeOutputFile, { force: true });
3361
- fs.rmSync(legacyGeneratedMigrationsUnderscoreOutputFile, { force: true });
3362
- if (generateAuth) writeFileIfChanged(authOutputFile, emitGeneratedAuthFile(authOutputFile, functionsDir, hasOrmSchema, authContract));
3363
- else fs.rmSync(authOutputFile, { force: true });
3364
- fs.rmSync(getLegacyGeneratedOutputFile(functionsDir), { force: true });
3365
- const mergedProcedureEntries = dedupeProcedureEntries([
3366
- ...hasOrmSchema ? buildGeneratedOrmRuntimeProcedureEntries("generated/server") : [],
3367
- ...generateApi ? procedureEntries : [],
3368
- ...generateAuth && hasAuthDefaultExport ? buildAuthRuntimeProcedureEntries(generatedAuthModuleName) : []
3369
- ]);
3370
- const runtimeProcedureEntriesByModule = /* @__PURE__ */ new Map();
3371
- for (const entry of mergedProcedureEntries) {
3372
- if (RUNTIME_CALLER_RESERVED_EXPORTS.has(entry.exportName)) throw new Error(`Codegen conflict: "${entry.moduleName}.${entry.exportName}" uses reserved runtime caller namespace "${entry.exportName}". Rename the procedure export.`);
3373
- const existingEntries = runtimeProcedureEntriesByModule.get(entry.moduleName);
3374
- if (existingEntries) {
3375
- existingEntries.push(entry);
3376
- continue;
3377
- }
3378
- runtimeProcedureEntriesByModule.set(entry.moduleName, [entry]);
3379
- }
3380
- const runtimeOutputFiles = [];
3381
- const runtimeExportNames = resolveModuleRuntimeExportNames([...runtimeProcedureEntriesByModule.keys()], normalizedTrimSegments);
3382
- for (const [moduleName, moduleEntries] of [...runtimeProcedureEntriesByModule].sort(([moduleA], [moduleB]) => moduleA.localeCompare(moduleB))) {
3383
- const runtimeOutputFile = getGeneratedRuntimeOutputFile(functionsDir, moduleName);
3384
- const runtimeOutput = emitGeneratedModuleRuntimeFile(runtimeOutputFile, functionsDir, moduleName, moduleEntries, runtimeExportNames);
3385
- fs.mkdirSync(path.dirname(runtimeOutputFile), { recursive: true });
3386
- writeFileIfChanged(runtimeOutputFile, runtimeOutput);
3387
- runtimeOutputFiles.push(runtimeOutputFile);
3388
- }
3389
- const runtimeOutputFileSet = new Set(runtimeOutputFiles);
3390
- const existingRuntimeFiles = listGeneratedRuntimeFiles(functionsDir);
3391
- for (const existingRuntimeFile of existingRuntimeFiles) {
3392
- if (runtimeOutputFileSet.has(existingRuntimeFile) || runtimeFilesPreservedFromParseFailures.has(existingRuntimeFile)) continue;
3393
- fs.rmSync(existingRuntimeFile, { force: true });
3394
- }
3395
- for (const createdRuntimePlaceholder of createdRuntimePlaceholders) {
3396
- if (runtimeOutputFileSet.has(createdRuntimePlaceholder) || runtimeFilesPreservedFromParseFailures.has(createdRuntimePlaceholder)) continue;
3397
- fs.rmSync(createdRuntimePlaceholder, { force: true });
3398
- }
3399
- const elapsed = ((Date.now() - startTime) / 1e3).toFixed(2);
3400
- const time = (/* @__PURE__ */ new Date()).toLocaleTimeString("en-US", {
3401
- hour12: false,
3402
- hour: "2-digit",
3403
- minute: "2-digit",
3404
- second: "2-digit"
3405
- });
3406
- if (!silent) if (debug) {
3407
- if (generateApi) logger.success(`\nGenerated ${outputFile}`);
3408
- else logger.info(`\nRemoved ${outputFile}`);
3409
- logger.success(`Generated ${serverOutputFile}`);
3410
- logger.success(`Generated ${migrationsHelperOutputFile}`);
3411
- if (generateAuth) logger.success(`Generated ${authOutputFile}`);
3412
- else logger.info(`Removed ${authOutputFile}`);
3413
- for (const runtimeOutputFile of runtimeOutputFiles) logger.success(`Generated ${runtimeOutputFile}`);
3414
- if (generateApi) logger.info(` ${Object.keys(meta).length} modules, ${totalFunctions} functions`);
3415
- else logger.info(" cRPC scan skipped for scoped generation");
3416
- } else logger.success(`${time} Convex api ready! (${elapsed}s)`);
3417
- }
3418
-
3419
- //#endregion
3420
- export { isColorEnabled as a, EnableRLS as c, TableName as d, createSystemFields as f, highlighter as i, OrmSchemaExtensions as l, getConvexConfig as n, getSchemaRelations as o, logger as r, Columns as s, generateMeta as t, RlsPolicies as u };