@prisma-next/sql-contract 0.3.0-dev.124 → 0.3.0-dev.126

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { D as SqlStorage, N as UniqueConstraint, O as StorageColumn, T as SqlMappings, _ as PrimaryKey, f as Index, k as StorageTable, l as ForeignKey, m as ModelField, p as ModelDefinition, u as ForeignKeyOptions, w as SqlContract } from "./types-mnqqP_Au.mjs";
1
+ import { A as SqlStorage, I as UniqueConstraint, M as StorageTable, T as SqlMappings, _ as PrimaryKey, f as Index, j as StorageColumn, l as ForeignKey, m as ModelField, p as ModelDefinition, u as ForeignKeyOptions, w as SqlContract } from "./types-CB821Pqa.mjs";
2
2
  import { ExecutionHashBase, ProfileHashBase, StorageHashBase } from "@prisma-next/contract/types";
3
3
 
4
4
  //#region src/factories.d.ts
@@ -1,4 +1,4 @@
1
- import { ColumnDefault, ContractBase, ExecutionHashBase, ExecutionSection, ProfileHashBase, StorageHashBase } from "@prisma-next/contract/types";
1
+ import { ColumnDefault, ContractBase, DomainRelationOn, ExecutionHashBase, ExecutionSection, ProfileHashBase, StorageHashBase } from "@prisma-next/contract/types";
2
2
 
3
3
  //#region src/types.d.ts
4
4
 
@@ -113,6 +113,21 @@ type ModelDefinition = {
113
113
  readonly storage: ModelStorage;
114
114
  readonly fields: Record<string, ModelField>;
115
115
  readonly relations: Record<string, unknown>;
116
+ readonly owner?: string;
117
+ };
118
+ type SqlModelFieldStorage = {
119
+ readonly column: string;
120
+ readonly codecId?: string;
121
+ readonly nullable?: boolean;
122
+ };
123
+ type SqlModelStorage = {
124
+ readonly table: string;
125
+ readonly fields: Record<string, SqlModelFieldStorage>;
126
+ };
127
+ type SqlRelation = {
128
+ readonly to: string;
129
+ readonly cardinality: '1:1' | '1:N' | 'N:1';
130
+ readonly on: DomainRelationOn;
116
131
  };
117
132
  type SqlMappings = {
118
133
  readonly modelToTable?: Record<string, string>;
@@ -164,10 +179,9 @@ type QueryOperationTypesOf<T> = [T] extends [never] ? Record<string, never> : T
164
179
  } ? Q extends Record<string, unknown> ? Q : Record<string, never> : Record<string, never>;
165
180
  type TypeMapsPhantomKey = '__@prisma-next/sql-contract/typeMaps@__';
166
181
  type ContractWithTypeMaps<TContract, TTypeMaps> = TContract & { readonly [K in TypeMapsPhantomKey]?: TTypeMaps };
167
- 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> & {
182
+ 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, M> & {
168
183
  readonly targetFamily: string;
169
184
  readonly storage: S;
170
- readonly models: M;
171
185
  readonly relations: R;
172
186
  readonly mappings: Map;
173
187
  readonly execution?: ExecutionSection;
@@ -179,5 +193,5 @@ type ExtractQueryOperationTypes<T> = QueryOperationTypesOf<ExtractTypeMapsFromCo
179
193
  type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never] ? ExtractCodecTypes<TContract> : CodecTypesOf<TTypeMaps>;
180
194
  type ResolveOperationTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never] ? ExtractOperationTypes<TContract> : OperationTypesOf<TTypeMaps>;
181
195
  //#endregion
