@prisma-next/sql-contract 0.3.0-dev.8 → 0.3.0-dev.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +84 -10
  3. package/dist/factories.d.mts +48 -0
  4. package/dist/factories.d.mts.map +1 -0
  5. package/dist/factories.mjs +84 -0
  6. package/dist/factories.mjs.map +1 -0
  7. package/dist/pack-types.d.mts +13 -0
  8. package/dist/pack-types.d.mts.map +1 -0
  9. package/dist/pack-types.mjs +1 -0
  10. package/dist/types-CcCSXOlR.d.mts +166 -0
  11. package/dist/types-CcCSXOlR.d.mts.map +1 -0
  12. package/dist/types-DRR5stkj.mjs +13 -0
  13. package/dist/types-DRR5stkj.mjs.map +1 -0
  14. package/dist/types.d.mts +2 -0
  15. package/dist/types.mjs +3 -0
  16. package/dist/validate.d.mts +11 -0
  17. package/dist/validate.d.mts.map +1 -0
  18. package/dist/validate.mjs +244 -0
  19. package/dist/validate.mjs.map +1 -0
  20. package/dist/validators-CQXvLZa7.mjs +216 -0
  21. package/dist/validators-CQXvLZa7.mjs.map +1 -0
  22. package/dist/validators.d.mts +71 -0
  23. package/dist/validators.d.mts.map +1 -0
  24. package/dist/validators.mjs +3 -0
  25. package/package.json +24 -28
  26. package/src/construct.ts +178 -0
  27. package/src/exports/types.ts +13 -0
  28. package/src/exports/validate.ts +6 -0
  29. package/src/exports/validators.ts +1 -1
  30. package/src/factories.ts +41 -8
  31. package/src/index.ts +1 -0
  32. package/src/types.ts +137 -8
  33. package/src/validate.ts +272 -0
  34. package/src/validators.ts +164 -12
  35. package/dist/exports/factories.d.ts +0 -2
  36. package/dist/exports/factories.d.ts.map +0 -1
  37. package/dist/exports/factories.js +0 -83
  38. package/dist/exports/factories.js.map +0 -1
  39. package/dist/exports/pack-types.d.ts +0 -2
  40. package/dist/exports/pack-types.d.ts.map +0 -1
  41. package/dist/exports/pack-types.js +0 -1
  42. package/dist/exports/pack-types.js.map +0 -1
  43. package/dist/exports/types.d.ts +0 -2
  44. package/dist/exports/types.d.ts.map +0 -1
  45. package/dist/exports/types.js +0 -1
  46. package/dist/exports/types.js.map +0 -1
  47. package/dist/exports/validators.d.ts +0 -2
  48. package/dist/exports/validators.d.ts.map +0 -1
  49. package/dist/exports/validators.js +0 -96
  50. package/dist/exports/validators.js.map +0 -1
  51. package/dist/factories.d.ts +0 -38
  52. package/dist/factories.d.ts.map +0 -1
  53. package/dist/index.d.ts +0 -4
  54. package/dist/index.d.ts.map +0 -1
  55. package/dist/pack-types.d.ts +0 -10
  56. package/dist/pack-types.d.ts.map +0 -1
  57. package/dist/types.d.ts +0 -68
  58. package/dist/types.d.ts.map +0 -1
  59. package/dist/validators.d.ts +0 -35
  60. package/dist/validators.d.ts.map +0 -1
