@prisma-next/sql-contract 0.3.0-dev.36 → 0.3.0-dev.38
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/README.md +16 -2
- package/dist/factories.d.mts +6 -2
- package/dist/factories.d.mts.map +1 -1
- package/dist/factories.mjs +8 -2
- package/dist/factories.mjs.map +1 -1
- package/dist/{types-D3AOQgwo.d.mts → types-DTFobApb.d.mts} +22 -2
- package/dist/types-DTFobApb.d.mts.map +1 -0
- package/dist/types-kacOgEya.mjs +17 -0
- package/dist/types-kacOgEya.mjs.map +1 -0
- package/dist/types.d.mts +2 -2
- package/dist/types.mjs +3 -1
- package/dist/validate.d.mts +3 -2
- package/dist/validate.d.mts.map +1 -1
- package/dist/validate.mjs +11 -3
- package/dist/validate.mjs.map +1 -1
- package/dist/{validators-DY_87pfS.mjs → validators-Dfw5_HSi.mjs} +4 -2
- package/dist/validators-Dfw5_HSi.mjs.map +1 -0
- package/dist/validators.d.mts +1 -1
- package/dist/validators.d.mts.map +1 -1
- package/dist/validators.mjs +1 -1
- package/package.json +3 -3
- package/src/exports/types.ts +2 -0
- package/src/exports/validate.ts +1 -1
- package/src/factories.ts +4 -2
- package/src/types.ts +21 -0
- package/src/validate.ts +13 -2
- package/src/validators.ts +2 -0
- package/dist/types-D3AOQgwo.d.mts.map +0 -1
- package/dist/validators-DY_87pfS.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ This package provides TypeScript type definitions, Arktype validators, and facto
|
|
|
8
8
|
|
|
9
9
|
## Responsibilities
|
|
10
10
|
|
|
11
|
-
- **SQL Contract Types**: Defines SQL-specific contract types (`SqlContract`, `SqlStorage`, `StorageTable`, `ModelDefinition`, `SqlMappings`) that extend framework-level contract types
|
|
11
|
+
- **SQL Contract Types**: Defines SQL-specific contract types (`SqlContract`, `SqlStorage`, `StorageTable`, `ModelDefinition`, `SqlMappings`, `ForeignKeysConfig`) that extend framework-level contract types
|
|
12
12
|
- **Contract Validation**: Provides Arktype-based structural validators and the shared `validateContract` entrypoint for runtime-safe contract validation
|
|
13
13
|
- **IR Factories**: Provides pure factory functions for constructing contract IR structures in tests and authoring
|
|
14
14
|
- **Shared Plane Access**: Enables both migration-plane and runtime-plane packages to import SQL contract types without violating plane boundaries
|
|
@@ -25,7 +25,7 @@ Both `nativeType` and `codecId` are required to ensure contracts are consumable
|
|
|
25
25
|
|
|
26
26
|
## Package Contents
|
|
27
27
|
|
|
28
|
-
- **TypeScript Types**: Type definitions for `SqlContract`, `SqlStorage`, `StorageTable`, `ModelDefinition`, and related types
|
|
28
|
+
- **TypeScript Types**: Type definitions for `SqlContract`, `SqlStorage`, `StorageTable`, `ModelDefinition`, `ForeignKeysConfig`, and related types
|
|
29
29
|
- **Validators**: Arktype-based validators for structural validation of contracts, storage, and models
|
|
30
30
|
- **Factories**: Pure factory functions for constructing contract IR structures in tests and authoring
|
|
31
31
|
|
|
@@ -41,9 +41,23 @@ import type {
|
|
|
41
41
|
SqlStorage,
|
|
42
42
|
StorageTable,
|
|
43
43
|
ModelDefinition,
|
|
44
|
+
ForeignKeysConfig,
|
|
44
45
|
} from '@prisma-next/sql-contract/types';
|
|
45
46
|
```
|
|
46
47
|
|
|
48
|
+
### Foreign Keys Configuration
|
|
49
|
+
|
|
50
|
+
`SqlContract` includes an optional `foreignKeys` field of type `ForeignKeysConfig` that controls whether the planner emits foreign key constraints and their backing indexes:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
type ForeignKeysConfig = {
|
|
54
|
+
readonly constraints: boolean; // Emit FOREIGN KEY constraints
|
|
55
|
+
readonly indexes: boolean; // Emit FK-backing indexes
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
When omitted, defaults to `{ constraints: true, indexes: true }`. See [ADR 161](../../../docs/architecture%20docs/adrs/ADR%20161%20-%20Explicit%20foreign%20key%20constraint%20and%20index%20configuration.md) for design rationale.
|
|
60
|
+
|
|
47
61
|
### Validators
|
|
48
62
|
|
|
49
63
|
Validate contract structures using Arktype validators:
|
package/dist/factories.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as ForeignKey, c as ModelDefinition, d as PrimaryKey, f as SqlContract, g as StorageTable, h as StorageColumn, l as ModelField, m as SqlStorage, p as SqlMappings, s as Index, v as UniqueConstraint } from "./types-DTFobApb.mjs";
|
|
2
2
|
import { ExecutionHashBase, ProfileHashBase, StorageHashBase } from "@prisma-next/contract/types";
|
|
3
3
|
|
|
4
4
|
//#region src/factories.d.ts
|
|
@@ -15,7 +15,11 @@ declare function col(nativeType: string, codecId: string, nullable?: boolean): S
|
|
|
15
15
|
declare function pk(...columns: readonly string[]): PrimaryKey;
|
|
16
16
|
declare function unique(...columns: readonly string[]): UniqueConstraint;
|
|
17
17
|
declare function index(...columns: readonly string[]): Index;
|
|
18
|
-
declare function fk(columns: readonly string[], refTable: string, refColumns: readonly string[],
|
|
18
|
+
declare function fk(columns: readonly string[], refTable: string, refColumns: readonly string[], opts?: {
|
|
19
|
+
name?: string;
|
|
20
|
+
constraint?: boolean;
|
|
21
|
+
index?: boolean;
|
|
22
|
+
}): ForeignKey;
|
|
19
23
|
declare function table(columns: Record<string, StorageColumn>, opts?: {
|
|
20
24
|
pk?: PrimaryKey;
|
|
21
25
|
uniques?: readonly UniqueConstraint[];
|
package/dist/factories.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factories.d.mts","names":[],"sources":["../src/factories.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"factories.d.mts","names":[],"sources":["../src/factories.ts"],"sourcesContent":[],"mappings":";;;;;;;AA8BA;AAQA;AAMA;AAMA;AAMA;AAkBA;AAC0B,iBA7CV,GAAA,CA6CU,UAAA,EAAA,MAAA,EAAA,OAAA,EAAA,MAAA,EAAA,QAAA,CAAA,EAAA,OAAA,CAAA,EA7CkD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iBANsC,CAAA,MAAA,CAAA,GAMV,iBANU,CAAA,MAAA,CAAA,EAAA,qBAOxC,eAPwC,CAAA,MAAA,CAAA,GAOd,eAPc,CAAA,MAAA,CAAA,CAAA,CAAA,IAAA,EAAA;EAAU,MAAA,EAAA,MAAA;EAIzD,WAAQ,EAMT,YANS;EACD,aAAA,CAAA,EAML,cANK;EAA0B,OAAA,EAOtC,UAPsC;EACxB,MAAA,CAAA,EAOd,MAPc,CAAA,MAAA,EAOC,eAPD,CAAA;EAA4B,SAAA,CAAA,EAQvC,MARuC,CAAA,MAAA,EAAA,OAAA,CAAA;EAC9B,QAAA,CAAA,EAQV,OARU,CAQF,WARE,CAAA;EAA0B,aAAA,CAAA,EAAA,GAAA;EAGlC,YAAA,CAAA,EAAA,KAAA;EACG,WAAA,CAAA,EAOF,YAPE;EACP,YAAA,CAAA,EAOM,MAPN,CAAA,MAAA,EAOqB,MAPrB,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EACe,cAAA,CAAA,EAOP,MAPO,CAAA,MAAA,EAAA,OAAA,CAAA;EAAf,IAAA,CAAA,EAQF,MARE,CAAA,MAAA,EAAA,OAAA,CAAA;EACG,OAAA,CAAA,EAQF,MARE,CAAA,MAAA,EAAA,OAAA,CAAA;CACO,CAAA,EAQjB,WARiB,CASnB,UATmB,EAUnB,MAVmB,CAAA,MAAA,EAAA,OAAA,CAAA,EAWnB,MAXmB,CAAA,MAAA,EAAA,OAAA,CAAA,EAYnB,WAZmB,EAanB,YAbmB,EAcnB,cAdmB,EAenB,YAfmB,CAAA"}
|
package/dist/factories.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { r as applyFkDefaults } from "./types-kacOgEya.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/factories.ts
|
|
2
4
|
/**
|
|
3
5
|
* Creates a StorageColumn with nativeType and codecId.
|
|
@@ -23,14 +25,18 @@ function unique(...columns) {
|
|
|
23
25
|
function index(...columns) {
|
|
24
26
|
return { columns };
|
|
25
27
|
}
|
|
26
|
-
function fk(columns, refTable, refColumns,
|
|
28
|
+
function fk(columns, refTable, refColumns, opts) {
|
|
27
29
|
return {
|
|
28
30
|
columns,
|
|
29
31
|
references: {
|
|
30
32
|
table: refTable,
|
|
31
33
|
columns: refColumns
|
|
32
34
|
},
|
|
33
|
-
...
|
|
35
|
+
...applyFkDefaults({
|
|
36
|
+
constraint: opts?.constraint,
|
|
37
|
+
index: opts?.index
|
|
38
|
+
}),
|
|
39
|
+
...opts?.name !== void 0 && { name: opts.name }
|
|
34
40
|
};
|
|
35
41
|
}
|
|
36
42
|
function table(columns, opts) {
|
package/dist/factories.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factories.mjs","names":[],"sources":["../src/factories.ts"],"sourcesContent":["import type {\n ExecutionHashBase,\n ProfileHashBase,\n StorageHashBase,\n} from '@prisma-next/contract/types';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlMappings,\n SqlStorage,\n StorageColumn,\n StorageTable,\n UniqueConstraint,\n} from './types';\n\n/**\n * Creates a StorageColumn with nativeType and codecId.\n *\n * @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')\n * @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')\n * @param nullable - Whether the column is nullable (default: false)\n * @returns StorageColumn with nativeType and codecId\n */\nexport function col(nativeType: string, codecId: string, nullable = false): StorageColumn {\n return {\n nativeType,\n codecId,\n nullable,\n };\n}\n\nexport function pk(...columns: readonly string[]): PrimaryKey {\n return {\n columns,\n };\n}\n\nexport function unique(...columns: readonly string[]): UniqueConstraint {\n return {\n columns,\n };\n}\n\nexport function index(...columns: readonly string[]): Index {\n return {\n columns,\n };\n}\n\nexport function fk(\n columns: readonly string[],\n refTable: string,\n refColumns: readonly string[],\n name?: string,\n): ForeignKey {\n const references: ForeignKeyReferences = {\n table: refTable,\n columns: refColumns,\n };\n return {\n columns,\n references,\n ...(name !== undefined && { name }),\n };\n}\n\nexport function table(\n columns: Record<string, StorageColumn>,\n opts?: {\n pk?: PrimaryKey;\n uniques?: readonly UniqueConstraint[];\n indexes?: readonly Index[];\n fks?: readonly ForeignKey[];\n },\n): StorageTable {\n return {\n columns,\n ...(opts?.pk !== undefined && { primaryKey: opts.pk }),\n uniques: opts?.uniques ?? [],\n indexes: opts?.indexes ?? [],\n foreignKeys: opts?.fks ?? [],\n };\n}\n\nexport function model(\n table: string,\n fields: Record<string, ModelField>,\n relations: Record<string, unknown> = {},\n): ModelDefinition {\n const storage: ModelStorage = { table };\n return {\n storage,\n fields,\n relations,\n };\n}\n\nexport function storage(tables: Record<string, StorageTable>): SqlStorage {\n return { tables };\n}\n\nexport function contract<\n TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,\n TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,\n TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,\n>(opts: {\n target: string;\n storageHash: TStorageHash;\n executionHash?: TExecutionHash;\n storage: SqlStorage;\n models?: Record<string, ModelDefinition>;\n relations?: Record<string, unknown>;\n mappings?: Partial<SqlMappings>;\n schemaVersion?: '1';\n targetFamily?: 'sql';\n profileHash?: TProfileHash;\n capabilities?: Record<string, Record<string, boolean>>;\n extensionPacks?: Record<string, unknown>;\n meta?: Record<string, unknown>;\n sources?: Record<string, unknown>;\n}): SqlContract<\n SqlStorage,\n Record<string, unknown>,\n Record<string, unknown>,\n SqlMappings,\n TStorageHash,\n TExecutionHash,\n TProfileHash\n> {\n return {\n schemaVersion: opts.schemaVersion ?? '1',\n target: opts.target,\n targetFamily: opts.targetFamily ?? 'sql',\n storageHash: opts.storageHash,\n ...(opts.executionHash !== undefined && { executionHash: opts.executionHash }),\n storage: opts.storage,\n models: opts.models ?? {},\n relations: opts.relations ?? {},\n mappings: (opts.mappings ?? {}) as SqlMappings,\n ...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),\n ...(opts.capabilities !== undefined && { capabilities: opts.capabilities }),\n ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),\n ...(opts.meta !== undefined && { meta: opts.meta }),\n ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),\n } as SqlContract<\n SqlStorage,\n Record<string, unknown>,\n Record<string, unknown>,\n SqlMappings,\n TStorageHash,\n TExecutionHash,\n TProfileHash\n >;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"factories.mjs","names":[],"sources":["../src/factories.ts"],"sourcesContent":["import type {\n ExecutionHashBase,\n ProfileHashBase,\n StorageHashBase,\n} from '@prisma-next/contract/types';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlMappings,\n SqlStorage,\n StorageColumn,\n StorageTable,\n UniqueConstraint,\n} from './types';\nimport { applyFkDefaults } from './types';\n\n/**\n * Creates a StorageColumn with nativeType and codecId.\n *\n * @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')\n * @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')\n * @param nullable - Whether the column is nullable (default: false)\n * @returns StorageColumn with nativeType and codecId\n */\nexport function col(nativeType: string, codecId: string, nullable = false): StorageColumn {\n return {\n nativeType,\n codecId,\n nullable,\n };\n}\n\nexport function pk(...columns: readonly string[]): PrimaryKey {\n return {\n columns,\n };\n}\n\nexport function unique(...columns: readonly string[]): UniqueConstraint {\n return {\n columns,\n };\n}\n\nexport function index(...columns: readonly string[]): Index {\n return {\n columns,\n };\n}\n\nexport function fk(\n columns: readonly string[],\n refTable: string,\n refColumns: readonly string[],\n opts?: { name?: string; constraint?: boolean; index?: boolean },\n): ForeignKey {\n const references: ForeignKeyReferences = {\n table: refTable,\n columns: refColumns,\n };\n return {\n columns,\n references,\n ...applyFkDefaults({ constraint: opts?.constraint, index: opts?.index }),\n ...(opts?.name !== undefined && { name: opts.name }),\n };\n}\n\nexport function table(\n columns: Record<string, StorageColumn>,\n opts?: {\n pk?: PrimaryKey;\n uniques?: readonly UniqueConstraint[];\n indexes?: readonly Index[];\n fks?: readonly ForeignKey[];\n },\n): StorageTable {\n return {\n columns,\n ...(opts?.pk !== undefined && { primaryKey: opts.pk }),\n uniques: opts?.uniques ?? [],\n indexes: opts?.indexes ?? [],\n foreignKeys: opts?.fks ?? [],\n };\n}\n\nexport function model(\n table: string,\n fields: Record<string, ModelField>,\n relations: Record<string, unknown> = {},\n): ModelDefinition {\n const storage: ModelStorage = { table };\n return {\n storage,\n fields,\n relations,\n };\n}\n\nexport function storage(tables: Record<string, StorageTable>): SqlStorage {\n return { tables };\n}\n\nexport function contract<\n TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,\n TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,\n TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,\n>(opts: {\n target: string;\n storageHash: TStorageHash;\n executionHash?: TExecutionHash;\n storage: SqlStorage;\n models?: Record<string, ModelDefinition>;\n relations?: Record<string, unknown>;\n mappings?: Partial<SqlMappings>;\n schemaVersion?: '1';\n targetFamily?: 'sql';\n profileHash?: TProfileHash;\n capabilities?: Record<string, Record<string, boolean>>;\n extensionPacks?: Record<string, unknown>;\n meta?: Record<string, unknown>;\n sources?: Record<string, unknown>;\n}): SqlContract<\n SqlStorage,\n Record<string, unknown>,\n Record<string, unknown>,\n SqlMappings,\n TStorageHash,\n TExecutionHash,\n TProfileHash\n> {\n return {\n schemaVersion: opts.schemaVersion ?? '1',\n target: opts.target,\n targetFamily: opts.targetFamily ?? 'sql',\n storageHash: opts.storageHash,\n ...(opts.executionHash !== undefined && { executionHash: opts.executionHash }),\n storage: opts.storage,\n models: opts.models ?? {},\n relations: opts.relations ?? {},\n mappings: (opts.mappings ?? {}) as SqlMappings,\n ...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),\n ...(opts.capabilities !== undefined && { capabilities: opts.capabilities }),\n ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),\n ...(opts.meta !== undefined && { meta: opts.meta }),\n ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),\n } as SqlContract<\n SqlStorage,\n Record<string, unknown>,\n Record<string, unknown>,\n SqlMappings,\n TStorageHash,\n TExecutionHash,\n TProfileHash\n >;\n}\n"],"mappings":";;;;;;;;;;;AA8BA,SAAgB,IAAI,YAAoB,SAAiB,WAAW,OAAsB;AACxF,QAAO;EACL;EACA;EACA;EACD;;AAGH,SAAgB,GAAG,GAAG,SAAwC;AAC5D,QAAO,EACL,SACD;;AAGH,SAAgB,OAAO,GAAG,SAA8C;AACtE,QAAO,EACL,SACD;;AAGH,SAAgB,MAAM,GAAG,SAAmC;AAC1D,QAAO,EACL,SACD;;AAGH,SAAgB,GACd,SACA,UACA,YACA,MACY;AAKZ,QAAO;EACL;EACA,YANuC;GACvC,OAAO;GACP,SAAS;GACV;EAIC,GAAG,gBAAgB;GAAE,YAAY,MAAM;GAAY,OAAO,MAAM;GAAO,CAAC;EACxE,GAAI,MAAM,SAAS,UAAa,EAAE,MAAM,KAAK,MAAM;EACpD;;AAGH,SAAgB,MACd,SACA,MAMc;AACd,QAAO;EACL;EACA,GAAI,MAAM,OAAO,UAAa,EAAE,YAAY,KAAK,IAAI;EACrD,SAAS,MAAM,WAAW,EAAE;EAC5B,SAAS,MAAM,WAAW,EAAE;EAC5B,aAAa,MAAM,OAAO,EAAE;EAC7B;;AAGH,SAAgB,MACd,SACA,QACA,YAAqC,EAAE,EACtB;AAEjB,QAAO;EACL,SAF4B,EAAE,gBAAO;EAGrC;EACA;EACD;;AAGH,SAAgB,QAAQ,QAAkD;AACxE,QAAO,EAAE,QAAQ;;AAGnB,SAAgB,SAId,MAuBA;AACA,QAAO;EACL,eAAe,KAAK,iBAAiB;EACrC,QAAQ,KAAK;EACb,cAAc,KAAK,gBAAgB;EACnC,aAAa,KAAK;EAClB,GAAI,KAAK,kBAAkB,UAAa,EAAE,eAAe,KAAK,eAAe;EAC7E,SAAS,KAAK;EACd,QAAQ,KAAK,UAAU,EAAE;EACzB,WAAW,KAAK,aAAa,EAAE;EAC/B,UAAW,KAAK,YAAY,EAAE;EAC9B,GAAI,KAAK,gBAAgB,UAAa,EAAE,aAAa,KAAK,aAAa;EACvE,GAAI,KAAK,iBAAiB,UAAa,EAAE,cAAc,KAAK,cAAc;EAC1E,GAAI,KAAK,mBAAmB,UAAa,EAAE,gBAAgB,KAAK,gBAAgB;EAChF,GAAI,KAAK,SAAS,UAAa,EAAE,MAAM,KAAK,MAAM;EAClD,GAAI,KAAK,YAAY,UAAa,EAAE,SAAS,KAAK,SAAoC;EACvF"}
|
|
@@ -50,6 +50,10 @@ type ForeignKey = {
|
|
|
50
50
|
readonly columns: readonly string[];
|
|
51
51
|
readonly references: ForeignKeyReferences;
|
|
52
52
|
readonly name?: string;
|
|
53
|
+
/** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
|
|
54
|
+
readonly constraint: boolean;
|
|
55
|
+
/** Whether to emit a backing index for the FK columns. */
|
|
56
|
+
readonly index: boolean;
|
|
53
57
|
};
|
|
54
58
|
type StorageTable = {
|
|
55
59
|
readonly columns: Record<string, StorageColumn>;
|
|
@@ -102,6 +106,22 @@ type SqlMappings = {
|
|
|
102
106
|
}>;
|
|
103
107
|
readonly operationTypes: Record<string, Record<string, unknown>>;
|
|
104
108
|
};
|
|
109
|
+
declare const DEFAULT_FK_CONSTRAINT = true;
|
|
110
|
+
declare const DEFAULT_FK_INDEX = true;
|
|
111
|
+
/**
|
|
112
|
+
* Resolves foreign key `constraint` and `index` fields to their effective boolean values,
|
|
113
|
+
* falling back through optional override defaults, then to the global defaults.
|
|
114
|
+
*/
|
|
115
|
+
declare function applyFkDefaults(fk: {
|
|
116
|
+
constraint?: boolean | undefined;
|
|
117
|
+
index?: boolean | undefined;
|
|
118
|
+
}, overrideDefaults?: {
|
|
119
|
+
constraint?: boolean | undefined;
|
|
120
|
+
index?: boolean | undefined;
|
|
121
|
+
}): {
|
|
122
|
+
constraint: boolean;
|
|
123
|
+
index: boolean;
|
|
124
|
+
};
|
|
105
125
|
type SqlContract<S extends SqlStorage = SqlStorage, M extends Record<string, unknown> = Record<string, unknown>, R extends Record<string, unknown> = Record<string, unknown>, Map extends SqlMappings = SqlMappings, TStorageHash extends StorageHashBase<string> = StorageHashBase<string>, TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>, TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>> = ContractBase<TStorageHash, TExecutionHash, TProfileHash> & {
|
|
106
126
|
readonly targetFamily: string;
|
|
107
127
|
readonly storage: S;
|
|
@@ -113,5 +133,5 @@ type SqlContract<S extends SqlStorage = SqlStorage, M extends Record<string, unk
|
|
|
113
133
|
type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['codecTypes'];
|
|
114
134
|
type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['operationTypes'];
|
|
115
135
|
//#endregion
|
|
116
|
-
export {
|
|
117
|
-
//# sourceMappingURL=types-
|
|
136
|
+
export { StorageTypeInstance as _, ForeignKey as a, ModelDefinition as c, PrimaryKey as d, SqlContract as f, StorageTable as g, StorageColumn as h, ExtractOperationTypes as i, ModelField as l, SqlStorage as m, DEFAULT_FK_INDEX as n, ForeignKeyReferences as o, SqlMappings as p, ExtractCodecTypes as r, Index as s, DEFAULT_FK_CONSTRAINT as t, ModelStorage as u, UniqueConstraint as v, applyFkDefaults as y };
|
|
137
|
+
//# sourceMappingURL=types-DTFobApb.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-DTFobApb.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;;;AAgBA;AAsBA;AAKA;AAKA;AAKA;AAKY,KA1CA,aAAA,GA4CW;EAQX,SAAA,UAAY,EAAA,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J,KApET,mBAAA,GAoES;EACE,SAAA,OAAA,EAAA,MAAA;EAAgB,SAAA,UAAA,EAAA,MAAA;EAG3B,SAAA,UAAA,EArEW,MAqEM,CAAA,MAAA,EAAA,OAAA,CAAA;CAA+B;AAAZ,KAlEpC,UAAA,GAkEoC;EAC9C,SAAA,MAAA,EAlEiB,MAkEjB,CAAA,MAAA,EAlEgC,YAkEhC,CAAA;EAAS;AAEX;;;EACE,SAAA,KAAA,CAAA,EAhEiB,MAgEjB,CAAA,MAAA,EAhEgC,mBAgEhC,CAAA;CAAS;KA7DC,UAAA;;;KAIA,YAAA;;;KAIA,eAAA;oBACQ;mBACD,eAAe;sBACZ;;KAGV,WAAA;0BACc;0BACA;2BACC,eAAe;2BACf,eAAe;uBACnB;;;2BACI,eAAe;;cAG7B,qBAAA;cACA,gBAAA;;;;;iBAMG,eAAA;;;;;;;;;;KAUJ,sBACA,aAAa,sBACb,0BAA0B,mCAC1B,0BAA0B,qCACxB,cAAc,kCACL,0BAA0B,gDACxB,4BAA4B,gDAC9B,0BAA0B,2BAC7C,aAAa,cAAc,gBAAgB;;oBAE3B;mBACD;sBACG;qBACD;uBACE;;KAGX,oCAAoC,YAAY,eAC1D;KAEU,wCAAwC,YAAY,eAC9D"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/types.ts
|
|
2
|
+
const DEFAULT_FK_CONSTRAINT = true;
|
|
3
|
+
const DEFAULT_FK_INDEX = true;
|
|
4
|
+
/**
|
|
5
|
+
* Resolves foreign key `constraint` and `index` fields to their effective boolean values,
|
|
6
|
+
* falling back through optional override defaults, then to the global defaults.
|
|
7
|
+
*/
|
|
8
|
+
function applyFkDefaults(fk, overrideDefaults) {
|
|
9
|
+
return {
|
|
10
|
+
constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
|
|
11
|
+
index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { DEFAULT_FK_INDEX as n, applyFkDefaults as r, DEFAULT_FK_CONSTRAINT as t };
|
|
17
|
+
//# sourceMappingURL=types-kacOgEya.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-kacOgEya.mjs","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type {\n ColumnDefault,\n ContractBase,\n ExecutionHashBase,\n ExecutionSection,\n ProfileHashBase,\n StorageHashBase,\n} from '@prisma-next/contract/types';\n\n/**\n * A column definition in storage.\n *\n * `typeParams` is optional because most columns use non-parameterized types.\n * Columns with parameterized types can either inline `typeParams` or reference\n * a named {@link StorageTypeInstance} via `typeRef`.\n */\nexport type StorageColumn = {\n readonly nativeType: string;\n readonly codecId: string;\n readonly nullable: boolean;\n /**\n * Opaque, codec-owned JS/type parameters.\n * The codec that owns `codecId` defines the shape and semantics.\n * Mutually exclusive with `typeRef`.\n */\n readonly typeParams?: Record<string, unknown>;\n /**\n * Reference to a named type instance in `storage.types`.\n * Mutually exclusive with `typeParams`.\n */\n readonly typeRef?: string;\n /**\n * Default value for the column.\n * Can be a literal value or database function.\n */\n readonly default?: ColumnDefault;\n};\n\nexport type PrimaryKey = {\n readonly columns: readonly string[];\n readonly name?: string;\n};\n\nexport type UniqueConstraint = {\n readonly columns: readonly string[];\n readonly name?: string;\n};\n\nexport type Index = {\n readonly columns: readonly string[];\n readonly name?: string;\n};\n\nexport type ForeignKeyReferences = {\n readonly table: string;\n readonly columns: readonly string[];\n};\n\nexport type ForeignKey = {\n readonly columns: readonly string[];\n readonly references: ForeignKeyReferences;\n readonly name?: string;\n /** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */\n readonly constraint: boolean;\n /** Whether to emit a backing index for the FK columns. */\n readonly index: boolean;\n};\n\nexport type StorageTable = {\n readonly columns: Record<string, StorageColumn>;\n readonly primaryKey?: PrimaryKey;\n readonly uniques: ReadonlyArray<UniqueConstraint>;\n readonly indexes: ReadonlyArray<Index>;\n readonly foreignKeys: ReadonlyArray<ForeignKey>;\n};\n\n/**\n * A named, parameterized type instance.\n * These are registered in `storage.types` for reuse across columns\n * and to enable ergonomic schema surfaces like `schema.types.MyType`.\n *\n * Unlike {@link StorageColumn}, `typeParams` is required here because\n * `StorageTypeInstance` exists specifically to define reusable parameterized types.\n * A type instance without parameters would be redundant—columns can reference\n * the codec directly via `codecId`.\n */\nexport type StorageTypeInstance = {\n readonly codecId: string;\n readonly nativeType: string;\n readonly typeParams: Record<string, unknown>;\n};\n\nexport type SqlStorage = {\n readonly tables: Record<string, StorageTable>;\n /**\n * Named type instances for parameterized/custom types.\n * Columns can reference these via `typeRef`.\n */\n readonly types?: Record<string, StorageTypeInstance>;\n};\n\nexport type ModelField = {\n readonly column: string;\n};\n\nexport type ModelStorage = {\n readonly table: string;\n};\n\nexport type ModelDefinition = {\n readonly storage: ModelStorage;\n readonly fields: Record<string, ModelField>;\n readonly relations: Record<string, unknown>;\n};\n\nexport type SqlMappings = {\n readonly modelToTable?: Record<string, string>;\n readonly tableToModel?: Record<string, string>;\n readonly fieldToColumn?: Record<string, Record<string, string>>;\n readonly columnToField?: Record<string, Record<string, string>>;\n readonly codecTypes: Record<string, { readonly output: unknown }>;\n readonly operationTypes: Record<string, Record<string, unknown>>;\n};\n\nexport const DEFAULT_FK_CONSTRAINT = true;\nexport const DEFAULT_FK_INDEX = true;\n\n/**\n * Resolves foreign key `constraint` and `index` fields to their effective boolean values,\n * falling back through optional override defaults, then to the global defaults.\n */\nexport function applyFkDefaults(\n fk: { constraint?: boolean | undefined; index?: boolean | undefined },\n overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },\n): { constraint: boolean; index: boolean } {\n return {\n constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,\n index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,\n };\n}\n\nexport type SqlContract<\n S extends SqlStorage = SqlStorage,\n M extends Record<string, unknown> = Record<string, unknown>,\n R extends Record<string, unknown> = Record<string, unknown>,\n Map extends SqlMappings = SqlMappings,\n TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,\n TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,\n TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,\n> = ContractBase<TStorageHash, TExecutionHash, TProfileHash> & {\n readonly targetFamily: string;\n readonly storage: S;\n readonly models: M;\n readonly relations: R;\n readonly mappings: Map;\n readonly execution?: ExecutionSection;\n};\n\nexport type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =\n TContract['mappings']['codecTypes'];\n\nexport type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> =\n TContract['mappings']['operationTypes'];\n"],"mappings":";AA4HA,MAAa,wBAAwB;AACrC,MAAa,mBAAmB;;;;;AAMhC,SAAgB,gBACd,IACA,kBACyC;AACzC,QAAO;EACL,YAAY,GAAG,cAAc,kBAAkB,cAAc;EAC7D,OAAO,GAAG,SAAS,kBAAkB,SAAS;EAC/C"}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { type ExtractCodecTypes, type ExtractOperationTypes, type ForeignKey, type ForeignKeyReferences, type Index, type ModelDefinition, type ModelField, type ModelStorage, type PrimaryKey, type SqlContract, type SqlMappings, type SqlStorage, type StorageColumn, type StorageTable, type StorageTypeInstance, type UniqueConstraint };
|
|
1
|
+
import { _ as StorageTypeInstance, a as ForeignKey, c as ModelDefinition, d as PrimaryKey, f as SqlContract, g as StorageTable, h as StorageColumn, i as ExtractOperationTypes, l as ModelField, m as SqlStorage, n as DEFAULT_FK_INDEX, o as ForeignKeyReferences, p as SqlMappings, r as ExtractCodecTypes, s as Index, t as DEFAULT_FK_CONSTRAINT, u as ModelStorage, v as UniqueConstraint, y as applyFkDefaults } from "./types-DTFobApb.mjs";
|
|
2
|
+
export { DEFAULT_FK_CONSTRAINT, DEFAULT_FK_INDEX, type ExtractCodecTypes, type ExtractOperationTypes, type ForeignKey, type ForeignKeyReferences, type Index, type ModelDefinition, type ModelField, type ModelStorage, type PrimaryKey, type SqlContract, type SqlMappings, type SqlStorage, type StorageColumn, type StorageTable, type StorageTypeInstance, type UniqueConstraint, applyFkDefaults };
|
package/dist/types.mjs
CHANGED
package/dist/validate.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { f as
|
|
1
|
+
import { f as SqlContract, m as SqlStorage } from "./types-DTFobApb.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/validate.d.ts
|
|
4
|
+
declare function normalizeContract(contract: unknown): SqlContract<SqlStorage>;
|
|
4
5
|
declare function validateContract<TContract extends SqlContract<SqlStorage>>(value: unknown): TContract;
|
|
5
6
|
//#endregion
|
|
6
|
-
export { validateContract };
|
|
7
|
+
export { normalizeContract, validateContract };
|
|
7
8
|
//# sourceMappingURL=validate.d.mts.map
|
package/dist/validate.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.mts","names":[],"sources":["../src/validate.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"validate.d.mts","names":[],"sources":["../src/validate.ts"],"sourcesContent":[],"mappings":";;;iBA0OgB,iBAAA,qBAAsC,YAAY;iBAmFlD,mCAAmC,YAAY,8BAE5D"}
|
package/dist/validate.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as applyFkDefaults } from "./types-kacOgEya.mjs";
|
|
2
|
+
import { a as validateSqlContract } from "./validators-Dfw5_HSi.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/validate.ts
|
|
4
5
|
function computeDefaultMappings(models) {
|
|
@@ -116,12 +117,19 @@ function normalizeContract(contract) {
|
|
|
116
117
|
nullable: columnObj["nullable"] ?? false
|
|
117
118
|
};
|
|
118
119
|
}
|
|
120
|
+
const normalizedForeignKeys = (tableObj["foreignKeys"] ?? []).map((fk) => ({
|
|
121
|
+
...fk,
|
|
122
|
+
...applyFkDefaults({
|
|
123
|
+
constraint: typeof fk["constraint"] === "boolean" ? fk["constraint"] : void 0,
|
|
124
|
+
index: typeof fk["index"] === "boolean" ? fk["index"] : void 0
|
|
125
|
+
})
|
|
126
|
+
}));
|
|
119
127
|
normalizedTables[tableName] = {
|
|
120
128
|
...tableObj,
|
|
121
129
|
columns: normalizedColumns,
|
|
122
130
|
uniques: tableObj["uniques"] ?? [],
|
|
123
131
|
indexes: tableObj["indexes"] ?? [],
|
|
124
|
-
foreignKeys:
|
|
132
|
+
foreignKeys: normalizedForeignKeys
|
|
125
133
|
};
|
|
126
134
|
} else normalizedTables[tableName] = tableObj;
|
|
127
135
|
}
|
|
@@ -167,5 +175,5 @@ function validateContract(value) {
|
|
|
167
175
|
}
|
|
168
176
|
|
|
169
177
|
//#endregion
|
|
170
|
-
export { validateContract };
|
|
178
|
+
export { normalizeContract, validateContract };
|
|
171
179
|
//# sourceMappingURL=validate.mjs.map
|
package/dist/validate.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.mjs","names":["modelToTable: Record<string, string>","tableToModel: Record<string, string>","fieldToColumn: Record<string, Record<string, string>>","columnToField: Record<string, Record<string, string>>","modelFieldToColumn: Record<string, string>","normalizedTables: Record<string, unknown>","normalizedColumns: Record<string, unknown>","normalizedModelsObj: Record<string, unknown>"],"sources":["../src/validate.ts"],"sourcesContent":["import type { ModelDefinition, SqlContract, SqlMappings, SqlStorage } from './types';\nimport { validateSqlContract } from './validators';\n\ntype ResolvedMappings = {\n modelToTable: Record<string, string>;\n tableToModel: Record<string, string>;\n fieldToColumn: Record<string, Record<string, string>>;\n columnToField: Record<string, Record<string, string>>;\n codecTypes: Record<string, { readonly output: unknown }>;\n operationTypes: Record<string, Record<string, unknown>>;\n};\n\nfunction computeDefaultMappings(models: Record<string, ModelDefinition>): ResolvedMappings {\n const modelToTable: Record<string, string> = {};\n const tableToModel: Record<string, string> = {};\n const fieldToColumn: Record<string, Record<string, string>> = {};\n const columnToField: Record<string, Record<string, string>> = {};\n\n for (const [modelName, model] of Object.entries(models)) {\n const tableName = model.storage.table;\n modelToTable[modelName] = tableName;\n tableToModel[tableName] = modelName;\n\n const modelFieldToColumn: Record<string, string> = {};\n for (const [fieldName, field] of Object.entries(model.fields)) {\n const columnName = field.column;\n modelFieldToColumn[fieldName] = columnName;\n if (!columnToField[tableName]) {\n columnToField[tableName] = {};\n }\n columnToField[tableName][columnName] = fieldName;\n }\n\n fieldToColumn[modelName] = modelFieldToColumn;\n }\n\n return {\n modelToTable,\n tableToModel,\n fieldToColumn,\n columnToField,\n codecTypes: {},\n operationTypes: {},\n };\n}\n\nfunction assertInverseModelMappings(\n modelToTable: Record<string, string>,\n tableToModel: Record<string, string>,\n) {\n for (const [model, table] of Object.entries(modelToTable)) {\n if (tableToModel[table] !== model) {\n throw new Error(\n `Mappings override mismatch: modelToTable.${model}=\"${table}\" is not mirrored in tableToModel`,\n );\n }\n }\n for (const [table, model] of Object.entries(tableToModel)) {\n if (modelToTable[model] !== table) {\n throw new Error(\n `Mappings override mismatch: tableToModel.${table}=\"${model}\" is not mirrored in modelToTable`,\n );\n }\n }\n}\n\nfunction assertInverseFieldMappings(\n fieldToColumn: Record<string, Record<string, string>>,\n columnToField: Record<string, Record<string, string>>,\n modelToTable: Record<string, string>,\n tableToModel: Record<string, string>,\n) {\n for (const [model, fields] of Object.entries(fieldToColumn)) {\n const table = modelToTable[model];\n if (!table) {\n throw new Error(\n `Mappings override mismatch: fieldToColumn references unknown model \"${model}\"`,\n );\n }\n const reverseFields = columnToField[table];\n if (!reverseFields) {\n throw new Error(\n `Mappings override mismatch: columnToField is missing table \"${table}\" for model \"${model}\"`,\n );\n }\n for (const [field, column] of Object.entries(fields)) {\n if (reverseFields[column] !== field) {\n throw new Error(\n `Mappings override mismatch: fieldToColumn.${model}.${field}=\"${column}\" is not mirrored in columnToField.${table}`,\n );\n }\n }\n }\n\n for (const [table, columns] of Object.entries(columnToField)) {\n const model = tableToModel[table];\n if (!model) {\n throw new Error(\n `Mappings override mismatch: columnToField references unknown table \"${table}\"`,\n );\n }\n const forwardFields = fieldToColumn[model];\n if (!forwardFields) {\n throw new Error(\n `Mappings override mismatch: fieldToColumn is missing model \"${model}\" for table \"${table}\"`,\n );\n }\n for (const [column, field] of Object.entries(columns)) {\n if (forwardFields[field] !== column) {\n throw new Error(\n `Mappings override mismatch: columnToField.${table}.${column}=\"${field}\" is not mirrored in fieldToColumn.${model}`,\n );\n }\n }\n }\n}\n\nfunction mergeMappings(\n defaults: ResolvedMappings,\n existingMappings?: Partial<SqlMappings>,\n): ResolvedMappings {\n const hasModelToTable = existingMappings?.modelToTable !== undefined;\n const hasTableToModel = existingMappings?.tableToModel !== undefined;\n if (hasModelToTable !== hasTableToModel) {\n throw new Error(\n 'Mappings override mismatch: modelToTable and tableToModel must be provided together',\n );\n }\n\n const hasFieldToColumn = existingMappings?.fieldToColumn !== undefined;\n const hasColumnToField = existingMappings?.columnToField !== undefined;\n if (hasFieldToColumn !== hasColumnToField) {\n throw new Error(\n 'Mappings override mismatch: fieldToColumn and columnToField must be provided together',\n );\n }\n\n const modelToTable: Record<string, string> = hasModelToTable\n ? (existingMappings?.modelToTable ?? {})\n : defaults.modelToTable;\n const tableToModel: Record<string, string> = hasTableToModel\n ? (existingMappings?.tableToModel ?? {})\n : defaults.tableToModel;\n assertInverseModelMappings(modelToTable, tableToModel);\n\n const fieldToColumn: Record<string, Record<string, string>> = hasFieldToColumn\n ? (existingMappings?.fieldToColumn ?? {})\n : defaults.fieldToColumn;\n const columnToField: Record<string, Record<string, string>> = hasColumnToField\n ? (existingMappings?.columnToField ?? {})\n : defaults.columnToField;\n assertInverseFieldMappings(fieldToColumn, columnToField, modelToTable, tableToModel);\n\n return {\n modelToTable,\n tableToModel,\n fieldToColumn,\n columnToField,\n codecTypes: { ...defaults.codecTypes, ...(existingMappings?.codecTypes ?? {}) },\n operationTypes: { ...defaults.operationTypes, ...(existingMappings?.operationTypes ?? {}) },\n };\n}\n\nfunction validateContractLogic(contract: SqlContract<SqlStorage>): void {\n const tableNames = new Set(Object.keys(contract.storage.tables));\n\n for (const [tableName, table] of Object.entries(contract.storage.tables)) {\n const columnNames = new Set(Object.keys(table.columns));\n\n if (table.primaryKey) {\n for (const colName of table.primaryKey.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" primaryKey references non-existent column \"${colName}\"`,\n );\n }\n }\n }\n\n for (const unique of table.uniques) {\n for (const colName of unique.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" unique constraint references non-existent column \"${colName}\"`,\n );\n }\n }\n }\n\n for (const index of table.indexes) {\n for (const colName of index.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(`Table \"${tableName}\" index references non-existent column \"${colName}\"`);\n }\n }\n }\n\n for (const fk of table.foreignKeys) {\n for (const colName of fk.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\"`,\n );\n }\n }\n\n if (!tableNames.has(fk.references.table)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.references.table}\"`,\n );\n }\n\n const referencedTable = contract.storage.tables[\n fk.references.table\n ] as (typeof contract.storage.tables)[string];\n const referencedColumnNames = new Set(Object.keys(referencedTable.columns));\n for (const colName of fk.references.columns) {\n if (!referencedColumnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\" in table \"${fk.references.table}\"`,\n );\n }\n }\n\n if (fk.columns.length !== fk.references.columns.length) {\n throw new Error(\n `Table \"${tableName}\" foreignKey column count (${fk.columns.length}) does not match referenced column count (${fk.references.columns.length})`,\n );\n }\n }\n }\n}\n\nfunction normalizeContract(contract: unknown): SqlContract<SqlStorage> {\n if (typeof contract !== 'object' || contract === null) {\n return contract as SqlContract<SqlStorage>;\n }\n\n const contractObj = contract as Record<string, unknown>;\n\n let normalizedStorage = contractObj['storage'];\n if (normalizedStorage && typeof normalizedStorage === 'object' && normalizedStorage !== null) {\n const storage = normalizedStorage as Record<string, unknown>;\n const tables = storage['tables'] as Record<string, unknown> | undefined;\n\n if (tables) {\n const normalizedTables: Record<string, unknown> = {};\n for (const [tableName, table] of Object.entries(tables)) {\n const tableObj = table as Record<string, unknown>;\n const columns = tableObj['columns'] as Record<string, unknown> | undefined;\n\n if (columns) {\n const normalizedColumns: Record<string, unknown> = {};\n for (const [columnName, column] of Object.entries(columns)) {\n const columnObj = column as Record<string, unknown>;\n normalizedColumns[columnName] = {\n ...columnObj,\n nullable: columnObj['nullable'] ?? false,\n };\n }\n\n normalizedTables[tableName] = {\n ...tableObj,\n columns: normalizedColumns,\n uniques: tableObj['uniques'] ?? [],\n indexes: tableObj['indexes'] ?? [],\n foreignKeys: tableObj['foreignKeys'] ?? [],\n };\n } else {\n normalizedTables[tableName] = tableObj;\n }\n }\n\n normalizedStorage = {\n ...storage,\n tables: normalizedTables,\n };\n }\n }\n\n let normalizedModels = contractObj['models'];\n if (normalizedModels && typeof normalizedModels === 'object' && normalizedModels !== null) {\n const models = normalizedModels as Record<string, unknown>;\n const normalizedModelsObj: Record<string, unknown> = {};\n for (const [modelName, model] of Object.entries(models)) {\n const modelObj = model as Record<string, unknown>;\n normalizedModelsObj[modelName] = {\n ...modelObj,\n relations: modelObj['relations'] ?? {},\n };\n }\n normalizedModels = normalizedModelsObj;\n }\n\n return {\n ...contractObj,\n models: normalizedModels,\n relations: contractObj['relations'] ?? {},\n storage: normalizedStorage,\n extensionPacks: contractObj['extensionPacks'] ?? {},\n capabilities: contractObj['capabilities'] ?? {},\n meta: contractObj['meta'] ?? {},\n sources: contractObj['sources'] ?? {},\n } as SqlContract<SqlStorage>;\n}\n\nexport function validateContract<TContract extends SqlContract<SqlStorage>>(\n value: unknown,\n): TContract {\n const normalized = normalizeContract(value);\n const structurallyValid = validateSqlContract<SqlContract<SqlStorage>>(normalized);\n validateContractLogic(structurallyValid);\n\n const existingMappings = (structurallyValid as { mappings?: Partial<SqlMappings> }).mappings;\n const defaultMappings = computeDefaultMappings(\n structurallyValid.models as Record<string, ModelDefinition>,\n );\n const mappings = mergeMappings(defaultMappings, existingMappings);\n\n return {\n ...structurallyValid,\n mappings,\n } as TContract;\n}\n"],"mappings":";;;AAYA,SAAS,uBAAuB,QAA2D;CACzF,MAAMA,eAAuC,EAAE;CAC/C,MAAMC,eAAuC,EAAE;CAC/C,MAAMC,gBAAwD,EAAE;CAChE,MAAMC,gBAAwD,EAAE;AAEhE,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,OAAO,EAAE;EACvD,MAAM,YAAY,MAAM,QAAQ;AAChC,eAAa,aAAa;AAC1B,eAAa,aAAa;EAE1B,MAAMC,qBAA6C,EAAE;AACrD,OAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,MAAM,OAAO,EAAE;GAC7D,MAAM,aAAa,MAAM;AACzB,sBAAmB,aAAa;AAChC,OAAI,CAAC,cAAc,WACjB,eAAc,aAAa,EAAE;AAE/B,iBAAc,WAAW,cAAc;;AAGzC,gBAAc,aAAa;;AAG7B,QAAO;EACL;EACA;EACA;EACA;EACA,YAAY,EAAE;EACd,gBAAgB,EAAE;EACnB;;AAGH,SAAS,2BACP,cACA,cACA;AACA,MAAK,MAAM,CAAC,OAAO,UAAU,OAAO,QAAQ,aAAa,CACvD,KAAI,aAAa,WAAW,MAC1B,OAAM,IAAI,MACR,4CAA4C,MAAM,IAAI,MAAM,mCAC7D;AAGL,MAAK,MAAM,CAAC,OAAO,UAAU,OAAO,QAAQ,aAAa,CACvD,KAAI,aAAa,WAAW,MAC1B,OAAM,IAAI,MACR,4CAA4C,MAAM,IAAI,MAAM,mCAC7D;;AAKP,SAAS,2BACP,eACA,eACA,cACA,cACA;AACA,MAAK,MAAM,CAAC,OAAO,WAAW,OAAO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,aAAa;;AAIlC,uBAAoB;IAClB,GAAG;IACH,QAAQ;IACT;;;CAIL,IAAI,mBAAmB,YAAY;AACnC,KAAI,oBAAoB,OAAO,qBAAqB,YAAY,qBAAqB,MAAM;EACzF,MAAM,SAAS;EACf,MAAMC,sBAA+C,EAAE;AACvD,OAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,OAAO,EAAE;GACvD,MAAM,WAAW;AACjB,uBAAoB,aAAa;IAC/B,GAAG;IACH,WAAW,SAAS,gBAAgB,EAAE;IACvC;;AAEH,qBAAmB;;AAGrB,QAAO;EACL,GAAG;EACH,QAAQ;EACR,WAAW,YAAY,gBAAgB,EAAE;EACzC,SAAS;EACT,gBAAgB,YAAY,qBAAqB,EAAE;EACnD,cAAc,YAAY,mBAAmB,EAAE;EAC/C,MAAM,YAAY,WAAW,EAAE;EAC/B,SAAS,YAAY,cAAc,EAAE;EACtC;;AAGH,SAAgB,iBACd,OACW;CAEX,MAAM,oBAAoB,oBADP,kBAAkB,MAAM,CACuC;AAClF,uBAAsB,kBAAkB;CAExC,MAAM,mBAAoB,kBAA0D;CAIpF,MAAM,WAAW,cAHO,uBACtB,kBAAkB,OACnB,EAC+C,iBAAiB;AAEjE,QAAO;EACL,GAAG;EACH;EACD"}
|
|
1
|
+
{"version":3,"file":"validate.mjs","names":["modelToTable: Record<string, string>","tableToModel: Record<string, string>","fieldToColumn: Record<string, Record<string, string>>","columnToField: Record<string, Record<string, string>>","modelFieldToColumn: Record<string, string>","normalizedTables: Record<string, unknown>","normalizedColumns: Record<string, unknown>","normalizedModelsObj: Record<string, unknown>"],"sources":["../src/validate.ts"],"sourcesContent":["import type { ModelDefinition, SqlContract, SqlMappings, SqlStorage } from './types';\nimport { applyFkDefaults } from './types';\nimport { validateSqlContract } from './validators';\n\ntype ResolvedMappings = {\n modelToTable: Record<string, string>;\n tableToModel: Record<string, string>;\n fieldToColumn: Record<string, Record<string, string>>;\n columnToField: Record<string, Record<string, string>>;\n codecTypes: Record<string, { readonly output: unknown }>;\n operationTypes: Record<string, Record<string, unknown>>;\n};\n\nfunction computeDefaultMappings(models: Record<string, ModelDefinition>): ResolvedMappings {\n const modelToTable: Record<string, string> = {};\n const tableToModel: Record<string, string> = {};\n const fieldToColumn: Record<string, Record<string, string>> = {};\n const columnToField: Record<string, Record<string, string>> = {};\n\n for (const [modelName, model] of Object.entries(models)) {\n const tableName = model.storage.table;\n modelToTable[modelName] = tableName;\n tableToModel[tableName] = modelName;\n\n const modelFieldToColumn: Record<string, string> = {};\n for (const [fieldName, field] of Object.entries(model.fields)) {\n const columnName = field.column;\n modelFieldToColumn[fieldName] = columnName;\n if (!columnToField[tableName]) {\n columnToField[tableName] = {};\n }\n columnToField[tableName][columnName] = fieldName;\n }\n\n fieldToColumn[modelName] = modelFieldToColumn;\n }\n\n return {\n modelToTable,\n tableToModel,\n fieldToColumn,\n columnToField,\n codecTypes: {},\n operationTypes: {},\n };\n}\n\nfunction assertInverseModelMappings(\n modelToTable: Record<string, string>,\n tableToModel: Record<string, string>,\n) {\n for (const [model, table] of Object.entries(modelToTable)) {\n if (tableToModel[table] !== model) {\n throw new Error(\n `Mappings override mismatch: modelToTable.${model}=\"${table}\" is not mirrored in tableToModel`,\n );\n }\n }\n for (const [table, model] of Object.entries(tableToModel)) {\n if (modelToTable[model] !== table) {\n throw new Error(\n `Mappings override mismatch: tableToModel.${table}=\"${model}\" is not mirrored in modelToTable`,\n );\n }\n }\n}\n\nfunction assertInverseFieldMappings(\n fieldToColumn: Record<string, Record<string, string>>,\n columnToField: Record<string, Record<string, string>>,\n modelToTable: Record<string, string>,\n tableToModel: Record<string, string>,\n) {\n for (const [model, fields] of Object.entries(fieldToColumn)) {\n const table = modelToTable[model];\n if (!table) {\n throw new Error(\n `Mappings override mismatch: fieldToColumn references unknown model \"${model}\"`,\n );\n }\n const reverseFields = columnToField[table];\n if (!reverseFields) {\n throw new Error(\n `Mappings override mismatch: columnToField is missing table \"${table}\" for model \"${model}\"`,\n );\n }\n for (const [field, column] of Object.entries(fields)) {\n if (reverseFields[column] !== field) {\n throw new Error(\n `Mappings override mismatch: fieldToColumn.${model}.${field}=\"${column}\" is not mirrored in columnToField.${table}`,\n );\n }\n }\n }\n\n for (const [table, columns] of Object.entries(columnToField)) {\n const model = tableToModel[table];\n if (!model) {\n throw new Error(\n `Mappings override mismatch: columnToField references unknown table \"${table}\"`,\n );\n }\n const forwardFields = fieldToColumn[model];\n if (!forwardFields) {\n throw new Error(\n `Mappings override mismatch: fieldToColumn is missing model \"${model}\" for table \"${table}\"`,\n );\n }\n for (const [column, field] of Object.entries(columns)) {\n if (forwardFields[field] !== column) {\n throw new Error(\n `Mappings override mismatch: columnToField.${table}.${column}=\"${field}\" is not mirrored in fieldToColumn.${model}`,\n );\n }\n }\n }\n}\n\nfunction mergeMappings(\n defaults: ResolvedMappings,\n existingMappings?: Partial<SqlMappings>,\n): ResolvedMappings {\n const hasModelToTable = existingMappings?.modelToTable !== undefined;\n const hasTableToModel = existingMappings?.tableToModel !== undefined;\n if (hasModelToTable !== hasTableToModel) {\n throw new Error(\n 'Mappings override mismatch: modelToTable and tableToModel must be provided together',\n );\n }\n\n const hasFieldToColumn = existingMappings?.fieldToColumn !== undefined;\n const hasColumnToField = existingMappings?.columnToField !== undefined;\n if (hasFieldToColumn !== hasColumnToField) {\n throw new Error(\n 'Mappings override mismatch: fieldToColumn and columnToField must be provided together',\n );\n }\n\n const modelToTable: Record<string, string> = hasModelToTable\n ? (existingMappings?.modelToTable ?? {})\n : defaults.modelToTable;\n const tableToModel: Record<string, string> = hasTableToModel\n ? (existingMappings?.tableToModel ?? {})\n : defaults.tableToModel;\n assertInverseModelMappings(modelToTable, tableToModel);\n\n const fieldToColumn: Record<string, Record<string, string>> = hasFieldToColumn\n ? (existingMappings?.fieldToColumn ?? {})\n : defaults.fieldToColumn;\n const columnToField: Record<string, Record<string, string>> = hasColumnToField\n ? (existingMappings?.columnToField ?? {})\n : defaults.columnToField;\n assertInverseFieldMappings(fieldToColumn, columnToField, modelToTable, tableToModel);\n\n return {\n modelToTable,\n tableToModel,\n fieldToColumn,\n columnToField,\n codecTypes: { ...defaults.codecTypes, ...(existingMappings?.codecTypes ?? {}) },\n operationTypes: { ...defaults.operationTypes, ...(existingMappings?.operationTypes ?? {}) },\n };\n}\n\nfunction validateContractLogic(contract: SqlContract<SqlStorage>): void {\n const tableNames = new Set(Object.keys(contract.storage.tables));\n\n for (const [tableName, table] of Object.entries(contract.storage.tables)) {\n const columnNames = new Set(Object.keys(table.columns));\n\n if (table.primaryKey) {\n for (const colName of table.primaryKey.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" primaryKey references non-existent column \"${colName}\"`,\n );\n }\n }\n }\n\n for (const unique of table.uniques) {\n for (const colName of unique.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" unique constraint references non-existent column \"${colName}\"`,\n );\n }\n }\n }\n\n for (const index of table.indexes) {\n for (const colName of index.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(`Table \"${tableName}\" index references non-existent column \"${colName}\"`);\n }\n }\n }\n\n for (const fk of table.foreignKeys) {\n for (const colName of fk.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\"`,\n );\n }\n }\n\n if (!tableNames.has(fk.references.table)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.references.table}\"`,\n );\n }\n\n const referencedTable = contract.storage.tables[\n fk.references.table\n ] as (typeof contract.storage.tables)[string];\n const referencedColumnNames = new Set(Object.keys(referencedTable.columns));\n for (const colName of fk.references.columns) {\n if (!referencedColumnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\" in table \"${fk.references.table}\"`,\n );\n }\n }\n\n if (fk.columns.length !== fk.references.columns.length) {\n throw new Error(\n `Table \"${tableName}\" foreignKey column count (${fk.columns.length}) does not match referenced column count (${fk.references.columns.length})`,\n );\n }\n }\n }\n}\n\nexport function normalizeContract(contract: unknown): SqlContract<SqlStorage> {\n if (typeof contract !== 'object' || contract === null) {\n return contract as SqlContract<SqlStorage>;\n }\n\n const contractObj = contract as Record<string, unknown>;\n\n let normalizedStorage = contractObj['storage'];\n if (normalizedStorage && typeof normalizedStorage === 'object' && normalizedStorage !== null) {\n const storage = normalizedStorage as Record<string, unknown>;\n const tables = storage['tables'] as Record<string, unknown> | undefined;\n\n if (tables) {\n const normalizedTables: Record<string, unknown> = {};\n for (const [tableName, table] of Object.entries(tables)) {\n const tableObj = table as Record<string, unknown>;\n const columns = tableObj['columns'] as Record<string, unknown> | undefined;\n\n if (columns) {\n const normalizedColumns: Record<string, unknown> = {};\n for (const [columnName, column] of Object.entries(columns)) {\n const columnObj = column as Record<string, unknown>;\n normalizedColumns[columnName] = {\n ...columnObj,\n nullable: columnObj['nullable'] ?? false,\n };\n }\n\n // Normalize foreign keys: add constraint/index defaults if missing\n const rawForeignKeys = (tableObj['foreignKeys'] ?? []) as Array<Record<string, unknown>>;\n const normalizedForeignKeys = rawForeignKeys.map((fk) => ({\n ...fk,\n ...applyFkDefaults({\n constraint: typeof fk['constraint'] === 'boolean' ? fk['constraint'] : undefined,\n index: typeof fk['index'] === 'boolean' ? fk['index'] : undefined,\n }),\n }));\n\n normalizedTables[tableName] = {\n ...tableObj,\n columns: normalizedColumns,\n uniques: tableObj['uniques'] ?? [],\n indexes: tableObj['indexes'] ?? [],\n foreignKeys: normalizedForeignKeys,\n };\n } else {\n normalizedTables[tableName] = tableObj;\n }\n }\n\n normalizedStorage = {\n ...storage,\n tables: normalizedTables,\n };\n }\n }\n\n let normalizedModels = contractObj['models'];\n if (normalizedModels && typeof normalizedModels === 'object' && normalizedModels !== null) {\n const models = normalizedModels as Record<string, unknown>;\n const normalizedModelsObj: Record<string, unknown> = {};\n for (const [modelName, model] of Object.entries(models)) {\n const modelObj = model as Record<string, unknown>;\n normalizedModelsObj[modelName] = {\n ...modelObj,\n relations: modelObj['relations'] ?? {},\n };\n }\n normalizedModels = normalizedModelsObj;\n }\n\n return {\n ...contractObj,\n models: normalizedModels,\n relations: contractObj['relations'] ?? {},\n storage: normalizedStorage,\n extensionPacks: contractObj['extensionPacks'] ?? {},\n capabilities: contractObj['capabilities'] ?? {},\n meta: contractObj['meta'] ?? {},\n sources: contractObj['sources'] ?? {},\n } as SqlContract<SqlStorage>;\n}\n\nexport function validateContract<TContract extends SqlContract<SqlStorage>>(\n value: unknown,\n): TContract {\n const normalized = normalizeContract(value);\n const structurallyValid = validateSqlContract<SqlContract<SqlStorage>>(normalized);\n validateContractLogic(structurallyValid);\n\n const existingMappings = (structurallyValid as { mappings?: Partial<SqlMappings> }).mappings;\n const defaultMappings = computeDefaultMappings(\n structurallyValid.models as Record<string, ModelDefinition>,\n );\n const mappings = mergeMappings(defaultMappings, existingMappings);\n\n return {\n ...structurallyValid,\n mappings,\n } as TContract;\n}\n"],"mappings":";;;;AAaA,SAAS,uBAAuB,QAA2D;CACzF,MAAMA,eAAuC,EAAE;CAC/C,MAAMC,eAAuC,EAAE;CAC/C,MAAMC,gBAAwD,EAAE;CAChE,MAAMC,gBAAwD,EAAE;AAEhE,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,OAAO,EAAE;EACvD,MAAM,YAAY,MAAM,QAAQ;AAChC,eAAa,aAAa;AAC1B,eAAa,aAAa;EAE1B,MAAMC,qBAA6C,EAAE;AACrD,OAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,MAAM,OAAO,EAAE;GAC7D,MAAM,aAAa,MAAM;AACzB,sBAAmB,aAAa;AAChC,OAAI,CAAC,cAAc,WACjB,eAAc,aAAa,EAAE;AAE/B,iBAAc,WAAW,cAAc;;AAGzC,gBAAc,aAAa;;AAG7B,QAAO;EACL;EACA;EACA;EACA;EACA,YAAY,EAAE;EACd,gBAAgB,EAAE;EACnB;;AAGH,SAAS,2BACP,cACA,cACA;AACA,MAAK,MAAM,CAAC,OA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,uBACtB,kBAAkB,OACnB,EAC+C,iBAAiB;AAEjE,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -62,7 +62,9 @@ const ForeignKeyReferencesSchema = type.declare().type({
|
|
|
62
62
|
const ForeignKeySchema = type.declare().type({
|
|
63
63
|
columns: type.string.array().readonly(),
|
|
64
64
|
references: ForeignKeyReferencesSchema,
|
|
65
|
-
"name?": "string"
|
|
65
|
+
"name?": "string",
|
|
66
|
+
constraint: "boolean",
|
|
67
|
+
index: "boolean"
|
|
66
68
|
});
|
|
67
69
|
const StorageTableSchema = type.declare().type({
|
|
68
70
|
columns: type({ "[string]": StorageColumnSchema }),
|
|
@@ -157,4 +159,4 @@ function validateSqlContract(value) {
|
|
|
157
159
|
|
|
158
160
|
//#endregion
|
|
159
161
|
export { validateSqlContract as a, validateModel as i, ColumnDefaultLiteralSchema as n, validateStorage as o, ColumnDefaultSchema as r, ColumnDefaultFunctionSchema as t };
|
|
160
|
-
//# sourceMappingURL=validators-
|
|
162
|
+
//# sourceMappingURL=validators-Dfw5_HSi.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators-Dfw5_HSi.mjs","names":[],"sources":["../src/validators.ts"],"sourcesContent":["import { type } from 'arktype';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlStorage,\n StorageTable,\n StorageTypeInstance,\n UniqueConstraint,\n} from './types';\n\ntype ColumnDefaultLiteral = { readonly kind: 'literal'; readonly expression: string };\ntype ColumnDefaultFunction = { readonly kind: 'function'; readonly expression: string };\nconst literalKindSchema = type(\"'literal'\");\nconst functionKindSchema = type(\"'function'\");\nconst generatorKindSchema = type(\"'generator'\");\nconst generatorIdSchema = type(\"'ulid' | 'nanoid' | 'uuidv7' | 'uuidv4' | 'cuid2' | 'ksuid'\");\n\nexport const ColumnDefaultLiteralSchema = type.declare<ColumnDefaultLiteral>().type({\n kind: literalKindSchema,\n expression: 'string',\n});\n\nexport const ColumnDefaultFunctionSchema = type.declare<ColumnDefaultFunction>().type({\n kind: functionKindSchema,\n expression: 'string',\n});\n\nexport const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);\n\nconst ExecutionMutationDefaultValueSchema = type({\n kind: generatorKindSchema,\n id: generatorIdSchema,\n 'params?': 'Record<string, unknown>',\n});\n\nconst ExecutionMutationDefaultSchema = type({\n ref: {\n table: 'string',\n column: 'string',\n },\n 'onCreate?': ExecutionMutationDefaultValueSchema,\n 'onUpdate?': ExecutionMutationDefaultValueSchema,\n});\n\nconst ExecutionSchema = type({\n mutations: {\n defaults: ExecutionMutationDefaultSchema.array().readonly(),\n },\n});\n\nconst StorageColumnSchema = type({\n nativeType: 'string',\n codecId: 'string',\n nullable: 'boolean',\n 'typeParams?': 'Record<string, unknown>',\n 'typeRef?': 'string',\n 'default?': ColumnDefaultSchema,\n}).narrow((col, ctx) => {\n if (col.typeParams !== undefined && col.typeRef !== undefined) {\n return ctx.mustBe('a column with either typeParams or typeRef, not both');\n }\n return true;\n});\n\nconst StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({\n codecId: 'string',\n nativeType: 'string',\n typeParams: 'Record<string, unknown>',\n});\n\nconst PrimaryKeySchema = type.declare<PrimaryKey>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst UniqueConstraintSchema = type.declare<UniqueConstraint>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst IndexSchema = type.declare<Index>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({\n table: 'string',\n columns: type.string.array().readonly(),\n});\n\nconst ForeignKeySchema = type.declare<ForeignKey>().type({\n columns: type.string.array().readonly(),\n references: ForeignKeyReferencesSchema,\n 'name?': 'string',\n constraint: 'boolean',\n index: 'boolean',\n});\n\nconst StorageTableSchema = type.declare<StorageTable>().type({\n columns: type({ '[string]': StorageColumnSchema }),\n 'primaryKey?': PrimaryKeySchema,\n uniques: UniqueConstraintSchema.array().readonly(),\n indexes: IndexSchema.array().readonly(),\n foreignKeys: ForeignKeySchema.array().readonly(),\n});\n\nconst StorageSchema = type.declare<SqlStorage>().type({\n tables: type({ '[string]': StorageTableSchema }),\n 'types?': type({ '[string]': StorageTypeInstanceSchema }),\n});\n\nconst ModelFieldSchema = type.declare<ModelField>().type({\n column: 'string',\n});\n\nconst ModelStorageSchema = type.declare<ModelStorage>().type({\n table: 'string',\n});\n\nconst ModelSchema = type.declare<ModelDefinition>().type({\n storage: ModelStorageSchema,\n fields: type({ '[string]': ModelFieldSchema }),\n relations: type({ '[string]': 'unknown' }),\n});\n\nconst SqlContractSchema = type({\n 'schemaVersion?': \"'1'\",\n target: 'string',\n targetFamily: \"'sql'\",\n storageHash: 'string',\n 'executionHash?': 'string',\n 'profileHash?': 'string',\n 'capabilities?': 'Record<string, Record<string, boolean>>',\n 'extensionPacks?': 'Record<string, unknown>',\n 'meta?': 'Record<string, unknown>',\n 'sources?': 'Record<string, unknown>',\n models: type({ '[string]': ModelSchema }),\n storage: StorageSchema,\n 'execution?': ExecutionSchema,\n});\n\n/**\n * Validates the structural shape of SqlStorage using Arktype.\n *\n * @param value - The storage value to validate\n * @returns The validated storage if structure is valid\n * @throws Error if the storage structure is invalid\n */\nexport function validateStorage(value: unknown): SqlStorage {\n const result = StorageSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Storage validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of ModelDefinition using Arktype.\n *\n * @param value - The model value to validate\n * @returns The validated model if structure is valid\n * @throws Error if the model structure is invalid\n */\nexport function validateModel(value: unknown): ModelDefinition {\n const result = ModelSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Model validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of a SqlContract using Arktype.\n *\n * **Responsibility: Validation Only**\n * This function validates that the contract has the correct structure and types.\n * It does NOT normalize the contract - normalization must happen in the contract builder.\n *\n * The contract passed to this function must already be normalized (all required fields present).\n * If normalization is needed, it should be done by the contract builder before calling this function.\n *\n * This ensures all required fields are present and have the correct types.\n *\n * @param value - The contract value to validate (typically from a JSON import)\n * @returns The validated contract if structure is valid\n * @throws Error if the contract structure is invalid\n */\nexport function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {\n if (typeof value !== 'object' || value === null) {\n throw new Error('Contract structural validation failed: value must be an object');\n }\n\n // Check targetFamily first to provide a clear error message for unsupported target families\n const rawValue = value as { targetFamily?: string };\n if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {\n throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);\n }\n\n const contractResult = SqlContractSchema(value);\n\n if (contractResult instanceof type.errors) {\n const messages = contractResult.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Contract structural validation failed: ${messages}`);\n }\n\n // After validation, contractResult matches the schema and preserves the input structure\n // TypeScript needs an assertion here due to exactOptionalPropertyTypes differences\n // between Arktype's inferred type and the generic T, but runtime-wise they're compatible\n return contractResult as T;\n}\n"],"mappings":";;;AAkBA,MAAM,oBAAoB,KAAK,YAAY;AAC3C,MAAM,qBAAqB,KAAK,aAAa;AAC7C,MAAM,sBAAsB,KAAK,cAAc;AAC/C,MAAM,oBAAoB,KAAK,8DAA8D;AAE7F,MAAa,6BAA6B,KAAK,SAA+B,CAAC,KAAK;CAClF,MAAM;CACN,YAAY;CACb,CAAC;AAEF,MAAa,8BAA8B,KAAK,SAAgC,CAAC,KAAK;CACpF,MAAM;CACN,YAAY;CACb,CAAC;AAEF,MAAa,sBAAsB,2BAA2B,GAAG,4BAA4B;AAE7F,MAAM,sCAAsC,KAAK;CAC/C,MAAM;CACN,IAAI;CACJ,WAAW;CACZ,CAAC;AAWF,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BAAuD,OAAmB;AACxF,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC,OAAM,IAAI,MAAM,iEAAiE;CAInF,MAAM,WAAW;AACjB,KAAI,SAAS,iBAAiB,UAAa,SAAS,iBAAiB,MACnE,OAAM,IAAI,MAAM,8BAA8B,SAAS,eAAe;CAGxE,MAAM,iBAAiB,kBAAkB,MAAM;AAE/C,KAAI,0BAA0B,KAAK,QAAQ;EACzC,MAAM,WAAW,eAAe,KAAK,MAA2B,EAAE,QAAQ,CAAC,KAAK,KAAK;AACrF,QAAM,IAAI,MAAM,0CAA0C,WAAW;;AAMvE,QAAO"}
|
package/dist/validators.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as ModelDefinition, f as SqlContract, m as SqlStorage } from "./types-DTFobApb.mjs";
|
|
2
2
|
import * as arktype_internal_variants_object_ts0 from "arktype/internal/variants/object.ts";
|
|
3
3
|
|
|
4
4
|
//#region src/validators.d.ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.mts","names":[],"sources":["../src/validators.ts"],"sourcesContent":[],"mappings":";;;;KAgBK,oBAAA;;;AAFY,CAAA;AAEQ,KACpB,qBAAA,GAAqB;EAMb,SAAA,IAAA,EAAA,UAAA;EAKA,SAAA,UAAA,EAAA,MAAA;AAKb,CAAA;AAAgC,cAVnB,0BAUmB,EAVO,oCAAA,CAAA,UAUP,CAVO,oBAUP,EAAA,CAAA,CAAA,CAAA;AAAA,cALnB,2BAKmB,EALQ,oCAAA,CAAA,UAKR,CALQ,qBAKR,EAAA,CAAA,CAAA,CAAA;AAAA,cAAnB,mBAAmB,EAAA,oCAAA,CAAA,UAAA,CAAA,oBAAA,GAAA,qBAAA,EAAA,CAAA,CAAA,CAAA;;
|
|
1
|
+
{"version":3,"file":"validators.d.mts","names":[],"sources":["../src/validators.ts"],"sourcesContent":[],"mappings":";;;;KAgBK,oBAAA;;;AAFY,CAAA;AAEQ,KACpB,qBAAA,GAAqB;EAMb,SAAA,IAAA,EAAA,UAAA;EAKA,SAAA,UAAA,EAAA,MAAA;AAKb,CAAA;AAAgC,cAVnB,0BAUmB,EAVO,oCAAA,CAAA,UAUP,CAVO,oBAUP,EAAA,CAAA,CAAA,CAAA;AAAA,cALnB,2BAKmB,EALQ,oCAAA,CAAA,UAKR,CALQ,qBAKR,EAAA,CAAA,CAAA,CAAA;AAAA,cAAnB,mBAAmB,EAAA,oCAAA,CAAA,UAAA,CAAA,oBAAA,GAAA,qBAAA,EAAA,CAAA,CAAA,CAAA;;AAyHhC;AAgBA;AAyBA;;;;AAAyF,iBAzCzE,eAAA,CAyCyE,KAAA,EAAA,OAAA,CAAA,EAzCxC,UAyCwC;;;;;;;;iBAzBzE,aAAA,kBAA+B;;;;;;;;;;;;;;;;;iBAyB/B,8BAA8B,YAAY,8BAA8B"}
|
package/dist/validators.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as validateSqlContract, i as validateModel, n as ColumnDefaultLiteralSchema, o as validateStorage, r as ColumnDefaultSchema, t as ColumnDefaultFunctionSchema } from "./validators-
|
|
1
|
+
import { a as validateSqlContract, i as validateModel, n as ColumnDefaultLiteralSchema, o as validateStorage, r as ColumnDefaultSchema, t as ColumnDefaultFunctionSchema } from "./validators-Dfw5_HSi.mjs";
|
|
2
2
|
|
|
3
3
|
export { ColumnDefaultFunctionSchema, ColumnDefaultLiteralSchema, ColumnDefaultSchema, validateModel, validateSqlContract, validateStorage };
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.38",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"plane": "shared",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"description": "SQL contract types, validators, and IR factories for Prisma Next",
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"arktype": "^2.1.25",
|
|
10
|
-
"@prisma-next/contract": "0.3.0-dev.
|
|
10
|
+
"@prisma-next/contract": "0.3.0-dev.38"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"tsdown": "0.18.4",
|
|
14
14
|
"typescript": "5.9.3",
|
|
15
15
|
"vitest": "4.0.17",
|
|
16
|
-
"@prisma-next/tsconfig": "0.0.0",
|
|
17
16
|
"@prisma-next/test-utils": "0.0.1",
|
|
17
|
+
"@prisma-next/tsconfig": "0.0.0",
|
|
18
18
|
"@prisma-next/tsdown": "0.0.0"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
package/src/exports/types.ts
CHANGED
package/src/exports/validate.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { validateContract } from '../validate';
|
|
1
|
+
export { normalizeContract, validateContract } from '../validate';
|
package/src/factories.ts
CHANGED
|
@@ -18,6 +18,7 @@ import type {
|
|
|
18
18
|
StorageTable,
|
|
19
19
|
UniqueConstraint,
|
|
20
20
|
} from './types';
|
|
21
|
+
import { applyFkDefaults } from './types';
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* Creates a StorageColumn with nativeType and codecId.
|
|
@@ -57,7 +58,7 @@ export function fk(
|
|
|
57
58
|
columns: readonly string[],
|
|
58
59
|
refTable: string,
|
|
59
60
|
refColumns: readonly string[],
|
|
60
|
-
name?: string,
|
|
61
|
+
opts?: { name?: string; constraint?: boolean; index?: boolean },
|
|
61
62
|
): ForeignKey {
|
|
62
63
|
const references: ForeignKeyReferences = {
|
|
63
64
|
table: refTable,
|
|
@@ -66,7 +67,8 @@ export function fk(
|
|
|
66
67
|
return {
|
|
67
68
|
columns,
|
|
68
69
|
references,
|
|
69
|
-
...(
|
|
70
|
+
...applyFkDefaults({ constraint: opts?.constraint, index: opts?.index }),
|
|
71
|
+
...(opts?.name !== undefined && { name: opts.name }),
|
|
70
72
|
};
|
|
71
73
|
}
|
|
72
74
|
|
package/src/types.ts
CHANGED
|
@@ -60,6 +60,10 @@ export type ForeignKey = {
|
|
|
60
60
|
readonly columns: readonly string[];
|
|
61
61
|
readonly references: ForeignKeyReferences;
|
|
62
62
|
readonly name?: string;
|
|
63
|
+
/** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
|
|
64
|
+
readonly constraint: boolean;
|
|
65
|
+
/** Whether to emit a backing index for the FK columns. */
|
|
66
|
+
readonly index: boolean;
|
|
63
67
|
};
|
|
64
68
|
|
|
65
69
|
export type StorageTable = {
|
|
@@ -118,6 +122,23 @@ export type SqlMappings = {
|
|
|
118
122
|
readonly operationTypes: Record<string, Record<string, unknown>>;
|
|
119
123
|
};
|
|
120
124
|
|
|
125
|
+
export const DEFAULT_FK_CONSTRAINT = true;
|
|
126
|
+
export const DEFAULT_FK_INDEX = true;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Resolves foreign key `constraint` and `index` fields to their effective boolean values,
|
|
130
|
+
* falling back through optional override defaults, then to the global defaults.
|
|
131
|
+
*/
|
|
132
|
+
export function applyFkDefaults(
|
|
133
|
+
fk: { constraint?: boolean | undefined; index?: boolean | undefined },
|
|
134
|
+
overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },
|
|
135
|
+
): { constraint: boolean; index: boolean } {
|
|
136
|
+
return {
|
|
137
|
+
constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
|
|
138
|
+
index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
121
142
|
export type SqlContract<
|
|
122
143
|
S extends SqlStorage = SqlStorage,
|
|
123
144
|
M extends Record<string, unknown> = Record<string, unknown>,
|
package/src/validate.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ModelDefinition, SqlContract, SqlMappings, SqlStorage } from './types';
|
|
2
|
+
import { applyFkDefaults } from './types';
|
|
2
3
|
import { validateSqlContract } from './validators';
|
|
3
4
|
|
|
4
5
|
type ResolvedMappings = {
|
|
@@ -231,7 +232,7 @@ function validateContractLogic(contract: SqlContract<SqlStorage>): void {
|
|
|
231
232
|
}
|
|
232
233
|
}
|
|
233
234
|
|
|
234
|
-
function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
235
|
+
export function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
235
236
|
if (typeof contract !== 'object' || contract === null) {
|
|
236
237
|
return contract as SqlContract<SqlStorage>;
|
|
237
238
|
}
|
|
@@ -259,12 +260,22 @@ function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
|
259
260
|
};
|
|
260
261
|
}
|
|
261
262
|
|
|
263
|
+
// Normalize foreign keys: add constraint/index defaults if missing
|
|
264
|
+
const rawForeignKeys = (tableObj['foreignKeys'] ?? []) as Array<Record<string, unknown>>;
|
|
265
|
+
const normalizedForeignKeys = rawForeignKeys.map((fk) => ({
|
|
266
|
+
...fk,
|
|
267
|
+
...applyFkDefaults({
|
|
268
|
+
constraint: typeof fk['constraint'] === 'boolean' ? fk['constraint'] : undefined,
|
|
269
|
+
index: typeof fk['index'] === 'boolean' ? fk['index'] : undefined,
|
|
270
|
+
}),
|
|
271
|
+
}));
|
|
272
|
+
|
|
262
273
|
normalizedTables[tableName] = {
|
|
263
274
|
...tableObj,
|
|
264
275
|
columns: normalizedColumns,
|
|
265
276
|
uniques: tableObj['uniques'] ?? [],
|
|
266
277
|
indexes: tableObj['indexes'] ?? [],
|
|
267
|
-
foreignKeys:
|
|
278
|
+
foreignKeys: normalizedForeignKeys,
|
|
268
279
|
};
|
|
269
280
|
} else {
|
|
270
281
|
normalizedTables[tableName] = tableObj;
|
package/src/validators.ts
CHANGED
|
@@ -98,6 +98,8 @@ const ForeignKeySchema = type.declare<ForeignKey>().type({
|
|
|
98
98
|
columns: type.string.array().readonly(),
|
|
99
99
|
references: ForeignKeyReferencesSchema,
|
|
100
100
|
'name?': 'string',
|
|
101
|
+
constraint: 'boolean',
|
|
102
|
+
index: 'boolean',
|
|
101
103
|
});
|
|
102
104
|
|
|
103
105
|
const StorageTableSchema = type.declare<StorageTable>().type({
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-D3AOQgwo.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;;;AAgBA;AAsBA;AAKA;AAKA;AAKA;AAKY,KA1CA,aAAA,GA4CW;EAIX,SAAA,UAAY,EAAA,MAAA;EACW,SAAA,OAAA,EAAA,MAAA;EAAf,SAAA,QAAA,EAAA,OAAA;EACI;;;;;EAGc,SAAA,UAAA,CAAA,EA5Cd,MA4Cc,CAAA,MAAA,EAAA,OAAA,CAAA;EAAd;;AAaxB;AAMA;EACkC,SAAA,OAAA,CAAA,EAAA,MAAA;EAAf;;;;EAQP,SAAA,OAAU,CAAA,EA9DD,aA8DC;AAItB,CAAA;AAIY,KAnEA,UAAA,GAmEe;EACP,SAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EACc,SAAA,IAAA,CAAA,EAAA,MAAA;CAAf;AACG,KAjEV,gBAAA,GAiEU;EAAM,SAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EAGhB,SAAA,IAAA,CAAW,EAAA,MAAA;CACG;AACA,KAjEd,KAAA,GAiEc;EACgB,SAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EAAf,SAAA,IAAA,CAAA,EAAA,MAAA;CACe;AAAf,KA9Df,oBAAA,GA8De;EACJ,SAAA,KAAA,EAAA,MAAA;EACmB,SAAA,OAAA,EAAA,SAAA,MAAA,EAAA;CAAf;AAAM,KA3DrB,UAAA,GA2DqB;EAGrB,SAAA,OAAW,EAAA,SAAA,MAAA,EAAA;EACX,SAAA,UAAA,EA7DW,oBA6DX;EAAa,SAAA,IAAA,CAAA,EAAA,MAAA;CACb;AAA0B,KA1D1B,YAAA,GA0D0B;EAC1B,SAAA,OAAA,EA1DQ,MA0DR,CAAA,MAAA,EA1DuB,aA0DvB,CAAA;EAA0B,SAAA,UAAA,CAAA,EAzDd,UAyDc;EACxB,SAAA,OAAA,EAzDM,aAyDN,CAzDoB,gBAyDpB,CAAA;EAAc,SAAA,OAAA,EAxDR,aAwDQ,CAxDM,KAwDN,CAAA;EACL,SAAA,WAAA,EAxDC,aAwDD,CAxDe,UAwDf,CAAA;CAA0B;;;;;;;;;;;AAO3B,KAlDV,mBAAA,GAkDU;EACD,SAAA,OAAA,EAAA,MAAA;EACE,SAAA,UAAA,EAAA,MAAA;EAAgB,SAAA,UAAA,EAjDhB,MAiDgB,CAAA,MAAA,EAAA,OAAA,CAAA;AAGvC,CAAA;AAA4D,KAjDhD,UAAA,GAiDgD;EAAZ,SAAA,MAAA,EAhD7B,MAgD6B,CAAA,MAAA,EAhDd,YAgDc,CAAA;EAC9C;;AAEF;;EAAoD,SAAA,KAAA,CAAA,EA9CjC,MA8CiC,CAAA,MAAA,EA9ClB,mBA8CkB,CAAA;CAClD;AAAS,KA5CC,UAAA,GA4CD;;;KAxCC,YAAA;;;KAIA,eAAA;oBACQ;mBACD,eAAe;sBACZ;;KAGV,WAAA;0BACc;0BACA;2BACC,eAAe;2BACf,eAAe;uBACnB;;;2BACI,eAAe;;KAG9B,sBACA,aAAa,sBACb,0BAA0B,mCAC1B,0BAA0B,qCACxB,cAAc,kCACL,0BAA0B,gDACxB,4BAA4B,gDAC9B,0BAA0B,2BAC7C,aAAa,cAAc,gBAAgB;;oBAE3B;mBACD;sBACG;qBACD;uBACE;;KAGX,oCAAoC,YAAY,eAC1D;KAEU,wCAAwC,YAAY,eAC9D"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validators-DY_87pfS.mjs","names":[],"sources":["../src/validators.ts"],"sourcesContent":["import { type } from 'arktype';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlStorage,\n StorageTable,\n StorageTypeInstance,\n UniqueConstraint,\n} from './types';\n\ntype ColumnDefaultLiteral = { readonly kind: 'literal'; readonly expression: string };\ntype ColumnDefaultFunction = { readonly kind: 'function'; readonly expression: string };\nconst literalKindSchema = type(\"'literal'\");\nconst functionKindSchema = type(\"'function'\");\nconst generatorKindSchema = type(\"'generator'\");\nconst generatorIdSchema = type(\"'ulid' | 'nanoid' | 'uuidv7' | 'uuidv4' | 'cuid2' | 'ksuid'\");\n\nexport const ColumnDefaultLiteralSchema = type.declare<ColumnDefaultLiteral>().type({\n kind: literalKindSchema,\n expression: 'string',\n});\n\nexport const ColumnDefaultFunctionSchema = type.declare<ColumnDefaultFunction>().type({\n kind: functionKindSchema,\n expression: 'string',\n});\n\nexport const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);\n\nconst ExecutionMutationDefaultValueSchema = type({\n kind: generatorKindSchema,\n id: generatorIdSchema,\n 'params?': 'Record<string, unknown>',\n});\n\nconst ExecutionMutationDefaultSchema = type({\n ref: {\n table: 'string',\n column: 'string',\n },\n 'onCreate?': ExecutionMutationDefaultValueSchema,\n 'onUpdate?': ExecutionMutationDefaultValueSchema,\n});\n\nconst ExecutionSchema = type({\n mutations: {\n defaults: ExecutionMutationDefaultSchema.array().readonly(),\n },\n});\n\nconst StorageColumnSchema = type({\n nativeType: 'string',\n codecId: 'string',\n nullable: 'boolean',\n 'typeParams?': 'Record<string, unknown>',\n 'typeRef?': 'string',\n 'default?': ColumnDefaultSchema,\n}).narrow((col, ctx) => {\n if (col.typeParams !== undefined && col.typeRef !== undefined) {\n return ctx.mustBe('a column with either typeParams or typeRef, not both');\n }\n return true;\n});\n\nconst StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({\n codecId: 'string',\n nativeType: 'string',\n typeParams: 'Record<string, unknown>',\n});\n\nconst PrimaryKeySchema = type.declare<PrimaryKey>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst UniqueConstraintSchema = type.declare<UniqueConstraint>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst IndexSchema = type.declare<Index>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({\n table: 'string',\n columns: type.string.array().readonly(),\n});\n\nconst ForeignKeySchema = type.declare<ForeignKey>().type({\n columns: type.string.array().readonly(),\n references: ForeignKeyReferencesSchema,\n 'name?': 'string',\n});\n\nconst StorageTableSchema = type.declare<StorageTable>().type({\n columns: type({ '[string]': StorageColumnSchema }),\n 'primaryKey?': PrimaryKeySchema,\n uniques: UniqueConstraintSchema.array().readonly(),\n indexes: IndexSchema.array().readonly(),\n foreignKeys: ForeignKeySchema.array().readonly(),\n});\n\nconst StorageSchema = type.declare<SqlStorage>().type({\n tables: type({ '[string]': StorageTableSchema }),\n 'types?': type({ '[string]': StorageTypeInstanceSchema }),\n});\n\nconst ModelFieldSchema = type.declare<ModelField>().type({\n column: 'string',\n});\n\nconst ModelStorageSchema = type.declare<ModelStorage>().type({\n table: 'string',\n});\n\nconst ModelSchema = type.declare<ModelDefinition>().type({\n storage: ModelStorageSchema,\n fields: type({ '[string]': ModelFieldSchema }),\n relations: type({ '[string]': 'unknown' }),\n});\n\nconst SqlContractSchema = type({\n 'schemaVersion?': \"'1'\",\n target: 'string',\n targetFamily: \"'sql'\",\n storageHash: 'string',\n 'executionHash?': 'string',\n 'profileHash?': 'string',\n 'capabilities?': 'Record<string, Record<string, boolean>>',\n 'extensionPacks?': 'Record<string, unknown>',\n 'meta?': 'Record<string, unknown>',\n 'sources?': 'Record<string, unknown>',\n models: type({ '[string]': ModelSchema }),\n storage: StorageSchema,\n 'execution?': ExecutionSchema,\n});\n\n/**\n * Validates the structural shape of SqlStorage using Arktype.\n *\n * @param value - The storage value to validate\n * @returns The validated storage if structure is valid\n * @throws Error if the storage structure is invalid\n */\nexport function validateStorage(value: unknown): SqlStorage {\n const result = StorageSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Storage validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of ModelDefinition using Arktype.\n *\n * @param value - The model value to validate\n * @returns The validated model if structure is valid\n * @throws Error if the model structure is invalid\n */\nexport function validateModel(value: unknown): ModelDefinition {\n const result = ModelSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Model validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of a SqlContract using Arktype.\n *\n * **Responsibility: Validation Only**\n * This function validates that the contract has the correct structure and types.\n * It does NOT normalize the contract - normalization must happen in the contract builder.\n *\n * The contract passed to this function must already be normalized (all required fields present).\n * If normalization is needed, it should be done by the contract builder before calling this function.\n *\n * This ensures all required fields are present and have the correct types.\n *\n * @param value - The contract value to validate (typically from a JSON import)\n * @returns The validated contract if structure is valid\n * @throws Error if the contract structure is invalid\n */\nexport function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {\n if (typeof value !== 'object' || value === null) {\n throw new Error('Contract structural validation failed: value must be an object');\n }\n\n // Check targetFamily first to provide a clear error message for unsupported target families\n const rawValue = value as { targetFamily?: string };\n if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {\n throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);\n }\n\n const contractResult = SqlContractSchema(value);\n\n if (contractResult instanceof type.errors) {\n const messages = contractResult.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Contract structural validation failed: ${messages}`);\n }\n\n // After validation, contractResult matches the schema and preserves the input structure\n // TypeScript needs an assertion here due to exactOptionalPropertyTypes differences\n // between Arktype's inferred type and the generic T, but runtime-wise they're compatible\n return contractResult as T;\n}\n"],"mappings":";;;AAkBA,MAAM,oBAAoB,KAAK,YAAY;AAC3C,MAAM,qBAAqB,KAAK,aAAa;AAC7C,MAAM,sBAAsB,KAAK,cAAc;AAC/C,M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