182
- export { StorageTypeInstance as A, ResolveOperationTypes as C, SqlStorage as D, SqlQueryOperationTypes as E, TypeMapsPhantomKey as M, UniqueConstraint as N, StorageColumn as O, applyFkDefaults as P, ResolveCodecTypes as S, SqlMappings as T, PrimaryKey as _, ExtractCodecTypes as a, QueryOperationTypesOf as b, ExtractTypeMapsFromContract as c, ForeignKeyReferences as d, Index as f, OperationTypesOf as g, ModelStorage as h, DEFAULT_FK_INDEX as i, TypeMaps as j, StorageTable as k, ForeignKey as l, ModelField as m, ContractWithTypeMaps as n, ExtractOperationTypes as o, ModelDefinition as p, DEFAULT_FK_CONSTRAINT as r, ExtractQueryOperationTypes as s, CodecTypesOf as t, ForeignKeyOptions as u, QueryOperationTypeEntry as v, SqlContract as w, ReferentialAction as x, QueryOperationTypesBase as y };
183
- //# sourceMappingURL=types-mnqqP_Au.d.mts.map
196
+ export { SqlStorage as A, ResolveOperationTypes as C, SqlModelStorage as D, SqlModelFieldStorage as E, TypeMapsPhantomKey as F, UniqueConstraint as I, applyFkDefaults as L, StorageTable as M, StorageTypeInstance as N, SqlQueryOperationTypes as O, TypeMaps as P, ResolveCodecTypes as S, SqlMappings as T, PrimaryKey as _, ExtractCodecTypes as a, QueryOperationTypesOf as b, ExtractTypeMapsFromContract as c, ForeignKeyReferences as d, Index as f, OperationTypesOf as g, ModelStorage as h, DEFAULT_FK_INDEX as i, StorageColumn as j, SqlRelation as k, ForeignKey as l, ModelField as m, ContractWithTypeMaps as n, ExtractOperationTypes as o, ModelDefinition as p, DEFAULT_FK_CONSTRAINT as r, ExtractQueryOperationTypes as s, CodecTypesOf as t, ForeignKeyOptions as u, QueryOperationTypeEntry as v, SqlContract as w, ReferentialAction as x, QueryOperationTypesBase as y };
197
+ //# sourceMappingURL=types-CB821Pqa.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types-CB821Pqa.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;;;AAiBA;AAsBA;AAKA;AAKA;AAeA;AAKY,KApDA,aAAA,GAoDiB;EAEjB,SAAA,UAAA,EAAiB,MAAA;EA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AA,QAAA,EAAA,OAAA;;;;;;;KAhDR,iCAAiC,eAAe,4BAA4B;KAE5E,uBAAA,GAA0B,eAAe;KAEzC,4BAA4B,qBACpC,wBACA;;cACY,8BAER,wBACF;KAEM,kBAAA;KAEA,6CAA6C,6BACxC,sBAAsB;KAG3B,sBACA,aAAa,sBACb,0BAA0B,mCAC1B,0BAA0B,qCACxB,cAAc,kCACL,0BAA0B,gDACxB,4BAA4B,gDAC9B,0BAA0B,2BAC7C,aAAa,cAAc,gBAAgB,cAAc;;oBAEzC;sBACE;qBACD;uBACE;;KAGX,iCAAiC,iCAAiC,IAC1E,YAAY,EAAE,2BAA2B;KAGjC,uBAAuB,aAAa,4BAA4B;KAChE,2BAA2B,iBAAiB,4BAA4B;KACxE,gCAAgC,sBAAsB,4BAA4B;KAElF,2CAA2C,6BACnD,kBAAkB,aAClB,aAAa;KAEL,+CAA+C,6BACvD,sBAAsB,aACtB,iBAAiB"}
@@ -1 +1 @@
1
- {"version":3,"file":"types-DRR5stkj.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 * Optional access method identifier.\n * Extension-specific methods are represented as strings and interpreted\n * by the owning extension package.\n */\n readonly using?: string;\n /**\n * Optional extension-owned index configuration payload.\n */\n readonly config?: Record<string, unknown>;\n};\n\nexport type ForeignKeyReferences = {\n readonly table: string;\n readonly columns: readonly string[];\n};\n\nexport type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';\n\nexport type ForeignKeyOptions = {\n readonly name?: string;\n readonly onDelete?: ReferentialAction;\n readonly onUpdate?: ReferentialAction;\n};\n\nexport type ForeignKey = {\n readonly columns: readonly string[];\n readonly references: ForeignKeyReferences;\n readonly name?: string;\n readonly onDelete?: ReferentialAction;\n readonly onUpdate?: ReferentialAction;\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};\n\nexport const DEFAULT_FK_CONSTRAINT = true;\nexport const DEFAULT_FK_INDEX = true;\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 TypeMaps<\n TCodecTypes extends Record<string, { output: unknown }> = Record<string, never>,\n TOperationTypes extends Record<string, unknown> = Record<string, never>,\n TQueryOperationTypes extends Record<string, unknown> = Record<string, never>,\n> = {\n readonly codecTypes: TCodecTypes;\n readonly operationTypes: TOperationTypes;\n readonly queryOperationTypes: TQueryOperationTypes;\n};\n\nexport type CodecTypesOf<T> = [T] extends [never]\n ? Record<string, never>\n : T extends { readonly codecTypes: infer C }\n ? C extends Record<string, { output: unknown }>\n ? C\n : Record<string, never>\n : Record<string, never>;\n\nexport type OperationTypesOf<T> = [T] extends [never]\n ? Record<string, never>\n : T extends { readonly operationTypes: infer O }\n ? O extends Record<string, unknown>\n ? O\n : Record<string, never>\n : Record<string, never>;\n\nexport type QueryOperationTypeEntry = {\n readonly args: readonly { readonly codecId: string; readonly nullable: boolean }[];\n readonly returns: { readonly codecId: string; readonly nullable: boolean };\n};\n\nexport type SqlQueryOperationTypes<T extends Record<string, QueryOperationTypeEntry>> = T;\n\nexport type QueryOperationTypesBase = Record<string, QueryOperationTypeEntry>;\n\nexport type QueryOperationTypesOf<T> = [T] extends [never]\n ? Record<string, never>\n : T extends { readonly queryOperationTypes: infer Q }\n ? Q extends Record<string, unknown>\n ? Q\n : Record<string, never>\n : Record<string, never>;\n\nexport type TypeMapsPhantomKey = '__@prisma-next/sql-contract/typeMaps@__';\n\nexport type ContractWithTypeMaps<TContract, TTypeMaps> = TContract & {\n readonly [K in TypeMapsPhantomKey]?: TTypeMaps;\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 ExtractTypeMapsFromContract<T> = TypeMapsPhantomKey extends keyof T\n ? NonNullable<T[TypeMapsPhantomKey & keyof T]>\n : never;\n\nexport type ExtractCodecTypes<T> = CodecTypesOf<ExtractTypeMapsFromContract<T>>;\nexport type ExtractOperationTypes<T> = OperationTypesOf<ExtractTypeMapsFromContract<T>>;\nexport type ExtractQueryOperationTypes<T> = QueryOperationTypesOf<ExtractTypeMapsFromContract<T>>;\n\nexport type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]\n ? ExtractCodecTypes<TContract>\n : CodecTypesOf<TTypeMaps>;\n\nexport type ResolveOperationTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]\n ? ExtractOperationTypes<TContract>\n : OperationTypesOf<TTypeMaps>;\n"],"mappings":";AA8IA,MAAa,wBAAwB;AACrC,MAAa,mBAAmB;AAEhC,SAAgB,gBACd,IACA,kBACyC;AACzC,QAAO;EACL,YAAY,GAAG,cAAc,kBAAkB,cAAc;EAC7D,OAAO,GAAG,SAAS,kBAAkB,SAAS;EAC/C"}
1
+ {"version":3,"file":"types-DRR5stkj.mjs","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type {\n ColumnDefault,\n ContractBase,\n DomainRelationOn,\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 * Optional access method identifier.\n * Extension-specific methods are represented as strings and interpreted\n * by the owning extension package.\n */\n readonly using?: string;\n /**\n * Optional extension-owned index configuration payload.\n */\n readonly config?: Record<string, unknown>;\n};\n\nexport type ForeignKeyReferences = {\n readonly table: string;\n readonly columns: readonly string[];\n};\n\nexport type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';\n\nexport type ForeignKeyOptions = {\n readonly name?: string;\n readonly onDelete?: ReferentialAction;\n readonly onUpdate?: ReferentialAction;\n};\n\nexport type ForeignKey = {\n readonly columns: readonly string[];\n readonly references: ForeignKeyReferences;\n readonly name?: string;\n readonly onDelete?: ReferentialAction;\n readonly onUpdate?: ReferentialAction;\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 readonly owner?: string;\n};\n\nexport type SqlModelFieldStorage = {\n readonly column: string;\n readonly codecId?: string;\n readonly nullable?: boolean;\n};\n\nexport type SqlModelStorage = {\n readonly table: string;\n readonly fields: Record<string, SqlModelFieldStorage>;\n};\n\nexport type SqlRelation = {\n readonly to: string;\n readonly cardinality: '1:1' | '1:N' | 'N:1';\n readonly on: DomainRelationOn;\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};\n\nexport const DEFAULT_FK_CONSTRAINT = true;\nexport const DEFAULT_FK_INDEX = true;\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 TypeMaps<\n TCodecTypes extends Record<string, { output: unknown }> = Record<string, never>,\n TOperationTypes extends Record<string, unknown> = Record<string, never>,\n TQueryOperationTypes extends Record<string, unknown> = Record<string, never>,\n> = {\n readonly codecTypes: TCodecTypes;\n readonly operationTypes: TOperationTypes;\n readonly queryOperationTypes: TQueryOperationTypes;\n};\n\nexport type CodecTypesOf<T> = [T] extends [never]\n ? Record<string, never>\n : T extends { readonly codecTypes: infer C }\n ? C extends Record<string, { output: unknown }>\n ? C\n : Record<string, never>\n : Record<string, never>;\n\nexport type OperationTypesOf<T> = [T] extends [never]\n ? Record<string, never>\n : T extends { readonly operationTypes: infer O }\n ? O extends Record<string, unknown>\n ? O\n : Record<string, never>\n : Record<string, never>;\n\nexport type QueryOperationTypeEntry = {\n readonly args: readonly { readonly codecId: string; readonly nullable: boolean }[];\n readonly returns: { readonly codecId: string; readonly nullable: boolean };\n};\n\nexport type SqlQueryOperationTypes<T extends Record<string, QueryOperationTypeEntry>> = T;\n\nexport type QueryOperationTypesBase = Record<string, QueryOperationTypeEntry>;\n\nexport type QueryOperationTypesOf<T> = [T] extends [never]\n ? Record<string, never>\n : T extends { readonly queryOperationTypes: infer Q }\n ? Q extends Record<string, unknown>\n ? Q\n : Record<string, never>\n : Record<string, never>;\n\nexport type TypeMapsPhantomKey = '__@prisma-next/sql-contract/typeMaps@__';\n\nexport type ContractWithTypeMaps<TContract, TTypeMaps> = TContract & {\n readonly [K in TypeMapsPhantomKey]?: TTypeMaps;\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, M> & {\n readonly targetFamily: string;\n readonly storage: S;\n readonly relations: R;\n readonly mappings: Map;\n readonly execution?: ExecutionSection;\n};\n\nexport type ExtractTypeMapsFromContract<T> = TypeMapsPhantomKey extends keyof T\n ? NonNullable<T[TypeMapsPhantomKey & keyof T]>\n : never;\n\nexport type ExtractCodecTypes<T> = CodecTypesOf<ExtractTypeMapsFromContract<T>>;\nexport type ExtractOperationTypes<T> = OperationTypesOf<ExtractTypeMapsFromContract<T>>;\nexport type ExtractQueryOperationTypes<T> = QueryOperationTypesOf<ExtractTypeMapsFromContract<T>>;\n\nexport type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]\n ? ExtractCodecTypes<TContract>\n : CodecTypesOf<TTypeMaps>;\n\nexport type ResolveOperationTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]\n ? ExtractOperationTypes<TContract>\n : OperationTypesOf<TTypeMaps>;\n"],"mappings":";AAiKA,MAAa,wBAAwB;AACrC,MAAa,mBAAmB;AAEhC,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 StorageTypeInstance, C as ResolveOperationTypes, D as SqlStorage, E as SqlQueryOperationTypes, M as TypeMapsPhantomKey, N as UniqueConstraint, O as StorageColumn, P as applyFkDefaults, S as ResolveCodecTypes, T as SqlMappings, _ as PrimaryKey, a as ExtractCodecTypes, b as QueryOperationTypesOf, c as ExtractTypeMapsFromContract, d as ForeignKeyReferences, f as Index, g as OperationTypesOf, h as ModelStorage, i as DEFAULT_FK_INDEX, j as TypeMaps, k as StorageTable, l as ForeignKey, m as ModelField, n as ContractWithTypeMaps, o as ExtractOperationTypes, p as ModelDefinition, r as DEFAULT_FK_CONSTRAINT, s as ExtractQueryOperationTypes, t as CodecTypesOf, u as ForeignKeyOptions, v as QueryOperationTypeEntry, w as SqlContract, x as ReferentialAction, y as QueryOperationTypesBase } from "./types-mnqqP_Au.mjs";
2
- export { type CodecTypesOf, type ContractWithTypeMaps, DEFAULT_FK_CONSTRAINT, DEFAULT_FK_INDEX, type ExtractCodecTypes, type ExtractOperationTypes, type ExtractQueryOperationTypes, type ExtractTypeMapsFromContract, type ForeignKey, type ForeignKeyOptions, type ForeignKeyReferences, type Index, type ModelDefinition, type ModelField, type ModelStorage, type OperationTypesOf, type PrimaryKey, type QueryOperationTypeEntry, type QueryOperationTypesBase, type QueryOperationTypesOf, type ReferentialAction, type ResolveCodecTypes, type ResolveOperationTypes, type SqlContract, type SqlMappings, type SqlQueryOperationTypes, type SqlStorage, type StorageColumn, type StorageTable, type StorageTypeInstance, type TypeMaps, type TypeMapsPhantomKey, type UniqueConstraint, applyFkDefaults };
1
+ import { A as SqlStorage, C as ResolveOperationTypes, D as SqlModelStorage, E as SqlModelFieldStorage, F as TypeMapsPhantomKey, I as UniqueConstraint, L as applyFkDefaults, M as StorageTable, N as StorageTypeInstance, O as SqlQueryOperationTypes, P as TypeMaps, S as ResolveCodecTypes, T as SqlMappings, _ as PrimaryKey, a as ExtractCodecTypes, b as QueryOperationTypesOf, c as ExtractTypeMapsFromContract, d as ForeignKeyReferences, f as Index, g as OperationTypesOf, h as ModelStorage, i as DEFAULT_FK_INDEX, j as StorageColumn, k as SqlRelation, l as ForeignKey, m as ModelField, n as ContractWithTypeMaps, o as ExtractOperationTypes, p as ModelDefinition, r as DEFAULT_FK_CONSTRAINT, s as ExtractQueryOperationTypes, t as CodecTypesOf, u as ForeignKeyOptions, v as QueryOperationTypeEntry, w as SqlContract, x as ReferentialAction, y as QueryOperationTypesBase } from "./types-CB821Pqa.mjs";
2
+ export { type CodecTypesOf, type ContractWithTypeMaps, DEFAULT_FK_CONSTRAINT, DEFAULT_FK_INDEX, type ExtractCodecTypes, type ExtractOperationTypes, type ExtractQueryOperationTypes, type ExtractTypeMapsFromContract, type ForeignKey, type ForeignKeyOptions, type ForeignKeyReferences, type Index, type ModelDefinition, type ModelField, type ModelStorage, type OperationTypesOf, type PrimaryKey, type QueryOperationTypeEntry, type QueryOperationTypesBase, type QueryOperationTypesOf, type ReferentialAction, type ResolveCodecTypes, type ResolveOperationTypes, type SqlContract, type SqlMappings, type SqlModelFieldStorage, type SqlModelStorage, type SqlQueryOperationTypes, type SqlRelation, type SqlStorage, type StorageColumn, type StorageTable, type StorageTypeInstance, type TypeMaps, type TypeMapsPhantomKey, type UniqueConstraint, applyFkDefaults };
@@ -1,4 +1,4 @@
1
- import { D as SqlStorage, O as StorageColumn, w as SqlContract } from "./types-mnqqP_Au.mjs";
1
+ import { A as SqlStorage, j as StorageColumn, w as SqlContract } from "./types-CB821Pqa.mjs";
2
2
  import "@prisma-next/contract/types";