package/src/factories.ts CHANGED
@@ -1,5 +1,11 @@
1
+ import type {
2
+ ExecutionHashBase,
3
+ ProfileHashBase,
4
+ StorageHashBase,
5
+ } from '@prisma-next/contract/types';
1
6
  import type {
2
7
  ForeignKey,
8
+ ForeignKeyOptions,
3
9
  ForeignKeyReferences,
4
10
  Index,
5
11
  ModelDefinition,
@@ -13,6 +19,7 @@ import type {
13
19
  StorageTable,
14
20
  UniqueConstraint,
15
21
  } from './types';
22
+ import { applyFkDefaults } from './types';
16
23
 
17
24
  /**
18
25
  * Creates a StorageColumn with nativeType and codecId.
@@ -52,16 +59,20 @@ export function fk(
52
59
  columns: readonly string[],
53
60
  refTable: string,
54
61
  refColumns: readonly string[],
55
- name?: string,
62
+ opts?: ForeignKeyOptions & { constraint?: boolean; index?: boolean },
56
63
  ): ForeignKey {
57
64
  const references: ForeignKeyReferences = {
58
65
  table: refTable,
59
66
  columns: refColumns,
60
67
  };
68
+
61
69
  return {
62
70
  columns,
63
71
  references,
64
- ...(name !== undefined && { name }),
72
+ ...(opts?.name !== undefined && { name: opts.name }),
73
+ ...(opts?.onDelete !== undefined && { onDelete: opts.onDelete }),
74
+ ...(opts?.onUpdate !== undefined && { onUpdate: opts.onUpdate }),
75
+ ...applyFkDefaults({ constraint: opts?.constraint, index: opts?.index }),
65
76
  };
66
77
  }
67
78
 
@@ -100,26 +111,40 @@ export function storage(tables: Record<string, StorageTable>): SqlStorage {
100
111
  return { tables };
101
112
  }
102
113
 
103
- export function contract(opts: {
114
+ export function contract<
115
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
116
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
117
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
118
+ >(opts: {
104
119
  target: string;
105
- coreHash: string;
120
+ storageHash: TStorageHash;
121
+ executionHash?: TExecutionHash;
106
122
  storage: SqlStorage;
107
123
  models?: Record<string, ModelDefinition>;
108
124
  relations?: Record<string, unknown>;
109
125
  mappings?: Partial<SqlMappings>;
110
126
  schemaVersion?: '1';
111
127
  targetFamily?: 'sql';
112
- profileHash?: string;
128
+ profileHash?: TProfileHash;
113
129
  capabilities?: Record<string, Record<string, boolean>>;
114
130
  extensionPacks?: Record<string, unknown>;
115
131
  meta?: Record<string, unknown>;
116
132
  sources?: Record<string, unknown>;
117
- }): SqlContract {
133
+ }): SqlContract<
134
+ SqlStorage,
135
+ Record<string, unknown>,
136
+ Record<string, unknown>,
137
+ SqlMappings,
138
+ TStorageHash,
139
+ TExecutionHash,
140
+ TProfileHash
141
+ > {
118
142
  return {
119
143
  schemaVersion: opts.schemaVersion ?? '1',
120
144
  target: opts.target,
121
145
  targetFamily: opts.targetFamily ?? 'sql',
122
- coreHash: opts.coreHash,
146
+ storageHash: opts.storageHash,
147
+ ...(opts.executionHash !== undefined && { executionHash: opts.executionHash }),
123
148
  storage: opts.storage,
124
149
  models: opts.models ?? {},
125
150
  relations: opts.relations ?? {},
@@ -129,5 +154,13 @@ export function contract(opts: {
129
154
  ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),
130
155
  ...(opts.meta !== undefined && { meta: opts.meta }),
131
156
  ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),
132
- } as SqlContract;
157
+ } as SqlContract<
158
+ SqlStorage,
159
+ Record<string, unknown>,
160
+ Record<string, unknown>,
161
+ SqlMappings,
162
+ TStorageHash,
163
+ TExecutionHash,
164
+ TProfileHash
165
+ >;
133
166
  }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './exports/factories';
2
2
  export * from './exports/types';
3
+ export * from './exports/validate';
3
4
  export * from './exports/validators';
package/src/types.ts CHANGED
@@ -1,9 +1,39 @@
1
- import type { ContractBase } from '@prisma-next/contract/types';
1
+ import type {
2
+ ColumnDefault,
3
+ ContractBase,
4
+ ExecutionHashBase,
5
+ ExecutionSection,
6
+ ProfileHashBase,
7
+ StorageHashBase,
8
+ } from '@prisma-next/contract/types';
2
9
 
10
+ /**
11
+ * A column definition in storage.
12
+ *
13
+ * `typeParams` is optional because most columns use non-parameterized types.
14
+ * Columns with parameterized types can either inline `typeParams` or reference
15
+ * a named {@link StorageTypeInstance} via `typeRef`.
16
+ */
3
17
  export type StorageColumn = {
4
18
  readonly nativeType: string;
5
19
  readonly codecId: string;
6
20
  readonly nullable: boolean;
21
+ /**
22
+ * Opaque, codec-owned JS/type parameters.
23
+ * The codec that owns `codecId` defines the shape and semantics.
24
+ * Mutually exclusive with `typeRef`.
25
+ */
26
+ readonly typeParams?: Record<string, unknown>;
27
+ /**
28
+ * Reference to a named type instance in `storage.types`.
29
+ * Mutually exclusive with `typeParams`.
30
+ */
31
+ readonly typeRef?: string;
32
+ /**
33
+ * Default value for the column.
34
+ * Can be a literal value or database function.
35
+ */
36
+ readonly default?: ColumnDefault;
7
37
  };
8
38
 
9
39
  export type PrimaryKey = {
@@ -19,6 +49,16 @@ export type UniqueConstraint = {
19
49
  export type Index = {
20
50
  readonly columns: readonly string[];
21
51
  readonly name?: string;
52
+ /**
53
+ * Optional access method identifier.
54
+ * Extension-specific methods are represented as strings and interpreted
55
+ * by the owning extension package.
56
+ */
57
+ readonly using?: string;
58
+ /**
59
+ * Optional extension-owned index configuration payload.
60
+ */
61
+ readonly config?: Record<string, unknown>;
22
62
  };
23
63
 
24
64
  export type ForeignKeyReferences = {
@@ -26,10 +66,24 @@ export type ForeignKeyReferences = {
26
66
  readonly columns: readonly string[];
27
67
  };
28
68
 
69
+ export type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';
70
+
71
+ export type ForeignKeyOptions = {
72
+ readonly name?: string;
73
+ readonly onDelete?: ReferentialAction;
74
+ readonly onUpdate?: ReferentialAction;
75
+ };
76
+
29
77
  export type ForeignKey = {
30
78
  readonly columns: readonly string[];
31
79
  readonly references: ForeignKeyReferences;
32
80
  readonly name?: string;
81
+ readonly onDelete?: ReferentialAction;
82
+ readonly onUpdate?: ReferentialAction;
83
+ /** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
84
+ readonly constraint: boolean;
85
+ /** Whether to emit a backing index for the FK columns. */
86
+ readonly index: boolean;
33
87
  };
34
88
 
35
89
  export type StorageTable = {
@@ -40,8 +94,29 @@ export type StorageTable = {
40
94
  readonly foreignKeys: ReadonlyArray<ForeignKey>;
41
95
  };
42
96
 
97
+ /**
98
+ * A named, parameterized type instance.
99
+ * These are registered in `storage.types` for reuse across columns
100
+ * and to enable ergonomic schema surfaces like `schema.types.MyType`.
101
+ *
102
+ * Unlike {@link StorageColumn}, `typeParams` is required here because
103
+ * `StorageTypeInstance` exists specifically to define reusable parameterized types.
104
+ * A type instance without parameters would be redundant—columns can reference
105
+ * the codec directly via `codecId`.
106
+ */
107
+ export type StorageTypeInstance = {
108
+ readonly codecId: string;
109
+ readonly nativeType: string;
110
+ readonly typeParams: Record<string, unknown>;
111
+ };
112
+
43
113
  export type SqlStorage = {
44
114
  readonly tables: Record<string, StorageTable>;
115
+ /**
116
+ * Named type instances for parameterized/custom types.
117
+ * Columns can reference these via `typeRef`.
118
+ */
119
+ readonly types?: Record<string, StorageTypeInstance>;
45
120
  };
46
121
 
47
122
  export type ModelField = {
@@ -63,8 +138,49 @@ export type SqlMappings = {
63
138
  readonly tableToModel?: Record<string, string>;
64
139
  readonly fieldToColumn?: Record<string, Record<string, string>>;
65
140
  readonly columnToField?: Record<string, Record<string, string>>;
66
- readonly codecTypes: Record<string, { readonly output: unknown }>;
67
- readonly operationTypes: Record<string, Record<string, unknown>>;
141
+ };
142
+
143
+ export const DEFAULT_FK_CONSTRAINT = true;
144
+ export const DEFAULT_FK_INDEX = true;
145
+
146
+ export function applyFkDefaults(
147
+ fk: { constraint?: boolean | undefined; index?: boolean | undefined },
148
+ overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },
149
+ ): { constraint: boolean; index: boolean } {
150
+ return {
151
+ constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
152
+ index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,
153
+ };
154
+ }
155
+
156
+ export type TypeMaps<
157
+ TCodecTypes extends Record<string, { output: unknown }> = Record<string, never>,
158
+ TOperationTypes extends Record<string, unknown> = Record<string, never>,
159
+ > = {
160
+ readonly codecTypes: TCodecTypes;
161
+ readonly operationTypes: TOperationTypes;
162
+ };
163
+
164
+ export type CodecTypesOf<T> = [T] extends [never]
165
+ ? Record<string, never>
166
+ : T extends { readonly codecTypes: infer C }
167
+ ? C extends Record<string, { output: unknown }>
168
+ ? C
169
+ : Record<string, never>
170
+ : Record<string, never>;
171
+
172
+ export type OperationTypesOf<T> = [T] extends [never]
173
+ ? Record<string, never>
174
+ : T extends { readonly operationTypes: infer O }
175
+ ? O extends Record<string, unknown>
176
+ ? O
177
+ : Record<string, never>
178
+ : Record<string, never>;
179
+
180
+ export type TypeMapsPhantomKey = '__@prisma-next/sql-contract/typeMaps@__';
181
+
182
+ export type ContractWithTypeMaps<TContract, TTypeMaps> = TContract & {
183
+ readonly [K in TypeMapsPhantomKey]?: TTypeMaps;
68
184
  };
69
185
 
70
186
  export type SqlContract<
@@ -72,16 +188,29 @@ export type SqlContract<
72
188
  M extends Record<string, unknown> = Record<string, unknown>,
73
189
  R extends Record<string, unknown> = Record<string, unknown>,
74
190
  Map extends SqlMappings = SqlMappings,
75
- > = ContractBase & {
191
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
192
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
193
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
194
+ > = ContractBase<TStorageHash, TExecutionHash, TProfileHash> & {
76
195
  readonly targetFamily: string;
77
196
  readonly storage: S;
78
197
  readonly models: M;
79
198
  readonly relations: R;
80
199
  readonly mappings: Map;
200
+ readonly execution?: ExecutionSection;
81
201
  };
82
202
 
83
- export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =
84
- TContract['mappings']['codecTypes'];
203
+ export type ExtractTypeMapsFromContract<T> = TypeMapsPhantomKey extends keyof T
204
+ ? NonNullable<T[TypeMapsPhantomKey & keyof T]>
205
+ : never;
206
+
207
+ export type ExtractCodecTypes<T> = CodecTypesOf<ExtractTypeMapsFromContract<T>>;
208
+ export type ExtractOperationTypes<T> = OperationTypesOf<ExtractTypeMapsFromContract<T>>;
209
+
210
+ export type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]
211
+ ? ExtractCodecTypes<TContract>
212
+ : CodecTypesOf<TTypeMaps>;
85
213
 
86
- export type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> =
87
- TContract['mappings']['operationTypes'];
214
+ export type ResolveOperationTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]
215
+ ? ExtractOperationTypes<TContract>
216
+ : OperationTypesOf<TTypeMaps>;
@@ -0,0 +1,272 @@
1
+ import type { ColumnDefaultLiteralInputValue } from '@prisma-next/contract/types';
2
+ import { isTaggedBigInt, isTaggedRaw } from '@prisma-next/contract/types';
3
+ import { constructContract } from './construct';
4
+ import type { SqlContract, SqlStorage, StorageColumn, StorageTable } from './types';
5
+ import { applyFkDefaults } from './types';
6
+ import { validateSqlContract, validateStorageSemantics } from './validators';
7
+
8
+ function validateContractLogic(contract: SqlContract<SqlStorage>): void {
9
+ const tableNames = new Set(Object.keys(contract.storage.tables));
10
+
11
+ for (const [tableName, table] of Object.entries(contract.storage.tables)) {
12
+ const columnNames = new Set(Object.keys(table.columns));
13
+
14
+ if (table.primaryKey) {
15
+ for (const colName of table.primaryKey.columns) {
16
+ if (!columnNames.has(colName)) {
17
+ throw new Error(
18
+ `Table "${tableName}" primaryKey references non-existent column "${colName}"`,
19
+ );
20
+ }
21
+ }
22
+ }
23
+
24
+ for (const unique of table.uniques) {
25
+ for (const colName of unique.columns) {
26
+ if (!columnNames.has(colName)) {
27
+ throw new Error(
28
+ `Table "${tableName}" unique constraint references non-existent column "${colName}"`,
29
+ );
30
+ }
31
+ }
32
+ }
33
+
34
+ for (const index of table.indexes) {
35
+ for (const colName of index.columns) {
36
+ if (!columnNames.has(colName)) {
37
+ throw new Error(`Table "${tableName}" index references non-existent column "${colName}"`);
38
+ }
39
+ }
40
+ }
41
+
42
+ for (const [colName, column] of Object.entries(table.columns)) {
43
+ if (!column.nullable && column.default?.kind === 'literal' && column.default.value === null) {
44
+ throw new Error(
45
+ `Table "${tableName}" column "${colName}" is NOT NULL but has a literal null default`,
46
+ );
47
+ }
48
+ }
49
+
50
+ for (const fk of table.foreignKeys) {
51
+ for (const colName of fk.columns) {
52
+ if (!columnNames.has(colName)) {
53
+ throw new Error(
54
+ `Table "${tableName}" foreignKey references non-existent column "${colName}"`,
55
+ );
56
+ }
57
+ }
58
+
59
+ if (!tableNames.has(fk.references.table)) {
60
+ throw new Error(
61
+ `Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`,
62
+ );
63
+ }
64
+
65
+ const referencedTable = contract.storage.tables[
66
+ fk.references.table
67
+ ] as (typeof contract.storage.tables)[string];
68
+ const referencedColumnNames = new Set(Object.keys(referencedTable.columns));
69
+ for (const colName of fk.references.columns) {
70
+ if (!referencedColumnNames.has(colName)) {
71
+ throw new Error(
72
+ `Table "${tableName}" foreignKey references non-existent column "${colName}" in table "${fk.references.table}"`,
73
+ );
74
+ }
75
+ }
76
+
77
+ if (fk.columns.length !== fk.references.columns.length) {
78
+ throw new Error(
79
+ `Table "${tableName}" foreignKey column count (${fk.columns.length}) does not match referenced column count (${fk.references.columns.length})`,
80
+ );
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ const BIGINT_NATIVE_TYPES = new Set(['bigint', 'int8']);
87
+
88
+ export function isBigIntColumn(column: StorageColumn): boolean {
89
+ const nativeType = column.nativeType?.toLowerCase() ?? '';
90
+ if (BIGINT_NATIVE_TYPES.has(nativeType)) return true;
91
+ const codecId = column.codecId?.toLowerCase() ?? '';
92
+ return codecId.includes('int8') || codecId.includes('bigint');
93
+ }
94
+
95
+ export function decodeDefaultLiteralValue(
96
+ value: ColumnDefaultLiteralInputValue,
97
+ column: StorageColumn,
98
+ tableName: string,
99
+ columnName: string,
100
+ ): ColumnDefaultLiteralInputValue {
101
+ if (value instanceof Date) {
102
+ return value;
103
+ }
104
+ if (isTaggedRaw(value)) {
105
+ return value.value;
106
+ }
107
+ if (isTaggedBigInt(value)) {
108
+ if (!isBigIntColumn(column)) {
109
+ return value;
110
+ }
111
+ try {
112
+ return BigInt(value.value);
113
+ } catch {
114
+ throw new Error(
115
+ `Invalid tagged bigint for default value on "${tableName}.${columnName}": "${value.value}" is not a valid integer`,
116
+ );
117
+ }
118
+ }
119
+ return value;
120
+ }
121
+
122
+ export function decodeContractDefaults<T extends SqlContract<SqlStorage>>(contract: T): T {
123
+ const tables = contract.storage.tables;
124
+ let tablesChanged = false;
125
+ const decodedTables: Record<string, StorageTable> = {};
126
+
127
+ for (const [tableName, table] of Object.entries(tables)) {
128
+ let columnsChanged = false;
129
+ const decodedColumns: Record<string, StorageColumn> = {};
130
+
131
+ for (const [columnName, column] of Object.entries(table.columns)) {
132
+ if (column.default?.kind === 'literal') {
133
+ const decodedValue = decodeDefaultLiteralValue(
134
+ column.default.value,
135
+ column,
136
+ tableName,
137
+ columnName,
138
+ );
139
+ if (decodedValue !== column.default.value) {
140
+ columnsChanged = true;
141
+ decodedColumns[columnName] = {
142
+ ...column,
143
+ default: { kind: 'literal', value: decodedValue },
144
+ };
145
+ continue;
146
+ }
147
+ }
148
+ decodedColumns[columnName] = column;
149
+ }
150
+
151
+ if (columnsChanged) {
152
+ tablesChanged = true;
153
+ decodedTables[tableName] = { ...table, columns: decodedColumns };
154
+ } else {
155
+ decodedTables[tableName] = table;
156
+ }
157
+ }
158
+
159
+ if (!tablesChanged) {
160
+ return contract;
161
+ }
162
+
163
+ // The spread widens to SqlContract<SqlStorage>, but this transformation only
164
+ // decodes tagged bigint defaults for bigint-like columns and preserves all
165
+ // other properties of T.
166
+ return {
167
+ ...contract,
168
+ storage: {
169
+ ...contract.storage,
170
+ tables: decodedTables,
171
+ },
172
+ } as T;
173
+ }
174
+
175
+ export function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
176
+ if (typeof contract !== 'object' || contract === null) {
177
+ return contract as SqlContract<SqlStorage>;
178
+ }
179
+
180
+ const contractObj = contract as Record<string, unknown>;
181
+
182
+ let normalizedStorage = contractObj['storage'];
183
+ if (normalizedStorage && typeof normalizedStorage === 'object' && normalizedStorage !== null) {
184
+ const storage = normalizedStorage as Record<string, unknown>;
185
+ const tables = storage['tables'] as Record<string, unknown> | undefined;
186
+
187
+ if (tables) {
188
+ const normalizedTables: Record<string, unknown> = {};
189
+ for (const [tableName, table] of Object.entries(tables)) {
190
+ const tableObj = table as Record<string, unknown>;
191
+ const columns = tableObj['columns'] as Record<string, unknown> | undefined;
192
+
193
+ if (columns) {
194
+ const normalizedColumns: Record<string, unknown> = {};
195
+ for (const [columnName, column] of Object.entries(columns)) {
196
+ const columnObj = column as Record<string, unknown>;
197
+ normalizedColumns[columnName] = {
198
+ ...columnObj,
199
+ nullable: columnObj['nullable'] ?? false,
200
+ };
201
+ }
202
+
203
+ // Normalize foreign keys: add constraint/index defaults if missing
204
+ const rawForeignKeys = (tableObj['foreignKeys'] ?? []) as Array<Record<string, unknown>>;
205
+ const normalizedForeignKeys = rawForeignKeys.map((fk) => ({
206
+ ...fk,
207
+ ...applyFkDefaults({
208
+ constraint: typeof fk['constraint'] === 'boolean' ? fk['constraint'] : undefined,
209
+ index: typeof fk['index'] === 'boolean' ? fk['index'] : undefined,
210
+ }),
211
+ }));
212
+
213
+ normalizedTables[tableName] = {
214
+ ...tableObj,
215
+ columns: normalizedColumns,
216
+ uniques: tableObj['uniques'] ?? [],
217
+ indexes: tableObj['indexes'] ?? [],
218
+ foreignKeys: normalizedForeignKeys,
219
+ };
220
+ } else {
221
+ normalizedTables[tableName] = tableObj;
222
+ }
223
+ }
224
+
225
+ normalizedStorage = {
226
+ ...storage,
227
+ tables: normalizedTables,
228
+ };
229
+ }
230
+ }
231
+
232
+ let normalizedModels = contractObj['models'];
233
+ if (normalizedModels && typeof normalizedModels === 'object' && normalizedModels !== null) {
234
+ const models = normalizedModels as Record<string, unknown>;
235
+ const normalizedModelsObj: Record<string, unknown> = {};
236
+ for (const [modelName, model] of Object.entries(models)) {
237
+ const modelObj = model as Record<string, unknown>;
238
+ normalizedModelsObj[modelName] = {
239
+ ...modelObj,
240
+ relations: modelObj['relations'] ?? {},
241
+ };
242
+ }
243
+ normalizedModels = normalizedModelsObj;
244
+ }
245
+
246
+ return {
247
+ ...contractObj,
248
+ models: normalizedModels,
249
+ relations: contractObj['relations'] ?? {},
250
+ storage: normalizedStorage,
251
+ extensionPacks: contractObj['extensionPacks'] ?? {},
252
+ capabilities: contractObj['capabilities'] ?? {},
253
+ meta: contractObj['meta'] ?? {},
254
+ sources: contractObj['sources'] ?? {},
255
+ } as SqlContract<SqlStorage>;
256
+ }
257
+
258
+ export function validateContract<TContract extends SqlContract<SqlStorage>>(
259
+ value: unknown,
260
+ ): TContract {
261
+ const normalized = normalizeContract(value);
262
+ const structurallyValid = validateSqlContract<SqlContract<SqlStorage>>(normalized);
263
+ validateContractLogic(structurallyValid);
264
+
265
+ const semanticErrors = validateStorageSemantics(structurallyValid.storage);
266
+ if (semanticErrors.length > 0) {
267
+ throw new Error(`Contract semantic validation failed: ${semanticErrors.join('; ')}`);
268
+ }
269
+
270
+ const constructed = constructContract<TContract>(structurallyValid);
271
+ return decodeContractDefaults(constructed) as TContract;
272
+ }