@prisma-next/sql-contract-ts 0.3.0-pr.94.3 → 0.3.0-pr.95.2
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/dist/chunk-HTNUNGA2.js +346 -0
- package/dist/chunk-HTNUNGA2.js.map +1 -0
- package/dist/contract-builder.d.ts +101 -0
- package/dist/contract-builder.d.ts.map +1 -0
- package/dist/{contract.d.mts → contract.d.ts} +5 -9
- package/dist/contract.d.ts.map +1 -0
- package/dist/exports/contract-builder.d.ts +3 -0
- package/dist/exports/contract-builder.d.ts.map +1 -0
- package/dist/exports/contract-builder.js +231 -0
- package/dist/exports/contract-builder.js.map +1 -0
- package/dist/exports/contract.d.ts +2 -0
- package/dist/exports/contract.d.ts.map +1 -0
- package/dist/exports/contract.js +9 -0
- package/dist/exports/contract.js.map +1 -0
- package/package.json +17 -15
- package/schemas/data-contract-sql-v1.json +46 -4
- package/src/contract.ts +49 -0
- package/dist/contract-CbxDA5bF.mjs +0 -331
- package/dist/contract-CbxDA5bF.mjs.map +0 -1
- package/dist/contract-builder.d.mts +0 -100
- package/dist/contract-builder.d.mts.map +0 -1
- package/dist/contract-builder.mjs +0 -184
- package/dist/contract-builder.mjs.map +0 -1
- package/dist/contract.d.mts.map +0 -1
- package/dist/contract.mjs +0 -3
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computeMappings
|
|
3
|
+
} from "../chunk-HTNUNGA2.js";
|
|
4
|
+
|
|
5
|
+
// src/contract-builder.ts
|
|
6
|
+
import {
|
|
7
|
+
ContractBuilder,
|
|
8
|
+
createTable,
|
|
9
|
+
ModelBuilder,
|
|
10
|
+
TableBuilder
|
|
11
|
+
} from "@prisma-next/contract-authoring";
|
|
12
|
+
var SqlContractBuilder = class _SqlContractBuilder extends ContractBuilder {
|
|
13
|
+
/**
|
|
14
|
+
* This method is responsible for normalizing the contract IR by setting default values
|
|
15
|
+
* for all required fields:
|
|
16
|
+
* - `nullable`: defaults to `false` if not provided
|
|
17
|
+
* - `uniques`: defaults to `[]` (empty array)
|
|
18
|
+
* - `indexes`: defaults to `[]` (empty array)
|
|
19
|
+
* - `foreignKeys`: defaults to `[]` (empty array)
|
|
20
|
+
* - `relations`: defaults to `{}` (empty object) for both model-level and contract-level
|
|
21
|
+
* - `nativeType`: required field set from column type descriptor when columns are defined
|
|
22
|
+
*
|
|
23
|
+
* The contract builder is the **only** place where normalization should occur.
|
|
24
|
+
* Validators, parsers, and emitters should assume the contract is already normalized.
|
|
25
|
+
*
|
|
26
|
+
* **Required**: Use column type descriptors (e.g., `int4Column`, `textColumn`) when defining columns.
|
|
27
|
+
* This ensures `nativeType` is set correctly at build time.
|
|
28
|
+
*
|
|
29
|
+
* @returns A normalized SqlContract with all required fields present
|
|
30
|
+
*/
|
|
31
|
+
build() {
|
|
32
|
+
if (!this.state.target) {
|
|
33
|
+
throw new Error("target is required. Call .target() before .build()");
|
|
34
|
+
}
|
|
35
|
+
const target = this.state.target;
|
|
36
|
+
const storageTables = {};
|
|
37
|
+
for (const tableName of Object.keys(this.state.tables)) {
|
|
38
|
+
const tableState = this.state.tables[tableName];
|
|
39
|
+
if (!tableState) continue;
|
|
40
|
+
const columns = {};
|
|
41
|
+
for (const columnName in tableState.columns) {
|
|
42
|
+
const columnState = tableState.columns[columnName];
|
|
43
|
+
if (!columnState) continue;
|
|
44
|
+
const codecId = columnState.type;
|
|
45
|
+
const nativeType = columnState.nativeType;
|
|
46
|
+
columns[columnName] = {
|
|
47
|
+
nativeType,
|
|
48
|
+
codecId,
|
|
49
|
+
nullable: columnState.nullable ?? false
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const uniques = (tableState.uniques ?? []).map((u) => ({
|
|
53
|
+
columns: u.columns,
|
|
54
|
+
...u.name ? { name: u.name } : {}
|
|
55
|
+
}));
|
|
56
|
+
const indexes = (tableState.indexes ?? []).map((i) => ({
|
|
57
|
+
columns: i.columns,
|
|
58
|
+
...i.name ? { name: i.name } : {}
|
|
59
|
+
}));
|
|
60
|
+
const foreignKeys = (tableState.foreignKeys ?? []).map((fk) => ({
|
|
61
|
+
columns: fk.columns,
|
|
62
|
+
references: fk.references,
|
|
63
|
+
...fk.name ? { name: fk.name } : {}
|
|
64
|
+
}));
|
|
65
|
+
const table = {
|
|
66
|
+
columns,
|
|
67
|
+
uniques,
|
|
68
|
+
indexes,
|
|
69
|
+
foreignKeys,
|
|
70
|
+
...tableState.primaryKey ? {
|
|
71
|
+
primaryKey: {
|
|
72
|
+
columns: tableState.primaryKey,
|
|
73
|
+
...tableState.primaryKeyName ? { name: tableState.primaryKeyName } : {}
|
|
74
|
+
}
|
|
75
|
+
} : {}
|
|
76
|
+
};
|
|
77
|
+
storageTables[tableName] = table;
|
|
78
|
+
}
|
|
79
|
+
const storage = { tables: storageTables };
|
|
80
|
+
const modelsPartial = {};
|
|
81
|
+
for (const modelName in this.state.models) {
|
|
82
|
+
const modelState = this.state.models[modelName];
|
|
83
|
+
if (!modelState) continue;
|
|
84
|
+
const modelStateTyped = modelState;
|
|
85
|
+
const fields = {};
|
|
86
|
+
for (const fieldName in modelStateTyped.fields) {
|
|
87
|
+
const columnName = modelStateTyped.fields[fieldName];
|
|
88
|
+
if (columnName) {
|
|
89
|
+
fields[fieldName] = {
|
|
90
|
+
column: columnName
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
modelsPartial[modelName] = {
|
|
95
|
+
storage: {
|
|
96
|
+
table: modelStateTyped.table
|
|
97
|
+
},
|
|
98
|
+
fields,
|
|
99
|
+
relations: {}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const relationsPartial = {};
|
|
103
|
+
for (const modelName in this.state.models) {
|
|
104
|
+
const modelState = this.state.models[modelName];
|
|
105
|
+
if (!modelState) continue;
|
|
106
|
+
const modelStateTyped = modelState;
|
|
107
|
+
const tableName = modelStateTyped.table;
|
|
108
|
+
if (!tableName) continue;
|
|
109
|
+
if (modelStateTyped.relations && Object.keys(modelStateTyped.relations).length > 0) {
|
|
110
|
+
if (!relationsPartial[tableName]) {
|
|
111
|
+
relationsPartial[tableName] = {};
|
|
112
|
+
}
|
|
113
|
+
const tableRelations = relationsPartial[tableName];
|
|
114
|
+
if (tableRelations) {
|
|
115
|
+
for (const relationName in modelStateTyped.relations) {
|
|
116
|
+
const relation = modelStateTyped.relations[relationName];
|
|
117
|
+
if (relation) {
|
|
118
|
+
tableRelations[relationName] = relation;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const models = modelsPartial;
|
|
125
|
+
const baseMappings = computeMappings(
|
|
126
|
+
models,
|
|
127
|
+
storage
|
|
128
|
+
);
|
|
129
|
+
const mappings = {
|
|
130
|
+
...baseMappings,
|
|
131
|
+
codecTypes: {},
|
|
132
|
+
operationTypes: {}
|
|
133
|
+
};
|
|
134
|
+
const extensionNamespaces = this.state.extensionNamespaces ?? [];
|
|
135
|
+
const extensionPacks = { ...this.state.extensionPacks || {} };
|
|
136
|
+
for (const namespace of extensionNamespaces) {
|
|
137
|
+
if (!Object.hasOwn(extensionPacks, namespace)) {
|
|
138
|
+
extensionPacks[namespace] = {};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const contract = {
|
|
142
|
+
schemaVersion: "1",
|
|
143
|
+
target,
|
|
144
|
+
targetFamily: "sql",
|
|
145
|
+
coreHash: this.state.coreHash || "sha256:ts-builder-placeholder",
|
|
146
|
+
models,
|
|
147
|
+
relations: relationsPartial,
|
|
148
|
+
storage,
|
|
149
|
+
mappings,
|
|
150
|
+
extensionPacks,
|
|
151
|
+
capabilities: this.state.capabilities || {},
|
|
152
|
+
meta: {},
|
|
153
|
+
sources: {}
|
|
154
|
+
};
|
|
155
|
+
return contract;
|
|
156
|
+
}
|
|
157
|
+
target(packRef) {
|
|
158
|
+
return new _SqlContractBuilder({
|
|
159
|
+
...this.state,
|
|
160
|
+
target: packRef.targetId
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
extensionPacks(packs) {
|
|
164
|
+
if (!this.state.target) {
|
|
165
|
+
throw new Error("extensionPacks() requires target() to be called first");
|
|
166
|
+
}
|
|
167
|
+
const namespaces = new Set(this.state.extensionNamespaces ?? []);
|
|
168
|
+
for (const packRef of Object.values(packs)) {
|
|
169
|
+
if (!packRef) continue;
|
|
170
|
+
if (packRef.kind !== "extension") {
|
|
171
|
+
throw new Error(
|
|
172
|
+
`extensionPacks() only accepts extension pack refs. Received kind "${packRef.kind}".`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (packRef.familyId !== "sql") {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`extension pack "${packRef.id}" targets family "${packRef.familyId}" but this builder targets "sql".`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (packRef.targetId && packRef.targetId !== this.state.target) {
|
|
181
|
+
throw new Error(
|
|
182
|
+
`extension pack "${packRef.id}" targets "${packRef.targetId}" but builder target is "${this.state.target}".`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
namespaces.add(packRef.id);
|
|
186
|
+
}
|
|
187
|
+
return new _SqlContractBuilder({
|
|
188
|
+
...this.state,
|
|
189
|
+
extensionNamespaces: [...namespaces]
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
capabilities(capabilities) {
|
|
193
|
+
return new _SqlContractBuilder({
|
|
194
|
+
...this.state,
|
|
195
|
+
capabilities
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
coreHash(hash) {
|
|
199
|
+
return new _SqlContractBuilder({
|
|
200
|
+
...this.state,
|
|
201
|
+
coreHash: hash
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
table(name, callback) {
|
|
205
|
+
const tableBuilder = createTable(name);
|
|
206
|
+
const result = callback(tableBuilder);
|
|
207
|
+
const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
|
|
208
|
+
const tableState = finalBuilder.build();
|
|
209
|
+
return new _SqlContractBuilder({
|
|
210
|
+
...this.state,
|
|
211
|
+
tables: { ...this.state.tables, [name]: tableState }
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
model(name, table, callback) {
|
|
215
|
+
const modelBuilder = new ModelBuilder(name, table);
|
|
216
|
+
const result = callback(modelBuilder);
|
|
217
|
+
const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;
|
|
218
|
+
const modelState = finalBuilder.build();
|
|
219
|
+
return new _SqlContractBuilder({
|
|
220
|
+
...this.state,
|
|
221
|
+
models: { ...this.state.models, [name]: modelState }
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
function defineContract() {
|
|
226
|
+
return new SqlContractBuilder();
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
defineContract
|
|
230
|
+
};
|
|
231
|
+
//# sourceMappingURL=contract-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/contract-builder.ts"],"sourcesContent":["import type { ExtensionPackRef, TargetPackRef } from '@prisma-next/contract/framework-components';\nimport type {\n ColumnBuilderState,\n ModelBuilderState,\n RelationDefinition,\n TableBuilderState,\n} from '@prisma-next/contract-authoring';\nimport {\n type BuildModels,\n type BuildRelations,\n type BuildStorageColumn,\n ContractBuilder,\n createTable,\n type ExtractColumns,\n type ExtractPrimaryKey,\n ModelBuilder,\n type Mutable,\n TableBuilder,\n} from '@prisma-next/contract-authoring';\nimport type {\n ModelDefinition,\n ModelField,\n SqlContract,\n SqlMappings,\n SqlStorage,\n} from '@prisma-next/sql-contract/types';\nimport { computeMappings } from './contract';\n\n/**\n * Type-level mappings structure for contracts built via `defineContract()`.\n *\n * Compile-time type helper (not a runtime object) that ensures mappings match what the builder\n * produces. `codecTypes` uses the generic `CodecTypes` parameter; `operationTypes` is always\n * empty since operations are added via extensions at runtime.\n *\n * **Difference from RuntimeContext**: This is a compile-time type for contract construction.\n * `RuntimeContext` is a runtime object with populated registries for query execution.\n *\n * @template C - The `CodecTypes` generic parameter passed to `defineContract<CodecTypes>()`\n */\ntype ContractBuilderMappings<C extends Record<string, { output: unknown }>> = Omit<\n SqlMappings,\n 'codecTypes' | 'operationTypes'\n> & {\n readonly codecTypes: C;\n readonly operationTypes: Record<string, never>;\n};\n\ntype BuildStorageTable<\n _TableName extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,\n PK extends readonly string[] | undefined,\n> = {\n readonly columns: {\n readonly [K in keyof Columns]: Columns[K] extends ColumnBuilderState<\n string,\n infer Null,\n infer TType\n >\n ? BuildStorageColumn<Null & boolean, TType>\n : never;\n };\n readonly uniques: ReadonlyArray<{ readonly columns: readonly string[]; readonly name?: string }>;\n readonly indexes: ReadonlyArray<{ readonly columns: readonly string[]; readonly name?: string }>;\n readonly foreignKeys: ReadonlyArray<{\n readonly columns: readonly string[];\n readonly references: { readonly table: string; readonly columns: readonly string[] };\n readonly name?: string;\n }>;\n} & (PK extends readonly string[]\n ? { readonly primaryKey: { readonly columns: PK; readonly name?: string } }\n : Record<string, never>);\n\ntype BuildStorage<\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n >,\n> = {\n readonly tables: {\n readonly [K in keyof Tables]: BuildStorageTable<\n K & string,\n ExtractColumns<Tables[K]>,\n ExtractPrimaryKey<Tables[K]>\n >;\n };\n};\n\ntype BuildStorageTables<\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n >,\n> = {\n readonly [K in keyof Tables]: BuildStorageTable<\n K & string,\n ExtractColumns<Tables[K]>,\n ExtractPrimaryKey<Tables[K]>\n >;\n};\n\nexport interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {\n nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;\n type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;\n build(): ColumnBuilderState<Name, Nullable, Type>;\n}\n\nclass SqlContractBuilder<\n CodecTypes extends Record<string, { output: unknown }> = Record<string, never>,\n Target extends string | undefined = undefined,\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n > = Record<never, never>,\n Models extends Record<\n string,\n ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>\n > = Record<never, never>,\n CoreHash extends string | undefined = undefined,\n ExtensionPacks extends Record<string, unknown> | undefined = undefined,\n Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,\n> extends ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {\n /**\n * This method is responsible for normalizing the contract IR by setting default values\n * for all required fields:\n * - `nullable`: defaults to `false` if not provided\n * - `uniques`: defaults to `[]` (empty array)\n * - `indexes`: defaults to `[]` (empty array)\n * - `foreignKeys`: defaults to `[]` (empty array)\n * - `relations`: defaults to `{}` (empty object) for both model-level and contract-level\n * - `nativeType`: required field set from column type descriptor when columns are defined\n *\n * The contract builder is the **only** place where normalization should occur.\n * Validators, parsers, and emitters should assume the contract is already normalized.\n *\n * **Required**: Use column type descriptors (e.g., `int4Column`, `textColumn`) when defining columns.\n * This ensures `nativeType` is set correctly at build time.\n *\n * @returns A normalized SqlContract with all required fields present\n */\n build(): Target extends string\n ? SqlContract<\n BuildStorage<Tables>,\n BuildModels<Models>,\n BuildRelations<Models>,\n ContractBuilderMappings<CodecTypes>\n > & {\n readonly schemaVersion: '1';\n readonly target: Target;\n readonly targetFamily: 'sql';\n readonly coreHash: CoreHash extends string ? CoreHash : string;\n } & (ExtensionPacks extends Record<string, unknown>\n ? { readonly extensionPacks: ExtensionPacks }\n : Record<string, never>) &\n (Capabilities extends Record<string, Record<string, boolean>>\n ? { readonly capabilities: Capabilities }\n : Record<string, never>)\n : never {\n // Type helper to ensure literal types are preserved in return type\n type BuiltContract = Target extends string\n ? SqlContract<\n BuildStorage<Tables>,\n BuildModels<Models>,\n BuildRelations<Models>,\n ContractBuilderMappings<CodecTypes>\n > & {\n readonly schemaVersion: '1';\n readonly target: Target;\n readonly targetFamily: 'sql';\n readonly coreHash: CoreHash extends string ? CoreHash : string;\n } & (ExtensionPacks extends Record<string, unknown>\n ? { readonly extensionPacks: ExtensionPacks }\n : Record<string, never>) &\n (Capabilities extends Record<string, Record<string, boolean>>\n ? { readonly capabilities: Capabilities }\n : Record<string, never>)\n : never;\n if (!this.state.target) {\n throw new Error('target is required. Call .target() before .build()');\n }\n\n const target = this.state.target as Target & string;\n\n const storageTables = {} as Partial<Mutable<BuildStorageTables<Tables>>>;\n\n for (const tableName of Object.keys(this.state.tables) as Array<keyof Tables & string>) {\n const tableState = this.state.tables[tableName];\n if (!tableState) continue;\n\n type TableKey = typeof tableName;\n type ColumnDefs = ExtractColumns<Tables[TableKey]>;\n type PrimaryKey = ExtractPrimaryKey<Tables[TableKey]>;\n\n const columns = {} as Partial<{\n [K in keyof ColumnDefs]: BuildStorageColumn<\n ColumnDefs[K]['nullable'] & boolean,\n ColumnDefs[K]['type']\n >;\n }>;\n\n for (const columnName in tableState.columns) {\n const columnState = tableState.columns[columnName];\n if (!columnState) continue;\n const codecId = columnState.type;\n const nativeType = columnState.nativeType;\n\n columns[columnName as keyof ColumnDefs] = {\n nativeType,\n codecId,\n nullable: (columnState.nullable ?? false) as ColumnDefs[keyof ColumnDefs]['nullable'] &\n boolean,\n } as BuildStorageColumn<\n ColumnDefs[keyof ColumnDefs]['nullable'] & boolean,\n ColumnDefs[keyof ColumnDefs]['type']\n >;\n }\n\n // Build uniques from table state\n const uniques = (tableState.uniques ?? []).map((u) => ({\n columns: u.columns,\n ...(u.name ? { name: u.name } : {}),\n }));\n\n // Build indexes from table state\n const indexes = (tableState.indexes ?? []).map((i) => ({\n columns: i.columns,\n ...(i.name ? { name: i.name } : {}),\n }));\n\n // Build foreign keys from table state\n const foreignKeys = (tableState.foreignKeys ?? []).map((fk) => ({\n columns: fk.columns,\n references: fk.references,\n ...(fk.name ? { name: fk.name } : {}),\n }));\n\n const table = {\n columns: columns as {\n [K in keyof ColumnDefs]: BuildStorageColumn<\n ColumnDefs[K]['nullable'] & boolean,\n ColumnDefs[K]['type']\n >;\n },\n uniques,\n indexes,\n foreignKeys,\n ...(tableState.primaryKey\n ? {\n primaryKey: {\n columns: tableState.primaryKey,\n ...(tableState.primaryKeyName ? { name: tableState.primaryKeyName } : {}),\n },\n }\n : {}),\n } as unknown as BuildStorageTable<TableKey & string, ColumnDefs, PrimaryKey>;\n\n (storageTables as Mutable<BuildStorageTables<Tables>>)[tableName] = table;\n }\n\n const storage = { tables: storageTables as BuildStorageTables<Tables> } as BuildStorage<Tables>;\n\n // Build models - construct as partial first, then assert full type\n const modelsPartial: Partial<BuildModels<Models>> = {};\n\n // Iterate over models - TypeScript will see keys as string, but type assertion preserves literals\n for (const modelName in this.state.models) {\n const modelState = this.state.models[modelName];\n if (!modelState) continue;\n\n const modelStateTyped = modelState as unknown as {\n name: string;\n table: string;\n fields: Record<string, string>;\n };\n\n // Build fields object\n const fields: Partial<Record<string, ModelField>> = {};\n\n // Iterate over fields\n for (const fieldName in modelStateTyped.fields) {\n const columnName = modelStateTyped.fields[fieldName];\n if (columnName) {\n fields[fieldName] = {\n column: columnName,\n };\n }\n }\n\n // Assign to models - type assertion preserves literal keys\n (modelsPartial as unknown as Record<string, ModelDefinition>)[modelName] = {\n storage: {\n table: modelStateTyped.table,\n },\n fields: fields as Record<string, ModelField>,\n relations: {},\n };\n }\n\n // Build relations object - organized by table name\n const relationsPartial: Partial<Record<string, Record<string, RelationDefinition>>> = {};\n\n // Iterate over models to collect relations\n for (const modelName in this.state.models) {\n const modelState = this.state.models[modelName];\n if (!modelState) continue;\n\n const modelStateTyped = modelState as unknown as {\n name: string;\n table: string;\n fields: Record<string, string>;\n relations: Record<string, RelationDefinition>;\n };\n\n const tableName = modelStateTyped.table;\n if (!tableName) continue;\n\n // Only initialize relations object for this table if it has relations\n if (modelStateTyped.relations && Object.keys(modelStateTyped.relations).length > 0) {\n if (!relationsPartial[tableName]) {\n relationsPartial[tableName] = {};\n }\n\n // Add relations from this model to the table's relations\n const tableRelations = relationsPartial[tableName];\n if (tableRelations) {\n for (const relationName in modelStateTyped.relations) {\n const relation = modelStateTyped.relations[relationName];\n if (relation) {\n tableRelations[relationName] = relation;\n }\n }\n }\n }\n }\n\n const models = modelsPartial as unknown as BuildModels<Models>;\n\n const baseMappings = computeMappings(\n models as unknown as Record<string, ModelDefinition>,\n storage as SqlStorage,\n );\n\n const mappings = {\n ...baseMappings,\n codecTypes: {} as CodecTypes,\n operationTypes: {} as Record<string, never>,\n } as ContractBuilderMappings<CodecTypes>;\n\n const extensionNamespaces = this.state.extensionNamespaces ?? [];\n const extensionPacks: Record<string, unknown> = { ...(this.state.extensionPacks || {}) };\n for (const namespace of extensionNamespaces) {\n if (!Object.hasOwn(extensionPacks, namespace)) {\n extensionPacks[namespace] = {};\n }\n }\n\n // Construct contract with explicit type that matches the generic parameters\n // This ensures TypeScript infers literal types from the generics, not runtime values\n // Always include relations, even if empty (normalized to empty object)\n const contract = {\n schemaVersion: '1' as const,\n target,\n targetFamily: 'sql' as const,\n coreHash: this.state.coreHash || 'sha256:ts-builder-placeholder',\n models,\n relations: relationsPartial,\n storage,\n mappings,\n extensionPacks,\n capabilities: this.state.capabilities || {},\n meta: {},\n sources: {},\n } as unknown as BuiltContract;\n\n return contract as unknown as ReturnType<\n SqlContractBuilder<\n CodecTypes,\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >['build']\n >;\n }\n\n override target<T extends string>(\n packRef: TargetPackRef<'sql', T>,\n ): SqlContractBuilder<CodecTypes, T, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {\n return new SqlContractBuilder<\n CodecTypes,\n T,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n target: packRef.targetId,\n });\n }\n\n extensionPacks(\n packs: Record<string, ExtensionPackRef<'sql', string>>,\n ): SqlContractBuilder<\n CodecTypes,\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n if (!this.state.target) {\n throw new Error('extensionPacks() requires target() to be called first');\n }\n\n const namespaces = new Set(this.state.extensionNamespaces ?? []);\n\n for (const packRef of Object.values(packs)) {\n if (!packRef) continue;\n\n if (packRef.kind !== 'extension') {\n throw new Error(\n `extensionPacks() only accepts extension pack refs. Received kind \"${packRef.kind}\".`,\n );\n }\n\n if (packRef.familyId !== 'sql') {\n throw new Error(\n `extension pack \"${packRef.id}\" targets family \"${packRef.familyId}\" but this builder targets \"sql\".`,\n );\n }\n\n if (packRef.targetId && packRef.targetId !== this.state.target) {\n throw new Error(\n `extension pack \"${packRef.id}\" targets \"${packRef.targetId}\" but builder target is \"${this.state.target}\".`,\n );\n }\n\n namespaces.add(packRef.id);\n }\n\n return new SqlContractBuilder<\n CodecTypes,\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n extensionNamespaces: [...namespaces],\n });\n }\n\n override capabilities<C extends Record<string, Record<string, boolean>>>(\n capabilities: C,\n ): SqlContractBuilder<CodecTypes, Target, Tables, Models, CoreHash, ExtensionPacks, C> {\n return new SqlContractBuilder<CodecTypes, Target, Tables, Models, CoreHash, ExtensionPacks, C>({\n ...this.state,\n capabilities,\n });\n }\n\n override coreHash<H extends string>(\n hash: H,\n ): SqlContractBuilder<CodecTypes, Target, Tables, Models, H, ExtensionPacks, Capabilities> {\n return new SqlContractBuilder<\n CodecTypes,\n Target,\n Tables,\n Models,\n H,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n coreHash: hash,\n });\n }\n\n override table<\n TableName extends string,\n T extends TableBuilder<\n TableName,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >,\n >(\n name: TableName,\n callback: (t: TableBuilder<TableName>) => T | undefined,\n ): SqlContractBuilder<\n CodecTypes,\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const tableBuilder = createTable(name);\n const result = callback(tableBuilder);\n const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;\n const tableState = finalBuilder.build();\n\n return new SqlContractBuilder<\n CodecTypes,\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n tables: { ...this.state.tables, [name]: tableState } as Tables &\n Record<TableName, ReturnType<T['build']>>,\n });\n }\n\n override model<\n ModelName extends string,\n TableName extends string,\n M extends ModelBuilder<\n ModelName,\n TableName,\n Record<string, string>,\n Record<string, RelationDefinition>\n >,\n >(\n name: ModelName,\n table: TableName,\n callback: (\n m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,\n ) => M | undefined,\n ): SqlContractBuilder<\n CodecTypes,\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);\n const result = callback(modelBuilder);\n const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;\n const modelState = finalBuilder.build();\n\n return new SqlContractBuilder<\n CodecTypes,\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n models: { ...this.state.models, [name]: modelState } as Models &\n Record<ModelName, ReturnType<M['build']>>,\n });\n }\n}\n\nexport function defineContract<\n CodecTypes extends Record<string, { output: unknown }> = Record<string, never>,\n>(): SqlContractBuilder<CodecTypes> {\n return new SqlContractBuilder<CodecTypes>();\n}\n"],"mappings":";;;;;AAOA;AAAA,EAIE;AAAA,EACA;AAAA,EAGA;AAAA,EAEA;AAAA,OACK;AAiGP,IAAM,qBAAN,MAAM,4BAkBI,gBAAgF;AAA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,aAAa,aAAa,MAAM;AAEtC,WAAO,IAAI,oBAQT;AAAA,MACA,GAAG,KAAK;AAAA,MACR,QAAQ,EAAE,GAAG,KAAK,MAAM,QAAQ,CAAC,IAAI,GAAG,WAAW;AAAA,IAErD,CAAC;AAAA,EACH;AACF;AAEO,SAAS,iBAEoB;AAClC,SAAO,IAAI,mBAA+B;AAC5C;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/exports/contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract-ts",
|
|
3
|
-
"version": "0.3.0-pr.
|
|
3
|
+
"version": "0.3.0-pr.95.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
|
-
"engines": {
|
|
7
|
-
"node": ">=20"
|
|
8
|
-
},
|
|
9
6
|
"description": "SQL-specific TypeScript contract authoring surface for Prisma Next",
|
|
10
7
|
"dependencies": {
|
|
11
8
|
"arktype": "^2.1.25",
|
|
12
9
|
"ts-toolbelt": "^9.6.0",
|
|
13
|
-
"@prisma-next/contract": "0.3.0-pr.
|
|
14
|
-
"@prisma-next/contract-authoring": "0.3.0-pr.
|
|
15
|
-
"@prisma-next/sql-contract": "0.3.0-pr.
|
|
10
|
+
"@prisma-next/contract": "0.3.0-pr.95.2",
|
|
11
|
+
"@prisma-next/contract-authoring": "0.3.0-pr.95.2",
|
|
12
|
+
"@prisma-next/sql-contract": "0.3.0-pr.95.2"
|
|
16
13
|
},
|
|
17
14
|
"devDependencies": {
|
|
18
15
|
"@types/pg": "8.16.0",
|
|
19
16
|
"pg": "8.16.3",
|
|
20
|
-
"
|
|
17
|
+
"tsup": "8.5.1",
|
|
21
18
|
"typescript": "5.9.3",
|
|
22
19
|
"vitest": "4.0.16",
|
|
23
20
|
"@prisma-next/test-utils": "0.0.1",
|
|
24
|
-
"@prisma-next/tsconfig": "0.0.0"
|
|
25
|
-
"@prisma-next/tsdown": "0.0.0"
|
|
21
|
+
"@prisma-next/tsconfig": "0.0.0"
|
|
26
22
|
},
|
|
27
23
|
"files": [
|
|
28
24
|
"dist",
|
|
@@ -30,15 +26,21 @@
|
|
|
30
26
|
"schemas"
|
|
31
27
|
],
|
|
32
28
|
"exports": {
|
|
33
|
-
"./contract":
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
"./contract-builder": {
|
|
30
|
+
"types": "./dist/exports/contract-builder.d.ts",
|
|
31
|
+
"import": "./dist/exports/contract-builder.js"
|
|
32
|
+
},
|
|
33
|
+
"./contract": {
|
|
34
|
+
"types": "./dist/exports/contract.d.ts",
|
|
35
|
+
"import": "./dist/exports/contract.js"
|
|
36
|
+
},
|
|
37
|
+
"./schema-sql": "./schemas/data-contract-sql-v1.json"
|
|
36
38
|
},
|
|
37
39
|
"scripts": {
|
|
38
|
-
"build": "
|
|
40
|
+
"build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
|
|
39
41
|
"test": "vitest run",
|
|
40
42
|
"test:coverage": "vitest run --coverage",
|
|
41
|
-
"typecheck": "tsc --noEmit",
|
|
43
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
42
44
|
"lint": "biome check . --error-on-warnings",
|
|
43
45
|
"lint:fix": "biome check --write .",
|
|
44
46
|
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
@@ -79,6 +79,13 @@
|
|
|
79
79
|
"additionalProperties": {
|
|
80
80
|
"$ref": "#/$defs/StorageTable"
|
|
81
81
|
}
|
|
82
|
+
},
|
|
83
|
+
"types": {
|
|
84
|
+
"type": "object",
|
|
85
|
+
"description": "Named type instances for parameterized/custom types (e.g., vectors, enums)",
|
|
86
|
+
"additionalProperties": {
|
|
87
|
+
"$ref": "#/$defs/StorageTypeInstance"
|
|
88
|
+
}
|
|
82
89
|
}
|
|
83
90
|
},
|
|
84
91
|
"required": ["tables"]
|
|
@@ -128,19 +135,54 @@
|
|
|
128
135
|
},
|
|
129
136
|
"StorageColumn": {
|
|
130
137
|
"type": "object",
|
|
131
|
-
"description": "Column definition with type and
|
|
138
|
+
"description": "Column definition with type, nullability, and optional parameterized type info",
|
|
132
139
|
"additionalProperties": false,
|
|
133
140
|
"properties": {
|
|
134
|
-
"
|
|
141
|
+
"nativeType": {
|
|
142
|
+
"type": "string",
|
|
143
|
+
"description": "Database-native type (e.g., 'text', 'int4', 'timestamptz', 'vector(1536)')"
|
|
144
|
+
},
|
|
145
|
+
"codecId": {
|
|
135
146
|
"type": "string",
|
|
136
|
-
"description": "
|
|
147
|
+
"description": "Codec identifier for encoding/decoding (e.g., 'pg/text@1', 'pg/vector@1')"
|
|
137
148
|
},
|
|
138
149
|
"nullable": {
|
|
139
150
|
"type": "boolean",
|
|
140
151
|
"default": false,
|
|
141
152
|
"description": "Whether the column allows NULL values"
|
|
153
|
+
},
|
|
154
|
+
"typeParams": {
|
|
155
|
+
"type": "object",
|
|
156
|
+
"description": "Opaque, codec-owned JS/type parameters (e.g., { length: 1536 } for vectors)",
|
|
157
|
+
"additionalProperties": true
|
|
158
|
+
},
|
|
159
|
+
"typeRef": {
|
|
160
|
+
"type": "string",
|
|
161
|
+
"description": "Reference to a named type instance in storage.types"
|
|
142
162
|
}
|
|
143
|
-
}
|
|
163
|
+
},
|
|
164
|
+
"required": ["nativeType", "codecId", "nullable"]
|
|
165
|
+
},
|
|
166
|
+
"StorageTypeInstance": {
|
|
167
|
+
"type": "object",
|
|
168
|
+
"description": "Named, parameterized type instance for reuse across columns",
|
|
169
|
+
"additionalProperties": false,
|
|
170
|
+
"properties": {
|
|
171
|
+
"codecId": {
|
|
172
|
+
"type": "string",
|
|
173
|
+
"description": "Codec identifier for encoding/decoding"
|
|
174
|
+
},
|
|
175
|
+
"nativeType": {
|
|
176
|
+
"type": "string",
|
|
177
|
+
"description": "Database-native type"
|
|
178
|
+
},
|
|
179
|
+
"typeParams": {
|
|
180
|
+
"type": "object",
|
|
181
|
+
"description": "Codec-owned type parameters",
|
|
182
|
+
"additionalProperties": true
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
"required": ["codecId", "nativeType", "typeParams"]
|
|
144
186
|
},
|
|
145
187
|
"PrimaryKey": {
|
|
146
188
|
"type": "object",
|
package/src/contract.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
SqlStorage,
|
|
12
12
|
StorageColumn,
|
|
13
13
|
StorageTable,
|
|
14
|
+
StorageTypeInstance,
|
|
14
15
|
UniqueConstraint,
|
|
15
16
|
} from '@prisma-next/sql-contract/types';
|
|
16
17
|
import { type } from 'arktype';
|
|
@@ -24,6 +25,14 @@ const StorageColumnSchema = type.declare<StorageColumn>().type({
|
|
|
24
25
|
nativeType: 'string',
|
|
25
26
|
codecId: 'string',
|
|
26
27
|
nullable: 'boolean',
|
|
28
|
+
'typeParams?': 'Record<string, unknown>',
|
|
29
|
+
'typeRef?': 'string',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({
|
|
33
|
+
codecId: 'string',
|
|
34
|
+
nativeType: 'string',
|
|
35
|
+
typeParams: 'Record<string, unknown>',
|
|
27
36
|
});
|
|
28
37
|
|
|
29
38
|
const PrimaryKeySchema = type.declare<PrimaryKey>().type({
|
|
@@ -62,6 +71,7 @@ const StorageTableSchema = type.declare<StorageTable>().type({
|
|
|
62
71
|
|
|
63
72
|
const StorageSchema = type.declare<SqlStorage>().type({
|
|
64
73
|
tables: type({ '[string]': StorageTableSchema }),
|
|
74
|
+
'types?': type({ '[string]': StorageTypeInstanceSchema }),
|
|
65
75
|
});
|
|
66
76
|
|
|
67
77
|
const ModelFieldSchema = type.declare<ModelField>().type({
|
|
@@ -194,6 +204,45 @@ export function computeMappings(
|
|
|
194
204
|
function validateContractLogic(structurallyValidatedContract: SqlContract<SqlStorage>): void {
|
|
195
205
|
const { storage, models } = structurallyValidatedContract;
|
|
196
206
|
const tableNames = new Set(Object.keys(storage.tables));
|
|
207
|
+
const typeInstanceNames = new Set(Object.keys(storage.types ?? {}));
|
|
208
|
+
|
|
209
|
+
// Validate storage.types if present
|
|
210
|
+
if (storage.types) {
|
|
211
|
+
for (const [typeName, typeInstance] of Object.entries(storage.types)) {
|
|
212
|
+
// Validate typeParams is not an array (arrays are objects in JS but not valid here)
|
|
213
|
+
if (Array.isArray(typeInstance.typeParams)) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`Type instance "${typeName}" has invalid typeParams: must be a plain object, not an array`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Validate columns in all tables
|
|
222
|
+
for (const [tableName, table] of Object.entries(storage.tables)) {
|
|
223
|
+
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
224
|
+
// Validate typeParams and typeRef are mutually exclusive
|
|
225
|
+
if (column.typeParams !== undefined && column.typeRef !== undefined) {
|
|
226
|
+
throw new Error(
|
|
227
|
+
`Column "${columnName}" in table "${tableName}" has both typeParams and typeRef; these are mutually exclusive`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Validate typeParams is not an array (arrays are objects in JS but not valid here)
|
|
232
|
+
if (column.typeParams !== undefined && Array.isArray(column.typeParams)) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
`Column "${columnName}" in table "${tableName}" has invalid typeParams: must be a plain object, not an array`,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Validate typeRef points to an existing storage.types key
|
|
239
|
+
if (column.typeRef !== undefined && !typeInstanceNames.has(column.typeRef)) {
|
|
240
|
+
throw new Error(
|
|
241
|
+
`Column "${columnName}" in table "${tableName}" references non-existent type instance "${column.typeRef}" (not found in storage.types)`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
197
246
|
|
|
198
247
|
// Validate models
|
|
199
248
|
for (const [modelName, modelUnknown] of Object.entries(models)) {
|