3
3
 
4
4
  //#region src/validate.d.ts
@@ -1 +1 @@
1
- {"version":3,"file":"validate.d.mts","names":[],"sources":["../src/validate.ts"],"sourcesContent":[],"mappings":";;;;iBAuFgB,cAAA,SAAuB;AAAvB,iBAkCA,sBAlCuB,CAAa,UAkCH,WAlCG,CAkCS,UAlCT,CAAA,CAAA,CAAA,QAAA,EAkCgC,CAlChC,CAAA,EAkCoC,CAlCpC;AAkCpC,iBAqDA,iBAAA,CArDsB,QAAA,EAAA,OAAA,CAAA,EAqDgB,WArDhB,CAqD4B,UArD5B,CAAA;AAAuB,iBAwI7C,gBAxI6C,CAAA,kBAwIV,WAxIU,CAwIE,UAxIF,CAAA,CAAA,CAAA,KAAA,EAAA,OAAA,CAAA,EA0I1D,SA1I0D"}
1
+ {"version":3,"file":"validate.d.mts","names":[],"sources":["../src/validate.ts"],"sourcesContent":[],"mappings":";;;;iBAgGgB,cAAA,SAAuB;AAAvB,iBAkCA,sBAlCuB,CAAa,UAkCH,WAlCG,CAkCS,UAlCT,CAAA,CAAA,CAAA,QAAA,EAkCgC,CAlChC,CAAA,EAkCoC,CAlCpC;AAkCpC,iBAmVA,iBAAA,CAnVsB,QAAA,EAAA,OAAA,CAAA,EAmVgB,WAnVhB,CAmV4B,UAnV5B,CAAA;AAAuB,iBA2Z7C,gBA3Z6C,CAAA,kBA2ZV,WA3ZU,CA2ZE,UA3ZF,CAAA,CAAA,CAAA,KAAA,EAAA,OAAA,CAAA,EA6Z1D,SA7Z0D"}
package/dist/validate.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { r as applyFkDefaults } from "./types-DRR5stkj.mjs";
2
- import { d as validateStorageSemantics, l as validateSqlContract } from "./validators-DuHeCiKZ.mjs";
2
+ import { d as validateStorageSemantics, l as validateSqlContract } from "./validators-CNxeypbZ.mjs";
3
3
  import { isTaggedBigInt, isTaggedRaw } from "@prisma-next/contract/types";
