@prisma-next/sql-contract-emitter 0.3.0-dev.8 → 0.3.0-pr.100.1
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/index.d.ts +24 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +118 -15
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
- package/src/index.ts +169 -19
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
1
|
import type { ContractIR } from '@prisma-next/contract/ir';
|
|
2
|
-
import type { TypesImportSpec, ValidationContext } from '@prisma-next/contract/types';
|
|
3
|
-
import type { ModelDefinition, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
2
|
+
import type { GenerateContractTypesOptions, TypeRenderContext, TypeRenderEntry, TypesImportSpec, ValidationContext } from '@prisma-next/contract/types';
|
|
3
|
+
import type { ModelDefinition, SqlStorage, StorageColumn } from '@prisma-next/sql-contract/types';
|
|
4
4
|
export declare const sqlTargetFamilyHook: {
|
|
5
5
|
readonly id: "sql";
|
|
6
6
|
readonly validateTypes: (ir: ContractIR, _ctx: ValidationContext) => void;
|
|
7
7
|
readonly validateStructure: (ir: ContractIR) => void;
|
|
8
|
-
readonly generateContractTypes: (ir: ContractIR, codecTypeImports: ReadonlyArray<TypesImportSpec>, operationTypeImports: ReadonlyArray<TypesImportSpec
|
|
8
|
+
readonly generateContractTypes: (ir: ContractIR, codecTypeImports: ReadonlyArray<TypesImportSpec>, operationTypeImports: ReadonlyArray<TypesImportSpec>, options?: GenerateContractTypesOptions) => string;
|
|
9
9
|
readonly generateStorageType: (storage: SqlStorage) => string;
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Generates the TypeScript type for storage.types with literal types.
|
|
12
|
+
* This preserves type params as literal values for precise typing.
|
|
13
|
+
*/
|
|
14
|
+
readonly generateStorageTypesType: (types: SqlStorage["types"]) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Serializes a typeParams object to a TypeScript literal type.
|
|
17
|
+
* Converts { length: 1536 } to "{ readonly length: 1536 }".
|
|
18
|
+
*/
|
|
19
|
+
readonly serializeTypeParamsLiteral: (params: Record<string, unknown>) => string;
|
|
20
|
+
/**
|
|
21
|
+
* Serializes a value to a TypeScript literal type expression.
|
|
22
|
+
*/
|
|
23
|
+
readonly serializeValue: (value: unknown) => string;
|
|
24
|
+
readonly generateModelsType: (models: Record<string, ModelDefinition> | undefined, storage: SqlStorage, parameterizedRenderers?: Map<string, TypeRenderEntry>) => string;
|
|
25
|
+
/**
|
|
26
|
+
* Generates the TypeScript type expression for a column.
|
|
27
|
+
* Uses parameterized renderer if the column has typeParams and a matching renderer exists,
|
|
28
|
+
* otherwise falls back to CodecTypes[codecId]['output'].
|
|
29
|
+
*/
|
|
30
|
+
readonly generateColumnType: (column: StorageColumn, storage: SqlStorage, parameterizedRenderers: Map<string, TypeRenderEntry> | undefined, renderCtx: TypeRenderContext) => string;
|
|
11
31
|
readonly generateRelationsType: (relations: Record<string, unknown> | undefined) => string;
|
|
12
32
|
readonly generateMappingsType: (models: Record<string, ModelDefinition> | undefined, storage: SqlStorage, codecTypes: string, operationTypes: string) => string;
|
|
13
33
|
};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EACV,4BAA4B,EAC5B,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,iBAAiB,EAClB,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EACV,eAAe,EAEf,UAAU,EACV,aAAa,EAGd,MAAM,iCAAiC,CAAC;AAwBzC,eAAO,MAAM,mBAAmB;;iCAGZ,UAAU,QAAQ,iBAAiB,KAAG,IAAI;qCA6BtC,UAAU,KAAG,IAAI;yCA2JjC,UAAU,oBACI,aAAa,CAAC,eAAe,CAAC,wBAC1B,aAAa,CAAC,eAAe,CAAC,YAC1C,4BAA4B,KACrC,MAAM;4CAsDoB,UAAU,KAAG,MAAM;IAyDhD;;;OAGG;+CAC6B,UAAU,CAAC,OAAO,CAAC,KAAG,MAAM;IAkB5D;;;OAGG;kDACgC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM;IAcnE;;OAEG;qCACmB,OAAO,KAAG,MAAM;0CA4B5B,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,GAAG,SAAS,WAC1C,UAAU,2BACM,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,KACpD,MAAM;IAgET;;;;OAIG;0CAEO,aAAa,WACZ,UAAU,0BACK,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,GAAG,SAAS,aACrD,iBAAiB,KAC3B,MAAM;gDAqBwB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,KAAG,MAAM;4CAkEnE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,GAAG,SAAS,WAC1C,UAAU,cACP,MAAM,kBACF,MAAM,KACrB,MAAM;CAsDD,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
function resolveColumnTypeParams(column, storage) {
|
|
3
|
+
if (column.typeParams && Object.keys(column.typeParams).length > 0) {
|
|
4
|
+
return column.typeParams;
|
|
5
|
+
}
|
|
6
|
+
if (column.typeRef && storage.types) {
|
|
7
|
+
const typeInstance = storage.types[column.typeRef];
|
|
8
|
+
if (typeInstance?.typeParams) {
|
|
9
|
+
return typeInstance.typeParams;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
2
14
|
var sqlTargetFamilyHook = {
|
|
3
15
|
id: "sql",
|
|
4
16
|
validateTypes(ir, _ctx) {
|
|
@@ -153,22 +165,24 @@ var sqlTargetFamilyHook = {
|
|
|
153
165
|
}
|
|
154
166
|
}
|
|
155
167
|
},
|
|
156
|
-
generateContractTypes(ir, codecTypeImports, operationTypeImports) {
|
|
157
|
-
const
|
|
158
|
-
const importLines = allImports.map(
|
|
159
|
-
(imp) => `import type { ${imp.named} as ${imp.alias} } from '${imp.package}';`
|
|
160
|
-
);
|
|
161
|
-
const codecTypes = codecTypeImports.map((imp) => imp.alias).join(" & ");
|
|
162
|
-
const operationTypes = operationTypeImports.map((imp) => imp.alias).join(" & ");
|
|
168
|
+
generateContractTypes(ir, codecTypeImports, operationTypeImports, options) {
|
|
169
|
+
const parameterizedRenderers = options?.parameterizedRenderers;
|
|
163
170
|
const storage = ir.storage;
|
|
164
171
|
const models = ir.models;
|
|
172
|
+
const allImports = [...codecTypeImports, ...operationTypeImports];
|
|
173
|
+
const importLines = allImports.map((imp) => {
|
|
174
|
+
const importClause = imp.named === imp.alias ? imp.named : `${imp.named} as ${imp.alias}`;
|
|
175
|
+
return `import type { ${importClause} } from '${imp.package}';`;
|
|
176
|
+
});
|
|
177
|
+
const codecTypes = codecTypeImports.filter((imp) => imp.named === "CodecTypes").map((imp) => imp.alias).join(" & ");
|
|
178
|
+
const operationTypes = operationTypeImports.filter((imp) => imp.named === "OperationTypes").map((imp) => imp.alias).join(" & ");
|
|
165
179
|
const storageType = this.generateStorageType(storage);
|
|
166
|
-
const modelsType = this.generateModelsType(models, storage);
|
|
180
|
+
const modelsType = this.generateModelsType(models, storage, parameterizedRenderers);
|
|
167
181
|
const relationsType = this.generateRelationsType(ir.relations);
|
|
168
182
|
const mappingsType = this.generateMappingsType(models, storage, codecTypes, operationTypes);
|
|
169
183
|
return `// \u26A0\uFE0F GENERATED FILE - DO NOT EDIT
|
|
170
|
-
// This file is automatically generated by 'prisma-next emit'.
|
|
171
|
-
// To regenerate, run: prisma-next emit
|
|
184
|
+
// This file is automatically generated by 'prisma-next contract emit'.
|
|
185
|
+
// To regenerate, run: prisma-next contract emit
|
|
172
186
|
${importLines.join("\n")}
|
|
173
187
|
|
|
174
188
|
import type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';
|
|
@@ -228,12 +242,77 @@ export type Relations = Contract['relations'];
|
|
|
228
242
|
tableParts.push(`foreignKeys: readonly [${fks}]`);
|
|
229
243
|
tables.push(`readonly ${tableName}: { ${tableParts.join("; ")} }`);
|
|
230
244
|
}
|
|
231
|
-
|
|
245
|
+
const typesType = this.generateStorageTypesType(storage.types);
|
|
246
|
+
return `{ readonly tables: { ${tables.join("; ")} }; readonly types: ${typesType} }`;
|
|
247
|
+
},
|
|
248
|
+
/**
|
|
249
|
+
* Generates the TypeScript type for storage.types with literal types.
|
|
250
|
+
* This preserves type params as literal values for precise typing.
|
|
251
|
+
*/
|
|
252
|
+
generateStorageTypesType(types) {
|
|
253
|
+
if (!types || Object.keys(types).length === 0) {
|
|
254
|
+
return "Record<string, never>";
|
|
255
|
+
}
|
|
256
|
+
const typeEntries = [];
|
|
257
|
+
for (const [typeName, typeInstance] of Object.entries(types)) {
|
|
258
|
+
const codecId = `'${typeInstance.codecId}'`;
|
|
259
|
+
const nativeType = `'${typeInstance.nativeType}'`;
|
|
260
|
+
const typeParamsStr = this.serializeTypeParamsLiteral(typeInstance.typeParams);
|
|
261
|
+
typeEntries.push(
|
|
262
|
+
`readonly ${typeName}: { readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
return `{ ${typeEntries.join("; ")} }`;
|
|
266
|
+
},
|
|
267
|
+
/**
|
|
268
|
+
* Serializes a typeParams object to a TypeScript literal type.
|
|
269
|
+
* Converts { length: 1536 } to "{ readonly length: 1536 }".
|
|
270
|
+
*/
|
|
271
|
+
serializeTypeParamsLiteral(params) {
|
|
272
|
+
if (!params || Object.keys(params).length === 0) {
|
|
273
|
+
return "Record<string, never>";
|
|
274
|
+
}
|
|
275
|
+
const entries = [];
|
|
276
|
+
for (const [key, value] of Object.entries(params)) {
|
|
277
|
+
const serialized = this.serializeValue(value);
|
|
278
|
+
entries.push(`readonly ${key}: ${serialized}`);
|
|
279
|
+
}
|
|
280
|
+
return `{ ${entries.join("; ")} }`;
|
|
232
281
|
},
|
|
233
|
-
|
|
282
|
+
/**
|
|
283
|
+
* Serializes a value to a TypeScript literal type expression.
|
|
284
|
+
*/
|
|
285
|
+
serializeValue(value) {
|
|
286
|
+
if (value === null) {
|
|
287
|
+
return "null";
|
|
288
|
+
}
|
|
289
|
+
if (value === void 0) {
|
|
290
|
+
return "undefined";
|
|
291
|
+
}
|
|
292
|
+
if (typeof value === "string") {
|
|
293
|
+
return `'${value}'`;
|
|
294
|
+
}
|
|
295
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
296
|
+
return String(value);
|
|
297
|
+
}
|
|
298
|
+
if (Array.isArray(value)) {
|
|
299
|
+
const items = value.map((v) => this.serializeValue(v)).join(", ");
|
|
300
|
+
return `readonly [${items}]`;
|
|
301
|
+
}
|
|
302
|
+
if (typeof value === "object") {
|
|
303
|
+
const entries = [];
|
|
304
|
+
for (const [k, v] of Object.entries(value)) {
|
|
305
|
+
entries.push(`readonly ${k}: ${this.serializeValue(v)}`);
|
|
306
|
+
}
|
|
307
|
+
return `{ ${entries.join("; ")} }`;
|
|
308
|
+
}
|
|
309
|
+
return "unknown";
|
|
310
|
+
},
|
|
311
|
+
generateModelsType(models, storage, parameterizedRenderers) {
|
|
234
312
|
if (!models) {
|
|
235
313
|
return "Record<string, never>";
|
|
236
314
|
}
|
|
315
|
+
const renderCtx = { codecTypesName: "CodecTypes" };
|
|
237
316
|
const modelTypes = [];
|
|
238
317
|
for (const [modelName, model] of Object.entries(models)) {
|
|
239
318
|
const fields = [];
|
|
@@ -246,9 +325,12 @@ export type Relations = Contract['relations'];
|
|
|
246
325
|
fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);
|
|
247
326
|
continue;
|
|
248
327
|
}
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
328
|
+
const jsType = this.generateColumnType(
|
|
329
|
+
column,
|
|
330
|
+
storage,
|
|
331
|
+
parameterizedRenderers,
|
|
332
|
+
renderCtx
|
|
333
|
+
);
|
|
252
334
|
fields.push(`readonly ${fieldName}: ${jsType}`);
|
|
253
335
|
}
|
|
254
336
|
} else {
|
|
@@ -280,6 +362,27 @@ export type Relations = Contract['relations'];
|
|
|
280
362
|
}
|
|
281
363
|
return `{ ${modelTypes.join("; ")} }`;
|
|
282
364
|
},
|
|
365
|
+
/**
|
|
366
|
+
* Generates the TypeScript type expression for a column.
|
|
367
|
+
* Uses parameterized renderer if the column has typeParams and a matching renderer exists,
|
|
368
|
+
* otherwise falls back to CodecTypes[codecId]['output'].
|
|
369
|
+
*/
|
|
370
|
+
generateColumnType(column, storage, parameterizedRenderers, renderCtx) {
|
|
371
|
+
const typeParams = resolveColumnTypeParams(column, storage);
|
|
372
|
+
const nullable = column.nullable ?? false;
|
|
373
|
+
let baseType;
|
|
374
|
+
if (typeParams && parameterizedRenderers) {
|
|
375
|
+
const renderer = parameterizedRenderers.get(column.codecId);
|
|
376
|
+
if (renderer) {
|
|
377
|
+
baseType = renderer.render(typeParams, renderCtx);
|
|
378
|
+
} else {
|
|
379
|
+
baseType = `CodecTypes['${column.codecId}']['output']`;
|
|
380
|
+
}
|
|
381
|
+
} else {
|
|
382
|
+
baseType = `CodecTypes['${column.codecId}']['output']`;
|
|
383
|
+
}
|
|
384
|
+
return nullable ? `${baseType} | null` : baseType;
|
|
385
|
+
},
|
|
283
386
|
generateRelationsType(relations) {
|
|
284
387
|
if (!relations || Object.keys(relations).length === 0) {
|
|
285
388
|
return "Record<string, never>";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ContractIR } from '@prisma-next/contract/ir';\nimport type { TypesImportSpec, ValidationContext } from '@prisma-next/contract/types';\nimport type {\n ModelDefinition,\n ModelField,\n SqlStorage,\n StorageTable,\n} from '@prisma-next/sql-contract/types';\n\nexport const sqlTargetFamilyHook = {\n id: 'sql',\n\n validateTypes(ir: ContractIR, _ctx: ValidationContext): void {\n const storage = ir.storage as SqlStorage | undefined;\n if (!storage || !storage.tables) {\n return;\n }\n\n // Validate codec ID format (ns/name@version). Adapter-provided codecs are available regardless of contract.extensionPacks (which is for framework extensions); TypeScript prevents invalid usage and runtime validates availability.\n\n const typeIdRegex = /^([^/]+)\\/([^@]+)@(\\d+)$/;\n\n for (const [tableName, tableUnknown] of Object.entries(storage.tables)) {\n const table = tableUnknown as StorageTable;\n for (const [colName, colUnknown] of Object.entries(table.columns)) {\n const col = colUnknown as { codecId?: string };\n const codecId = col.codecId;\n if (!codecId) {\n throw new Error(`Column \"${colName}\" in table \"${tableName}\" is missing codecId`);\n }\n\n const match = codecId.match(typeIdRegex);\n if (!match || !match[1]) {\n throw new Error(\n `Column \"${colName}\" in table \"${tableName}\" has invalid codec ID format \"${codecId}\". Expected format: ns/name@version`,\n );\n }\n }\n }\n },\n\n validateStructure(ir: ContractIR): void {\n if (ir.targetFamily !== 'sql') {\n throw new Error(`Expected targetFamily \"sql\", got \"${ir.targetFamily}\"`);\n }\n\n const storage = ir.storage as SqlStorage | undefined;\n if (!storage || !storage.tables) {\n throw new Error('SQL contract must have storage.tables');\n }\n\n const models = ir.models as Record<string, ModelDefinition> | undefined;\n const tableNames = new Set(Object.keys(storage.tables));\n\n if (models) {\n for (const [modelName, modelUnknown] of Object.entries(models)) {\n const model = modelUnknown as ModelDefinition;\n if (!model.storage?.table) {\n throw new Error(`Model \"${modelName}\" is missing storage.table`);\n }\n\n const tableName = model.storage.table;\n if (!tableNames.has(tableName)) {\n throw new Error(`Model \"${modelName}\" references non-existent table \"${tableName}\"`);\n }\n\n const table = storage.tables[tableName];\n if (!table) {\n throw new Error(`Model \"${modelName}\" references non-existent table \"${tableName}\"`);\n }\n\n if (!table.primaryKey) {\n throw new Error(`Model \"${modelName}\" table \"${tableName}\" is missing a primary key`);\n }\n\n const columnNames = new Set(Object.keys(table.columns));\n if (!model.fields || Object.keys(model.fields).length === 0) {\n throw new Error(`Model \"${modelName}\" is missing fields`);\n }\n\n for (const [fieldName, fieldUnknown] of Object.entries(model.fields)) {\n const field = fieldUnknown as ModelField;\n if (!field.column) {\n throw new Error(`Model \"${modelName}\" field \"${fieldName}\" is missing column property`);\n }\n\n if (!columnNames.has(field.column)) {\n throw new Error(\n `Model \"${modelName}\" field \"${fieldName}\" references non-existent column \"${field.column}\" in table \"${tableName}\"`,\n );\n }\n }\n\n if (!model.relations || typeof model.relations !== 'object') {\n throw new Error(\n `Model \"${modelName}\" is missing required field \"relations\" (must be an object)`,\n );\n }\n }\n }\n\n for (const [tableName, tableUnknown] of Object.entries(storage.tables)) {\n const table = tableUnknown as StorageTable;\n const columnNames = new Set(Object.keys(table.columns));\n\n // Column structure (nullable, nativeType, codecId) and table arrays (uniques, indexes, foreignKeys)\n // are validated by Arktype schema validation - no need to re-check here.\n // We only validate logical consistency (foreign key references, model references, etc.)\n\n if (!Array.isArray(table.uniques)) {\n throw new Error(\n `Table \"${tableName}\" is missing required field \"uniques\" (must be an array)`,\n );\n }\n if (!Array.isArray(table.indexes)) {\n throw new Error(\n `Table \"${tableName}\" is missing required field \"indexes\" (must be an array)`,\n );\n }\n if (!Array.isArray(table.foreignKeys)) {\n throw new Error(\n `Table \"${tableName}\" is missing required field \"foreignKeys\" (must be an array)`,\n );\n }\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(\n `Table \"${tableName}\" index references non-existent column \"${colName}\"`,\n );\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 = storage.tables[fk.references.table];\n if (!referencedTable) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.references.table}\"`,\n );\n }\n\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\n generateContractTypes(\n ir: ContractIR,\n codecTypeImports: ReadonlyArray<TypesImportSpec>,\n operationTypeImports: ReadonlyArray<TypesImportSpec>,\n ): string {\n const allImports = [...codecTypeImports, ...operationTypeImports];\n const importLines = allImports.map(\n (imp) => `import type { ${imp.named} as ${imp.alias} } from '${imp.package}';`,\n );\n\n const codecTypes = codecTypeImports.map((imp) => imp.alias).join(' & ');\n const operationTypes = operationTypeImports.map((imp) => imp.alias).join(' & ');\n\n const storage = ir.storage as SqlStorage;\n const models = ir.models as Record<string, ModelDefinition>;\n\n const storageType = this.generateStorageType(storage);\n const modelsType = this.generateModelsType(models, storage);\n const relationsType = this.generateRelationsType(ir.relations);\n const mappingsType = this.generateMappingsType(models, storage, codecTypes, operationTypes);\n\n return `// ⚠️ GENERATED FILE - DO NOT EDIT\n// This file is automatically generated by 'prisma-next emit'.\n// To regenerate, run: prisma-next emit\n${importLines.join('\\n')}\n\nimport type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';\n\nexport type CodecTypes = ${codecTypes || 'Record<string, never>'};\nexport type LaneCodecTypes = CodecTypes;\nexport type OperationTypes = ${operationTypes || 'Record<string, never>'};\n\nexport type Contract = SqlContract<\n ${storageType},\n ${modelsType},\n ${relationsType},\n ${mappingsType}\n>;\n\nexport type Tables = Contract['storage']['tables'];\nexport type Models = Contract['models'];\nexport type Relations = Contract['relations'];\n`;\n },\n\n generateStorageType(storage: SqlStorage): string {\n const tables: string[] = [];\n for (const [tableName, table] of Object.entries(storage.tables)) {\n const columns: string[] = [];\n for (const [colName, col] of Object.entries(table.columns)) {\n const nullable = col.nullable ? 'true' : 'false';\n const nativeType = `'${col.nativeType}'`;\n const codecId = `'${col.codecId}'`;\n columns.push(\n `readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable} }`,\n );\n }\n\n const tableParts: string[] = [`columns: { ${columns.join('; ')} }`];\n\n if (table.primaryKey) {\n const pkCols = table.primaryKey.columns.map((c) => `'${c}'`).join(', ');\n const pkName = table.primaryKey.name ? `; readonly name: '${table.primaryKey.name}'` : '';\n tableParts.push(`primaryKey: { readonly columns: readonly [${pkCols}]${pkName} }`);\n }\n\n const uniques = table.uniques\n .map((u) => {\n const cols = u.columns.map((c: string) => `'${c}'`).join(', ');\n const name = u.name ? `; readonly name: '${u.name}'` : '';\n return `{ readonly columns: readonly [${cols}]${name} }`;\n })\n .join(', ');\n tableParts.push(`uniques: readonly [${uniques}]`);\n\n const indexes = table.indexes\n .map((i) => {\n const cols = i.columns.map((c: string) => `'${c}'`).join(', ');\n const name = i.name ? `; readonly name: '${i.name}'` : '';\n return `{ readonly columns: readonly [${cols}]${name} }`;\n })\n .join(', ');\n tableParts.push(`indexes: readonly [${indexes}]`);\n\n const fks = table.foreignKeys\n .map((fk) => {\n const cols = fk.columns.map((c: string) => `'${c}'`).join(', ');\n const refCols = fk.references.columns.map((c: string) => `'${c}'`).join(', ');\n const name = fk.name ? `; readonly name: '${fk.name}'` : '';\n return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: '${fk.references.table}'; readonly columns: readonly [${refCols}] }${name} }`;\n })\n .join(', ');\n tableParts.push(`foreignKeys: readonly [${fks}]`);\n\n tables.push(`readonly ${tableName}: { ${tableParts.join('; ')} }`);\n }\n\n return `{ readonly tables: { ${tables.join('; ')} } }`;\n },\n\n generateModelsType(\n models: Record<string, ModelDefinition> | undefined,\n storage: SqlStorage,\n ): string {\n if (!models) {\n return 'Record<string, never>';\n }\n\n const modelTypes: string[] = [];\n for (const [modelName, model] of Object.entries(models)) {\n const fields: string[] = [];\n const tableName = model.storage.table;\n const table = storage.tables[tableName];\n\n if (table) {\n for (const [fieldName, field] of Object.entries(model.fields)) {\n const column = table.columns[field.column];\n if (!column) {\n fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);\n continue;\n }\n\n const typeId = column.codecId;\n const nullable = column.nullable ?? false;\n const jsType = nullable\n ? `CodecTypes['${typeId}']['output'] | null`\n : `CodecTypes['${typeId}']['output']`;\n\n fields.push(`readonly ${fieldName}: ${jsType}`);\n }\n } else {\n for (const [fieldName, field] of Object.entries(model.fields)) {\n fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);\n }\n }\n\n const relations: string[] = [];\n for (const [relName, rel] of Object.entries(model.relations)) {\n if (typeof rel === 'object' && rel !== null && 'on' in rel) {\n const on = rel.on as { parentCols?: string[]; childCols?: string[] };\n if (on.parentCols && on.childCols) {\n const parentCols = on.parentCols.map((c) => `'${c}'`).join(', ');\n const childCols = on.childCols.map((c) => `'${c}'`).join(', ');\n relations.push(\n `readonly ${relName}: { readonly on: { readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] } }`,\n );\n }\n }\n }\n\n const modelParts: string[] = [\n `storage: { readonly table: '${tableName}' }`,\n `fields: { ${fields.join('; ')} }`,\n ];\n\n if (relations.length > 0) {\n modelParts.push(`relations: { ${relations.join('; ')} }`);\n }\n\n modelTypes.push(`readonly ${modelName}: { ${modelParts.join('; ')} }`);\n }\n\n return `{ ${modelTypes.join('; ')} }`;\n },\n\n generateRelationsType(relations: Record<string, unknown> | undefined): string {\n if (!relations || Object.keys(relations).length === 0) {\n return 'Record<string, never>';\n }\n\n const tableEntries: string[] = [];\n for (const [tableName, relsValue] of Object.entries(relations)) {\n if (typeof relsValue !== 'object' || relsValue === null) {\n continue;\n }\n const rels = relsValue as Record<string, unknown>;\n const relationEntries: string[] = [];\n for (const [relName, relValue] of Object.entries(rels)) {\n if (typeof relValue !== 'object' || relValue === null) {\n relationEntries.push(`readonly ${relName}: unknown`);\n continue;\n }\n const { to, cardinality, on, through } = relValue as {\n readonly to?: string;\n readonly cardinality?: string;\n readonly on?: {\n readonly parentCols?: readonly string[];\n readonly childCols?: readonly string[];\n };\n readonly through?: {\n readonly table: string;\n readonly parentCols: readonly string[];\n readonly childCols: readonly string[];\n };\n };\n\n const parts: string[] = [];\n if (to) {\n parts.push(`readonly to: '${to}'`);\n }\n if (cardinality) {\n parts.push(`readonly cardinality: '${cardinality}'`);\n }\n if (on?.parentCols && on.childCols) {\n const parentCols = on.parentCols.map((c) => `'${c}'`).join(', ');\n const childCols = on.childCols.map((c) => `'${c}'`).join(', ');\n parts.push(\n `readonly on: { readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] }`,\n );\n }\n if (through) {\n const parentCols = through.parentCols.map((c) => `'${c}'`).join(', ');\n const childCols = through.childCols.map((c) => `'${c}'`).join(', ');\n parts.push(\n `readonly through: { readonly table: '${through.table}'; readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] }`,\n );\n }\n\n relationEntries.push(\n parts.length > 0\n ? `readonly ${relName}: { ${parts.join('; ')} }`\n : `readonly ${relName}: unknown`,\n );\n }\n tableEntries.push(`readonly ${tableName}: { ${relationEntries.join('; ')} }`);\n }\n\n return `{ ${tableEntries.join('; ')} }`;\n },\n\n generateMappingsType(\n models: Record<string, ModelDefinition> | undefined,\n storage: SqlStorage,\n codecTypes: string,\n operationTypes: string,\n ): string {\n if (!models) {\n return `SqlMappings & { readonly codecTypes: ${codecTypes || 'Record<string, never>'}; readonly operationTypes: ${operationTypes || 'Record<string, never>'}; }`;\n }\n\n const modelToTable: string[] = [];\n const tableToModel: string[] = [];\n const fieldToColumn: string[] = [];\n const columnToField: string[] = [];\n\n for (const [modelName, model] of Object.entries(models)) {\n const tableName = model.storage.table;\n modelToTable.push(`readonly ${modelName}: '${tableName}'`);\n tableToModel.push(`readonly ${tableName}: '${modelName}'`);\n\n const fieldMap: string[] = [];\n for (const [fieldName, field] of Object.entries(model.fields)) {\n fieldMap.push(`readonly ${fieldName}: '${field.column}'`);\n }\n\n if (fieldMap.length > 0) {\n fieldToColumn.push(`readonly ${modelName}: { ${fieldMap.join('; ')} }`);\n }\n\n if (storage.tables[tableName]) {\n const colMap: string[] = [];\n for (const [fieldName, field] of Object.entries(model.fields)) {\n colMap.push(`readonly ${field.column}: '${fieldName}'`);\n }\n\n if (colMap.length > 0) {\n columnToField.push(`readonly ${tableName}: { ${colMap.join('; ')} }`);\n }\n }\n }\n\n const parts: string[] = [];\n if (modelToTable.length > 0) {\n parts.push(`modelToTable: { ${modelToTable.join('; ')} }`);\n }\n if (tableToModel.length > 0) {\n parts.push(`tableToModel: { ${tableToModel.join('; ')} }`);\n }\n if (fieldToColumn.length > 0) {\n parts.push(`fieldToColumn: { ${fieldToColumn.join('; ')} }`);\n }\n if (columnToField.length > 0) {\n parts.push(`columnToField: { ${columnToField.join('; ')} }`);\n }\n parts.push(`codecTypes: ${codecTypes || 'Record<string, never>'}`);\n parts.push(`operationTypes: ${operationTypes || 'Record<string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,CAAC,IAAI;AAAA,MACxE;AAEA,UAAI,QAAQ,OAAO,SAAS,GAAG;AAC7B,cAAM,SAAmB,CAAC;AAC1B,mBAAW,CAAC,WAAW,KAAK,KAAK,OAAO,QAAQ,MAAM,MAAM,GAAG;AAC7D,iBAAO,KAAK,YAAY,MAAM,MAAM,MAAM,SAAS,GAAG;AAAA,QACxD;AAEA,YAAI,OAAO,SAAS,GAAG;AACrB,wBAAc,KAAK,YAAY,SAAS,OAAO,OAAO,KAAK,IAAI,CAAC,IAAI;AAAA,QACtE;AAAA,MACF;AAAA,IACF;AAEA,UAAM,QAAkB,CAAC;AACzB,QAAI,aAAa,SAAS,GAAG;AAC3B,YAAM,KAAK,mBAAmB,aAAa,KAAK,IAAI,CAAC,IAAI;AAAA,IAC3D;AACA,QAAI,aAAa,SAAS,GAAG;AAC3B,YAAM,KAAK,mBAAmB,aAAa,KAAK,IAAI,CAAC,IAAI;AAAA,IAC3D;AACA,QAAI,cAAc,SAAS,GAAG;AAC5B,YAAM,KAAK,oBAAoB,cAAc,KAAK,IAAI,CAAC,IAAI;AAAA,IAC7D;AACA,QAAI,cAAc,SAAS,GAAG;AAC5B,YAAM,KAAK,oBAAoB,cAAc,KAAK,IAAI,CAAC,IAAI;AAAA,IAC7D;AACA,UAAM,KAAK,eAAe,cAAc,uBAAuB,EAAE;AACjE,UAAM,KAAK,mBAAmB,kBAAkB,uBAAuB,EAAE;AAEzE,WAAO,KAAK,MAAM,KAAK,IAAI,CAAC;AAAA,EAC9B;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ContractIR } from '@prisma-next/contract/ir';\nimport type {\n GenerateContractTypesOptions,\n TypeRenderContext,\n TypeRenderEntry,\n TypesImportSpec,\n ValidationContext,\n} from '@prisma-next/contract/types';\nimport type {\n ModelDefinition,\n ModelField,\n SqlStorage,\n StorageColumn,\n StorageTable,\n StorageTypeInstance,\n} from '@prisma-next/sql-contract/types';\n\n/**\n * Resolves the typeParams for a column, either from inline typeParams or from typeRef.\n * Returns undefined if no typeParams are available.\n */\nfunction resolveColumnTypeParams(\n column: StorageColumn,\n storage: SqlStorage,\n): Record<string, unknown> | undefined {\n // Inline typeParams take precedence\n if (column.typeParams && Object.keys(column.typeParams).length > 0) {\n return column.typeParams;\n }\n // Check typeRef\n if (column.typeRef && storage.types) {\n const typeInstance = storage.types[column.typeRef] as StorageTypeInstance | undefined;\n if (typeInstance?.typeParams) {\n return typeInstance.typeParams;\n }\n }\n return undefined;\n}\n\nexport const sqlTargetFamilyHook = {\n id: 'sql',\n\n validateTypes(ir: ContractIR, _ctx: ValidationContext): void {\n const storage = ir.storage as SqlStorage | undefined;\n if (!storage || !storage.tables) {\n return;\n }\n\n // Validate codec ID format (ns/name@version). Adapter-provided codecs are available regardless of contract.extensionPacks (which is for framework extensions); TypeScript prevents invalid usage and runtime validates availability.\n\n const typeIdRegex = /^([^/]+)\\/([^@]+)@(\\d+)$/;\n\n for (const [tableName, tableUnknown] of Object.entries(storage.tables)) {\n const table = tableUnknown as StorageTable;\n for (const [colName, colUnknown] of Object.entries(table.columns)) {\n const col = colUnknown as { codecId?: string };\n const codecId = col.codecId;\n if (!codecId) {\n throw new Error(`Column \"${colName}\" in table \"${tableName}\" is missing codecId`);\n }\n\n const match = codecId.match(typeIdRegex);\n if (!match || !match[1]) {\n throw new Error(\n `Column \"${colName}\" in table \"${tableName}\" has invalid codec ID format \"${codecId}\". Expected format: ns/name@version`,\n );\n }\n }\n }\n },\n\n validateStructure(ir: ContractIR): void {\n if (ir.targetFamily !== 'sql') {\n throw new Error(`Expected targetFamily \"sql\", got \"${ir.targetFamily}\"`);\n }\n\n const storage = ir.storage as SqlStorage | undefined;\n if (!storage || !storage.tables) {\n throw new Error('SQL contract must have storage.tables');\n }\n\n const models = ir.models as Record<string, ModelDefinition> | undefined;\n const tableNames = new Set(Object.keys(storage.tables));\n\n if (models) {\n for (const [modelName, modelUnknown] of Object.entries(models)) {\n const model = modelUnknown as ModelDefinition;\n if (!model.storage?.table) {\n throw new Error(`Model \"${modelName}\" is missing storage.table`);\n }\n\n const tableName = model.storage.table;\n if (!tableNames.has(tableName)) {\n throw new Error(`Model \"${modelName}\" references non-existent table \"${tableName}\"`);\n }\n\n const table = storage.tables[tableName];\n if (!table) {\n throw new Error(`Model \"${modelName}\" references non-existent table \"${tableName}\"`);\n }\n\n if (!table.primaryKey) {\n throw new Error(`Model \"${modelName}\" table \"${tableName}\" is missing a primary key`);\n }\n\n const columnNames = new Set(Object.keys(table.columns));\n if (!model.fields || Object.keys(model.fields).length === 0) {\n throw new Error(`Model \"${modelName}\" is missing fields`);\n }\n\n for (const [fieldName, fieldUnknown] of Object.entries(model.fields)) {\n const field = fieldUnknown as ModelField;\n if (!field.column) {\n throw new Error(`Model \"${modelName}\" field \"${fieldName}\" is missing column property`);\n }\n\n if (!columnNames.has(field.column)) {\n throw new Error(\n `Model \"${modelName}\" field \"${fieldName}\" references non-existent column \"${field.column}\" in table \"${tableName}\"`,\n );\n }\n }\n\n if (!model.relations || typeof model.relations !== 'object') {\n throw new Error(\n `Model \"${modelName}\" is missing required field \"relations\" (must be an object)`,\n );\n }\n }\n }\n\n for (const [tableName, tableUnknown] of Object.entries(storage.tables)) {\n const table = tableUnknown as StorageTable;\n const columnNames = new Set(Object.keys(table.columns));\n\n // Column structure (nullable, nativeType, codecId) and table arrays (uniques, indexes, foreignKeys)\n // are validated by Arktype schema validation - no need to re-check here.\n // We only validate logical consistency (foreign key references, model references, etc.)\n\n if (!Array.isArray(table.uniques)) {\n throw new Error(\n `Table \"${tableName}\" is missing required field \"uniques\" (must be an array)`,\n );\n }\n if (!Array.isArray(table.indexes)) {\n throw new Error(\n `Table \"${tableName}\" is missing required field \"indexes\" (must be an array)`,\n );\n }\n if (!Array.isArray(table.foreignKeys)) {\n throw new Error(\n `Table \"${tableName}\" is missing required field \"foreignKeys\" (must be an array)`,\n );\n }\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(\n `Table \"${tableName}\" index references non-existent column \"${colName}\"`,\n );\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 = storage.tables[fk.references.table];\n if (!referencedTable) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.references.table}\"`,\n );\n }\n\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\n generateContractTypes(\n ir: ContractIR,\n codecTypeImports: ReadonlyArray<TypesImportSpec>,\n operationTypeImports: ReadonlyArray<TypesImportSpec>,\n options?: GenerateContractTypesOptions,\n ): string {\n const parameterizedRenderers = options?.parameterizedRenderers;\n const storage = ir.storage as SqlStorage;\n const models = ir.models as Record<string, ModelDefinition>;\n\n // Note: Parameterized type imports (e.g., Vector<N>) are expected to be included\n // in codecTypeImports by the assembly layer. The renderer only generates types.\n const allImports = [...codecTypeImports, ...operationTypeImports];\n const importLines = allImports.map((imp) => {\n // Simplify import when named === alias (e.g., `import type { Vector }` instead of `{ Vector as Vector }`)\n const importClause = imp.named === imp.alias ? imp.named : `${imp.named} as ${imp.alias}`;\n return `import type { ${importClause} } from '${imp.package}';`;\n });\n\n // Only intersect actual codec/operation type maps. Extra type-only imports (e.g. Vector<N>) are\n // included in importLines via codecTypeImports but must not be intersected into CodecTypes.\n const codecTypes = codecTypeImports\n .filter((imp) => imp.named === 'CodecTypes')\n .map((imp) => imp.alias)\n .join(' & ');\n const operationTypes = operationTypeImports\n .filter((imp) => imp.named === 'OperationTypes')\n .map((imp) => imp.alias)\n .join(' & ');\n\n const storageType = this.generateStorageType(storage);\n const modelsType = this.generateModelsType(models, storage, parameterizedRenderers);\n const relationsType = this.generateRelationsType(ir.relations);\n const mappingsType = this.generateMappingsType(models, storage, codecTypes, operationTypes);\n\n return `// ⚠️ GENERATED FILE - DO NOT EDIT\n// This file is automatically generated by 'prisma-next contract emit'.\n// To regenerate, run: prisma-next contract emit\n${importLines.join('\\n')}\n\nimport type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';\n\nexport type CodecTypes = ${codecTypes || 'Record<string, never>'};\nexport type LaneCodecTypes = CodecTypes;\nexport type OperationTypes = ${operationTypes || 'Record<string, never>'};\n\nexport type Contract = SqlContract<\n ${storageType},\n ${modelsType},\n ${relationsType},\n ${mappingsType}\n>;\n\nexport type Tables = Contract['storage']['tables'];\nexport type Models = Contract['models'];\nexport type Relations = Contract['relations'];\n`;\n },\n\n generateStorageType(storage: SqlStorage): string {\n const tables: string[] = [];\n for (const [tableName, table] of Object.entries(storage.tables)) {\n const columns: string[] = [];\n for (const [colName, col] of Object.entries(table.columns)) {\n const nullable = col.nullable ? 'true' : 'false';\n const nativeType = `'${col.nativeType}'`;\n const codecId = `'${col.codecId}'`;\n columns.push(\n `readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable} }`,\n );\n }\n\n const tableParts: string[] = [`columns: { ${columns.join('; ')} }`];\n\n if (table.primaryKey) {\n const pkCols = table.primaryKey.columns.map((c) => `'${c}'`).join(', ');\n const pkName = table.primaryKey.name ? `; readonly name: '${table.primaryKey.name}'` : '';\n tableParts.push(`primaryKey: { readonly columns: readonly [${pkCols}]${pkName} }`);\n }\n\n const uniques = table.uniques\n .map((u) => {\n const cols = u.columns.map((c: string) => `'${c}'`).join(', ');\n const name = u.name ? `; readonly name: '${u.name}'` : '';\n return `{ readonly columns: readonly [${cols}]${name} }`;\n })\n .join(', ');\n tableParts.push(`uniques: readonly [${uniques}]`);\n\n const indexes = table.indexes\n .map((i) => {\n const cols = i.columns.map((c: string) => `'${c}'`).join(', ');\n const name = i.name ? `; readonly name: '${i.name}'` : '';\n return `{ readonly columns: readonly [${cols}]${name} }`;\n })\n .join(', ');\n tableParts.push(`indexes: readonly [${indexes}]`);\n\n const fks = table.foreignKeys\n .map((fk) => {\n const cols = fk.columns.map((c: string) => `'${c}'`).join(', ');\n const refCols = fk.references.columns.map((c: string) => `'${c}'`).join(', ');\n const name = fk.name ? `; readonly name: '${fk.name}'` : '';\n return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: '${fk.references.table}'; readonly columns: readonly [${refCols}] }${name} }`;\n })\n .join(', ');\n tableParts.push(`foreignKeys: readonly [${fks}]`);\n\n tables.push(`readonly ${tableName}: { ${tableParts.join('; ')} }`);\n }\n\n const typesType = this.generateStorageTypesType(storage.types);\n\n return `{ readonly tables: { ${tables.join('; ')} }; readonly types: ${typesType} }`;\n },\n\n /**\n * Generates the TypeScript type for storage.types with literal types.\n * This preserves type params as literal values for precise typing.\n */\n generateStorageTypesType(types: SqlStorage['types']): string {\n if (!types || Object.keys(types).length === 0) {\n return 'Record<string, never>';\n }\n\n const typeEntries: string[] = [];\n for (const [typeName, typeInstance] of Object.entries(types)) {\n const codecId = `'${typeInstance.codecId}'`;\n const nativeType = `'${typeInstance.nativeType}'`;\n const typeParamsStr = this.serializeTypeParamsLiteral(typeInstance.typeParams);\n typeEntries.push(\n `readonly ${typeName}: { readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,\n );\n }\n\n return `{ ${typeEntries.join('; ')} }`;\n },\n\n /**\n * Serializes a typeParams object to a TypeScript literal type.\n * Converts { length: 1536 } to \"{ readonly length: 1536 }\".\n */\n serializeTypeParamsLiteral(params: Record<string, unknown>): string {\n if (!params || Object.keys(params).length === 0) {\n return 'Record<string, never>';\n }\n\n const entries: string[] = [];\n for (const [key, value] of Object.entries(params)) {\n const serialized = this.serializeValue(value);\n entries.push(`readonly ${key}: ${serialized}`);\n }\n\n return `{ ${entries.join('; ')} }`;\n },\n\n /**\n * Serializes a value to a TypeScript literal type expression.\n */\n serializeValue(value: unknown): string {\n if (value === null) {\n return 'null';\n }\n if (value === undefined) {\n return 'undefined';\n }\n if (typeof value === 'string') {\n return `'${value}'`;\n }\n if (typeof value === 'number' || typeof value === 'boolean') {\n return String(value);\n }\n if (Array.isArray(value)) {\n const items = value.map((v) => this.serializeValue(v)).join(', ');\n return `readonly [${items}]`;\n }\n if (typeof value === 'object') {\n const entries: string[] = [];\n for (const [k, v] of Object.entries(value)) {\n entries.push(`readonly ${k}: ${this.serializeValue(v)}`);\n }\n return `{ ${entries.join('; ')} }`;\n }\n return 'unknown';\n },\n\n generateModelsType(\n models: Record<string, ModelDefinition> | undefined,\n storage: SqlStorage,\n parameterizedRenderers?: Map<string, TypeRenderEntry>,\n ): string {\n if (!models) {\n return 'Record<string, never>';\n }\n\n const renderCtx: TypeRenderContext = { codecTypesName: 'CodecTypes' };\n\n const modelTypes: string[] = [];\n for (const [modelName, model] of Object.entries(models)) {\n const fields: string[] = [];\n const tableName = model.storage.table;\n const table = storage.tables[tableName];\n\n if (table) {\n for (const [fieldName, field] of Object.entries(model.fields)) {\n const column = table.columns[field.column];\n if (!column) {\n fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);\n continue;\n }\n\n const jsType = this.generateColumnType(\n column,\n storage,\n parameterizedRenderers,\n renderCtx,\n );\n fields.push(`readonly ${fieldName}: ${jsType}`);\n }\n } else {\n for (const [fieldName, field] of Object.entries(model.fields)) {\n fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);\n }\n }\n\n const relations: string[] = [];\n for (const [relName, rel] of Object.entries(model.relations)) {\n if (typeof rel === 'object' && rel !== null && 'on' in rel) {\n const on = rel.on as { parentCols?: string[]; childCols?: string[] };\n if (on.parentCols && on.childCols) {\n const parentCols = on.parentCols.map((c) => `'${c}'`).join(', ');\n const childCols = on.childCols.map((c) => `'${c}'`).join(', ');\n relations.push(\n `readonly ${relName}: { readonly on: { readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] } }`,\n );\n }\n }\n }\n\n const modelParts: string[] = [\n `storage: { readonly table: '${tableName}' }`,\n `fields: { ${fields.join('; ')} }`,\n ];\n\n if (relations.length > 0) {\n modelParts.push(`relations: { ${relations.join('; ')} }`);\n }\n\n modelTypes.push(`readonly ${modelName}: { ${modelParts.join('; ')} }`);\n }\n\n return `{ ${modelTypes.join('; ')} }`;\n },\n\n /**\n * Generates the TypeScript type expression for a column.\n * Uses parameterized renderer if the column has typeParams and a matching renderer exists,\n * otherwise falls back to CodecTypes[codecId]['output'].\n */\n generateColumnType(\n column: StorageColumn,\n storage: SqlStorage,\n parameterizedRenderers: Map<string, TypeRenderEntry> | undefined,\n renderCtx: TypeRenderContext,\n ): string {\n const typeParams = resolveColumnTypeParams(column, storage);\n const nullable = column.nullable ?? false;\n let baseType: string;\n\n if (typeParams && parameterizedRenderers) {\n const renderer = parameterizedRenderers.get(column.codecId);\n if (renderer) {\n baseType = renderer.render(typeParams, renderCtx);\n } else {\n // No renderer for this codecId, fall back to standard lookup\n baseType = `CodecTypes['${column.codecId}']['output']`;\n }\n } else {\n // No typeParams, use standard codec lookup\n baseType = `CodecTypes['${column.codecId}']['output']`;\n }\n\n return nullable ? `${baseType} | null` : baseType;\n },\n\n generateRelationsType(relations: Record<string, unknown> | undefined): string {\n if (!relations || Object.keys(relations).length === 0) {\n return 'Record<string, never>';\n }\n\n const tableEntries: string[] = [];\n for (const [tableName, relsValue] of Object.entries(relations)) {\n if (typeof relsValue !== 'object' || relsValue === null) {\n continue;\n }\n const rels = relsValue as Record<string, unknown>;\n const relationEntries: string[] = [];\n for (const [relName, relValue] of Object.entries(rels)) {\n if (typeof relValue !== 'object' || relValue === null) {\n relationEntries.push(`readonly ${relName}: unknown`);\n continue;\n }\n const { to, cardinality, on, through } = relValue as {\n readonly to?: string;\n readonly cardinality?: string;\n readonly on?: {\n readonly parentCols?: readonly string[];\n readonly childCols?: readonly string[];\n };\n readonly through?: {\n readonly table: string;\n readonly parentCols: readonly string[];\n readonly childCols: readonly string[];\n };\n };\n\n const parts: string[] = [];\n if (to) {\n parts.push(`readonly to: '${to}'`);\n }\n if (cardinality) {\n parts.push(`readonly cardinality: '${cardinality}'`);\n }\n if (on?.parentCols && on.childCols) {\n const parentCols = on.parentCols.map((c) => `'${c}'`).join(', ');\n const childCols = on.childCols.map((c) => `'${c}'`).join(', ');\n parts.push(\n `readonly on: { readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] }`,\n );\n }\n if (through) {\n const parentCols = through.parentCols.map((c) => `'${c}'`).join(', ');\n const childCols = through.childCols.map((c) => `'${c}'`).join(', ');\n parts.push(\n `readonly through: { readonly table: '${through.table}'; readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] }`,\n );\n }\n\n relationEntries.push(\n parts.length > 0\n ? `readonly ${relName}: { ${parts.join('; ')} }`\n : `readonly ${relName}: unknown`,\n );\n }\n tableEntries.push(`readonly ${tableName}: { ${relationEntries.join('; ')} }`);\n }\n\n return `{ ${tableEntries.join('; ')} }`;\n },\n\n generateMappingsType(\n models: Record<string, ModelDefinition> | undefined,\n storage: SqlStorage,\n codecTypes: string,\n operationTypes: string,\n ): string {\n if (!models) {\n return `SqlMappings & { readonly codecTypes: ${codecTypes || 'Record<string, never>'}; readonly operationTypes: ${operationTypes || 'Record<string, never>'}; }`;\n }\n\n const modelToTable: string[] = [];\n const tableToModel: string[] = [];\n const fieldToColumn: string[] = [];\n const columnToField: string[] = [];\n\n for (const [modelName, model] of Object.entries(models)) {\n const tableName = model.storage.table;\n modelToTable.push(`readonly ${modelName}: '${tableName}'`);\n tableToModel.push(`readonly ${tableName}: '${modelName}'`);\n\n const fieldMap: string[] = [];\n for (const [fieldName, field] of Object.entries(model.fields)) {\n fieldMap.push(`readonly ${fieldName}: '${field.column}'`);\n }\n\n if (fieldMap.length > 0) {\n fieldToColumn.push(`readonly ${modelName}: { ${fieldMap.join('; ')} }`);\n }\n\n if (storage.tables[tableName]) {\n const colMap: string[] = [];\n for (const [fieldName, field] of Object.entries(model.fields)) {\n colMap.push(`readonly ${field.column}: '${fieldName}'`);\n }\n\n if (colMap.length > 0) {\n columnToField.push(`readonly ${tableName}: { ${colMap.join('; ')} }`);\n }\n }\n }\n\n const parts: string[] = [];\n if (modelToTable.length > 0) {\n parts.push(`modelToTable: { ${modelToTable.join('; ')} }`);\n }\n if (tableToModel.length > 0) {\n parts.push(`tableToModel: { ${tableToModel.join('; ')} }`);\n }\n if (fieldToColumn.length > 0) {\n parts.push(`fieldToColumn: { ${fieldToColumn.join('; ')} }`);\n }\n if (columnToField.length > 0) {\n parts.push(`columnToField: { ${columnToField.join('; ')} }`);\n }\n parts.push(`codecTypes: ${codecTypes || 'Record<string, never>'}`);\n parts.push(`operationTypes: ${operationTypes || 'Record<string, never>'}`);\n\n return `{ ${parts.join('; ')} }`;\n },\n} as const;\n"],"mappings":";AAqBA,SAAS,wBACP,QACA,SACqC;AAErC,MAAI,OAAO,cAAc,OAAO,KAAK,OAAO,UAAU,EAAE,SAAS,GAAG;AAClE,WAAO,OAAO;AAAA,EAChB;AAEA,MAAI,OAAO,WAAW,QAAQ,OAAO;AACnC,UAAM,eAAe,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AM,SAAmB,CAAC;AAC1B,mBAAW,CAAC,WAAW,KAAK,KAAK,OAAO,QAAQ,MAAM,MAAM,GAAG;AAC7D,iBAAO,KAAK,YAAY,MAAM,MAAM,MAAM,SAAS,GAAG;AAAA,QACxD;AAEA,YAAI,OAAO,SAAS,GAAG;AACrB,wBAAc,KAAK,YAAY,SAAS,OAAO,OAAO,KAAK,IAAI,CAAC,IAAI;AAAA,QACtE;AAAA,MACF;AAAA,IACF;AAEA,UAAM,QAAkB,CAAC;AACzB,QAAI,aAAa,SAAS,GAAG;AAC3B,YAAM,KAAK,mBAAmB,aAAa,KAAK,IAAI,CAAC,IAAI;AAAA,IAC3D;AACA,QAAI,aAAa,SAAS,GAAG;AAC3B,YAAM,KAAK,mBAAmB,aAAa,KAAK,IAAI,CAAC,IAAI;AAAA,IAC3D;AACA,QAAI,cAAc,SAAS,GAAG;AAC5B,YAAM,KAAK,oBAAoB,cAAc,KAAK,IAAI,CAAC,IAAI;AAAA,IAC7D;AACA,QAAI,cAAc,SAAS,GAAG;AAC5B,YAAM,KAAK,oBAAoB,cAAc,KAAK,IAAI,CAAC,IAAI;AAAA,IAC7D;AACA,UAAM,KAAK,eAAe,cAAc,uBAAuB,EAAE;AACjE,UAAM,KAAK,mBAAmB,kBAAkB,uBAAuB,EAAE;AAEzE,WAAO,KAAK,MAAM,KAAK,IAAI,CAAC;AAAA,EAC9B;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract-emitter",
|
|
3
|
-
"version": "0.3.0-
|
|
3
|
+
"version": "0.3.0-pr.100.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "SQL emitter hook for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@prisma-next/contract": "0.3.0-
|
|
9
|
-
"@prisma-next/emitter": "0.3.0-
|
|
10
|
-
"@prisma-next/sql-contract": "0.3.0-
|
|
8
|
+
"@prisma-next/contract": "0.3.0-pr.100.1",
|
|
9
|
+
"@prisma-next/emitter": "0.3.0-pr.100.1",
|
|
10
|
+
"@prisma-next/sql-contract": "0.3.0-pr.100.1"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@vitest/coverage-v8": "4.0.16",
|
|
14
13
|
"tsup": "8.5.1",
|
|
15
14
|
"typescript": "5.9.3",
|
|
16
15
|
"vitest": "4.0.16",
|
|
17
|
-
"@prisma-next/test-utils": "0.0.1"
|
|
16
|
+
"@prisma-next/test-utils": "0.0.1",
|
|
17
|
+
"@prisma-next/tsconfig": "0.0.0"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"test": "vitest run --passWithNoTests",
|
|
32
32
|
"test:coverage": "vitest run --coverage --passWithNoTests",
|
|
33
33
|
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
34
|
-
"lint": "biome check . --
|
|
35
|
-
"lint:fix": "biome check --write .
|
|
36
|
-
"lint:fix:unsafe": "biome check --write --unsafe .
|
|
37
|
-
"clean": "
|
|
34
|
+
"lint": "biome check . --error-on-warnings",
|
|
35
|
+
"lint:fix": "biome check --write .",
|
|
36
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
37
|
+
"clean": "rm -rf dist coverage .tmp-output"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,42 @@
|
|
|
1
1
|
import type { ContractIR } from '@prisma-next/contract/ir';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GenerateContractTypesOptions,
|
|
4
|
+
TypeRenderContext,
|
|
5
|
+
TypeRenderEntry,
|
|
6
|
+
TypesImportSpec,
|
|
7
|
+
ValidationContext,
|
|
8
|
+
} from '@prisma-next/contract/types';
|
|
3
9
|
import type {
|
|
4
10
|
ModelDefinition,
|
|
5
11
|
ModelField,
|
|
6
12
|
SqlStorage,
|
|
13
|
+
StorageColumn,
|
|
7
14
|
StorageTable,
|
|
15
|
+
StorageTypeInstance,
|
|
8
16
|
} from '@prisma-next/sql-contract/types';
|
|
9
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the typeParams for a column, either from inline typeParams or from typeRef.
|
|
20
|
+
* Returns undefined if no typeParams are available.
|
|
21
|
+
*/
|
|
22
|
+
function resolveColumnTypeParams(
|
|
23
|
+
column: StorageColumn,
|
|
24
|
+
storage: SqlStorage,
|
|
25
|
+
): Record<string, unknown> | undefined {
|
|
26
|
+
// Inline typeParams take precedence
|
|
27
|
+
if (column.typeParams && Object.keys(column.typeParams).length > 0) {
|
|
28
|
+
return column.typeParams;
|
|
29
|
+
}
|
|
30
|
+
// Check typeRef
|
|
31
|
+
if (column.typeRef && storage.types) {
|
|
32
|
+
const typeInstance = storage.types[column.typeRef] as StorageTypeInstance | undefined;
|
|
33
|
+
if (typeInstance?.typeParams) {
|
|
34
|
+
return typeInstance.typeParams;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
10
40
|
export const sqlTargetFamilyHook = {
|
|
11
41
|
id: 'sql',
|
|
12
42
|
|
|
@@ -197,26 +227,40 @@ export const sqlTargetFamilyHook = {
|
|
|
197
227
|
ir: ContractIR,
|
|
198
228
|
codecTypeImports: ReadonlyArray<TypesImportSpec>,
|
|
199
229
|
operationTypeImports: ReadonlyArray<TypesImportSpec>,
|
|
230
|
+
options?: GenerateContractTypesOptions,
|
|
200
231
|
): string {
|
|
201
|
-
const
|
|
202
|
-
const importLines = allImports.map(
|
|
203
|
-
(imp) => `import type { ${imp.named} as ${imp.alias} } from '${imp.package}';`,
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
const codecTypes = codecTypeImports.map((imp) => imp.alias).join(' & ');
|
|
207
|
-
const operationTypes = operationTypeImports.map((imp) => imp.alias).join(' & ');
|
|
208
|
-
|
|
232
|
+
const parameterizedRenderers = options?.parameterizedRenderers;
|
|
209
233
|
const storage = ir.storage as SqlStorage;
|
|
210
234
|
const models = ir.models as Record<string, ModelDefinition>;
|
|
211
235
|
|
|
236
|
+
// Note: Parameterized type imports (e.g., Vector<N>) are expected to be included
|
|
237
|
+
// in codecTypeImports by the assembly layer. The renderer only generates types.
|
|
238
|
+
const allImports = [...codecTypeImports, ...operationTypeImports];
|
|
239
|
+
const importLines = allImports.map((imp) => {
|
|
240
|
+
// Simplify import when named === alias (e.g., `import type { Vector }` instead of `{ Vector as Vector }`)
|
|
241
|
+
const importClause = imp.named === imp.alias ? imp.named : `${imp.named} as ${imp.alias}`;
|
|
242
|
+
return `import type { ${importClause} } from '${imp.package}';`;
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Only intersect actual codec/operation type maps. Extra type-only imports (e.g. Vector<N>) are
|
|
246
|
+
// included in importLines via codecTypeImports but must not be intersected into CodecTypes.
|
|
247
|
+
const codecTypes = codecTypeImports
|
|
248
|
+
.filter((imp) => imp.named === 'CodecTypes')
|
|
249
|
+
.map((imp) => imp.alias)
|
|
250
|
+
.join(' & ');
|
|
251
|
+
const operationTypes = operationTypeImports
|
|
252
|
+
.filter((imp) => imp.named === 'OperationTypes')
|
|
253
|
+
.map((imp) => imp.alias)
|
|
254
|
+
.join(' & ');
|
|
255
|
+
|
|
212
256
|
const storageType = this.generateStorageType(storage);
|
|
213
|
-
const modelsType = this.generateModelsType(models, storage);
|
|
257
|
+
const modelsType = this.generateModelsType(models, storage, parameterizedRenderers);
|
|
214
258
|
const relationsType = this.generateRelationsType(ir.relations);
|
|
215
259
|
const mappingsType = this.generateMappingsType(models, storage, codecTypes, operationTypes);
|
|
216
260
|
|
|
217
261
|
return `// ⚠️ GENERATED FILE - DO NOT EDIT
|
|
218
|
-
// This file is automatically generated by 'prisma-next emit'.
|
|
219
|
-
// To regenerate, run: prisma-next emit
|
|
262
|
+
// This file is automatically generated by 'prisma-next contract emit'.
|
|
263
|
+
// To regenerate, run: prisma-next contract emit
|
|
220
264
|
${importLines.join('\n')}
|
|
221
265
|
|
|
222
266
|
import type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';
|
|
@@ -290,17 +334,92 @@ export type Relations = Contract['relations'];
|
|
|
290
334
|
tables.push(`readonly ${tableName}: { ${tableParts.join('; ')} }`);
|
|
291
335
|
}
|
|
292
336
|
|
|
293
|
-
|
|
337
|
+
const typesType = this.generateStorageTypesType(storage.types);
|
|
338
|
+
|
|
339
|
+
return `{ readonly tables: { ${tables.join('; ')} }; readonly types: ${typesType} }`;
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Generates the TypeScript type for storage.types with literal types.
|
|
344
|
+
* This preserves type params as literal values for precise typing.
|
|
345
|
+
*/
|
|
346
|
+
generateStorageTypesType(types: SqlStorage['types']): string {
|
|
347
|
+
if (!types || Object.keys(types).length === 0) {
|
|
348
|
+
return 'Record<string, never>';
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const typeEntries: string[] = [];
|
|
352
|
+
for (const [typeName, typeInstance] of Object.entries(types)) {
|
|
353
|
+
const codecId = `'${typeInstance.codecId}'`;
|
|
354
|
+
const nativeType = `'${typeInstance.nativeType}'`;
|
|
355
|
+
const typeParamsStr = this.serializeTypeParamsLiteral(typeInstance.typeParams);
|
|
356
|
+
typeEntries.push(
|
|
357
|
+
`readonly ${typeName}: { readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return `{ ${typeEntries.join('; ')} }`;
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Serializes a typeParams object to a TypeScript literal type.
|
|
366
|
+
* Converts { length: 1536 } to "{ readonly length: 1536 }".
|
|
367
|
+
*/
|
|
368
|
+
serializeTypeParamsLiteral(params: Record<string, unknown>): string {
|
|
369
|
+
if (!params || Object.keys(params).length === 0) {
|
|
370
|
+
return 'Record<string, never>';
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const entries: string[] = [];
|
|
374
|
+
for (const [key, value] of Object.entries(params)) {
|
|
375
|
+
const serialized = this.serializeValue(value);
|
|
376
|
+
entries.push(`readonly ${key}: ${serialized}`);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return `{ ${entries.join('; ')} }`;
|
|
380
|
+
},
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Serializes a value to a TypeScript literal type expression.
|
|
384
|
+
*/
|
|
385
|
+
serializeValue(value: unknown): string {
|
|
386
|
+
if (value === null) {
|
|
387
|
+
return 'null';
|
|
388
|
+
}
|
|
389
|
+
if (value === undefined) {
|
|
390
|
+
return 'undefined';
|
|
391
|
+
}
|
|
392
|
+
if (typeof value === 'string') {
|
|
393
|
+
return `'${value}'`;
|
|
394
|
+
}
|
|
395
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
396
|
+
return String(value);
|
|
397
|
+
}
|
|
398
|
+
if (Array.isArray(value)) {
|
|
399
|
+
const items = value.map((v) => this.serializeValue(v)).join(', ');
|
|
400
|
+
return `readonly [${items}]`;
|
|
401
|
+
}
|
|
402
|
+
if (typeof value === 'object') {
|
|
403
|
+
const entries: string[] = [];
|
|
404
|
+
for (const [k, v] of Object.entries(value)) {
|
|
405
|
+
entries.push(`readonly ${k}: ${this.serializeValue(v)}`);
|
|
406
|
+
}
|
|
407
|
+
return `{ ${entries.join('; ')} }`;
|
|
408
|
+
}
|
|
409
|
+
return 'unknown';
|
|
294
410
|
},
|
|
295
411
|
|
|
296
412
|
generateModelsType(
|
|
297
413
|
models: Record<string, ModelDefinition> | undefined,
|
|
298
414
|
storage: SqlStorage,
|
|
415
|
+
parameterizedRenderers?: Map<string, TypeRenderEntry>,
|
|
299
416
|
): string {
|
|
300
417
|
if (!models) {
|
|
301
418
|
return 'Record<string, never>';
|
|
302
419
|
}
|
|
303
420
|
|
|
421
|
+
const renderCtx: TypeRenderContext = { codecTypesName: 'CodecTypes' };
|
|
422
|
+
|
|
304
423
|
const modelTypes: string[] = [];
|
|
305
424
|
for (const [modelName, model] of Object.entries(models)) {
|
|
306
425
|
const fields: string[] = [];
|
|
@@ -315,12 +434,12 @@ export type Relations = Contract['relations'];
|
|
|
315
434
|
continue;
|
|
316
435
|
}
|
|
317
436
|
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
437
|
+
const jsType = this.generateColumnType(
|
|
438
|
+
column,
|
|
439
|
+
storage,
|
|
440
|
+
parameterizedRenderers,
|
|
441
|
+
renderCtx,
|
|
442
|
+
);
|
|
324
443
|
fields.push(`readonly ${fieldName}: ${jsType}`);
|
|
325
444
|
}
|
|
326
445
|
} else {
|
|
@@ -358,6 +477,37 @@ export type Relations = Contract['relations'];
|
|
|
358
477
|
return `{ ${modelTypes.join('; ')} }`;
|
|
359
478
|
},
|
|
360
479
|
|
|
480
|
+
/**
|
|
481
|
+
* Generates the TypeScript type expression for a column.
|
|
482
|
+
* Uses parameterized renderer if the column has typeParams and a matching renderer exists,
|
|
483
|
+
* otherwise falls back to CodecTypes[codecId]['output'].
|
|
484
|
+
*/
|
|
485
|
+
generateColumnType(
|
|
486
|
+
column: StorageColumn,
|
|
487
|
+
storage: SqlStorage,
|
|
488
|
+
parameterizedRenderers: Map<string, TypeRenderEntry> | undefined,
|
|
489
|
+
renderCtx: TypeRenderContext,
|
|
490
|
+
): string {
|
|
491
|
+
const typeParams = resolveColumnTypeParams(column, storage);
|
|
492
|
+
const nullable = column.nullable ?? false;
|
|
493
|
+
let baseType: string;
|
|
494
|
+
|
|
495
|
+
if (typeParams && parameterizedRenderers) {
|
|
496
|
+
const renderer = parameterizedRenderers.get(column.codecId);
|
|
497
|
+
if (renderer) {
|
|
498
|
+
baseType = renderer.render(typeParams, renderCtx);
|
|
499
|
+
} else {
|
|
500
|
+
// No renderer for this codecId, fall back to standard lookup
|
|
501
|
+
baseType = `CodecTypes['${column.codecId}']['output']`;
|
|
502
|
+
}
|
|
503
|
+
} else {
|
|
504
|
+
// No typeParams, use standard codec lookup
|
|
505
|
+
baseType = `CodecTypes['${column.codecId}']['output']`;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return nullable ? `${baseType} | null` : baseType;
|
|
509
|
+
},
|
|
510
|
+
|
|
361
511
|
generateRelationsType(relations: Record<string, unknown> | undefined): string {
|
|
362
512
|
if (!relations || Object.keys(relations).length === 0) {
|
|
363
513
|
return 'Record<string, never>';
|