@prisma-next/sql-contract-ts 0.3.0-dev.4 → 0.3.0-dev.40

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.
@@ -0,0 +1,734 @@
1
+ import type { ExtensionPackRef, TargetPackRef } from '@prisma-next/contract/framework-components';
2
+ import type { ExecutionMutationDefault } from '@prisma-next/contract/types';
3
+ import type {
4
+ ColumnBuilderState,
5
+ ContractBuilderState,
6
+ ForeignKeyDefaultsState,
7
+ ModelBuilderState,
8
+ RelationDefinition,
9
+ TableBuilderState,
10
+ } from '@prisma-next/contract-authoring';
11
+ import {
12
+ type BuildModels,
13
+ type BuildRelations,
14
+ type BuildStorageColumn,
15
+ ContractBuilder,
16
+ createTable,
17
+ type ExtractColumns,
18
+ type ExtractPrimaryKey,
19
+ ModelBuilder,
20
+ type Mutable,
21
+ TableBuilder,
22
+ } from '@prisma-next/contract-authoring';
23
+ import {
24
+ applyFkDefaults,
25
+ type ModelDefinition,
26
+ type ModelField,
27
+ type SqlContract,
28
+ type SqlMappings,
29
+ type SqlStorage,
30
+ type StorageTypeInstance,
31
+ } from '@prisma-next/sql-contract/types';
32
+ import { ifDefined } from '@prisma-next/utils/defined';
33
+ import { computeMappings } from './contract';
34
+
35
+ /**
36
+ * Type-level mappings structure for contracts built via `defineContract()`.
37
+ *
38
+ * Compile-time type helper (not a runtime object) that ensures mappings match what the builder
39
+ * produces. `codecTypes` uses the generic `CodecTypes` parameter; `operationTypes` is always
40
+ * empty since operations are added via extensions at runtime.
41
+ *
42
+ * **Difference from ExecutionContext**: This is a compile-time type for contract construction.
43
+ * `ExecutionContext` is a runtime object with populated registries for query execution.
44
+ *
45
+ * @template C - The `CodecTypes` generic parameter passed to `defineContract<CodecTypes>()`
46
+ */
47
+ type ContractBuilderMappings<C extends Record<string, { output: unknown }>> = Omit<
48
+ SqlMappings,
49
+ 'codecTypes' | 'operationTypes'
50
+ > & {
51
+ readonly codecTypes: C;
52
+ readonly operationTypes: Record<string, never>;
53
+ };
54
+
55
+ type BuildStorageTable<
56
+ _TableName extends string,
57
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
58
+ PK extends readonly string[] | undefined,
59
+ > = {
60
+ readonly columns: {
61
+ readonly [K in keyof Columns]: Columns[K] extends ColumnBuilderState<
62
+ string,
63
+ infer Null,
64
+ infer TType
65
+ >
66
+ ? BuildStorageColumn<Null & boolean, TType>
67
+ : never;
68
+ };
69
+ readonly uniques: ReadonlyArray<{ readonly columns: readonly string[]; readonly name?: string }>;
70
+ readonly indexes: ReadonlyArray<{ readonly columns: readonly string[]; readonly name?: string }>;
71
+ readonly foreignKeys: ReadonlyArray<{
72
+ readonly columns: readonly string[];
73
+ readonly references: { readonly table: string; readonly columns: readonly string[] };
74
+ readonly name?: string;
75
+ readonly constraint: boolean;
76
+ readonly index: boolean;
77
+ }>;
78
+ } & (PK extends readonly string[]
79
+ ? { readonly primaryKey: { readonly columns: PK; readonly name?: string } }
80
+ : Record<string, never>);
81
+
82
+ type BuildStorage<
83
+ Tables extends Record<
84
+ string,
85
+ TableBuilderState<
86
+ string,
87
+ Record<string, ColumnBuilderState<string, boolean, string>>,
88
+ readonly string[] | undefined
89
+ >
90
+ >,
91
+ Types extends Record<string, StorageTypeInstance>,
92
+ > = {
93
+ readonly tables: {
94
+ readonly [K in keyof Tables]: BuildStorageTable<
95
+ K & string,
96
+ ExtractColumns<Tables[K]>,
97
+ ExtractPrimaryKey<Tables[K]>
98
+ >;
99
+ };
100
+ readonly types: Types;
101
+ };
102
+
103
+ type BuildStorageTables<
104
+ Tables extends Record<
105
+ string,
106
+ TableBuilderState<
107
+ string,
108
+ Record<string, ColumnBuilderState<string, boolean, string>>,
109
+ readonly string[] | undefined
110
+ >
111
+ >,
112
+ > = {
113
+ readonly [K in keyof Tables]: BuildStorageTable<
114
+ K & string,
115
+ ExtractColumns<Tables[K]>,
116
+ ExtractPrimaryKey<Tables[K]>
117
+ >;
118
+ };
119
+
120
+ export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
121
+ nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
122
+ type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
123
+ build(): ColumnBuilderState<Name, Nullable, Type>;
124
+ }
125
+
126
+ class SqlContractBuilder<
127
+ CodecTypes extends Record<string, { output: unknown }> = Record<string, never>,
128
+ Target extends string | undefined = undefined,
129
+ Tables extends Record<
130
+ string,
131
+ TableBuilderState<
132
+ string,
133
+ Record<string, ColumnBuilderState<string, boolean, string>>,
134
+ readonly string[] | undefined
135
+ >
136
+ > = Record<never, never>,
137
+ Models extends Record<
138
+ string,
139
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
140
+ > = Record<never, never>,
141
+ Types extends Record<string, StorageTypeInstance> = Record<never, never>,
142
+ StorageHash extends string | undefined = undefined,
143
+ ExtensionPacks extends Record<string, unknown> | undefined = undefined,
144
+ Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,
145
+ > extends ContractBuilder<Target, Tables, Models, StorageHash, ExtensionPacks, Capabilities> {
146
+ protected declare readonly state: ContractBuilderState<
147
+ Target,
148
+ Tables,
149
+ Models,
150
+ StorageHash,
151
+ ExtensionPacks,
152
+ Capabilities
153
+ > & {
154
+ readonly storageTypes?: Types;
155
+ };
156
+ /**
157
+ * This method is responsible for normalizing the contract IR by setting default values
158
+ * for all required fields:
159
+ * - `nullable`: defaults to `false` if not provided
160
+ * - `uniques`: defaults to `[]` (empty array)
161
+ * - `indexes`: defaults to `[]` (empty array)
162
+ * - `foreignKeys`: defaults to `[]` (empty array)
163
+ * - `relations`: defaults to `{}` (empty object) for both model-level and contract-level
164
+ * - `nativeType`: required field set from column type descriptor when columns are defined
165
+ *
166
+ * The contract builder is the **only** place where normalization should occur.
167
+ * Validators, parsers, and emitters should assume the contract is already normalized.
168
+ *
169
+ * **Required**: Use column type descriptors (e.g., `int4Column`, `textColumn`) when defining columns.
170
+ * This ensures `nativeType` is set correctly at build time.
171
+ *
172
+ * @returns A normalized SqlContract with all required fields present
173
+ */
174
+ build(): Target extends string
175
+ ? SqlContract<
176
+ BuildStorage<Tables, Types>,
177
+ BuildModels<Models>,
178
+ BuildRelations<Models>,
179
+ ContractBuilderMappings<CodecTypes>
180
+ > & {
181
+ readonly schemaVersion: '1';
182
+ readonly target: Target;
183
+ readonly targetFamily: 'sql';
184
+ readonly storageHash: StorageHash extends string ? StorageHash : string;
185
+ } & (ExtensionPacks extends Record<string, unknown>
186
+ ? { readonly extensionPacks: ExtensionPacks }
187
+ : Record<string, never>) &
188
+ (Capabilities extends Record<string, Record<string, boolean>>
189
+ ? { readonly capabilities: Capabilities }
190
+ : Record<string, never>)
191
+ : never {
192
+ // Type helper to ensure literal types are preserved in return type
193
+ type BuiltContract = Target extends string
194
+ ? SqlContract<
195
+ BuildStorage<Tables, Types>,
196
+ BuildModels<Models>,
197
+ BuildRelations<Models>,
198
+ ContractBuilderMappings<CodecTypes>
199
+ > & {
200
+ readonly schemaVersion: '1';
201
+ readonly target: Target;
202
+ readonly targetFamily: 'sql';
203
+ readonly storageHash: StorageHash extends string ? StorageHash : string;
204
+ } & (ExtensionPacks extends Record<string, unknown>
205
+ ? { readonly extensionPacks: ExtensionPacks }
206
+ : Record<string, never>) &
207
+ (Capabilities extends Record<string, Record<string, boolean>>
208
+ ? { readonly capabilities: Capabilities }
209
+ : Record<string, never>)
210
+ : never;
211
+ if (!this.state.target) {
212
+ throw new Error('target is required. Call .target() before .build()');
213
+ }
214
+
215
+ const target = this.state.target as Target & string;
216
+
217
+ const storageTables = {} as Partial<Mutable<BuildStorageTables<Tables>>>;
218
+ const executionDefaults: ExecutionMutationDefault[] = [];
219
+
220
+ for (const tableName of Object.keys(this.state.tables) as Array<keyof Tables & string>) {
221
+ const tableState = this.state.tables[tableName];
222
+ if (!tableState) continue;
223
+
224
+ type TableKey = typeof tableName;
225
+ type ColumnDefs = ExtractColumns<Tables[TableKey]>;
226
+ type PrimaryKey = ExtractPrimaryKey<Tables[TableKey]>;
227
+
228
+ const columns = {} as Partial<{
229
+ [K in keyof ColumnDefs]: BuildStorageColumn<
230
+ ColumnDefs[K]['nullable'] & boolean,
231
+ ColumnDefs[K]['type']
232
+ >;
233
+ }>;
234
+
235
+ for (const columnName in tableState.columns) {
236
+ const columnState = tableState.columns[columnName];
237
+ if (!columnState) continue;
238
+ const codecId = columnState.type;
239
+ const nativeType = columnState.nativeType;
240
+ const typeRef = columnState.typeRef;
241
+
242
+ columns[columnName as keyof ColumnDefs] = {
243
+ nativeType,
244
+ codecId,
245
+ nullable: (columnState.nullable ?? false) as ColumnDefs[keyof ColumnDefs]['nullable'] &
246
+ boolean,
247
+ ...ifDefined('typeParams', columnState.typeParams),
248
+ ...ifDefined('default', columnState.default),
249
+ ...ifDefined('typeRef', typeRef),
250
+ } as BuildStorageColumn<
251
+ ColumnDefs[keyof ColumnDefs]['nullable'] & boolean,
252
+ ColumnDefs[keyof ColumnDefs]['type']
253
+ >;
254
+
255
+ if ('executionDefault' in columnState && columnState.executionDefault) {
256
+ executionDefaults.push({
257
+ ref: { table: tableName, column: columnName },
258
+ onCreate: columnState.executionDefault,
259
+ });
260
+ }
261
+ }
262
+
263
+ // Build uniques from table state
264
+ const uniques = (tableState.uniques ?? []).map((u) => ({
265
+ columns: u.columns,
266
+ ...(u.name ? { name: u.name } : {}),
267
+ }));
268
+
269
+ // Build indexes from table state
270
+ const indexes = (tableState.indexes ?? []).map((i) => ({
271
+ columns: i.columns,
272
+ ...(i.name ? { name: i.name } : {}),
273
+ }));
274
+
275
+ // Build foreign keys from table state, materializing defaults
276
+ const foreignKeys = (tableState.foreignKeys ?? []).map((fk) => ({
277
+ columns: fk.columns,
278
+ references: fk.references,
279
+ ...applyFkDefaults(fk, this.state.foreignKeyDefaults),
280
+ ...(fk.name ? { name: fk.name } : {}),
281
+ }));
282
+
283
+ const table = {
284
+ columns: columns as {
285
+ [K in keyof ColumnDefs]: BuildStorageColumn<
286
+ ColumnDefs[K]['nullable'] & boolean,
287
+ ColumnDefs[K]['type']
288
+ >;
289
+ },
290
+ uniques,
291
+ indexes,
292
+ foreignKeys,
293
+ ...(tableState.primaryKey
294
+ ? {
295
+ primaryKey: {
296
+ columns: tableState.primaryKey,
297
+ ...(tableState.primaryKeyName ? { name: tableState.primaryKeyName } : {}),
298
+ },
299
+ }
300
+ : {}),
301
+ } as unknown as BuildStorageTable<TableKey & string, ColumnDefs, PrimaryKey>;
302
+
303
+ (storageTables as Mutable<BuildStorageTables<Tables>>)[tableName] = table;
304
+ }
305
+
306
+ const storageTypes = (this.state.storageTypes ?? {}) as Types;
307
+ const storage: BuildStorage<Tables, Types> = {
308
+ tables: storageTables as BuildStorageTables<Tables>,
309
+ types: storageTypes,
310
+ };
311
+
312
+ const execution =
313
+ executionDefaults.length > 0
314
+ ? {
315
+ mutations: {
316
+ defaults: executionDefaults.sort((a, b) => {
317
+ const tableCompare = a.ref.table.localeCompare(b.ref.table);
318
+ if (tableCompare !== 0) {
319
+ return tableCompare;
320
+ }
321
+ return a.ref.column.localeCompare(b.ref.column);
322
+ }),
323
+ },
324
+ }
325
+ : undefined;
326
+
327
+ // Build models - construct as partial first, then assert full type
328
+ const modelsPartial: Partial<BuildModels<Models>> = {};
329
+
330
+ // Iterate over models - TypeScript will see keys as string, but type assertion preserves literals
331
+ for (const modelName in this.state.models) {
332
+ const modelState = this.state.models[modelName];
333
+ if (!modelState) continue;
334
+
335
+ const modelStateTyped = modelState as unknown as {
336
+ name: string;
337
+ table: string;
338
+ fields: Record<string, string>;
339
+ };
340
+
341
+ // Build fields object
342
+ const fields: Partial<Record<string, ModelField>> = {};
343
+
344
+ // Iterate over fields
345
+ for (const fieldName in modelStateTyped.fields) {
346
+ const columnName = modelStateTyped.fields[fieldName];
347
+ if (columnName) {
348
+ fields[fieldName] = {
349
+ column: columnName,
350
+ };
351
+ }
352
+ }
353
+
354
+ // Assign to models - type assertion preserves literal keys
355
+ (modelsPartial as unknown as Record<string, ModelDefinition>)[modelName] = {
356
+ storage: {
357
+ table: modelStateTyped.table,
358
+ },
359
+ fields: fields as Record<string, ModelField>,
360
+ relations: {},
361
+ };
362
+ }
363
+
364
+ // Build relations object - organized by table name
365
+ const relationsPartial: Partial<Record<string, Record<string, RelationDefinition>>> = {};
366
+
367
+ // Iterate over models to collect relations
368
+ for (const modelName in this.state.models) {
369
+ const modelState = this.state.models[modelName];
370
+ if (!modelState) continue;
371
+
372
+ const modelStateTyped = modelState as unknown as {
373
+ name: string;
374
+ table: string;
375
+ fields: Record<string, string>;
376
+ relations: Record<string, RelationDefinition>;
377
+ };
378
+
379
+ const tableName = modelStateTyped.table;
380
+ if (!tableName) continue;
381
+
382
+ // Only initialize relations object for this table if it has relations
383
+ if (modelStateTyped.relations && Object.keys(modelStateTyped.relations).length > 0) {
384
+ if (!relationsPartial[tableName]) {
385
+ relationsPartial[tableName] = {};
386
+ }
387
+
388
+ // Add relations from this model to the table's relations
389
+ const tableRelations = relationsPartial[tableName];
390
+ if (tableRelations) {
391
+ for (const relationName in modelStateTyped.relations) {
392
+ const relation = modelStateTyped.relations[relationName];
393
+ if (relation) {
394
+ tableRelations[relationName] = relation;
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
400
+
401
+ const models = modelsPartial as unknown as BuildModels<Models>;
402
+
403
+ const baseMappings = computeMappings(
404
+ models as unknown as Record<string, ModelDefinition>,
405
+ storage as SqlStorage,
406
+ );
407
+
408
+ const mappings = {
409
+ ...baseMappings,
410
+ codecTypes: {} as CodecTypes,
411
+ operationTypes: {} as Record<string, never>,
412
+ } as ContractBuilderMappings<CodecTypes>;
413
+
414
+ const extensionNamespaces = this.state.extensionNamespaces ?? [];
415
+ const extensionPacks: Record<string, unknown> = { ...(this.state.extensionPacks || {}) };
416
+ for (const namespace of extensionNamespaces) {
417
+ if (!Object.hasOwn(extensionPacks, namespace)) {
418
+ extensionPacks[namespace] = {};
419
+ }
420
+ }
421
+
422
+ // Construct contract with explicit type that matches the generic parameters
423
+ // This ensures TypeScript infers literal types from the generics, not runtime values
424
+ // Always include relations, even if empty (normalized to empty object)
425
+ const contract = {
426
+ schemaVersion: '1' as const,
427
+ target,
428
+ targetFamily: 'sql' as const,
429
+ storageHash: this.state.storageHash || 'sha256:ts-builder-placeholder',
430
+ models,
431
+ relations: relationsPartial,
432
+ storage,
433
+ mappings,
434
+ ...(execution ? { execution } : {}),
435
+ extensionPacks,
436
+ capabilities: this.state.capabilities || {},
437
+ meta: {},
438
+ sources: {},
439
+ } as unknown as BuiltContract;
440
+
441
+ return contract as unknown as ReturnType<
442
+ SqlContractBuilder<
443
+ CodecTypes,
444
+ Target,
445
+ Tables,
446
+ Models,
447
+ Types,
448
+ StorageHash,
449
+ ExtensionPacks,
450
+ Capabilities
451
+ >['build']
452
+ >;
453
+ }
454
+
455
+ override target<T extends string>(
456
+ packRef: TargetPackRef<'sql', T>,
457
+ ): SqlContractBuilder<
458
+ CodecTypes,
459
+ T,
460
+ Tables,
461
+ Models,
462
+ Types,
463
+ StorageHash,
464
+ ExtensionPacks,
465
+ Capabilities
466
+ > {
467
+ return new SqlContractBuilder<
468
+ CodecTypes,
469
+ T,
470
+ Tables,
471
+ Models,
472
+ Types,
473
+ StorageHash,
474
+ ExtensionPacks,
475
+ Capabilities
476
+ >({
477
+ ...this.state,
478
+ target: packRef.targetId,
479
+ });
480
+ }
481
+
482
+ extensionPacks(
483
+ packs: Record<string, ExtensionPackRef<'sql', string>>,
484
+ ): SqlContractBuilder<
485
+ CodecTypes,
486
+ Target,
487
+ Tables,
488
+ Models,
489
+ Types,
490
+ StorageHash,
491
+ ExtensionPacks,
492
+ Capabilities
493
+ > {
494
+ if (!this.state.target) {
495
+ throw new Error('extensionPacks() requires target() to be called first');
496
+ }
497
+
498
+ const namespaces = new Set(this.state.extensionNamespaces ?? []);
499
+
500
+ for (const packRef of Object.values(packs)) {
501
+ if (!packRef) continue;
502
+
503
+ if (packRef.kind !== 'extension') {
504
+ throw new Error(
505
+ `extensionPacks() only accepts extension pack refs. Received kind "${packRef.kind}".`,
506
+ );
507
+ }
508
+
509
+ if (packRef.familyId !== 'sql') {
510
+ throw new Error(
511
+ `extension pack "${packRef.id}" targets family "${packRef.familyId}" but this builder targets "sql".`,
512
+ );
513
+ }
514
+
515
+ if (packRef.targetId && packRef.targetId !== this.state.target) {
516
+ throw new Error(
517
+ `extension pack "${packRef.id}" targets "${packRef.targetId}" but builder target is "${this.state.target}".`,
518
+ );
519
+ }
520
+
521
+ namespaces.add(packRef.id);
522
+ }
523
+
524
+ return new SqlContractBuilder<
525
+ CodecTypes,
526
+ Target,
527
+ Tables,
528
+ Models,
529
+ Types,
530
+ StorageHash,
531
+ ExtensionPacks,
532
+ Capabilities
533
+ >({
534
+ ...this.state,
535
+ extensionNamespaces: [...namespaces],
536
+ });
537
+ }
538
+
539
+ override capabilities<C extends Record<string, Record<string, boolean>>>(
540
+ capabilities: C,
541
+ ): SqlContractBuilder<CodecTypes, Target, Tables, Models, Types, StorageHash, ExtensionPacks, C> {
542
+ return new SqlContractBuilder<
543
+ CodecTypes,
544
+ Target,
545
+ Tables,
546
+ Models,
547
+ Types,
548
+ StorageHash,
549
+ ExtensionPacks,
550
+ C
551
+ >({
552
+ ...this.state,
553
+ capabilities,
554
+ });
555
+ }
556
+
557
+ override storageHash<H extends string>(
558
+ hash: H,
559
+ ): SqlContractBuilder<
560
+ CodecTypes,
561
+ Target,
562
+ Tables,
563
+ Models,
564
+ Types,
565
+ H,
566
+ ExtensionPacks,
567
+ Capabilities
568
+ > {
569
+ return new SqlContractBuilder<
570
+ CodecTypes,
571
+ Target,
572
+ Tables,
573
+ Models,
574
+ Types,
575
+ H,
576
+ ExtensionPacks,
577
+ Capabilities
578
+ >({
579
+ ...this.state,
580
+ storageHash: hash,
581
+ });
582
+ }
583
+
584
+ override table<
585
+ TableName extends string,
586
+ T extends TableBuilder<
587
+ TableName,
588
+ Record<string, ColumnBuilderState<string, boolean, string>>,
589
+ readonly string[] | undefined
590
+ >,
591
+ >(
592
+ name: TableName,
593
+ callback: (t: TableBuilder<TableName>) => T | undefined,
594
+ ): SqlContractBuilder<
595
+ CodecTypes,
596
+ Target,
597
+ Tables & Record<TableName, ReturnType<T['build']>>,
598
+ Models,
599
+ Types,
600
+ StorageHash,
601
+ ExtensionPacks,
602
+ Capabilities
603
+ > {
604
+ const tableBuilder = createTable(name);
605
+ const result = callback(tableBuilder);
606
+ const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
607
+ const tableState = finalBuilder.build();
608
+
609
+ return new SqlContractBuilder<
610
+ CodecTypes,
611
+ Target,
612
+ Tables & Record<TableName, ReturnType<T['build']>>,
613
+ Models,
614
+ Types,
615
+ StorageHash,
616
+ ExtensionPacks,
617
+ Capabilities
618
+ >({
619
+ ...this.state,
620
+ tables: { ...this.state.tables, [name]: tableState } as Tables &
621
+ Record<TableName, ReturnType<T['build']>>,
622
+ });
623
+ }
624
+
625
+ override model<
626
+ ModelName extends string,
627
+ TableName extends string,
628
+ M extends ModelBuilder<
629
+ ModelName,
630
+ TableName,
631
+ Record<string, string>,
632
+ Record<string, RelationDefinition>
633
+ >,
634
+ >(
635
+ name: ModelName,
636
+ table: TableName,
637
+ callback: (
638
+ m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,
639
+ ) => M | undefined,
640
+ ): SqlContractBuilder<
641
+ CodecTypes,
642
+ Target,
643
+ Tables,
644
+ Models & Record<ModelName, ReturnType<M['build']>>,
645
+ Types,
646
+ StorageHash,
647
+ ExtensionPacks,
648
+ Capabilities
649
+ > {
650
+ const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);
651
+ const result = callback(modelBuilder);
652
+ const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;
653
+ const modelState = finalBuilder.build();
654
+
655
+ return new SqlContractBuilder<
656
+ CodecTypes,
657
+ Target,
658
+ Tables,
659
+ Models & Record<ModelName, ReturnType<M['build']>>,
660
+ Types,
661
+ StorageHash,
662
+ ExtensionPacks,
663
+ Capabilities
664
+ >({
665
+ ...this.state,
666
+ models: { ...this.state.models, [name]: modelState } as Models &
667
+ Record<ModelName, ReturnType<M['build']>>,
668
+ });
669
+ }
670
+
671
+ override foreignKeyDefaults(
672
+ config: ForeignKeyDefaultsState,
673
+ ): SqlContractBuilder<
674
+ CodecTypes,
675
+ Target,
676
+ Tables,
677
+ Models,
678
+ Types,
679
+ StorageHash,
680
+ ExtensionPacks,
681
+ Capabilities
682
+ > {
683
+ return new SqlContractBuilder<
684
+ CodecTypes,
685
+ Target,
686
+ Tables,
687
+ Models,
688
+ Types,
689
+ StorageHash,
690
+ ExtensionPacks,
691
+ Capabilities
692
+ >({
693
+ ...this.state,
694
+ foreignKeyDefaults: config,
695
+ });
696
+ }
697
+
698
+ storageType<Name extends string, Type extends StorageTypeInstance>(
699
+ name: Name,
700
+ typeInstance: Type,
701
+ ): SqlContractBuilder<
702
+ CodecTypes,
703
+ Target,
704
+ Tables,
705
+ Models,
706
+ Types & Record<Name, Type>,
707
+ StorageHash,
708
+ ExtensionPacks,
709
+ Capabilities
710
+ > {
711
+ return new SqlContractBuilder<
712
+ CodecTypes,
713
+ Target,
714
+ Tables,
715
+ Models,
716
+ Types & Record<Name, Type>,
717
+ StorageHash,
718
+ ExtensionPacks,
719
+ Capabilities
720
+ >({
721
+ ...this.state,
722
+ storageTypes: {
723
+ ...(this.state.storageTypes ?? {}),
724
+ [name]: typeInstance,
725
+ },
726
+ });
727
+ }
728
+ }
729
+
730
+ export function defineContract<
731
+ CodecTypes extends Record<string, { output: unknown }> = Record<string, never>,
732
+ >(): SqlContractBuilder<CodecTypes> {
733
+ return new SqlContractBuilder<CodecTypes>();
734
+ }