@prisma-next/sql-contract-ts 0.3.0-dev.13 → 0.3.0-dev.130

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/contract.ts CHANGED
@@ -1,7 +1,4 @@
1
1
  import type {
2
- ForeignKey,
3
- ForeignKeyReferences,
4
- Index,
5
2
  ModelDefinition,
6
3
  ModelField,
7
4
  ModelStorage,
@@ -9,10 +6,15 @@ import type {
9
6
  SqlContract,
10
7
  SqlMappings,
11
8
  SqlStorage,
12
- StorageColumn,
13
- StorageTable,
9
+ StorageTypeInstance,
14
10
  UniqueConstraint,
15
11
  } from '@prisma-next/sql-contract/types';
12
+ import { decodeContractDefaults } from '@prisma-next/sql-contract/validate';
13
+ import {
14
+ ColumnDefaultSchema,
15
+ ForeignKeySchema,
16
+ IndexSchema,
17
+ } from '@prisma-next/sql-contract/validators';
16
18
  import { type } from 'arktype';
17
19
  import type { O } from 'ts-toolbelt';
18
20
 
@@ -20,39 +22,33 @@ import type { O } from 'ts-toolbelt';
20
22
  * Structural validation schema for SqlContract using Arktype.
21
23
  * This validates the shape and types of the contract structure.
22
24
  */
23
- const StorageColumnSchema = type.declare<StorageColumn>().type({
25
+
26
+ const StorageColumnSchema = type({
24
27
  nativeType: 'string',
25
28
  codecId: 'string',
26
29
  nullable: 'boolean',
30
+ 'typeParams?': 'Record<string, unknown>',
31
+ 'typeRef?': 'string',
32
+ 'default?': ColumnDefaultSchema,
27
33
  });
28
34
 
29
- const PrimaryKeySchema = type.declare<PrimaryKey>().type({
30
- columns: type.string.array().readonly(),
31
- 'name?': 'string',
32
- });
33
-
34
- const UniqueConstraintSchema = type.declare<UniqueConstraint>().type({
35
- columns: type.string.array().readonly(),
36
- 'name?': 'string',
35
+ const StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({
36
+ codecId: 'string',
37
+ nativeType: 'string',
38
+ typeParams: 'Record<string, unknown>',
37
39
  });
38
40
 
39
- const IndexSchema = type.declare<Index>().type({
41
+ const PrimaryKeySchema = type.declare<PrimaryKey>().type({
40
42
  columns: type.string.array().readonly(),
41
43
  'name?': 'string',
42
44
  });
43
45
 
44
- const ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({
45
- table: 'string',
46
- columns: type.string.array().readonly(),
47
- });
48
-
49
- const ForeignKeySchema = type.declare<ForeignKey>().type({
46
+ const UniqueConstraintSchema = type.declare<UniqueConstraint>().type({
50
47
  columns: type.string.array().readonly(),
51
- references: ForeignKeyReferencesSchema,
52
48
  'name?': 'string',
53
49
  });
54
50
 
55
- const StorageTableSchema = type.declare<StorageTable>().type({
51
+ const StorageTableSchema = type({
56
52
  columns: type({ '[string]': StorageColumnSchema }),
57
53
  'primaryKey?': PrimaryKeySchema,
58
54
  uniques: UniqueConstraintSchema.array().readonly(),
@@ -60,8 +56,9 @@ const StorageTableSchema = type.declare<StorageTable>().type({
60
56
  foreignKeys: ForeignKeySchema.array().readonly(),
61
57
  });
62
58
 
63
- const StorageSchema = type.declare<SqlStorage>().type({
59
+ const StorageSchema = type({
64
60
  tables: type({ '[string]': StorageTableSchema }),
61
+ 'types?': type({ '[string]': StorageTypeInstanceSchema }),
65
62
  });
66
63
 
67
64
  const ModelFieldSchema = type.declare<ModelField>().type({
@@ -76,6 +73,32 @@ const ModelSchema = type.declare<ModelDefinition>().type({
76
73
  storage: ModelStorageSchema,
77
74
  fields: type({ '[string]': ModelFieldSchema }),
78
75
  relations: type({ '[string]': 'unknown' }),
76
+ 'owner?': 'string',
77
+ });
78
+
79
+ const GeneratorIdSchema = type('string').narrow((value, ctx) => {
80
+ return /^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(value) ? true : ctx.mustBe('a flat generator id');
81
+ });
82
+
83
+ const ExecutionMutationDefaultValueSchema = type({
84
+ kind: "'generator'",
85
+ id: GeneratorIdSchema,
86
+ 'params?': 'Record<string, unknown>',
87
+ });
88
+
89
+ const ExecutionMutationDefaultSchema = type({
90
+ ref: {
91
+ table: 'string',
92
+ column: 'string',
93
+ },
94
+ 'onCreate?': ExecutionMutationDefaultValueSchema,
95
+ 'onUpdate?': ExecutionMutationDefaultValueSchema,
96
+ });
97
+
98
+ const ExecutionSchema = type({
99
+ mutations: {
100
+ defaults: ExecutionMutationDefaultSchema.array().readonly(),
101
+ },
79
102
  });
80
103
 
81
104
  /**
@@ -86,14 +109,17 @@ const SqlContractSchema = type({
86
109
  'schemaVersion?': "'1'",
87
110
  target: 'string',
88
111
  targetFamily: "'sql'",
89
- coreHash: 'string',
112
+ storageHash: 'string',
113
+ 'executionHash?': 'string',
90
114
  'profileHash?': 'string',
91
115
  'capabilities?': 'Record<string, Record<string, boolean>>',
92
116
  'extensionPacks?': 'Record<string, unknown>',
93
117
  'meta?': 'Record<string, unknown>',
94
118
  'sources?': 'Record<string, unknown>',
119
+ 'roots?': 'Record<string, string>',
95
120
  models: type({ '[string]': ModelSchema }),
96
121
  storage: StorageSchema,
122
+ 'execution?': ExecutionSchema,
97
123
  });
98
124
 
99
125
  /**
@@ -172,14 +198,11 @@ export function computeMappings(
172
198
  fieldToColumn[modelName] = modelFieldToColumn;
173
199
  }
174
200
 
175
- // Preserve existing mappings if provided, otherwise use computed ones
176
201
  return {
177
202
  modelToTable: existingMappings?.modelToTable ?? modelToTable,
178
203
  tableToModel: existingMappings?.tableToModel ?? tableToModel,
179
204
  fieldToColumn: existingMappings?.fieldToColumn ?? fieldToColumn,
180
205
  columnToField: existingMappings?.columnToField ?? columnToField,
181
- codecTypes: existingMappings?.codecTypes ?? {},
182
- operationTypes: existingMappings?.operationTypes ?? {},
183
206
  };
184
207
  }
185
208
 
@@ -188,6 +211,9 @@ export function computeMappings(
188
211
  * This checks that references (e.g., foreign keys, primary keys, uniques) point to storage objects that already exist.
189
212
  * Structural validation is expected to have already completed before this helper runs.
190
213
  *
214
+ * Rule: keep this focused on structural consistency only; capability/feature
215
+ * gating (e.g., defaults.*) belongs in migration/runtime verification, not here.
216
+ *
191
217
  * @param structurallyValidatedContract - The contract whose structure has already been validated
192
218
  * @throws Error if logical validation fails
193
219
  */
@@ -195,9 +221,70 @@ function validateContractLogic(structurallyValidatedContract: SqlContract<SqlSto
195
221
  const { storage, models } = structurallyValidatedContract;
196
222
  const tableNames = new Set(Object.keys(storage.tables));
197
223
 
224
+ // Validate storage.types if present
225
+ if (storage.types) {
226
+ for (const [typeName, typeInstance] of Object.entries(storage.types)) {
227
+ // Validate typeParams is not an array (arrays are objects in JS but not valid here)
228
+ if (Array.isArray(typeInstance.typeParams)) {
229
+ throw new Error(
230
+ `Type instance "${typeName}" has invalid typeParams: must be a plain object, not an array`,
231
+ );
232
+ }
233
+ }
234
+ }
235
+
236
+ // Validate columns in all tables
237
+ for (const [tableName, table] of Object.entries(storage.tables)) {
238
+ for (const [columnName, column] of Object.entries(table.columns)) {
239
+ // Validate typeParams and typeRef are mutually exclusive
240
+ if (column.typeParams !== undefined && column.typeRef !== undefined) {
241
+ throw new Error(
242
+ `Column "${columnName}" in table "${tableName}" has both typeParams and typeRef; these are mutually exclusive`,
243
+ );
244
+ }
245
+
246
+ // Validate typeParams is not an array (arrays are objects in JS but not valid here)
247
+ if (column.typeParams !== undefined && Array.isArray(column.typeParams)) {
248
+ throw new Error(
249
+ `Column "${columnName}" in table "${tableName}" has invalid typeParams: must be a plain object, not an array`,
250
+ );
251
+ }
252
+
253
+ // Validate NOT NULL columns do not have literal null defaults
254
+ if (!column.nullable && column.default?.kind === 'literal' && column.default.value === null) {
255
+ throw new Error(
256
+ `Table "${tableName}" column "${columnName}" is NOT NULL but has a literal null default`,
257
+ );
258
+ }
259
+
260
+ // Validate typeRef points to an existing storage.types key and matches codecId/nativeType
261
+ if (column.typeRef !== undefined) {
262
+ const referencedType = storage.types?.[column.typeRef];
263
+ if (!referencedType) {
264
+ throw new Error(
265
+ `Column "${columnName}" in table "${tableName}" references non-existent type instance "${column.typeRef}" (not found in storage.types)`,
266
+ );
267
+ }
268
+
269
+ if (column.codecId !== referencedType.codecId) {
270
+ throw new Error(
271
+ `Column "${columnName}" in table "${tableName}" has codecId "${column.codecId}" but references type instance "${column.typeRef}" with codecId "${referencedType.codecId}"`,
272
+ );
273
+ }
274
+
275
+ if (column.nativeType !== referencedType.nativeType) {
276
+ throw new Error(
277
+ `Column "${columnName}" in table "${tableName}" has nativeType "${column.nativeType}" but references type instance "${column.typeRef}" with nativeType "${referencedType.nativeType}"`,
278
+ );
279
+ }
280
+ }
281
+ }
282
+ }
283
+
198
284
  // Validate models
199
285
  for (const [modelName, modelUnknown] of Object.entries(models)) {
200
- const model = modelUnknown as ModelDefinition;
286
+ // normalizeContract() ensures models have both domain (DomainModel) and SQL-specific (ModelDefinition) properties
287
+ const model = modelUnknown as unknown as ModelDefinition;
201
288
  // Validate model has storage.table
202
289
  if (!model.storage?.table) {
203
290
  /* c8 ignore next */
@@ -382,82 +469,8 @@ function validateContractLogic(structurallyValidatedContract: SqlContract<SqlSto
382
469
  }
383
470
  }
384
471
 
385
- export function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
386
- const contractObj = contract as Record<string, unknown>;
387
-
388
- // Only normalize if storage exists (validation will catch if it's missing)
389
- let normalizedStorage = contractObj['storage'];
390
- if (normalizedStorage && typeof normalizedStorage === 'object' && normalizedStorage !== null) {
391
- const storage = normalizedStorage as Record<string, unknown>;
392
- const tables = storage['tables'] as Record<string, unknown> | undefined;
393
-
394
- if (tables) {
395
- // Normalize storage tables
396
- const normalizedTables: Record<string, unknown> = {};
397
- for (const [tableName, table] of Object.entries(tables)) {
398
- const tableObj = table as Record<string, unknown>;
399
- const columns = tableObj['columns'] as Record<string, unknown> | undefined;
400
-
401
- if (columns) {
402
- // Normalize columns: add nullable: false if missing
403
- const normalizedColumns: Record<string, unknown> = {};
404
- for (const [columnName, column] of Object.entries(columns)) {
405
- const columnObj = column as Record<string, unknown>;
406
- const normalizedColumn: Record<string, unknown> = {
407
- ...columnObj,
408
- nullable: columnObj['nullable'] ?? false,
409
- };
410
-
411
- normalizedColumns[columnName] = normalizedColumn;
412
- }
413
-
414
- // Normalize table arrays: add empty arrays if missing
415
- normalizedTables[tableName] = {
416
- ...tableObj,
417
- columns: normalizedColumns,
418
- uniques: tableObj['uniques'] ?? [],
419
- indexes: tableObj['indexes'] ?? [],
420
- foreignKeys: tableObj['foreignKeys'] ?? [],
421
- };
422
- } else {
423
- normalizedTables[tableName] = tableObj;
424
- }
425
- }
426
-
427
- normalizedStorage = {
428
- ...storage,
429
- tables: normalizedTables,
430
- };
431
- }
432
- }
433
-
434
- // Only normalize if models exists (validation will catch if it's missing)
435
- let normalizedModels = contractObj['models'];
436
- if (normalizedModels && typeof normalizedModels === 'object' && normalizedModels !== null) {
437
- const models = normalizedModels as Record<string, unknown>;
438
- const normalizedModelsObj: Record<string, unknown> = {};
439
- for (const [modelName, model] of Object.entries(models)) {
440
- const modelObj = model as Record<string, unknown>;
441
- normalizedModelsObj[modelName] = {
442
- ...modelObj,
443
- relations: modelObj['relations'] ?? {},
444
- };
445
- }
446
- normalizedModels = normalizedModelsObj;
447
- }
448
-
449
- // Normalize top-level fields: add empty objects if missing
450
- return {
451
- ...contractObj,
452
- models: normalizedModels,
453
- relations: contractObj['relations'] ?? {},
454
- storage: normalizedStorage,
455
- extensionPacks: contractObj['extensionPacks'] ?? {},
456
- capabilities: contractObj['capabilities'] ?? {},
457
- meta: contractObj['meta'] ?? {},
458
- sources: contractObj['sources'] ?? {},
459
- } as SqlContract<SqlStorage>;
460
- }
472
+ import { normalizeContract } from '@prisma-next/sql-contract/validate';
473
+ export { normalizeContract };
461
474
 
462
475
  /**
463
476
  * Validates that a JSON import conforms to the SqlContract structure
@@ -529,5 +542,5 @@ export function validateContract<TContract extends SqlContract<SqlStorage>>(
529
542
 
530
543
  // Type assertion: The caller provides the strict type via TContract.
531
544
  // We validate the structure matches, but the precise types come from contract.d.ts
532
- return contractWithMappings as TContract;
545
+ return decodeContractDefaults(contractWithMappings) as TContract;
533
546
  }
@@ -0,0 +1,2 @@
1
+ export type { ContractConfig } from '@prisma-next/config/config-types';
2
+ export { typescriptContract } from '../config-types';
@@ -1,309 +0,0 @@
1
- // src/contract.ts
2
- import { type } from "arktype";
3
- var StorageColumnSchema = type.declare().type({
4
- nativeType: "string",
5
- codecId: "string",
6
- nullable: "boolean"
7
- });
8
- var PrimaryKeySchema = type.declare().type({
9
- columns: type.string.array().readonly(),
10
- "name?": "string"
11
- });
12
- var UniqueConstraintSchema = type.declare().type({
13
- columns: type.string.array().readonly(),
14
- "name?": "string"
15
- });
16
- var IndexSchema = type.declare().type({
17
- columns: type.string.array().readonly(),
18
- "name?": "string"
19
- });
20
- var ForeignKeyReferencesSchema = type.declare().type({
21
- table: "string",
22
- columns: type.string.array().readonly()
23
- });
24
- var ForeignKeySchema = type.declare().type({
25
- columns: type.string.array().readonly(),
26
- references: ForeignKeyReferencesSchema,
27
- "name?": "string"
28
- });
29
- var StorageTableSchema = type.declare().type({
30
- columns: type({ "[string]": StorageColumnSchema }),
31
- "primaryKey?": PrimaryKeySchema,
32
- uniques: UniqueConstraintSchema.array().readonly(),
33
- indexes: IndexSchema.array().readonly(),
34
- foreignKeys: ForeignKeySchema.array().readonly()
35
- });
36
- var StorageSchema = type.declare().type({
37
- tables: type({ "[string]": StorageTableSchema })
38
- });
39
- var ModelFieldSchema = type.declare().type({
40
- column: "string"
41
- });
42
- var ModelStorageSchema = type.declare().type({
43
- table: "string"
44
- });
45
- var ModelSchema = type.declare().type({
46
- storage: ModelStorageSchema,
47
- fields: type({ "[string]": ModelFieldSchema }),
48
- relations: type({ "[string]": "unknown" })
49
- });
50
- var SqlContractSchema = type({
51
- "schemaVersion?": "'1'",
52
- target: "string",
53
- targetFamily: "'sql'",
54
- coreHash: "string",
55
- "profileHash?": "string",
56
- "capabilities?": "Record<string, Record<string, boolean>>",
57
- "extensionPacks?": "Record<string, unknown>",
58
- "meta?": "Record<string, unknown>",
59
- "sources?": "Record<string, unknown>",
60
- models: type({ "[string]": ModelSchema }),
61
- storage: StorageSchema
62
- });
63
- function validateContractStructure(value) {
64
- const rawValue = value;
65
- if (rawValue.targetFamily !== void 0 && rawValue.targetFamily !== "sql") {
66
- throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);
67
- }
68
- const contractResult = SqlContractSchema(value);
69
- if (contractResult instanceof type.errors) {
70
- const messages = contractResult.map((p) => p.message).join("; ");
71
- throw new Error(`Contract structural validation failed: ${messages}`);
72
- }
73
- return contractResult;
74
- }
75
- function computeMappings(models, _storage, existingMappings) {
76
- const modelToTable = {};
77
- const tableToModel = {};
78
- const fieldToColumn = {};
79
- const columnToField = {};
80
- for (const [modelName, model] of Object.entries(models)) {
81
- const tableName = model.storage.table;
82
- modelToTable[modelName] = tableName;
83
- tableToModel[tableName] = modelName;
84
- const modelFieldToColumn = {};
85
- for (const [fieldName, field] of Object.entries(model.fields)) {
86
- const columnName = field.column;
87
- modelFieldToColumn[fieldName] = columnName;
88
- if (!columnToField[tableName]) {
89
- columnToField[tableName] = {};
90
- }
91
- columnToField[tableName][columnName] = fieldName;
92
- }
93
- fieldToColumn[modelName] = modelFieldToColumn;
94
- }
95
- return {
96
- modelToTable: existingMappings?.modelToTable ?? modelToTable,
97
- tableToModel: existingMappings?.tableToModel ?? tableToModel,
98
- fieldToColumn: existingMappings?.fieldToColumn ?? fieldToColumn,
99
- columnToField: existingMappings?.columnToField ?? columnToField,
100
- codecTypes: existingMappings?.codecTypes ?? {},
101
- operationTypes: existingMappings?.operationTypes ?? {}
102
- };
103
- }
104
- function validateContractLogic(structurallyValidatedContract) {
105
- const { storage, models } = structurallyValidatedContract;
106
- const tableNames = new Set(Object.keys(storage.tables));
107
- for (const [modelName, modelUnknown] of Object.entries(models)) {
108
- const model = modelUnknown;
109
- if (!model.storage?.table) {
110
- throw new Error(`Model "${modelName}" is missing storage.table`);
111
- }
112
- const tableName = model.storage.table;
113
- if (!tableNames.has(tableName)) {
114
- throw new Error(`Model "${modelName}" references non-existent table "${tableName}"`);
115
- }
116
- const table = storage.tables[tableName];
117
- if (!table) {
118
- throw new Error(`Model "${modelName}" references non-existent table "${tableName}"`);
119
- }
120
- if (!table.primaryKey) {
121
- throw new Error(`Model "${modelName}" table "${tableName}" is missing a primary key`);
122
- }
123
- const columnNames = new Set(Object.keys(table.columns));
124
- if (!model.fields) {
125
- throw new Error(`Model "${modelName}" is missing fields`);
126
- }
127
- for (const [fieldName, fieldUnknown] of Object.entries(model.fields)) {
128
- const field = fieldUnknown;
129
- if (!field.column) {
130
- throw new Error(`Model "${modelName}" field "${fieldName}" is missing column property`);
131
- }
132
- if (!columnNames.has(field.column)) {
133
- throw new Error(
134
- `Model "${modelName}" field "${fieldName}" references non-existent column "${field.column}" in table "${tableName}"`
135
- );
136
- }
137
- }
138
- if (model.relations) {
139
- for (const [relationName, relation] of Object.entries(model.relations)) {
140
- if (typeof relation === "object" && relation !== null && "on" in relation && "to" in relation) {
141
- const on = relation.on;
142
- const cardinality = relation.cardinality;
143
- if (on.parentCols && on.childCols) {
144
- if (cardinality === "1:N") {
145
- continue;
146
- }
147
- const hasMatchingFk = table.foreignKeys?.some((fk) => {
148
- return fk.columns.length === on.childCols?.length && fk.columns.every((col, i) => col === on.childCols?.[i]) && fk.references.table && fk.references.columns.length === on.parentCols?.length && fk.references.columns.every((col, i) => col === on.parentCols?.[i]);
149
- });
150
- if (!hasMatchingFk) {
151
- throw new Error(
152
- `Model "${modelName}" relation "${relationName}" does not have a corresponding foreign key in table "${tableName}"`
153
- );
154
- }
155
- }
156
- }
157
- }
158
- }
159
- }
160
- for (const [tableName, table] of Object.entries(storage.tables)) {
161
- const columnNames = new Set(Object.keys(table.columns));
162
- if (table.primaryKey) {
163
- for (const colName of table.primaryKey.columns) {
164
- if (!columnNames.has(colName)) {
165
- throw new Error(
166
- `Table "${tableName}" primaryKey references non-existent column "${colName}"`
167
- );
168
- }
169
- }
170
- }
171
- for (const unique of table.uniques) {
172
- for (const colName of unique.columns) {
173
- if (!columnNames.has(colName)) {
174
- throw new Error(
175
- `Table "${tableName}" unique constraint references non-existent column "${colName}"`
176
- );
177
- }
178
- }
179
- }
180
- for (const index of table.indexes) {
181
- for (const colName of index.columns) {
182
- if (!columnNames.has(colName)) {
183
- throw new Error(`Table "${tableName}" index references non-existent column "${colName}"`);
184
- }
185
- }
186
- }
187
- for (const fk of table.foreignKeys) {
188
- for (const colName of fk.columns) {
189
- if (!columnNames.has(colName)) {
190
- throw new Error(
191
- `Table "${tableName}" foreignKey references non-existent column "${colName}"`
192
- );
193
- }
194
- }
195
- if (!tableNames.has(fk.references.table)) {
196
- throw new Error(
197
- `Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`
198
- );
199
- }
200
- const referencedTable = storage.tables[fk.references.table];
201
- if (!referencedTable) {
202
- throw new Error(
203
- `Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`
204
- );
205
- }
206
- const referencedColumnNames = new Set(Object.keys(referencedTable.columns));
207
- for (const colName of fk.references.columns) {
208
- if (!referencedColumnNames.has(colName)) {
209
- throw new Error(
210
- `Table "${tableName}" foreignKey references non-existent column "${colName}" in table "${fk.references.table}"`
211
- );
212
- }
213
- }
214
- if (fk.columns.length !== fk.references.columns.length) {
215
- throw new Error(
216
- `Table "${tableName}" foreignKey column count (${fk.columns.length}) does not match referenced column count (${fk.references.columns.length})`
217
- );
218
- }
219
- }
220
- }
221
- }
222
- function normalizeContract(contract) {
223
- const contractObj = contract;
224
- let normalizedStorage = contractObj["storage"];
225
- if (normalizedStorage && typeof normalizedStorage === "object" && normalizedStorage !== null) {
226
- const storage = normalizedStorage;
227
- const tables = storage["tables"];
228
- if (tables) {
229
- const normalizedTables = {};
230
- for (const [tableName, table] of Object.entries(tables)) {
231
- const tableObj = table;
232
- const columns = tableObj["columns"];
233
- if (columns) {
234
- const normalizedColumns = {};
235
- for (const [columnName, column] of Object.entries(columns)) {
236
- const columnObj = column;
237
- const normalizedColumn = {
238
- ...columnObj,
239
- nullable: columnObj["nullable"] ?? false
240
- };
241
- normalizedColumns[columnName] = normalizedColumn;
242
- }
243
- normalizedTables[tableName] = {
244
- ...tableObj,
245
- columns: normalizedColumns,
246
- uniques: tableObj["uniques"] ?? [],
247
- indexes: tableObj["indexes"] ?? [],
248
- foreignKeys: tableObj["foreignKeys"] ?? []
249
- };
250
- } else {
251
- normalizedTables[tableName] = tableObj;
252
- }
253
- }
254
- normalizedStorage = {
255
- ...storage,
256
- tables: normalizedTables
257
- };
258
- }
259
- }
260
- let normalizedModels = contractObj["models"];
261
- if (normalizedModels && typeof normalizedModels === "object" && normalizedModels !== null) {
262
- const models = normalizedModels;
263
- const normalizedModelsObj = {};
264
- for (const [modelName, model] of Object.entries(models)) {
265
- const modelObj = model;
266
- normalizedModelsObj[modelName] = {
267
- ...modelObj,
268
- relations: modelObj["relations"] ?? {}
269
- };
270
- }
271
- normalizedModels = normalizedModelsObj;
272
- }
273
- return {
274
- ...contractObj,
275
- models: normalizedModels,
276
- relations: contractObj["relations"] ?? {},
277
- storage: normalizedStorage,
278
- extensionPacks: contractObj["extensionPacks"] ?? {},
279
- capabilities: contractObj["capabilities"] ?? {},
280
- meta: contractObj["meta"] ?? {},
281
- sources: contractObj["sources"] ?? {}
282
- };
283
- }
284
- function validateContract(value) {
285
- const normalized = normalizeContract(value);
286
- const structurallyValid = validateContractStructure(normalized);
287
- const contractForValidation = structurallyValid;
288
- validateContractLogic(contractForValidation);
289
- const existingMappings = contractForValidation.mappings;
290
- const mappings = computeMappings(
291
- contractForValidation.models,
292
- contractForValidation.storage,
293
- existingMappings
294
- );
295
- const contractWithMappings = {
296
- ...structurallyValid,
297
- models: contractForValidation.models,
298
- relations: contractForValidation.relations,
299
- storage: contractForValidation.storage,
300
- mappings
301
- };
302
- return contractWithMappings;
303
- }
304
-
305
- export {
306
- computeMappings,
307
- validateContract
308
- };
309
- //# sourceMappingURL=chunk-SEOX3AAQ.js.map