@prisma-next/sql-contract-emitter 0.3.0-dev.7 → 0.3.0-dev.71
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/LICENSE +201 -0
- package/dist/index.d.mts +42 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +315 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +26 -17
- package/src/index.ts +249 -47
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -379
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["table: StorageTable | undefined","referencedTable: StorageTable | undefined","allImports: TypesImportSpec[]","uniqueImports: TypesImportSpec[]","tables: string[]","columns: string[]","tableParts: string[]","typeEntries: string[]","entries: string[]","renderCtx: TypeRenderContext","modelTypes: string[]","fields: string[]","relations: string[]","modelParts: string[]","tableEntries: string[]","relationEntries: string[]","parts: string[]","modelToTable: string[]","tableToModel: string[]","fieldToColumn: string[]","columnToField: string[]","fieldMap: string[]","colMap: string[]"],"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';\nimport { assertDefined } from '@prisma-next/utils/assertions';\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: StorageTable | undefined = storage.tables[tableName];\n assertDefined(table, `Model \"${modelName}\" references non-existent table \"${tableName}\"`);\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 // Table existence guaranteed by Set.has() check above\n const referencedTable: StorageTable | undefined = storage.tables[fk.references.table];\n assertDefined(\n referencedTable,\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.references.table}\"`,\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 hashes: {\n readonly storageHash: string;\n readonly executionHash?: string;\n readonly profileHash: string;\n },\n options?: GenerateContractTypesOptions,\n ): string {\n const parameterizedRenderers = options?.parameterizedRenderers;\n const parameterizedTypeImports = options?.parameterizedTypeImports;\n const storage = ir.storage as SqlStorage;\n const models = ir.models as Record<string, ModelDefinition>;\n\n // Collect all type imports from three sources:\n // 1. Codec type imports (from adapters, targets, and extensions)\n // 2. Operation type imports (from adapters, targets, and extensions)\n // 3. Parameterized type imports (for parameterized codec renderers, may contain duplicates)\n const allImports: TypesImportSpec[] = [...codecTypeImports, ...operationTypeImports];\n\n if (parameterizedTypeImports) {\n allImports.push(...parameterizedTypeImports);\n }\n\n // Deduplicate imports by package+named to avoid duplicate import statements.\n // Strategy: When the same package::named appears multiple times, keep the first\n // occurrence (and its alias); later duplicates with different aliases are silently ignored.\n //\n // Note: uniqueImports must be an array (not a Set) because:\n // - We need to preserve the full TypesImportSpec objects (package, named, alias)\n // - We need to preserve insertion order (first occurrence wins)\n // - seenImportKeys is a Set used only for O(1) duplicate detection\n const seenImportKeys = new Set<string>();\n const uniqueImports: TypesImportSpec[] = [];\n for (const imp of allImports) {\n const key = `${imp.package}::${imp.named}`;\n if (!seenImportKeys.has(key)) {\n seenImportKeys.add(key);\n uniqueImports.push(imp);\n }\n }\n\n // Generate import statements, omitting redundant \"as Alias\" when named === alias\n const importLines = uniqueImports.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 const executionHashType = hashes.executionHash\n ? `ExecutionHashBase<'${hashes.executionHash}'>`\n : 'ExecutionHashBase<string>';\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\n import type { ExecutionHashBase, ProfileHashBase, StorageHashBase } from '@prisma-next/contract/types';\n import type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';\n\n export type StorageHash = StorageHashBase<'${hashes.storageHash}'>;\n export type ExecutionHash = ${executionHashType};\n export type ProfileHash = ProfileHashBase<'${hashes.profileHash}'>;\n\n export type CodecTypes = ${codecTypes || 'Record<string, never>'};\n export type LaneCodecTypes = CodecTypes;\n export type OperationTypes = ${operationTypes || 'Record<string, never>'};\n type DefaultLiteralValue<CodecId extends string, Encoded> =\n CodecId extends keyof CodecTypes\n ? CodecTypes[CodecId] extends { readonly output: infer O }\n ? O extends Date | bigint ? O : Encoded\n : Encoded\n : Encoded;\n\n export type Contract = SqlContract<\n ${storageType},\n ${modelsType},\n ${relationsType},\n ${mappingsType},\n StorageHash,\n ExecutionHash,\n ProfileHash\n >;\n\n export type Tables = Contract['storage']['tables'];\n export type Models = Contract['models'];\n export 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 const defaultSpec = col.default\n ? col.default.kind === 'literal'\n ? `; readonly default: { readonly kind: 'literal'; readonly value: DefaultLiteralValue<${codecId}, ${this.serializeValue(\n col.default.value,\n )}> }`\n : `; readonly default: { readonly kind: 'function'; readonly expression: ${this.serializeValue(\n col.default.expression,\n )} }`\n : '';\n columns.push(\n `readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable}${defaultSpec} }`,\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}; readonly constraint: ${fk.constraint}; readonly index: ${fk.index} }`;\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 // Escape backslashes first, then single quotes\n const escaped = value.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\");\n return `'${escaped}'`;\n }\n if (typeof value === 'number' || typeof value === 'boolean') {\n return String(value);\n }\n if (typeof value === 'bigint') {\n return `${value}n`;\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 const fallbackType = `CodecTypes['${column.codecId}']['output']`;\n const renderer = typeParams && parameterizedRenderers?.get(column.codecId);\n const baseType = renderer ? renderer.render(typeParams, renderCtx) : fallbackType;\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":";;;;;;;AAsBA,SAAS,wBACP,QACA,SACqC;AAErC,KAAI,OAAO,cAAc,OAAO,KAAK,OAAO,WAAW,CAAC,SAAS,EAC/D,QAAO,OAAO;AAGhB,KAAI,OAAO,WAAW,QAAQ,OAAO;EACnC,MAAM,eAAe,QAAQ,MAAM,OAAO;AAC1C,MAAI,cAAc,WAChB,QAAO,aAAa;;;AAM1B,MAAa,sBAAsB;CACjC,IAAI;CAEJ,cAAc,IAAgB,MAA+B;EAC3D,MAAM,UAAU,GAAG;AACnB,MAAI,CAAC,WAAW,CAAC,QAAQ,OACvB;EAKF,MA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,EACzB,OAAM,KAAK,oBAAoB,cAAc,KAAK,KAAK,CAAC,IAAI;AAE9D,QAAM,KAAK,eAAe,cAAc,0BAA0B;AAClE,QAAM,KAAK,mBAAmB,kBAAkB,0BAA0B;AAE1E,SAAO,KAAK,MAAM,KAAK,KAAK,CAAC;;CAEhC"}
|
package/package.json
CHANGED
|
@@ -1,39 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract-emitter",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.71",
|
|
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-dev.
|
|
9
|
-
"@prisma-next/emitter": "0.3.0-dev.
|
|
10
|
-
"@prisma-next/sql-contract": "0.3.0-dev.
|
|
8
|
+
"@prisma-next/contract": "0.3.0-dev.71",
|
|
9
|
+
"@prisma-next/emitter": "0.3.0-dev.71",
|
|
10
|
+
"@prisma-next/sql-contract": "0.3.0-dev.71",
|
|
11
|
+
"@prisma-next/utils": "0.3.0-dev.71"
|
|
11
12
|
},
|
|
12
13
|
"devDependencies": {
|
|
13
|
-
"
|
|
14
|
-
"tsup": "8.5.1",
|
|
14
|
+
"tsdown": "0.18.4",
|
|
15
15
|
"typescript": "5.9.3",
|
|
16
|
-
"vitest": "4.0.
|
|
17
|
-
"@prisma-next/
|
|
16
|
+
"vitest": "4.0.17",
|
|
17
|
+
"@prisma-next/core-control-plane": "0.3.0-dev.71",
|
|
18
|
+
"@prisma-next/test-utils": "0.0.1",
|
|
19
|
+
"@prisma-next/tsconfig": "0.0.0",
|
|
20
|
+
"@prisma-next/tsdown": "0.0.0"
|
|
18
21
|
},
|
|
19
22
|
"files": [
|
|
20
23
|
"dist",
|
|
21
24
|
"src"
|
|
22
25
|
],
|
|
23
26
|
"exports": {
|
|
24
|
-
".":
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
".": "./dist/index.mjs",
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.mjs",
|
|
31
|
+
"module": "./dist/index.mjs",
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
36
|
+
"directory": "packages/2-sql/3-tooling/emitter"
|
|
28
37
|
},
|
|
29
38
|
"scripts": {
|
|
30
|
-
"build": "
|
|
39
|
+
"build": "tsdown",
|
|
31
40
|
"test": "vitest run --passWithNoTests",
|
|
32
41
|
"test:coverage": "vitest run --coverage --passWithNoTests",
|
|
33
42
|
"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": "
|
|
43
|
+
"lint": "biome check . --error-on-warnings",
|
|
44
|
+
"lint:fix": "biome check --write .",
|
|
45
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
46
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
38
47
|
}
|
|
39
48
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +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';
|
|
17
|
+
import { assertDefined } from '@prisma-next/utils/assertions';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Resolves the typeParams for a column, either from inline typeParams or from typeRef.
|
|
21
|
+
* Returns undefined if no typeParams are available.
|
|
22
|
+
*/
|
|
23
|
+
function resolveColumnTypeParams(
|
|
24
|
+
column: StorageColumn,
|
|
25
|
+
storage: SqlStorage,
|
|
26
|
+
): Record<string, unknown> | undefined {
|
|
27
|
+
// Inline typeParams take precedence
|
|
28
|
+
if (column.typeParams && Object.keys(column.typeParams).length > 0) {
|
|
29
|
+
return column.typeParams;
|
|
30
|
+
}
|
|
31
|
+
// Check typeRef
|
|
32
|
+
if (column.typeRef && storage.types) {
|
|
33
|
+
const typeInstance = storage.types[column.typeRef] as StorageTypeInstance | undefined;
|
|
34
|
+
if (typeInstance?.typeParams) {
|
|
35
|
+
return typeInstance.typeParams;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
9
40
|
|
|
10
41
|
export const sqlTargetFamilyHook = {
|
|
11
42
|
id: 'sql',
|
|
@@ -64,10 +95,8 @@ export const sqlTargetFamilyHook = {
|
|
|
64
95
|
throw new Error(`Model "${modelName}" references non-existent table "${tableName}"`);
|
|
65
96
|
}
|
|
66
97
|
|
|
67
|
-
const table = storage.tables[tableName];
|
|
68
|
-
|
|
69
|
-
throw new Error(`Model "${modelName}" references non-existent table "${tableName}"`);
|
|
70
|
-
}
|
|
98
|
+
const table: StorageTable | undefined = storage.tables[tableName];
|
|
99
|
+
assertDefined(table, `Model "${modelName}" references non-existent table "${tableName}"`);
|
|
71
100
|
|
|
72
101
|
if (!table.primaryKey) {
|
|
73
102
|
throw new Error(`Model "${modelName}" table "${tableName}" is missing a primary key`);
|
|
@@ -168,12 +197,12 @@ export const sqlTargetFamilyHook = {
|
|
|
168
197
|
);
|
|
169
198
|
}
|
|
170
199
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
200
|
+
// Table existence guaranteed by Set.has() check above
|
|
201
|
+
const referencedTable: StorageTable | undefined = storage.tables[fk.references.table];
|
|
202
|
+
assertDefined(
|
|
203
|
+
referencedTable,
|
|
204
|
+
`Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`,
|
|
205
|
+
);
|
|
177
206
|
|
|
178
207
|
const referencedColumnNames = new Set(Object.keys(referencedTable.columns));
|
|
179
208
|
for (const colName of fk.references.columns) {
|
|
@@ -197,45 +226,109 @@ export const sqlTargetFamilyHook = {
|
|
|
197
226
|
ir: ContractIR,
|
|
198
227
|
codecTypeImports: ReadonlyArray<TypesImportSpec>,
|
|
199
228
|
operationTypeImports: ReadonlyArray<TypesImportSpec>,
|
|
229
|
+
hashes: {
|
|
230
|
+
readonly storageHash: string;
|
|
231
|
+
readonly executionHash?: string;
|
|
232
|
+
readonly profileHash: string;
|
|
233
|
+
},
|
|
234
|
+
options?: GenerateContractTypesOptions,
|
|
200
235
|
): string {
|
|
201
|
-
const
|
|
202
|
-
const
|
|
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
|
-
|
|
236
|
+
const parameterizedRenderers = options?.parameterizedRenderers;
|
|
237
|
+
const parameterizedTypeImports = options?.parameterizedTypeImports;
|
|
209
238
|
const storage = ir.storage as SqlStorage;
|
|
210
239
|
const models = ir.models as Record<string, ModelDefinition>;
|
|
211
240
|
|
|
241
|
+
// Collect all type imports from three sources:
|
|
242
|
+
// 1. Codec type imports (from adapters, targets, and extensions)
|
|
243
|
+
// 2. Operation type imports (from adapters, targets, and extensions)
|
|
244
|
+
// 3. Parameterized type imports (for parameterized codec renderers, may contain duplicates)
|
|
245
|
+
const allImports: TypesImportSpec[] = [...codecTypeImports, ...operationTypeImports];
|
|
246
|
+
|
|
247
|
+
if (parameterizedTypeImports) {
|
|
248
|
+
allImports.push(...parameterizedTypeImports);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Deduplicate imports by package+named to avoid duplicate import statements.
|
|
252
|
+
// Strategy: When the same package::named appears multiple times, keep the first
|
|
253
|
+
// occurrence (and its alias); later duplicates with different aliases are silently ignored.
|
|
254
|
+
//
|
|
255
|
+
// Note: uniqueImports must be an array (not a Set) because:
|
|
256
|
+
// - We need to preserve the full TypesImportSpec objects (package, named, alias)
|
|
257
|
+
// - We need to preserve insertion order (first occurrence wins)
|
|
258
|
+
// - seenImportKeys is a Set used only for O(1) duplicate detection
|
|
259
|
+
const seenImportKeys = new Set<string>();
|
|
260
|
+
const uniqueImports: TypesImportSpec[] = [];
|
|
261
|
+
for (const imp of allImports) {
|
|
262
|
+
const key = `${imp.package}::${imp.named}`;
|
|
263
|
+
if (!seenImportKeys.has(key)) {
|
|
264
|
+
seenImportKeys.add(key);
|
|
265
|
+
uniqueImports.push(imp);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Generate import statements, omitting redundant "as Alias" when named === alias
|
|
270
|
+
const importLines = uniqueImports.map((imp) => {
|
|
271
|
+
// Simplify import when named === alias (e.g., `import type { Vector }` instead of `{ Vector as Vector }`)
|
|
272
|
+
const importClause = imp.named === imp.alias ? imp.named : `${imp.named} as ${imp.alias}`;
|
|
273
|
+
return `import type { ${importClause} } from '${imp.package}';`;
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// Only intersect actual codec/operation type maps. Extra type-only imports (e.g. Vector<N>) are
|
|
277
|
+
// included in importLines via codecTypeImports but must not be intersected into CodecTypes.
|
|
278
|
+
const codecTypes = codecTypeImports
|
|
279
|
+
.filter((imp) => imp.named === 'CodecTypes')
|
|
280
|
+
.map((imp) => imp.alias)
|
|
281
|
+
.join(' & ');
|
|
282
|
+
const operationTypes = operationTypeImports
|
|
283
|
+
.filter((imp) => imp.named === 'OperationTypes')
|
|
284
|
+
.map((imp) => imp.alias)
|
|
285
|
+
.join(' & ');
|
|
286
|
+
|
|
212
287
|
const storageType = this.generateStorageType(storage);
|
|
213
|
-
const modelsType = this.generateModelsType(models, storage);
|
|
288
|
+
const modelsType = this.generateModelsType(models, storage, parameterizedRenderers);
|
|
214
289
|
const relationsType = this.generateRelationsType(ir.relations);
|
|
215
290
|
const mappingsType = this.generateMappingsType(models, storage, codecTypes, operationTypes);
|
|
216
291
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
${importLines.join('\n')}
|
|
221
|
-
|
|
222
|
-
import type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';
|
|
223
|
-
|
|
224
|
-
export type CodecTypes = ${codecTypes || 'Record<string, never>'};
|
|
225
|
-
export type LaneCodecTypes = CodecTypes;
|
|
226
|
-
export type OperationTypes = ${operationTypes || 'Record<string, never>'};
|
|
292
|
+
const executionHashType = hashes.executionHash
|
|
293
|
+
? `ExecutionHashBase<'${hashes.executionHash}'>`
|
|
294
|
+
: 'ExecutionHashBase<string>';
|
|
227
295
|
|
|
228
|
-
|
|
296
|
+
return `// ⚠️ GENERATED FILE - DO NOT EDIT
|
|
297
|
+
// This file is automatically generated by 'prisma-next contract emit'.
|
|
298
|
+
// To regenerate, run: prisma-next contract emit
|
|
299
|
+
${importLines.join('\n')}
|
|
300
|
+
|
|
301
|
+
import type { ExecutionHashBase, ProfileHashBase, StorageHashBase } from '@prisma-next/contract/types';
|
|
302
|
+
import type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';
|
|
303
|
+
|
|
304
|
+
export type StorageHash = StorageHashBase<'${hashes.storageHash}'>;
|
|
305
|
+
export type ExecutionHash = ${executionHashType};
|
|
306
|
+
export type ProfileHash = ProfileHashBase<'${hashes.profileHash}'>;
|
|
307
|
+
|
|
308
|
+
export type CodecTypes = ${codecTypes || 'Record<string, never>'};
|
|
309
|
+
export type LaneCodecTypes = CodecTypes;
|
|
310
|
+
export type OperationTypes = ${operationTypes || 'Record<string, never>'};
|
|
311
|
+
type DefaultLiteralValue<CodecId extends string, Encoded> =
|
|
312
|
+
CodecId extends keyof CodecTypes
|
|
313
|
+
? CodecTypes[CodecId] extends { readonly output: infer O }
|
|
314
|
+
? O extends Date | bigint ? O : Encoded
|
|
315
|
+
: Encoded
|
|
316
|
+
: Encoded;
|
|
317
|
+
|
|
318
|
+
export type Contract = SqlContract<
|
|
229
319
|
${storageType},
|
|
230
320
|
${modelsType},
|
|
231
321
|
${relationsType},
|
|
232
|
-
${mappingsType}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
322
|
+
${mappingsType},
|
|
323
|
+
StorageHash,
|
|
324
|
+
ExecutionHash,
|
|
325
|
+
ProfileHash
|
|
326
|
+
>;
|
|
327
|
+
|
|
328
|
+
export type Tables = Contract['storage']['tables'];
|
|
329
|
+
export type Models = Contract['models'];
|
|
330
|
+
export type Relations = Contract['relations'];
|
|
331
|
+
`;
|
|
239
332
|
},
|
|
240
333
|
|
|
241
334
|
generateStorageType(storage: SqlStorage): string {
|
|
@@ -246,8 +339,17 @@ export type Relations = Contract['relations'];
|
|
|
246
339
|
const nullable = col.nullable ? 'true' : 'false';
|
|
247
340
|
const nativeType = `'${col.nativeType}'`;
|
|
248
341
|
const codecId = `'${col.codecId}'`;
|
|
342
|
+
const defaultSpec = col.default
|
|
343
|
+
? col.default.kind === 'literal'
|
|
344
|
+
? `; readonly default: { readonly kind: 'literal'; readonly value: DefaultLiteralValue<${codecId}, ${this.serializeValue(
|
|
345
|
+
col.default.value,
|
|
346
|
+
)}> }`
|
|
347
|
+
: `; readonly default: { readonly kind: 'function'; readonly expression: ${this.serializeValue(
|
|
348
|
+
col.default.expression,
|
|
349
|
+
)} }`
|
|
350
|
+
: '';
|
|
249
351
|
columns.push(
|
|
250
|
-
`readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable} }`,
|
|
352
|
+
`readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable}${defaultSpec} }`,
|
|
251
353
|
);
|
|
252
354
|
}
|
|
253
355
|
|
|
@@ -282,7 +384,7 @@ export type Relations = Contract['relations'];
|
|
|
282
384
|
const cols = fk.columns.map((c: string) => `'${c}'`).join(', ');
|
|
283
385
|
const refCols = fk.references.columns.map((c: string) => `'${c}'`).join(', ');
|
|
284
386
|
const name = fk.name ? `; readonly name: '${fk.name}'` : '';
|
|
285
|
-
return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: '${fk.references.table}'; readonly columns: readonly [${refCols}] }${name} }`;
|
|
387
|
+
return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: '${fk.references.table}'; readonly columns: readonly [${refCols}] }${name}; readonly constraint: ${fk.constraint}; readonly index: ${fk.index} }`;
|
|
286
388
|
})
|
|
287
389
|
.join(', ');
|
|
288
390
|
tableParts.push(`foreignKeys: readonly [${fks}]`);
|
|
@@ -290,17 +392,97 @@ export type Relations = Contract['relations'];
|
|
|
290
392
|
tables.push(`readonly ${tableName}: { ${tableParts.join('; ')} }`);
|
|
291
393
|
}
|
|
292
394
|
|
|
293
|
-
|
|
395
|
+
const typesType = this.generateStorageTypesType(storage.types);
|
|
396
|
+
|
|
397
|
+
return `{ readonly tables: { ${tables.join('; ')} }; readonly types: ${typesType} }`;
|
|
398
|
+
},
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Generates the TypeScript type for storage.types with literal types.
|
|
402
|
+
* This preserves type params as literal values for precise typing.
|
|
403
|
+
*/
|
|
404
|
+
generateStorageTypesType(types: SqlStorage['types']): string {
|
|
405
|
+
if (!types || Object.keys(types).length === 0) {
|
|
406
|
+
return 'Record<string, never>';
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const typeEntries: string[] = [];
|
|
410
|
+
for (const [typeName, typeInstance] of Object.entries(types)) {
|
|
411
|
+
const codecId = `'${typeInstance.codecId}'`;
|
|
412
|
+
const nativeType = `'${typeInstance.nativeType}'`;
|
|
413
|
+
const typeParamsStr = this.serializeTypeParamsLiteral(typeInstance.typeParams);
|
|
414
|
+
typeEntries.push(
|
|
415
|
+
`readonly ${typeName}: { readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return `{ ${typeEntries.join('; ')} }`;
|
|
420
|
+
},
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Serializes a typeParams object to a TypeScript literal type.
|
|
424
|
+
* Converts { length: 1536 } to "{ readonly length: 1536 }".
|
|
425
|
+
*/
|
|
426
|
+
serializeTypeParamsLiteral(params: Record<string, unknown>): string {
|
|
427
|
+
if (!params || Object.keys(params).length === 0) {
|
|
428
|
+
return 'Record<string, never>';
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const entries: string[] = [];
|
|
432
|
+
for (const [key, value] of Object.entries(params)) {
|
|
433
|
+
const serialized = this.serializeValue(value);
|
|
434
|
+
entries.push(`readonly ${key}: ${serialized}`);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return `{ ${entries.join('; ')} }`;
|
|
438
|
+
},
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Serializes a value to a TypeScript literal type expression.
|
|
442
|
+
*/
|
|
443
|
+
serializeValue(value: unknown): string {
|
|
444
|
+
if (value === null) {
|
|
445
|
+
return 'null';
|
|
446
|
+
}
|
|
447
|
+
if (value === undefined) {
|
|
448
|
+
return 'undefined';
|
|
449
|
+
}
|
|
450
|
+
if (typeof value === 'string') {
|
|
451
|
+
// Escape backslashes first, then single quotes
|
|
452
|
+
const escaped = value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
453
|
+
return `'${escaped}'`;
|
|
454
|
+
}
|
|
455
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
456
|
+
return String(value);
|
|
457
|
+
}
|
|
458
|
+
if (typeof value === 'bigint') {
|
|
459
|
+
return `${value}n`;
|
|
460
|
+
}
|
|
461
|
+
if (Array.isArray(value)) {
|
|
462
|
+
const items = value.map((v) => this.serializeValue(v)).join(', ');
|
|
463
|
+
return `readonly [${items}]`;
|
|
464
|
+
}
|
|
465
|
+
if (typeof value === 'object') {
|
|
466
|
+
const entries: string[] = [];
|
|
467
|
+
for (const [k, v] of Object.entries(value)) {
|
|
468
|
+
entries.push(`readonly ${k}: ${this.serializeValue(v)}`);
|
|
469
|
+
}
|
|
470
|
+
return `{ ${entries.join('; ')} }`;
|
|
471
|
+
}
|
|
472
|
+
return 'unknown';
|
|
294
473
|
},
|
|
295
474
|
|
|
296
475
|
generateModelsType(
|
|
297
476
|
models: Record<string, ModelDefinition> | undefined,
|
|
298
477
|
storage: SqlStorage,
|
|
478
|
+
parameterizedRenderers?: Map<string, TypeRenderEntry>,
|
|
299
479
|
): string {
|
|
300
480
|
if (!models) {
|
|
301
481
|
return 'Record<string, never>';
|
|
302
482
|
}
|
|
303
483
|
|
|
484
|
+
const renderCtx: TypeRenderContext = { codecTypesName: 'CodecTypes' };
|
|
485
|
+
|
|
304
486
|
const modelTypes: string[] = [];
|
|
305
487
|
for (const [modelName, model] of Object.entries(models)) {
|
|
306
488
|
const fields: string[] = [];
|
|
@@ -315,12 +497,12 @@ export type Relations = Contract['relations'];
|
|
|
315
497
|
continue;
|
|
316
498
|
}
|
|
317
499
|
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
500
|
+
const jsType = this.generateColumnType(
|
|
501
|
+
column,
|
|
502
|
+
storage,
|
|
503
|
+
parameterizedRenderers,
|
|
504
|
+
renderCtx,
|
|
505
|
+
);
|
|
324
506
|
fields.push(`readonly ${fieldName}: ${jsType}`);
|
|
325
507
|
}
|
|
326
508
|
} else {
|
|
@@ -358,6 +540,26 @@ export type Relations = Contract['relations'];
|
|
|
358
540
|
return `{ ${modelTypes.join('; ')} }`;
|
|
359
541
|
},
|
|
360
542
|
|
|
543
|
+
/**
|
|
544
|
+
* Generates the TypeScript type expression for a column.
|
|
545
|
+
* Uses parameterized renderer if the column has typeParams and a matching renderer exists,
|
|
546
|
+
* otherwise falls back to CodecTypes[codecId]['output'].
|
|
547
|
+
*/
|
|
548
|
+
generateColumnType(
|
|
549
|
+
column: StorageColumn,
|
|
550
|
+
storage: SqlStorage,
|
|
551
|
+
parameterizedRenderers: Map<string, TypeRenderEntry> | undefined,
|
|
552
|
+
renderCtx: TypeRenderContext,
|
|
553
|
+
): string {
|
|
554
|
+
const typeParams = resolveColumnTypeParams(column, storage);
|
|
555
|
+
const nullable = column.nullable ?? false;
|
|
556
|
+
const fallbackType = `CodecTypes['${column.codecId}']['output']`;
|
|
557
|
+
const renderer = typeParams && parameterizedRenderers?.get(column.codecId);
|
|
558
|
+
const baseType = renderer ? renderer.render(typeParams, renderCtx) : fallbackType;
|
|
559
|
+
|
|
560
|
+
return nullable ? `${baseType} | null` : baseType;
|
|
561
|
+
},
|
|
562
|
+
|
|
361
563
|
generateRelationsType(relations: Record<string, unknown> | undefined): string {
|
|
362
564
|
if (!relations || Object.keys(relations).length === 0) {
|
|
363
565
|
return 'Record<string, never>';
|
package/dist/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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';
|
|
4
|
-
export declare const sqlTargetFamilyHook: {
|
|
5
|
-
readonly id: "sql";
|
|
6
|
-
readonly validateTypes: (ir: ContractIR, _ctx: ValidationContext) => void;
|
|
7
|
-
readonly validateStructure: (ir: ContractIR) => void;
|
|
8
|
-
readonly generateContractTypes: (ir: ContractIR, codecTypeImports: ReadonlyArray<TypesImportSpec>, operationTypeImports: ReadonlyArray<TypesImportSpec>) => string;
|
|
9
|
-
readonly generateStorageType: (storage: SqlStorage) => string;
|
|
10
|
-
readonly generateModelsType: (models: Record<string, ModelDefinition> | undefined, storage: SqlStorage) => string;
|
|
11
|
-
readonly generateRelationsType: (relations: Record<string, unknown> | undefined) => string;
|
|
12
|
-
readonly generateMappingsType: (models: Record<string, ModelDefinition> | undefined, storage: SqlStorage, codecTypes: string, operationTypes: string) => string;
|
|
13
|
-
};
|
|
14
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AACtF,OAAO,KAAK,EACV,eAAe,EAEf,UAAU,EAEX,MAAM,iCAAiC,CAAC;AAEzC,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,KACnD,MAAM;4CAyCoB,UAAU,KAAG,MAAM;0CAwDtC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,GAAG,SAAS,WAC1C,UAAU,KAClB,MAAM;gDA8DwB,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"}
|