4
+ import { validateContractDomain } from "@prisma-next/contract/validate-domain";
4
5
 
5
6
  //#region src/construct.ts
6
7
  function computeDefaultMappings(models) {
@@ -75,14 +76,22 @@ function stripGenerated(obj) {
75
76
  function constructContract(input) {
76
77
  const existingMappings = input.mappings;
77
78
  const mappings = mergeMappings(computeDefaultMappings(input.models), existingMappings);
79
+ const stripped = stripGenerated(input);
78
80
  return {
79
- ...stripGenerated(input),
80
- mappings
81
+ ...stripped,
82
+ mappings,
83
+ roots: stripped.roots
81
84
  };
82
85
  }
83
86
 
84
87
  //#endregion
85
88
  //#region src/validate.ts
89
+ function extractDomainShape(contract) {
90
+ return {
91
+ roots: contract.roots,
92
+ models: contract.models
93
+ };
94
+ }
86
95
  function validateContractLogic(contract) {
87
96
  const tableNames = new Set(Object.keys(contract.storage.tables));
88
97
  for (const [tableName, table] of Object.entries(contract.storage.tables)) {
@@ -164,65 +173,216 @@ function decodeContractDefaults(contract) {
164
173
  }
165
174
  };
166
175
  }
167
- function normalizeContract(contract) {
168
- if (typeof contract !== "object" || contract === null) return contract;
169
- const contractObj = contract;
170
- let normalizedStorage = contractObj["storage"];
171
- if (normalizedStorage && typeof normalizedStorage === "object" && normalizedStorage !== null) {
172
- const storage = normalizedStorage;
173
- const tables = storage["tables"];
174
- if (tables) {
175
- const normalizedTables = {};
176
- for (const [tableName, table] of Object.entries(tables)) {
177
- const tableObj = table;
178
- const columns = tableObj["columns"];
179
- if (columns) {
180
- const normalizedColumns = {};
181
- for (const [columnName, column] of Object.entries(columns)) {
182
- const columnObj = column;
183
- normalizedColumns[columnName] = {
184
- ...columnObj,
185
- nullable: columnObj["nullable"] ?? false
186
- };
187
- }
188
- const normalizedForeignKeys = (tableObj["foreignKeys"] ?? []).map((fk) => ({
189
- ...fk,
190
- ...applyFkDefaults({
191
- constraint: typeof fk["constraint"] === "boolean" ? fk["constraint"] : void 0,
192
- index: typeof fk["index"] === "boolean" ? fk["index"] : void 0
193
- })
194
- }));
195
- normalizedTables[tableName] = {
196
- ...tableObj,
197
- columns: normalizedColumns,
198
- uniques: tableObj["uniques"] ?? [],
199
- indexes: tableObj["indexes"] ?? [],
200
- foreignKeys: normalizedForeignKeys
201
- };
202
- } else normalizedTables[tableName] = tableObj;
176
+ function normalizeStorage(contractObj) {
177
+ const normalizedStorage = contractObj["storage"];
178
+ if (!normalizedStorage || typeof normalizedStorage !== "object") return normalizedStorage;
179
+ const storage = normalizedStorage;
180
+ const tables = storage["tables"];
181
+ if (!tables) return storage;
182
+ const normalizedTables = {};
183
+ for (const [tableName, table] of Object.entries(tables)) {
184
+ const tableObj = table;
185
+ const columns = tableObj["columns"];
186
+ if (columns) {
187
+ const normalizedColumns = {};
188
+ for (const [columnName, column] of Object.entries(columns)) {
189
+ const columnObj = column;
190
+ normalizedColumns[columnName] = {
191
+ ...columnObj,
192
+ nullable: columnObj["nullable"] ?? false
193
+ };
203
194
  }
204
- normalizedStorage = {
205
- ...storage,
206
- tables: normalizedTables
195
+ const normalizedForeignKeys = (tableObj["foreignKeys"] ?? []).map((fk) => ({
196
+ ...fk,
197
+ ...applyFkDefaults({
198
+ constraint: typeof fk["constraint"] === "boolean" ? fk["constraint"] : void 0,
199
+ index: typeof fk["index"] === "boolean" ? fk["index"] : void 0
200
+ })
201
+ }));
202
+ normalizedTables[tableName] = {
203
+ ...tableObj,
204
+ columns: normalizedColumns,
205
+ uniques: tableObj["uniques"] ?? [],
206
+ indexes: tableObj["indexes"] ?? [],
207
+ foreignKeys: normalizedForeignKeys
207
208
  };
209
+ } else normalizedTables[tableName] = tableObj;
210
+ }
211
+ return {
212
+ ...storage,
213
+ tables: normalizedTables
214
+ };
215
+ }
216
+ function detectFormat(models) {
217
+ for (const model of Object.values(models)) {
218
+ const fields = model["fields"];
219
+ if (!fields) continue;
220
+ for (const field of Object.values(fields)) {
221
+ if ("column" in field) return "old";
222
+ if ("codecId" in field) return "new";
208
223
  }
209
224
  }
210
- let normalizedModels = contractObj["models"];
211
- if (normalizedModels && typeof normalizedModels === "object" && normalizedModels !== null) {
212
- const models = normalizedModels;
213
- const normalizedModelsObj = {};
214
- for (const [modelName, model] of Object.entries(models)) {
215
- const modelObj = model;
216
- normalizedModelsObj[modelName] = {
217
- ...modelObj,
218
- relations: modelObj["relations"] ?? {}
225
+ return "old";
226
+ }
227
+ function buildColumnToFieldMap(fields, modelName) {
228
+ const map = {};
229
+ for (const [fieldName, field] of Object.entries(fields)) {
230
+ const col = field["column"];
231
+ if (!col) continue;
232
+ if (Object.hasOwn(map, col)) throw new Error(`Model "${modelName}" has duplicate column mapping: fields "${map[col]}" and "${fieldName}" both map to column "${col}"`);
233
+ map[col] = fieldName;
234
+ }
235
+ return map;
236
+ }
237
+ function enrichOldFormatModels(models, storageObj, topRelations) {
238
+ const roots = {};
239
+ const tableToModel = {};
240
+ for (const [modelName, model] of Object.entries(models)) {
241
+ const tableName = model["storage"]?.["table"];
242
+ if (tableName) {
243
+ if (!model["owner"]) roots[modelName] = modelName;
244
+ tableToModel[tableName] = modelName;
245
+ }
246
+ }
247
+ const enrichedModels = {};
248
+ for (const [modelName, model] of Object.entries(models)) {
249
+ const fields = model["fields"] ?? {};
250
+ const modelStorage = model["storage"];
251
+ const tableName = modelStorage?.["table"];
252
+ const storageColumns = (tableName ? storageObj.tables[tableName] : void 0)?.["columns"] ?? {};
253
+ const enrichedFields = {};
254
+ const modelStorageFields = {};
255
+ const hasStorageColumns = Object.keys(storageColumns).length > 0;
256
+ for (const [fieldName, field] of Object.entries(fields)) {
257
+ const colName = field["column"];
258
+ const storageCol = storageColumns[colName];
259
+ if (!storageCol && hasStorageColumns && colName) throw new Error(`Model "${modelName}" field "${fieldName}" references non-existent column "${colName}" in table "${tableName}"`);
260
+ enrichedFields[fieldName] = {
261
+ ...field,
262
+ nullable: storageCol?.["nullable"] ?? false,
263
+ codecId: storageCol?.["codecId"] ?? ""
219
264
  };
265
+ modelStorageFields[fieldName] = { column: colName };
220
266
  }
221
- normalizedModels = normalizedModelsObj;
267
+ const enrichedStorage = {
268
+ ...modelStorage ?? {},
269
+ fields: modelStorageFields
270
+ };
271
+ enrichedModels[modelName] = {
272
+ ...model,
273
+ fields: enrichedFields,
274
+ storage: enrichedStorage,
275
+ relations: model["relations"] ?? {}
276
+ };
277
+ }
278
+ for (const [tableName, tableRels] of Object.entries(topRelations)) {
279
+ const modelName = tableToModel[tableName];
280
+ if (!modelName) continue;
281
+ const existingModel = enrichedModels[modelName];
282
+ if (!existingModel) continue;
283
+ const existingRels = existingModel["relations"] ?? {};
284
+ const targetColumnToField = {};
285
+ const modelRelations = { ...existingRels };
286
+ for (const [relName, rel] of Object.entries(tableRels)) {
287
+ const on = rel["on"];
288
+ const parentCols = on?.["parentCols"] ?? [];
289
+ const childCols = on?.["childCols"] ?? [];
290
+ const toModel = rel["to"];
291
+ const sourceColToField = buildColumnToFieldMap(existingModel["fields"] ?? {}, modelName);
292
+ if (!targetColumnToField[toModel]) {
293
+ const targetModelObj = enrichedModels[toModel];
294
+ if (targetModelObj) targetColumnToField[toModel] = buildColumnToFieldMap(targetModelObj["fields"] ?? {}, toModel);
295
+ else targetColumnToField[toModel] = {};
296
+ }
297
+ const targetColToField = targetColumnToField[toModel] ?? {};
298
+ const localFields = parentCols.map((c) => sourceColToField[c] ?? c);
299
+ const targetFields = childCols.map((c) => targetColToField[c] ?? c);
300
+ modelRelations[relName] = {
301
+ to: toModel,
302
+ cardinality: rel["cardinality"],
303
+ on: {
304
+ localFields,
305
+ targetFields
306
+ }
307
+ };
308
+ }
309
+ enrichedModels[modelName] = {
310
+ ...existingModel,
311
+ relations: modelRelations
312
+ };
222
313
  }
223
314
  return {
315
+ enrichedModels,
316
+ roots
317
+ };
318
+ }
319
+ function enrichNewFormatModels(models) {
320
+ const enrichedModels = {};
321
+ const topRelations = {};
322
+ const modelToTable = {};
323
+ for (const [modelName, model] of Object.entries(models)) {
324
+ const tableName = model["storage"]?.["table"];
325
+ if (tableName) modelToTable[modelName] = tableName;
326
+ }
327
+ for (const [modelName, model] of Object.entries(models)) {
328
+ const fields = model["fields"] ?? {};
329
+ const storageFields = model["storage"]?.["fields"] ?? {};
330
+ const enrichedFields = {};
331
+ for (const [fieldName, field] of Object.entries(fields)) {
332
+ const column = storageFields[fieldName]?.["column"];
333
+ enrichedFields[fieldName] = column ? {
334
+ ...field,
335
+ column
336
+ } : { ...field };
337
+ }
338
+ enrichedModels[modelName] = {
339
+ ...model,
340
+ fields: enrichedFields,
341
+ relations: model["relations"] ?? {}
342
+ };
343
+ const modelRels = model["relations"] ?? {};
344
+ const tableName = modelToTable[modelName];
345
+ if (!tableName) continue;
346
+ for (const [relName, rel] of Object.entries(modelRels)) {
347
+ const on = rel["on"];
348
+ if (!on) continue;
349
+ const toModel = rel["to"];
350
+ if (!modelToTable[toModel]) continue;
351
+ const sourceFields = enrichedFields;
352
+ const targetModelObj = models[toModel];
353
+ const targetFields = targetModelObj?.["fields"] ?? {};
354
+ const targetStorageFields = (targetModelObj?.["storage"])?.["fields"] ?? {};
355
+ const parentCols = (on.localFields ?? []).map((f) => {
356
+ return storageFields[f]?.["column"] ?? sourceFields[f]?.["column"] ?? f;
357
+ });
358
+ const childCols = (on.targetFields ?? []).map((f) => {
359
+ return targetStorageFields[f]?.["column"] ?? targetFields[f]?.["column"] ?? f;
360
+ });
361
+ if (!topRelations[tableName]) topRelations[tableName] = {};
362
+ topRelations[tableName][relName] = {
363
+ to: toModel,
364
+ cardinality: rel["cardinality"],
365
+ on: {
366
+ parentCols,
367
+ childCols
368
+ }
369
+ };
370
+ }
371
+ }
372
+ return {
373
+ enrichedModels,
374
+ topRelations
375
+ };
376
+ }
377
+ function normalizeContract(contract) {
378
+ if (typeof contract !== "object" || contract === null) return contract;
379
+ const contractObj = contract;
380
+ const normalizedStorage = normalizeStorage(contractObj);
381
+ const rawModels = contractObj["models"];
382
+ if (!rawModels || typeof rawModels !== "object" || rawModels === null) return {
224
383
  ...contractObj,
225
- models: normalizedModels,
384
+ roots: contractObj["roots"] ?? {},
385
+ models: rawModels ?? {},
226
386
  relations: contractObj["relations"] ?? {},
227
387
  storage: normalizedStorage,
228
388
  extensionPacks: contractObj["extensionPacks"] ?? {},
@@ -230,9 +390,42 @@ function normalizeContract(contract) {
230
390
  meta: contractObj["meta"] ?? {},
231
391
  sources: contractObj["sources"] ?? {}
232
392
  };
393
+ const modelsObj = rawModels;
394
+ const format = detectFormat(modelsObj);
395
+ let normalizedModels;
396
+ let roots;
397
+ let topRelations;
398
+ if (format === "new") {
399
+ const result = enrichNewFormatModels(modelsObj);
400
+ normalizedModels = result.enrichedModels;
401
+ topRelations = {
402
+ ...contractObj["relations"] ?? {},
403
+ ...result.topRelations
404
+ };
405
+ roots = contractObj["roots"] ?? {};
406
+ } else {
407
+ const storageObj = { tables: (normalizedStorage && typeof normalizedStorage === "object" ? normalizedStorage : {})["tables"] ?? {} };
408
+ const existingRelations = contractObj["relations"] ?? {};
409
+ const result = enrichOldFormatModels(modelsObj, storageObj, existingRelations);
410
+ normalizedModels = result.enrichedModels;
411
+ roots = result.roots;
412
+ topRelations = existingRelations;
413
+ }
414
+ return {
415
+ ...contractObj,
416
+ roots,
417
+ models: normalizedModels,
418
+ relations: topRelations,
419
+ storage: normalizedStorage,
420
+ extensionPacks: contractObj["extensionPacks"] ?? {},
421
+ capabilities: contractObj["capabilities"] ?? {},
422
+ meta: contractObj["meta"] ?? {},
423
+ sources: contractObj["sources"] ?? {}
424
+ };
233
425
  }
234
426
  function validateContract(value) {
235
427
  const structurallyValid = validateSqlContract(normalizeContract(value));
428
+ validateContractDomain(extractDomainShape(structurallyValid));
236
429
  validateContractLogic(structurallyValid);
237
430
  const semanticErrors = validateStorageSemantics(structurallyValid.storage);
238
431
  if (semanticErrors.length > 0) throw new Error(`Contract semantic validation failed: ${semanticErrors.join("; ")}`);