@prisma-next/sql-contract-emitter 0.8.0 → 0.9.0-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +29 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -11
- package/src/index.ts +74 -7
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":["TypesImportSpec","package","named","alias","t","t","TypesImportSpec","Contract","ContractModel","GenerateContractTypesOptions","ReadonlyArray","queryOperationTypeImports","ValidationContext","codecTypeImports","extensionIds","EmissionSpi","Record","id","generateStorageType","contract","storageHashTypeName","generateModelStorageType","modelName","model","getFamilyImports","getFamilyTypeAliases","options","getTypeMapsExpression","getContractWrapper","contractBaseName","typeMapsName","resolveFieldTypeParams","fieldName","n","r"],"sources":["../../../../1-framework/1-core/framework-components/dist/types-import-spec-BxI5cSQy.d.mts","../../../../1-framework/1-core/framework-components/dist/emission-types-CMv_053d.d.mts","../src/index.ts"],"mappings":";;;;;;;;UAKUA,eAAAA;EAAAA,SACCC,OAAAA;EAAAA,SACAC,KAAAA;EAAAA,SACAC,KAAAA;AAAAA;;;;UCJDM,4BAAAA;EAAAA,SACCE,yBAAAA,GAA4BD,aAAAA,CAAcJ,eAAAA;AAAAA;AAAAA,UAE3CM,iBAAAA;EAAAA,SACCC,gBAAAA,GAAmBH,aAAAA,CAAcJ,eAAAA;EAAAA,SACjCQ,YAAAA,GAAeJ,aAAAA;AAAAA;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":["TypesImportSpec","package","named","alias","t","t","TypesImportSpec","Contract","ContractModel","GenerateContractTypesOptions","ReadonlyArray","queryOperationTypeImports","ValidationContext","codecTypeImports","extensionIds","EmissionSpi","Record","id","generateStorageType","contract","storageHashTypeName","generateModelStorageType","modelName","model","getFamilyImports","getFamilyTypeAliases","options","getTypeMapsExpression","getContractWrapper","contractBaseName","typeMapsName","resolveFieldTypeParams","fieldName","n","r"],"sources":["../../../../1-framework/1-core/framework-components/dist/types-import-spec-BxI5cSQy.d.mts","../../../../1-framework/1-core/framework-components/dist/emission-types-CMv_053d.d.mts","../src/index.ts"],"mappings":";;;;;;;;UAKUA,eAAAA;EAAAA,SACCC,OAAAA;EAAAA,SACAC,KAAAA;EAAAA,SACAC,KAAAA;AAAAA;;;;UCJDM,4BAAAA;EAAAA,SACCE,yBAAAA,GAA4BD,aAAAA,CAAcJ,eAAAA;AAAAA;AAAAA,UAE3CM,iBAAAA;EAAAA,SACCC,gBAAAA,GAAmBH,aAAAA,CAAcJ,eAAAA;EAAAA,SACjCQ,YAAAA,GAAeJ,aAAAA;AAAAA;;;cCmBb,WAAA;EAAA;qCAGa,QAAA,EAAQ,IAAA,EAAQ,iBAAA;EAAA,uCA2BZ,QAAA;EAAA,yCA8IE,QAAA,EAAQ,mBAAA;EAAA,wDAkFK,KAAA,EAAS,aAAA;EAAA,sDAoBhC,SAAA,UACD,KAAA,EACV,aAAA,EAAa,QAAA,EACV,QAAA,KACT,MAAA;EAAA;4CAwC4B,4BAAA;EAAA;0DAwBY,YAAA;AAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { serializeObjectKey, serializeValue } from "@prisma-next/emitter/domain-type-generation";
|
|
2
|
+
import { isPostgresEnumStorageEntry } from "@prisma-next/sql-contract/types";
|
|
2
3
|
import { assertDefined } from "@prisma-next/utils/assertions";
|
|
3
4
|
//#region src/index.ts
|
|
4
5
|
function serializeTypeParamsLiteral(params) {
|
|
@@ -103,7 +104,8 @@ const sqlEmission = {
|
|
|
103
104
|
tables.push(`readonly ${tableName}: { ${tableParts.join("; ")} }`);
|
|
104
105
|
}
|
|
105
106
|
const typesType = generateStorageTypesType(storage.types);
|
|
106
|
-
|
|
107
|
+
const namespacesType = generateStorageNamespacesType(storage.namespaces);
|
|
108
|
+
return `{ readonly tables: { ${tables.join("; ")} }; readonly types: ${typesType}; readonly namespaces: ${namespacesType}; readonly storageHash: ${storageHashTypeName} }`;
|
|
107
109
|
},
|
|
108
110
|
generateModelStorageType(_modelName, model) {
|
|
109
111
|
const sqlModel = model;
|
|
@@ -128,7 +130,12 @@ const sqlEmission = {
|
|
|
128
130
|
if (!table) return void 0;
|
|
129
131
|
const column = table.columns[storageField.column];
|
|
130
132
|
if (!column) return void 0;
|
|
131
|
-
if (column.typeRef)
|
|
133
|
+
if (column.typeRef) {
|
|
134
|
+
const typeInstance = storage.types?.[column.typeRef];
|
|
135
|
+
if (typeInstance === void 0) return void 0;
|
|
136
|
+
if (isPostgresEnumStorageEntry(typeInstance)) return { values: typeInstance.values };
|
|
137
|
+
return typeInstance.typeParams;
|
|
138
|
+
}
|
|
132
139
|
return column.typeParams;
|
|
133
140
|
},
|
|
134
141
|
getFamilyImports() {
|
|
@@ -166,13 +173,29 @@ function generateStorageTypesType(types) {
|
|
|
166
173
|
if (!types || Object.keys(types).length === 0) return "Record<string, never>";
|
|
167
174
|
const typeEntries = [];
|
|
168
175
|
for (const [typeName, typeInstance] of Object.entries(types)) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
176
|
+
if (isPostgresEnumStorageEntry(typeInstance)) {
|
|
177
|
+
const codecId = serializeValue(typeInstance.codecId);
|
|
178
|
+
const nativeType = serializeValue(typeInstance.nativeType);
|
|
179
|
+
const typeParamsStr = serializeTypeParamsLiteral({ values: typeInstance.values });
|
|
180
|
+
typeEntries.push(`readonly ${typeName}: { readonly kind: 'codec-instance'; readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const codecInstanceShape = typeInstance;
|
|
184
|
+
if (typeof codecInstanceShape.codecId !== "string" || typeof codecInstanceShape.nativeType !== "string") throw new Error(`Unknown storage type kind for "${typeName}"; expected a codec-instance triple or a known IR-class kind.`);
|
|
185
|
+
const codecId = serializeValue(codecInstanceShape.codecId);
|
|
186
|
+
const nativeType = serializeValue(codecInstanceShape.nativeType);
|
|
187
|
+
const typeParamsStr = serializeTypeParamsLiteral(codecInstanceShape.typeParams);
|
|
188
|
+
typeEntries.push(`readonly ${typeName}: { readonly kind: 'codec-instance'; readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`);
|
|
173
189
|
}
|
|
174
190
|
return `{ ${typeEntries.join("; ")} }`;
|
|
175
191
|
}
|
|
192
|
+
function generateStorageNamespacesType(namespaces) {
|
|
193
|
+
const entries = Object.entries(namespaces ?? {}).sort(([a], [b]) => a.localeCompare(b));
|
|
194
|
+
if (entries.length === 0) return "Record<string, never>";
|
|
195
|
+
const parts = [];
|
|
196
|
+
for (const [name, ns] of entries) parts.push(`readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)} }`);
|
|
197
|
+
return `{ ${parts.join("; ")} }`;
|
|
198
|
+
}
|
|
176
199
|
//#endregion
|
|
177
200
|
export { sqlEmission };
|
|
178
201
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["col"],"sources":["../src/index.ts"],"sourcesContent":["import type { Contract, ContractModel } from '@prisma-next/contract/types';\nimport { serializeObjectKey, serializeValue } from '@prisma-next/emitter/domain-type-generation';\nimport type {\n GenerateContractTypesOptions,\n ValidationContext,\n} from '@prisma-next/framework-components/emission';\nimport type { SqlModelStorage, SqlStorage, StorageTable } from '@prisma-next/sql-contract/types';\nimport { assertDefined } from '@prisma-next/utils/assertions';\n\nfunction serializeTypeParamsLiteral(params: Record<string, unknown> | undefined): 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 entries.push(`readonly ${serializeObjectKey(key)}: ${serializeValue(value)}`);\n }\n\n return `{ ${entries.join('; ')} }`;\n}\n\nexport const sqlEmission = {\n id: 'sql',\n\n validateTypes(contract: Contract, _ctx: ValidationContext): void {\n const storage = contract.storage as unknown as SqlStorage | undefined;\n if (!storage?.tables) {\n return;\n }\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?.[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(contract: Contract): void {\n if (contract.targetFamily !== 'sql') {\n throw new Error(`Expected targetFamily \"sql\", got \"${contract.targetFamily}\"`);\n }\n\n const storage = contract.storage as unknown as SqlStorage | undefined;\n if (!storage?.tables) {\n throw new Error('SQL contract must have storage.tables');\n }\n\n const models = contract.models as Record<string, ContractModel<SqlModelStorage>>;\n const tableNames = new Set(Object.keys(storage.tables));\n\n if (models) {\n for (const [modelName, model] of Object.entries(models)) {\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 const columnNames = new Set(Object.keys(table.columns));\n const storageFields = model.storage.fields;\n if (!storageFields || Object.keys(storageFields).length === 0) {\n throw new Error(`Model \"${modelName}\" is missing storage.fields`);\n }\n\n for (const [fieldName, field] of Object.entries(storageFields)) {\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 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: 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 generateStorageType(contract: Contract, storageHashTypeName: string): string {\n const storage = contract.storage as unknown as SqlStorage;\n const tables: string[] = [];\n for (const [tableName, table] of Object.entries(storage.tables).sort(([a], [b]) =>\n a.localeCompare(b),\n )) {\n const columns: string[] = [];\n for (const [colName, col] of Object.entries(table.columns)) {\n const nullable = col.nullable ? 'true' : 'false';\n const nativeType = serializeValue(col.nativeType);\n const codecId = serializeValue(col.codecId);\n const defaultSpec = col.default\n ? col.default.kind === 'literal'\n ? `; readonly default: { readonly kind: 'literal'; readonly value: DefaultLiteralValue<${codecId}, ${serializeValue(\n col.default.value,\n )}> }`\n : `; readonly default: { readonly kind: 'function'; readonly expression: ${serializeValue(\n col.default.expression,\n )} }`\n : '';\n const typeParamsSpec =\n col.typeParams && Object.keys(col.typeParams).length > 0\n ? `; readonly typeParams: ${serializeTypeParamsLiteral(col.typeParams)}`\n : '';\n const typeRefSpec = col.typeRef ? `; readonly typeRef: ${serializeValue(col.typeRef)}` : '';\n columns.push(\n `readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable}${defaultSpec}${typeParamsSpec}${typeRefSpec} }`,\n );\n }\n\n const tableParts: string[] = [`columns: { ${columns.join('; ')} }`];\n\n if (table.primaryKey) {\n const pkCols = table.primaryKey.columns.map((c) => serializeValue(c)).join(', ');\n const pkName = table.primaryKey.name\n ? `; readonly name: ${serializeValue(table.primaryKey.name)}`\n : '';\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) => serializeValue(c)).join(', ');\n const name = u.name ? `; readonly name: ${serializeValue(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) => serializeValue(c)).join(', ');\n const name = i.name !== undefined ? `; readonly name: ${serializeValue(i.name)}` : '';\n const indexType =\n i.type !== undefined ? `; readonly type: ${serializeValue(i.type)}` : '';\n const indexOptions =\n i.options !== undefined ? `; readonly options: ${serializeValue(i.options)}` : '';\n return `{ readonly columns: readonly [${cols}]${name}${indexType}${indexOptions} }`;\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) => serializeValue(c)).join(', ');\n const refCols = fk.references.columns.map((c: string) => serializeValue(c)).join(', ');\n const name = fk.name ? `; readonly name: ${serializeValue(fk.name)}` : '';\n return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: ${serializeValue(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 = generateStorageTypesType(storage.types);\n\n return `{ readonly tables: { ${tables.join('; ')} }; readonly types: ${typesType}; readonly storageHash: ${storageHashTypeName} }`;\n },\n\n generateModelStorageType(_modelName: string, model: ContractModel): string {\n const sqlModel = model as ContractModel<SqlModelStorage>;\n const tableName = sqlModel.storage.table;\n const storageFields = sqlModel.storage.fields;\n\n const storageParts = [`readonly table: ${serializeValue(tableName)}`];\n if (Object.keys(storageFields).length > 0) {\n const fieldParts: string[] = [];\n for (const [fieldName, field] of Object.entries(storageFields)) {\n fieldParts.push(\n `readonly ${serializeObjectKey(fieldName)}: { readonly column: ${serializeValue(field.column)} }`,\n );\n }\n storageParts.push(`readonly fields: { ${fieldParts.join('; ')} }`);\n }\n\n return `{ ${storageParts.join('; ')} }`;\n },\n\n resolveFieldTypeParams(\n _modelName: string,\n fieldName: string,\n model: ContractModel,\n contract: Contract,\n ): Record<string, unknown> | undefined {\n const sqlModel = model as ContractModel<SqlModelStorage>;\n const storageField = sqlModel.storage?.fields?.[fieldName];\n if (!storageField) return undefined;\n\n const storage = contract.storage as unknown as SqlStorage | undefined;\n if (!storage) return undefined;\n\n const tableName = sqlModel.storage.table;\n const table: StorageTable | undefined = storage.tables[tableName];\n if (!table) return undefined;\n\n const column = table.columns[storageField.column];\n if (!column) return undefined;\n\n if (column.typeRef) {\n const typeInstance = storage.types?.[column.typeRef];\n return typeInstance?.typeParams;\n }\n return column.typeParams;\n },\n\n getFamilyImports(): string[] {\n return [\n 'import type {',\n ' ContractWithTypeMaps,',\n ' TypeMaps as TypeMapsType,',\n \"} from '@prisma-next/sql-contract/types';\",\n ];\n },\n\n getFamilyTypeAliases(options?: GenerateContractTypesOptions): string {\n const queryOperationTypeImports = options?.queryOperationTypeImports ?? [];\n const queryOperationAliases = queryOperationTypeImports\n .filter((imp) => imp.named === 'QueryOperationTypes')\n .map((imp) => `${imp.alias}<CodecTypes>`);\n const queryOperationTypes =\n queryOperationAliases.length > 0\n ? queryOperationAliases.join(' & ')\n : 'Record<string, never>';\n\n return [\n 'export type LaneCodecTypes = CodecTypes;',\n `export type QueryOperationTypes = ${queryOperationTypes};`,\n 'type DefaultLiteralValue<CodecId extends string, _Encoded> =',\n ' CodecId extends keyof CodecTypes',\n \" ? CodecTypes[CodecId]['output']\",\n ' : _Encoded;',\n ].join('\\n');\n },\n\n getTypeMapsExpression(): string {\n return 'TypeMapsType<CodecTypes, QueryOperationTypes, FieldOutputTypes, FieldInputTypes>';\n },\n\n getContractWrapper(contractBaseName: string, typeMapsName: string): string {\n return [\n `export type Contract = ContractWithTypeMaps<${contractBaseName}, ${typeMapsName}>;`,\n '',\n \"export type Tables = Contract['storage']['tables'];\",\n \"export type Models = Contract['models'];\",\n ].join('\\n');\n },\n} as const;\n\nfunction 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 = serializeValue(typeInstance.codecId);\n const nativeType = serializeValue(typeInstance.nativeType);\n const typeParamsStr = 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"],"mappings":";;;AASA,SAAS,2BAA2B,QAAqD;CACvF,IAAI,CAAC,UAAU,OAAO,KAAK,OAAO,CAAC,WAAW,GAC5C,OAAO;CAGT,MAAM,UAAoB,EAAE;CAC5B,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,EAC/C,QAAQ,KAAK,YAAY,mBAAmB,IAAI,CAAC,IAAI,eAAe,MAAM,GAAG;CAG/E,OAAO,KAAK,QAAQ,KAAK,KAAK,CAAC;;AAGjC,MAAa,cAAc;CACzB,IAAI;CAEJ,cAAc,UAAoB,MAA+B;EAC/D,MAAM,UAAU,SAAS;EACzB,IAAI,CAAC,SAAS,QACZ;EAGF,MAAM,cAAc;EAEpB,KAAK,MAAM,CAAC,WAAW,iBAAiB,OAAO,QAAQ,QAAQ,OAAO,EAAE;GACtE,MAAM,QAAQ;GACd,KAAK,MAAM,CAAC,SAAS,eAAe,OAAO,QAAQ,MAAM,QAAQ,EAAE;IAEjE,MAAM,UAAUA,WAAI;IACpB,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,WAAW,QAAQ,cAAc,UAAU,sBAAsB;IAInF,IAAI,CADU,QAAQ,MAAM,YAClB,GAA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CAAC,QAAQ,OAAO,KAAA;EAEpB,IAAI,OAAO,SAET,QADqB,QAAQ,QAAQ,OAAO,WACvB;EAEvB,OAAO,OAAO;;CAGhB,mBAA6B;EAC3B,OAAO;GACL;GACA;GACA;GACA;GACD;;CAGH,qBAAqB,SAAgD;EAEnE,MAAM,yBAD4B,SAAS,6BAA6B,EAAE,EAEvE,QAAQ,QAAQ,IAAI,UAAU,sBAAsB,CACpD,KAAK,QAAQ,GAAG,IAAI,MAAM,cAAc;EAM3C,OAAO;GACL;GACA,qCANA,sBAAsB,SAAS,IAC3B,sBAAsB,KAAK,MAAM,GACjC,wBAIqD;GACzD;GACA;GACA;GACA;GACD,CAAC,KAAK,KAAK;;CAGd,wBAAgC;EAC9B,OAAO;;CAGT,mBAAmB,kBAA0B,cAA8B;EACzE,OAAO;GACL,+CAA+C,iBAAiB,IAAI,aAAa;GACjF;GACA;GACA;GACD,CAAC,KAAK,KAAK;;CAEf;AAED,SAAS,yBAAyB,OAAoC;CACpE,IAAI,CAAC,SAAS,OAAO,KAAK,MAAM,CAAC,WAAW,GAC1C,OAAO;CAGT,MAAM,cAAwB,EAAE;CAChC,KAAK,MAAM,CAAC,UAAU,iBAAiB,OAAO,QAAQ,MAAM,EAAE;EAC5D,MAAM,UAAU,eAAe,aAAa,QAAQ;EACpD,MAAM,aAAa,eAAe,aAAa,WAAW;EAC1D,MAAM,gBAAgB,2BAA2B,aAAa,WAAW;EACzE,YAAY,KACV,YAAY,SAAS,wBAAwB,QAAQ,yBAAyB,WAAW,yBAAyB,cAAc,IACjI;;CAGH,OAAO,KAAK,YAAY,KAAK,KAAK,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["col","codecShape"],"sources":["../src/index.ts"],"sourcesContent":["import type { Contract, ContractModel } from '@prisma-next/contract/types';\nimport { serializeObjectKey, serializeValue } from '@prisma-next/emitter/domain-type-generation';\nimport type {\n GenerateContractTypesOptions,\n ValidationContext,\n} from '@prisma-next/framework-components/emission';\nimport {\n isPostgresEnumStorageEntry,\n type SqlModelStorage,\n type SqlStorage,\n type StorageTable,\n type StorageTypeInstance,\n} from '@prisma-next/sql-contract/types';\nimport { assertDefined } from '@prisma-next/utils/assertions';\n\nfunction serializeTypeParamsLiteral(params: Record<string, unknown> | undefined): 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 entries.push(`readonly ${serializeObjectKey(key)}: ${serializeValue(value)}`);\n }\n\n return `{ ${entries.join('; ')} }`;\n}\n\nexport const sqlEmission = {\n id: 'sql',\n\n validateTypes(contract: Contract, _ctx: ValidationContext): void {\n const storage = contract.storage as unknown as SqlStorage | undefined;\n if (!storage?.tables) {\n return;\n }\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?.[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(contract: Contract): void {\n if (contract.targetFamily !== 'sql') {\n throw new Error(`Expected targetFamily \"sql\", got \"${contract.targetFamily}\"`);\n }\n\n const storage = contract.storage as unknown as SqlStorage | undefined;\n if (!storage?.tables) {\n throw new Error('SQL contract must have storage.tables');\n }\n\n const models = contract.models as Record<string, ContractModel<SqlModelStorage>>;\n const tableNames = new Set(Object.keys(storage.tables));\n\n if (models) {\n for (const [modelName, model] of Object.entries(models)) {\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 const columnNames = new Set(Object.keys(table.columns));\n const storageFields = model.storage.fields;\n if (!storageFields || Object.keys(storageFields).length === 0) {\n throw new Error(`Model \"${modelName}\" is missing storage.fields`);\n }\n\n for (const [fieldName, field] of Object.entries(storageFields)) {\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 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: 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 generateStorageType(contract: Contract, storageHashTypeName: string): string {\n const storage = contract.storage as unknown as SqlStorage;\n const tables: string[] = [];\n for (const [tableName, table] of Object.entries(storage.tables).sort(([a], [b]) =>\n a.localeCompare(b),\n )) {\n const columns: string[] = [];\n for (const [colName, col] of Object.entries(table.columns)) {\n const nullable = col.nullable ? 'true' : 'false';\n const nativeType = serializeValue(col.nativeType);\n const codecId = serializeValue(col.codecId);\n const defaultSpec = col.default\n ? col.default.kind === 'literal'\n ? `; readonly default: { readonly kind: 'literal'; readonly value: DefaultLiteralValue<${codecId}, ${serializeValue(\n col.default.value,\n )}> }`\n : `; readonly default: { readonly kind: 'function'; readonly expression: ${serializeValue(\n col.default.expression,\n )} }`\n : '';\n const typeParamsSpec =\n col.typeParams && Object.keys(col.typeParams).length > 0\n ? `; readonly typeParams: ${serializeTypeParamsLiteral(col.typeParams)}`\n : '';\n const typeRefSpec = col.typeRef ? `; readonly typeRef: ${serializeValue(col.typeRef)}` : '';\n columns.push(\n `readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable}${defaultSpec}${typeParamsSpec}${typeRefSpec} }`,\n );\n }\n\n const tableParts: string[] = [`columns: { ${columns.join('; ')} }`];\n\n if (table.primaryKey) {\n const pkCols = table.primaryKey.columns.map((c) => serializeValue(c)).join(', ');\n const pkName = table.primaryKey.name\n ? `; readonly name: ${serializeValue(table.primaryKey.name)}`\n : '';\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) => serializeValue(c)).join(', ');\n const name = u.name ? `; readonly name: ${serializeValue(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) => serializeValue(c)).join(', ');\n const name = i.name !== undefined ? `; readonly name: ${serializeValue(i.name)}` : '';\n const indexType =\n i.type !== undefined ? `; readonly type: ${serializeValue(i.type)}` : '';\n const indexOptions =\n i.options !== undefined ? `; readonly options: ${serializeValue(i.options)}` : '';\n return `{ readonly columns: readonly [${cols}]${name}${indexType}${indexOptions} }`;\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) => serializeValue(c)).join(', ');\n const refCols = fk.references.columns.map((c: string) => serializeValue(c)).join(', ');\n const name = fk.name ? `; readonly name: ${serializeValue(fk.name)}` : '';\n return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: ${serializeValue(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 = generateStorageTypesType(storage.types);\n\n const namespacesType = generateStorageNamespacesType(storage.namespaces);\n\n return `{ readonly tables: { ${tables.join('; ')} }; readonly types: ${typesType}; readonly namespaces: ${namespacesType}; readonly storageHash: ${storageHashTypeName} }`;\n },\n\n generateModelStorageType(_modelName: string, model: ContractModel): string {\n const sqlModel = model as ContractModel<SqlModelStorage>;\n const tableName = sqlModel.storage.table;\n const storageFields = sqlModel.storage.fields;\n\n const storageParts = [`readonly table: ${serializeValue(tableName)}`];\n if (Object.keys(storageFields).length > 0) {\n const fieldParts: string[] = [];\n for (const [fieldName, field] of Object.entries(storageFields)) {\n fieldParts.push(\n `readonly ${serializeObjectKey(fieldName)}: { readonly column: ${serializeValue(field.column)} }`,\n );\n }\n storageParts.push(`readonly fields: { ${fieldParts.join('; ')} }`);\n }\n\n return `{ ${storageParts.join('; ')} }`;\n },\n\n resolveFieldTypeParams(\n _modelName: string,\n fieldName: string,\n model: ContractModel,\n contract: Contract,\n ): Record<string, unknown> | undefined {\n const sqlModel = model as ContractModel<SqlModelStorage>;\n const storageField = sqlModel.storage?.fields?.[fieldName];\n if (!storageField) return undefined;\n\n const storage = contract.storage as unknown as SqlStorage | undefined;\n if (!storage) return undefined;\n\n const tableName = sqlModel.storage.table;\n const table: StorageTable | undefined = storage.tables[tableName];\n if (!table) return undefined;\n\n const column = table.columns[storageField.column];\n if (!column) return undefined;\n\n if (column.typeRef) {\n const typeInstance = storage.types?.[column.typeRef];\n if (typeInstance === undefined) return undefined;\n if (isPostgresEnumStorageEntry(typeInstance)) {\n return { values: typeInstance.values };\n }\n // Fall back to structural codec-triple access when the literal\n // bypasses the runtime normaliser (e.g. test fixtures or\n // hand-written descriptor inputs that omit the\n // `kind: 'codec-instance'` discriminator).\n const codecShape = typeInstance as Partial<StorageTypeInstance>;\n return codecShape.typeParams;\n }\n return column.typeParams;\n },\n\n getFamilyImports(): string[] {\n return [\n 'import type {',\n ' ContractWithTypeMaps,',\n ' TypeMaps as TypeMapsType,',\n \"} from '@prisma-next/sql-contract/types';\",\n ];\n },\n\n getFamilyTypeAliases(options?: GenerateContractTypesOptions): string {\n const queryOperationTypeImports = options?.queryOperationTypeImports ?? [];\n const queryOperationAliases = queryOperationTypeImports\n .filter((imp) => imp.named === 'QueryOperationTypes')\n .map((imp) => `${imp.alias}<CodecTypes>`);\n const queryOperationTypes =\n queryOperationAliases.length > 0\n ? queryOperationAliases.join(' & ')\n : 'Record<string, never>';\n\n return [\n 'export type LaneCodecTypes = CodecTypes;',\n `export type QueryOperationTypes = ${queryOperationTypes};`,\n 'type DefaultLiteralValue<CodecId extends string, _Encoded> =',\n ' CodecId extends keyof CodecTypes',\n \" ? CodecTypes[CodecId]['output']\",\n ' : _Encoded;',\n ].join('\\n');\n },\n\n getTypeMapsExpression(): string {\n return 'TypeMapsType<CodecTypes, QueryOperationTypes, FieldOutputTypes, FieldInputTypes>';\n },\n\n getContractWrapper(contractBaseName: string, typeMapsName: string): string {\n return [\n `export type Contract = ContractWithTypeMaps<${contractBaseName}, ${typeMapsName}>;`,\n '',\n \"export type Tables = Contract['storage']['tables'];\",\n \"export type Models = Contract['models'];\",\n ].join('\\n');\n },\n} as const;\n\nfunction 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 if (isPostgresEnumStorageEntry(typeInstance)) {\n const codecId = serializeValue(\n // `codecBinding.codecId` lives on the live IR-class instance;\n // raw JSON envelopes carry `codecId` as an enumerable own\n // property. Read the structural-shape field so the emitter\n // works against both runtime forms.\n typeInstance.codecId,\n );\n const nativeType = serializeValue(typeInstance.nativeType);\n const typeParamsStr = serializeTypeParamsLiteral({\n values: typeInstance.values as unknown as readonly unknown[],\n });\n // Emit the resolved codec view (kind: 'codec-instance') so the\n // emitted .d.ts shape stays uniform across slot variants and\n // satisfies the polymorphic slot's structural alphabet. The\n // persisted JSON envelope still carries the IR's narrower kind\n // discriminator; the d.ts is the type-level codec-resolved view.\n typeEntries.push(\n `readonly ${typeName}: { readonly kind: 'codec-instance'; readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,\n );\n continue;\n }\n // The slot is polymorphic at the framework level; codec-instance\n // entries are the only non-IR-class kind today. The runtime\n // `SqlStorage` constructor stamps `kind: 'codec-instance'` on\n // plain codec triples; the emitter is forgiving about literal\n // inputs that bypass the constructor (test fixtures, hand-written\n // descriptors) and treats anything with the codec-triple shape as\n // a codec-instance.\n const codecInstanceShape = typeInstance as Partial<StorageTypeInstance>;\n if (\n typeof codecInstanceShape.codecId !== 'string' ||\n typeof codecInstanceShape.nativeType !== 'string'\n ) {\n throw new Error(\n `Unknown storage type kind for \"${typeName}\"; expected a codec-instance triple or a known IR-class kind.`,\n );\n }\n const codecId = serializeValue(codecInstanceShape.codecId);\n const nativeType = serializeValue(codecInstanceShape.nativeType);\n const typeParamsStr = serializeTypeParamsLiteral(codecInstanceShape.typeParams);\n typeEntries.push(\n `readonly ${typeName}: { readonly kind: 'codec-instance'; readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,\n );\n }\n\n return `{ ${typeEntries.join('; ')} }`;\n}\n\nfunction generateStorageNamespacesType(namespaces: SqlStorage['namespaces']): string {\n const entries = Object.entries(namespaces ?? {}).sort(([a], [b]) => a.localeCompare(b));\n if (entries.length === 0) {\n return 'Record<string, never>';\n }\n const parts: string[] = [];\n for (const [name, ns] of entries) {\n parts.push(`readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)} }`);\n }\n return `{ ${parts.join('; ')} }`;\n}\n"],"mappings":";;;;AAeA,SAAS,2BAA2B,QAAqD;CACvF,IAAI,CAAC,UAAU,OAAO,KAAK,OAAO,CAAC,WAAW,GAC5C,OAAO;CAGT,MAAM,UAAoB,EAAE;CAC5B,KAAK,MAAM,CAAC,KAAK,UAAU,OA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,CAAC,IAAI;CAE/F,OAAO,KAAK,MAAM,KAAK,KAAK,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract-emitter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0-dev.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"description": "SQL emitter hook for Prisma Next",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@prisma-next/contract": "0.
|
|
10
|
-
"@prisma-next/emitter": "0.
|
|
11
|
-
"@prisma-next/sql-contract": "0.
|
|
12
|
-
"@prisma-next/utils": "0.
|
|
9
|
+
"@prisma-next/contract": "0.9.0-dev.2",
|
|
10
|
+
"@prisma-next/emitter": "0.9.0-dev.2",
|
|
11
|
+
"@prisma-next/sql-contract": "0.9.0-dev.2",
|
|
12
|
+
"@prisma-next/utils": "0.9.0-dev.2"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@prisma-next/framework-components": "0.
|
|
16
|
-
"@prisma-next/test-utils": "0.
|
|
17
|
-
"@prisma-next/tsconfig": "0.
|
|
18
|
-
"@prisma-next/tsdown": "0.
|
|
15
|
+
"@prisma-next/framework-components": "0.9.0-dev.2",
|
|
16
|
+
"@prisma-next/test-utils": "0.9.0-dev.2",
|
|
17
|
+
"@prisma-next/tsconfig": "0.9.0-dev.2",
|
|
18
|
+
"@prisma-next/tsdown": "0.9.0-dev.2",
|
|
19
19
|
"tsdown": "0.22.0",
|
|
20
20
|
"typescript": "5.9.3",
|
|
21
21
|
"vitest": "4.1.5"
|
|
@@ -28,8 +28,6 @@
|
|
|
28
28
|
".": "./dist/index.mjs",
|
|
29
29
|
"./package.json": "./package.json"
|
|
30
30
|
},
|
|
31
|
-
"main": "./dist/index.mjs",
|
|
32
|
-
"module": "./dist/index.mjs",
|
|
33
31
|
"types": "./dist/index.d.mts",
|
|
34
32
|
"repository": {
|
|
35
33
|
"type": "git",
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,13 @@ import type {
|
|
|
4
4
|
GenerateContractTypesOptions,
|
|
5
5
|
ValidationContext,
|
|
6
6
|
} from '@prisma-next/framework-components/emission';
|
|
7
|
-
import
|
|
7
|
+
import {
|
|
8
|
+
isPostgresEnumStorageEntry,
|
|
9
|
+
type SqlModelStorage,
|
|
10
|
+
type SqlStorage,
|
|
11
|
+
type StorageTable,
|
|
12
|
+
type StorageTypeInstance,
|
|
13
|
+
} from '@prisma-next/sql-contract/types';
|
|
8
14
|
import { assertDefined } from '@prisma-next/utils/assertions';
|
|
9
15
|
|
|
10
16
|
function serializeTypeParamsLiteral(params: Record<string, unknown> | undefined): string {
|
|
@@ -269,7 +275,9 @@ export const sqlEmission = {
|
|
|
269
275
|
|
|
270
276
|
const typesType = generateStorageTypesType(storage.types);
|
|
271
277
|
|
|
272
|
-
|
|
278
|
+
const namespacesType = generateStorageNamespacesType(storage.namespaces);
|
|
279
|
+
|
|
280
|
+
return `{ readonly tables: { ${tables.join('; ')} }; readonly types: ${typesType}; readonly namespaces: ${namespacesType}; readonly storageHash: ${storageHashTypeName} }`;
|
|
273
281
|
},
|
|
274
282
|
|
|
275
283
|
generateModelStorageType(_modelName: string, model: ContractModel): string {
|
|
@@ -313,7 +321,16 @@ export const sqlEmission = {
|
|
|
313
321
|
|
|
314
322
|
if (column.typeRef) {
|
|
315
323
|
const typeInstance = storage.types?.[column.typeRef];
|
|
316
|
-
return
|
|
324
|
+
if (typeInstance === undefined) return undefined;
|
|
325
|
+
if (isPostgresEnumStorageEntry(typeInstance)) {
|
|
326
|
+
return { values: typeInstance.values };
|
|
327
|
+
}
|
|
328
|
+
// Fall back to structural codec-triple access when the literal
|
|
329
|
+
// bypasses the runtime normaliser (e.g. test fixtures or
|
|
330
|
+
// hand-written descriptor inputs that omit the
|
|
331
|
+
// `kind: 'codec-instance'` discriminator).
|
|
332
|
+
const codecShape = typeInstance as Partial<StorageTypeInstance>;
|
|
333
|
+
return codecShape.typeParams;
|
|
317
334
|
}
|
|
318
335
|
return column.typeParams;
|
|
319
336
|
},
|
|
@@ -368,13 +385,63 @@ function generateStorageTypesType(types: SqlStorage['types']): string {
|
|
|
368
385
|
|
|
369
386
|
const typeEntries: string[] = [];
|
|
370
387
|
for (const [typeName, typeInstance] of Object.entries(types)) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
388
|
+
if (isPostgresEnumStorageEntry(typeInstance)) {
|
|
389
|
+
const codecId = serializeValue(
|
|
390
|
+
// `codecBinding.codecId` lives on the live IR-class instance;
|
|
391
|
+
// raw JSON envelopes carry `codecId` as an enumerable own
|
|
392
|
+
// property. Read the structural-shape field so the emitter
|
|
393
|
+
// works against both runtime forms.
|
|
394
|
+
typeInstance.codecId,
|
|
395
|
+
);
|
|
396
|
+
const nativeType = serializeValue(typeInstance.nativeType);
|
|
397
|
+
const typeParamsStr = serializeTypeParamsLiteral({
|
|
398
|
+
values: typeInstance.values as unknown as readonly unknown[],
|
|
399
|
+
});
|
|
400
|
+
// Emit the resolved codec view (kind: 'codec-instance') so the
|
|
401
|
+
// emitted .d.ts shape stays uniform across slot variants and
|
|
402
|
+
// satisfies the polymorphic slot's structural alphabet. The
|
|
403
|
+
// persisted JSON envelope still carries the IR's narrower kind
|
|
404
|
+
// discriminator; the d.ts is the type-level codec-resolved view.
|
|
405
|
+
typeEntries.push(
|
|
406
|
+
`readonly ${typeName}: { readonly kind: 'codec-instance'; readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,
|
|
407
|
+
);
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
// The slot is polymorphic at the framework level; codec-instance
|
|
411
|
+
// entries are the only non-IR-class kind today. The runtime
|
|
412
|
+
// `SqlStorage` constructor stamps `kind: 'codec-instance'` on
|
|
413
|
+
// plain codec triples; the emitter is forgiving about literal
|
|
414
|
+
// inputs that bypass the constructor (test fixtures, hand-written
|
|
415
|
+
// descriptors) and treats anything with the codec-triple shape as
|
|
416
|
+
// a codec-instance.
|
|
417
|
+
const codecInstanceShape = typeInstance as Partial<StorageTypeInstance>;
|
|
418
|
+
if (
|
|
419
|
+
typeof codecInstanceShape.codecId !== 'string' ||
|
|
420
|
+
typeof codecInstanceShape.nativeType !== 'string'
|
|
421
|
+
) {
|
|
422
|
+
throw new Error(
|
|
423
|
+
`Unknown storage type kind for "${typeName}"; expected a codec-instance triple or a known IR-class kind.`,
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
const codecId = serializeValue(codecInstanceShape.codecId);
|
|
427
|
+
const nativeType = serializeValue(codecInstanceShape.nativeType);
|
|
428
|
+
const typeParamsStr = serializeTypeParamsLiteral(codecInstanceShape.typeParams);
|
|
374
429
|
typeEntries.push(
|
|
375
|
-
`readonly ${typeName}: { readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,
|
|
430
|
+
`readonly ${typeName}: { readonly kind: 'codec-instance'; readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`,
|
|
376
431
|
);
|
|
377
432
|
}
|
|
378
433
|
|
|
379
434
|
return `{ ${typeEntries.join('; ')} }`;
|
|
380
435
|
}
|
|
436
|
+
|
|
437
|
+
function generateStorageNamespacesType(namespaces: SqlStorage['namespaces']): string {
|
|
438
|
+
const entries = Object.entries(namespaces ?? {}).sort(([a], [b]) => a.localeCompare(b));
|
|
439
|
+
if (entries.length === 0) {
|
|
440
|
+
return 'Record<string, never>';
|
|
441
|
+
}
|
|
442
|
+
const parts: string[] = [];
|
|
443
|
+
for (const [name, ns] of entries) {
|
|
444
|
+
parts.push(`readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)} }`);
|
|
445
|
+
}
|
|
446
|
+
return `{ ${parts.join('; ')} }`;
|
|
447
|
+
}
|