🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@prisma-next/sql-contract-emitter

Package Overview
Dependencies
Maintainers
3
Versions
881
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prisma-next/sql-contract-emitter - npm Package Compare versions

Comparing version
0.13.0
to
0.14.0-dev.1
+3
-3
dist/index.d.mts

@@ -1,2 +0,2 @@

import { Contract, ContractModel } from "@prisma-next/contract/types";
import { Contract, ContractModelBase } from "@prisma-next/contract/types";
import { GenerateContractTypesOptions, ValidationContext } from "@prisma-next/framework-components/emission";

@@ -10,4 +10,4 @@

readonly generateStorageType: (contract: Contract, storageHashTypeName: string) => string;
readonly generateModelStorageType: (_modelName: string, model: ContractModel) => string;
readonly resolveFieldTypeParams: (_modelName: string, fieldName: string, model: ContractModel, contract: Contract) => Record<string, unknown> | undefined;
readonly generateModelStorageType: (_modelName: string, model: ContractModelBase) => string;
readonly resolveFieldTypeParams: (_modelName: string, fieldName: string, model: ContractModelBase, contract: Contract) => Record<string, unknown> | undefined;
readonly getFamilyImports: () => string[];

@@ -14,0 +14,0 @@ readonly getFamilyTypeAliases: (options?: GenerateContractTypesOptions) => string;

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;cAkCa,WAAA;EAAA;qCAGa,QAAA,EAAQ,IAAA,EAAQ,iBAAA;EAAA,uCA6BZ,QAAA;EAAA,yCA2KE,QAAA,EAAQ,mBAAA;EAAA,wDAQK,KAAA,EAAS,aAAA;EAAA,sDAuBhC,SAAA,UACD,KAAA,EACV,aAAA,EAAa,QAAA,EACV,QAAA,KACT,MAAA;EAAA;4CAkD4B,4BAAA;EAAA;0DAwBY,YAAA;AAAA"}
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;cAoCa,WAAA;EAAA;qCAGa,QAAA,EAAQ,IAAA,EAAQ,iBAAA;EAAA,uCA4BZ,QAAA;EAAA,yCA8KE,QAAA,EAAQ,mBAAA;EAAA,wDAQK,KAAA,EAAS,iBAAA;EAAA,sDAuBhC,SAAA,UACD,KAAA,EACV,iBAAA,EAAiB,QAAA,EACd,QAAA,KACT,MAAA;EAAA;4CAwC4B,4BAAA;EAAA;0DAwBY,YAAA;AAAA"}
import { serializeNamespaceId, serializeObjectKey, serializeValue } from "@prisma-next/emitter/domain-type-generation";
import { UNBOUND_NAMESPACE_ID } from "@prisma-next/framework-components/ir";
import { isPostgresEnumStorageEntry } from "@prisma-next/sql-contract/types";
import { blindCast } from "@prisma-next/utils/casts";
import { UNBOUND_NAMESPACE_ID, entityAt } from "@prisma-next/framework-components/ir";
//#region src/index.ts

@@ -18,9 +16,6 @@ function serializeTypeParamsLiteral(params) {

const typeIdRegex = /^([^/]+)\/([^@]+)@(\d+)$/;
for (const ns of Object.values(storage.namespaces)) for (const [tableName, tableUnknown] of Object.entries(ns.entries.table)) {
const table = tableUnknown;
for (const [colName, colUnknown] of Object.entries(table.columns)) {
const codecId = colUnknown.codecId;
if (!codecId) throw new Error(`Column "${colName}" in table "${tableName}" is missing codecId`);
if (!codecId.match(typeIdRegex)?.[1]) throw new Error(`Column "${colName}" in table "${tableName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`);
}
for (const ns of Object.values(storage.namespaces)) for (const [tableName, table] of Object.entries(ns.entries.table ?? {})) for (const [colName, colUnknown] of Object.entries(table.columns)) {
const codecId = colUnknown.codecId;
if (!codecId) throw new Error(`Column "${colName}" in table "${tableName}" is missing codecId`);
if (!codecId.match(typeIdRegex)?.[1]) throw new Error(`Column "${colName}" in table "${tableName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`);
}

@@ -33,3 +28,3 @@ },

const tableNamesSeenAcrossNamespaces = /* @__PURE__ */ new Map();
for (const [nsId, ns] of Object.entries(storage.namespaces)) for (const tableName of Object.keys(ns.entries.table)) {
for (const [nsId, ns] of Object.entries(storage.namespaces)) for (const tableName of Object.keys(ns.entries.table ?? {})) {
const existingNs = tableNamesSeenAcrossNamespaces.get(tableName);

@@ -40,3 +35,3 @@ if (existingNs !== void 0 && existingNs !== nsId) throw new Error(`Duplicate table name "${tableName}" in namespaces "${existingNs}" and "${nsId}"`);

const tableNames = /* @__PURE__ */ new Set();
for (const ns of Object.values(storage.namespaces)) for (const t of Object.keys(ns.entries.table)) tableNames.add(t);
for (const ns of Object.values(storage.namespaces)) for (const t of Object.keys(ns.entries.table ?? {})) tableNames.add(t);
for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {

@@ -50,3 +45,7 @@ const models = domainNs.models;

const tableName = model.storage.table;
const table = storage.namespaces[namespaceId]?.entries.table[tableName];
const table = entityAt(storage, {
namespaceId,
entityKind: "table",
entityName: tableName
});
if (!table) throw new Error(`Model "${qualifiedName}" references non-existent table "${namespaceId}.${tableName}"`);

@@ -63,4 +62,3 @@ const columnNames = new Set(Object.keys(table.columns));

}
for (const ns of Object.values(storage.namespaces)) for (const [tableName, tableUnknown] of Object.entries(ns.entries.table)) {
const table = tableUnknown;
for (const ns of Object.values(storage.namespaces)) for (const [tableName, table] of Object.entries(ns.entries.table ?? {})) {
const columnNames = new Set(Object.keys(table.columns));

@@ -77,3 +75,7 @@ if (!Array.isArray(table.uniques)) throw new Error(`Table "${tableName}" is missing required field "uniques" (must be an array)`);

for (const colName of fk.source.columns) if (!columnNames.has(colName)) throw new Error(`Table "${tableName}" foreignKey references non-existent column "${colName}"`);
const referencedTable = storage.namespaces[fk.target.namespaceId]?.entries.table[fk.target.tableName];
const referencedTable = entityAt(storage, {
namespaceId: fk.target.namespaceId,
entityKind: "table",
entityName: fk.target.tableName
});
if (!referencedTable) throw new Error(`Table "${tableName}" foreignKey references non-existent table "${fk.target.tableName}" in namespace "${fk.target.namespaceId}"`);

@@ -113,3 +115,7 @@ const referencedColumnNames = new Set(Object.keys(referencedTable.columns));

if (!storageNamespaceId) return void 0;
const table = storage.namespaces[storageNamespaceId]?.entries.table[tableName];
const table = entityAt(storage, {
namespaceId: storageNamespaceId,
entityKind: "table",
entityName: tableName
});
if (!table) return void 0;

@@ -119,6 +125,4 @@ const column = table.columns[storageField.column];

if (column.typeRef) {
const ns = storage.namespaces[storageNamespaceId];
const typeInstance = (ns !== void 0 ? blindCast(ns.entries).type : void 0)?.[column.typeRef] ?? storage.types?.[column.typeRef];
const typeInstance = storage.types?.[column.typeRef];
if (typeInstance === void 0) return void 0;
if (isPostgresEnumStorageEntry(typeInstance)) return { values: typeInstance.values };
return typeInstance.typeParams;

@@ -162,3 +166,2 @@ }

for (const [typeName, typeInstance] of Object.entries(types)) {
if (isPostgresEnumStorageEntry(typeInstance)) throw new Error(`Document-scoped storage.types entry "${typeName}" is a postgres-enum; enums belong under storage.namespaces[namespaceId].entries.type`);
const codecInstanceShape = typeInstance;

@@ -173,17 +176,10 @@ if (typeof codecInstanceShape.codecId !== "string" || typeof codecInstanceShape.nativeType !== "string") throw new Error(`Unknown storage type kind for "${typeName}" in document-scoped storage.types; expected a codec-instance triple.`);

}
function generatePostgresNamespaceTypesType(types) {
if (Object.keys(types).length === 0) return "Record<string, never>";
const typeEntries = [];
for (const [typeName, typeInstance] of Object.entries(types)) {
if (isPostgresEnumStorageEntry(typeInstance)) {
const codecId = serializeValue(typeInstance.codecId);
const nativeType = serializeValue(typeInstance.nativeType);
const name = serializeValue(typeInstance.name);
const valuesLiteral = typeInstance.values.map((v) => serializeValue(v)).join(", ");
typeEntries.push(`readonly ${serializeObjectKey(typeName)}: { readonly kind: 'postgres-enum'; readonly name: ${name}; readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly values: readonly [${valuesLiteral}] }`);
continue;
}
throw new Error(`Unknown namespace storage type kind for "${typeName}"; expected postgres-enum in namespace.entries.type.`);
function generateNamespaceValueSetType(valueSet) {
if (Object.keys(valueSet).length === 0) return "Record<string, never>";
const entries = [];
for (const [name, vs] of Object.entries(valueSet)) {
const valuesLiteral = vs.values.map((v) => serializeValue(v)).join(", ");
entries.push(`readonly ${serializeObjectKey(name)}: { readonly kind: 'valueSet'; readonly values: readonly [${valuesLiteral}] }`);
}
return `{ ${typeEntries.join("; ")} }`;
return `{ ${entries.join("; ")} }`;
}

@@ -244,5 +240,5 @@ const SQL_NAMESPACE_KIND_FALLBACK = "sql-namespace";

const tablesType = generateTablesMapType(ns.entries.table ?? {});
const typeSlot = ns.kind === "schema" ? blindCast(blindCast(ns.entries).type) : void 0;
const valueSetSlot = ns.entries.valueSet;
const entriesParts = [`readonly table: ${tablesType}`];
if (typeSlot !== void 0) entriesParts.push(`readonly type: ${generatePostgresNamespaceTypesType(typeSlot)}`);
if (valueSetSlot !== void 0 && Object.keys(valueSetSlot).length > 0) entriesParts.push(`readonly valueSet: ${generateNamespaceValueSetType(valueSetSlot)}`);
const entriesType = `{ ${entriesParts.join("; ")} }`;

@@ -249,0 +245,0 @@ parts.push(`readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)}${kindSuffix}; readonly entries: ${entriesType} }`);

@@ -1,1 +0,1 @@

{"version":3,"file":"index.mjs","names":["col","codecShape"],"sources":["../src/index.ts"],"sourcesContent":["import type { Contract, ContractModel } from '@prisma-next/contract/types';\nimport {\n serializeNamespaceId,\n serializeObjectKey,\n serializeValue,\n} from '@prisma-next/emitter/domain-type-generation';\nimport type {\n GenerateContractTypesOptions,\n ValidationContext,\n} from '@prisma-next/framework-components/emission';\nimport { type Namespace, UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';\nimport {\n isPostgresEnumStorageEntry,\n type PostgresEnumStorageEntry,\n type SqlModelStorage,\n type SqlStorage,\n type StorageTable,\n type StorageTypeInstance,\n} from '@prisma-next/sql-contract/types';\nimport { blindCast } from '@prisma-next/utils/casts';\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?.namespaces) {\n return;\n }\n\n const typeIdRegex = /^([^/]+)\\/([^@]+)@(\\d+)$/;\n\n for (const ns of Object.values(storage.namespaces)) {\n for (const [tableName, tableUnknown] of Object.entries(ns.entries.table)) {\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\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?.namespaces) {\n throw new Error('SQL contract must have storage.namespaces');\n }\n\n const tableNamesSeenAcrossNamespaces = new Map<string, string>();\n for (const [nsId, ns] of Object.entries(storage.namespaces)) {\n for (const tableName of Object.keys(ns.entries.table)) {\n const existingNs = tableNamesSeenAcrossNamespaces.get(tableName);\n if (existingNs !== undefined && existingNs !== nsId) {\n throw new Error(\n `Duplicate table name \"${tableName}\" in namespaces \"${existingNs}\" and \"${nsId}\"`,\n );\n }\n tableNamesSeenAcrossNamespaces.set(tableName, nsId);\n }\n }\n\n const tableNames = new Set<string>();\n for (const ns of Object.values(storage.namespaces)) {\n for (const t of Object.keys(ns.entries.table)) {\n tableNames.add(t);\n }\n }\n\n for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {\n const models = domainNs.models as Record<string, ContractModel<SqlModelStorage>>;\n for (const [modelName, model] of Object.entries(models)) {\n const qualifiedName = `${namespaceId}:${modelName}`;\n if (!model.storage?.table) {\n throw new Error(`Model \"${qualifiedName}\" is missing storage.table`);\n }\n if (!model.storage.namespaceId) {\n throw new Error(`Model \"${qualifiedName}\" is missing storage.namespaceId`);\n }\n if (model.storage.namespaceId !== namespaceId) {\n throw new Error(\n `Model \"${qualifiedName}\" storage.namespaceId \"${model.storage.namespaceId}\" does not match domain namespace \"${namespaceId}\"`,\n );\n }\n\n const tableName = model.storage.table;\n const table = storage.namespaces[namespaceId]?.entries.table[tableName] as\n | StorageTable\n | undefined;\n if (!table) {\n throw new Error(\n `Model \"${qualifiedName}\" references non-existent table \"${namespaceId}.${tableName}\"`,\n );\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 \"${qualifiedName}\" is missing storage.fields`);\n }\n\n for (const [fieldName, field] of Object.entries(storageFields)) {\n if (!field.column) {\n throw new Error(\n `Model \"${qualifiedName}\" field \"${fieldName}\" is missing column property`,\n );\n }\n\n if (!columnNames.has(field.column)) {\n throw new Error(\n `Model \"${qualifiedName}\" 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 \"${qualifiedName}\" is missing required field \"relations\" (must be an object)`,\n );\n }\n }\n }\n\n for (const ns of Object.values(storage.namespaces)) {\n for (const [tableName, tableUnknown] of Object.entries(ns.entries.table)) {\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.source.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\"`,\n );\n }\n }\n\n const referencedTable = storage.namespaces[fk.target.namespaceId]?.entries.table[\n fk.target.tableName\n ] as StorageTable | undefined;\n if (!referencedTable) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.target.tableName}\" in namespace \"${fk.target.namespaceId}\"`,\n );\n }\n\n const referencedColumnNames = new Set(Object.keys(referencedTable.columns));\n for (const colName of fk.target.columns) {\n if (!referencedColumnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\" in table \"${fk.target.tableName}\"`,\n );\n }\n }\n\n if (fk.source.columns.length !== fk.target.columns.length) {\n throw new Error(\n `Table \"${tableName}\" foreignKey column count (${fk.source.columns.length}) does not match referenced column count (${fk.target.columns.length})`,\n );\n }\n }\n }\n }\n },\n\n generateStorageType(contract: Contract, storageHashTypeName: string): string {\n const storage = contract.storage as unknown as SqlStorage;\n const namespacesType = generateStorageNamespacesType(storage.namespaces);\n const docTypes = generateDocumentScopedStorageTypesType(storage.types);\n const typesClause = docTypes === undefined ? '' : `; readonly types: ${docTypes}`;\n return `{ readonly namespaces: ${namespacesType}${typesClause}; 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 = [\n `readonly table: ${serializeValue(tableName)}`,\n `readonly namespaceId: ${serializeValue(sqlModel.storage.namespaceId)}`,\n ];\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 storageNamespaceId = sqlModel.storage.namespaceId;\n if (!storageNamespaceId) return undefined;\n\n const table = storage.namespaces[storageNamespaceId]?.entries.table[tableName] as\n | StorageTable\n | undefined;\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 ns = storage.namespaces[storageNamespaceId];\n const nsEnums =\n ns !== undefined\n ? blindCast<\n { readonly type?: Readonly<Record<string, PostgresEnumStorageEntry>> },\n 'postgres target namespace entries carry a type slot beyond the family-shared SqlNamespace.entries type'\n >(ns.entries).type\n : undefined;\n const fromNamespace = nsEnums?.[column.typeRef];\n const typeInstance = fromNamespace ?? storage.types?.[column.typeRef];\n if (typeInstance === undefined) return undefined;\n if (isPostgresEnumStorageEntry(typeInstance)) {\n return { values: typeInstance.values };\n }\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 Namespaces = Contract['storage']['namespaces'];\",\n ].join('\\n');\n },\n} as const;\n\nfunction generateDocumentScopedStorageTypesType(types: SqlStorage['types']): string | undefined {\n if (!types || Object.keys(types).length === 0) {\n return undefined;\n }\n\n const typeEntries: string[] = [];\n for (const [typeName, typeInstance] of Object.entries(types)) {\n if (isPostgresEnumStorageEntry(typeInstance)) {\n throw new Error(\n `Document-scoped storage.types entry \"${typeName}\" is a postgres-enum; enums belong under storage.namespaces[namespaceId].entries.type`,\n );\n }\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}\" in document-scoped storage.types; expected a codec-instance triple.`,\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 generatePostgresNamespaceTypesType(\n types: Readonly<Record<string, PostgresEnumStorageEntry | StorageTypeInstance>>,\n): string {\n if (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(typeInstance.codecId);\n const nativeType = serializeValue(typeInstance.nativeType);\n const name = serializeValue(typeInstance.name);\n const valuesLiteral = typeInstance.values.map((v) => serializeValue(v)).join(', ');\n typeEntries.push(\n `readonly ${serializeObjectKey(typeName)}: { readonly kind: 'postgres-enum'; readonly name: ${name}; readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly values: readonly [${valuesLiteral}] }`,\n );\n continue;\n }\n throw new Error(\n `Unknown namespace storage type kind for \"${typeName}\"; expected postgres-enum in namespace.entries.type.`,\n );\n }\n return `{ ${typeEntries.join('; ')} }`;\n}\n\nconst SQL_NAMESPACE_KIND_FALLBACK = 'sql-namespace' as const;\n\nfunction namespaceSerializedKind(ns: Namespace): string {\n const kind = ns.kind;\n if (kind === 'schema') {\n const id = ns.id;\n const lit = id === UNBOUND_NAMESPACE_ID ? 'postgres-unbound-schema' : 'postgres-schema';\n return `readonly kind: '${lit}'`;\n }\n if (typeof kind === 'string') {\n return `readonly kind: ${serializeValue(kind)}`;\n }\n // Plain-literal namespaces built via the contract-ts DSL bypass the\n // class-level `Object.defineProperty(this, 'kind', { value, enumerable: false })`\n // path, so `ns.kind` is missing on the runtime object. Surfacing the\n // framework-default kind here keeps the emitted `.d.ts` literal\n // structurally assignable to `Namespace`, which now requires `kind`.\n return `readonly kind: '${SQL_NAMESPACE_KIND_FALLBACK}'`;\n}\n\nfunction generateTableLiteralType(table: StorageTable): string {\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 = 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 srcCols = fk.source.columns.map((c: string) => serializeValue(c)).join(', ');\n const tgtCols = fk.target.columns.map((c: string) => serializeValue(c)).join(', ');\n const name = fk.name ? `; readonly name: ${serializeValue(fk.name)}` : '';\n const srcRef = `{ readonly namespaceId: ${serializeNamespaceId(String(fk.source.namespaceId))}; readonly tableName: ${serializeValue(fk.source.tableName)}; readonly columns: readonly [${srcCols}] }`;\n const tgtRef = `{ readonly namespaceId: ${serializeNamespaceId(String(fk.target.namespaceId))}; readonly tableName: ${serializeValue(fk.target.tableName)}; readonly columns: readonly [${tgtCols}] }`;\n return `{ readonly source: ${srcRef}; readonly target: ${tgtRef}${name}; readonly constraint: ${fk.constraint}; readonly index: ${fk.index} }`;\n })\n .join(', ');\n tableParts.push(`foreignKeys: readonly [${fks}]`);\n\n return `{ ${tableParts.join('; ')} }`;\n}\n\nfunction generateTablesMapType(tables: Readonly<Record<string, StorageTable>>): string {\n const tableEntries: string[] = [];\n for (const [tableName, table] of Object.entries(tables).sort(([a], [b]) => a.localeCompare(b))) {\n tableEntries.push(`readonly ${tableName}: ${generateTableLiteralType(table)}`);\n }\n if (tableEntries.length === 0) {\n // Empty namespaces must emit `{}` (whose `keyof` is `never`), not\n // `Record<string, never>` (whose `keyof` is `string`). The latter\n // collapses `Db<C>` to a string-indexed shape and erases literal\n // table-name inference at every consumer site that walks all\n // namespaces (e.g. `db.sql.<tableName>`).\n return '{}';\n }\n return `{ ${tableEntries.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 const kindSuffix = `; ${namespaceSerializedKind(ns)}`;\n const tablesType = generateTablesMapType(\n (ns.entries.table ?? {}) as Readonly<Record<string, StorageTable>>,\n );\n const typeSlot =\n ns.kind === 'schema'\n ? blindCast<\n Readonly<Record<string, PostgresEnumStorageEntry | StorageTypeInstance>> | undefined,\n 'postgres schema namespace entries carry a type slot beyond the family-shared SqlNamespace.entries type'\n >(\n blindCast<\n { readonly type?: Readonly<Record<string, unknown>> },\n 'access opaque type slot on postgres target namespace entries'\n >(ns.entries).type,\n )\n : undefined;\n const entriesParts = [`readonly table: ${tablesType}`];\n if (typeSlot !== undefined) {\n entriesParts.push(`readonly type: ${generatePostgresNamespaceTypesType(typeSlot)}`);\n }\n const entriesType = `{ ${entriesParts.join('; ')} }`;\n parts.push(\n `readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)}${kindSuffix}; readonly entries: ${entriesType} }`,\n );\n }\n return `{ ${parts.join('; ')} }`;\n}\n"],"mappings":";;;;;AAqBA,SAAS,2BAA2B,QAAqD;CACvF,IAAI,CAAC,UAAU,OAAO,KAAK,MAAM,CAAC,CAAC,WAAW,GAC5C,OAAO;CAGT,MAAM,UAAoB,CAAC;CAC3B,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,GAC9C,QAAQ,KAAK,YAAY,mBAAmB,GAAG,EAAE,IAAI,eAAe,KAAK,GAAG;CAG9E,OAAO,KAAK,QAAQ,KAAK,IAAI,EAAE;AACjC;AAEA,MAAa,cAAc;CACzB,IAAI;CAEJ,cAAc,UAAoB,MAA+B;EAC/D,MAAM,UAAU,SAAS;EACzB,IAAI,CAAC,SAAS,YACZ;EAGF,MAAM,cAAc;EAEpB,KAAK,MAAM,MAAM,OAAO,OAAO,QAAQ,UAAU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import type { Contract, ContractModel, ContractModelBase } from '@prisma-next/contract/types';\nimport {\n serializeNamespaceId,\n serializeObjectKey,\n serializeValue,\n} from '@prisma-next/emitter/domain-type-generation';\nimport type {\n GenerateContractTypesOptions,\n ValidationContext,\n} from '@prisma-next/framework-components/emission';\nimport {\n entityAt,\n type Namespace,\n UNBOUND_NAMESPACE_ID,\n} from '@prisma-next/framework-components/ir';\nimport type {\n SqlModelStorage,\n SqlStorage,\n StorageTable,\n StorageTypeInstance,\n StorageValueSet,\n} from '@prisma-next/sql-contract/types';\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?.namespaces) {\n return;\n }\n\n const typeIdRegex = /^([^/]+)\\/([^@]+)@(\\d+)$/;\n\n for (const ns of Object.values(storage.namespaces)) {\n for (const [tableName, table] of Object.entries(ns.entries.table ?? {})) {\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\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?.namespaces) {\n throw new Error('SQL contract must have storage.namespaces');\n }\n\n const tableNamesSeenAcrossNamespaces = new Map<string, string>();\n for (const [nsId, ns] of Object.entries(storage.namespaces)) {\n for (const tableName of Object.keys(ns.entries.table ?? {})) {\n const existingNs = tableNamesSeenAcrossNamespaces.get(tableName);\n if (existingNs !== undefined && existingNs !== nsId) {\n throw new Error(\n `Duplicate table name \"${tableName}\" in namespaces \"${existingNs}\" and \"${nsId}\"`,\n );\n }\n tableNamesSeenAcrossNamespaces.set(tableName, nsId);\n }\n }\n\n const tableNames = new Set<string>();\n for (const ns of Object.values(storage.namespaces)) {\n for (const t of Object.keys(ns.entries.table ?? {})) {\n tableNames.add(t);\n }\n }\n\n for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {\n const models = domainNs.models as Record<string, ContractModel<SqlModelStorage>>;\n for (const [modelName, model] of Object.entries(models)) {\n const qualifiedName = `${namespaceId}:${modelName}`;\n if (!model.storage?.table) {\n throw new Error(`Model \"${qualifiedName}\" is missing storage.table`);\n }\n if (!model.storage.namespaceId) {\n throw new Error(`Model \"${qualifiedName}\" is missing storage.namespaceId`);\n }\n if (model.storage.namespaceId !== namespaceId) {\n throw new Error(\n `Model \"${qualifiedName}\" storage.namespaceId \"${model.storage.namespaceId}\" does not match domain namespace \"${namespaceId}\"`,\n );\n }\n\n const tableName = model.storage.table;\n const table = entityAt<StorageTable>(storage, {\n namespaceId,\n entityKind: 'table',\n entityName: tableName,\n });\n if (!table) {\n throw new Error(\n `Model \"${qualifiedName}\" references non-existent table \"${namespaceId}.${tableName}\"`,\n );\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 \"${qualifiedName}\" is missing storage.fields`);\n }\n\n for (const [fieldName, field] of Object.entries(storageFields)) {\n if (!field.column) {\n throw new Error(\n `Model \"${qualifiedName}\" field \"${fieldName}\" is missing column property`,\n );\n }\n\n if (!columnNames.has(field.column)) {\n throw new Error(\n `Model \"${qualifiedName}\" 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 \"${qualifiedName}\" is missing required field \"relations\" (must be an object)`,\n );\n }\n }\n }\n\n for (const ns of Object.values(storage.namespaces)) {\n for (const [tableName, table] of Object.entries(ns.entries.table ?? {})) {\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.source.columns) {\n if (!columnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\"`,\n );\n }\n }\n\n const referencedTable = entityAt<StorageTable>(storage, {\n namespaceId: fk.target.namespaceId,\n entityKind: 'table',\n entityName: fk.target.tableName,\n });\n if (!referencedTable) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent table \"${fk.target.tableName}\" in namespace \"${fk.target.namespaceId}\"`,\n );\n }\n\n const referencedColumnNames = new Set(Object.keys(referencedTable.columns));\n for (const colName of fk.target.columns) {\n if (!referencedColumnNames.has(colName)) {\n throw new Error(\n `Table \"${tableName}\" foreignKey references non-existent column \"${colName}\" in table \"${fk.target.tableName}\"`,\n );\n }\n }\n\n if (fk.source.columns.length !== fk.target.columns.length) {\n throw new Error(\n `Table \"${tableName}\" foreignKey column count (${fk.source.columns.length}) does not match referenced column count (${fk.target.columns.length})`,\n );\n }\n }\n }\n }\n },\n\n generateStorageType(contract: Contract, storageHashTypeName: string): string {\n const storage = contract.storage as unknown as SqlStorage;\n const namespacesType = generateStorageNamespacesType(storage.namespaces);\n const docTypes = generateDocumentScopedStorageTypesType(storage.types);\n const typesClause = docTypes === undefined ? '' : `; readonly types: ${docTypes}`;\n return `{ readonly namespaces: ${namespacesType}${typesClause}; readonly storageHash: ${storageHashTypeName} }`;\n },\n\n generateModelStorageType(_modelName: string, model: ContractModelBase): string {\n const sqlModel = model as ContractModel<SqlModelStorage>;\n const tableName = sqlModel.storage.table;\n const storageFields = sqlModel.storage.fields;\n\n const storageParts = [\n `readonly table: ${serializeValue(tableName)}`,\n `readonly namespaceId: ${serializeValue(sqlModel.storage.namespaceId)}`,\n ];\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: ContractModelBase,\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 storageNamespaceId = sqlModel.storage.namespaceId;\n if (!storageNamespaceId) return undefined;\n\n const table = entityAt<StorageTable>(storage, {\n namespaceId: storageNamespaceId,\n entityKind: 'table',\n entityName: tableName,\n });\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 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 Namespaces = Contract['storage']['namespaces'];\",\n ].join('\\n');\n },\n} as const;\n\nfunction generateDocumentScopedStorageTypesType(types: SqlStorage['types']): string | undefined {\n if (!types || Object.keys(types).length === 0) {\n return undefined;\n }\n\n const typeEntries: string[] = [];\n for (const [typeName, typeInstance] of Object.entries(types)) {\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}\" in document-scoped storage.types; expected a codec-instance triple.`,\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 generateNamespaceValueSetType(\n valueSet: Readonly<Record<string, StorageValueSet>>,\n): string {\n if (Object.keys(valueSet).length === 0) {\n return 'Record<string, never>';\n }\n const entries: string[] = [];\n for (const [name, vs] of Object.entries(valueSet)) {\n const valuesLiteral = vs.values.map((v) => serializeValue(v)).join(', ');\n entries.push(\n `readonly ${serializeObjectKey(name)}: { readonly kind: 'valueSet'; readonly values: readonly [${valuesLiteral}] }`,\n );\n }\n return `{ ${entries.join('; ')} }`;\n}\n\nconst SQL_NAMESPACE_KIND_FALLBACK = 'sql-namespace' as const;\n\nfunction namespaceSerializedKind(ns: Namespace): string {\n const kind = ns.kind;\n if (kind === 'schema') {\n const id = ns.id;\n const lit = id === UNBOUND_NAMESPACE_ID ? 'postgres-unbound-schema' : 'postgres-schema';\n return `readonly kind: '${lit}'`;\n }\n if (typeof kind === 'string') {\n return `readonly kind: ${serializeValue(kind)}`;\n }\n // Plain-literal namespaces built via the contract-ts DSL bypass the\n // class-level `Object.defineProperty(this, 'kind', { value, enumerable: false })`\n // path, so `ns.kind` is missing on the runtime object. Surfacing the\n // framework-default kind here keeps the emitted `.d.ts` literal\n // structurally assignable to `Namespace`, which now requires `kind`.\n return `readonly kind: '${SQL_NAMESPACE_KIND_FALLBACK}'`;\n}\n\nfunction generateTableLiteralType(table: StorageTable): string {\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 = 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 srcCols = fk.source.columns.map((c: string) => serializeValue(c)).join(', ');\n const tgtCols = fk.target.columns.map((c: string) => serializeValue(c)).join(', ');\n const name = fk.name ? `; readonly name: ${serializeValue(fk.name)}` : '';\n const srcRef = `{ readonly namespaceId: ${serializeNamespaceId(String(fk.source.namespaceId))}; readonly tableName: ${serializeValue(fk.source.tableName)}; readonly columns: readonly [${srcCols}] }`;\n const tgtRef = `{ readonly namespaceId: ${serializeNamespaceId(String(fk.target.namespaceId))}; readonly tableName: ${serializeValue(fk.target.tableName)}; readonly columns: readonly [${tgtCols}] }`;\n return `{ readonly source: ${srcRef}; readonly target: ${tgtRef}${name}; readonly constraint: ${fk.constraint}; readonly index: ${fk.index} }`;\n })\n .join(', ');\n tableParts.push(`foreignKeys: readonly [${fks}]`);\n\n return `{ ${tableParts.join('; ')} }`;\n}\n\nfunction generateTablesMapType(tables: Readonly<Record<string, StorageTable>>): string {\n const tableEntries: string[] = [];\n for (const [tableName, table] of Object.entries(tables).sort(([a], [b]) => a.localeCompare(b))) {\n tableEntries.push(`readonly ${tableName}: ${generateTableLiteralType(table)}`);\n }\n if (tableEntries.length === 0) {\n // Empty namespaces must emit `{}` (whose `keyof` is `never`), not\n // `Record<string, never>` (whose `keyof` is `string`). The latter\n // collapses `Db<C>` to a string-indexed shape and erases literal\n // table-name inference at every consumer site that walks all\n // namespaces (e.g. `db.sql.<tableName>`).\n return '{}';\n }\n return `{ ${tableEntries.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 const kindSuffix = `; ${namespaceSerializedKind(ns)}`;\n const tablesType = generateTablesMapType(\n (ns.entries.table ?? {}) as Readonly<Record<string, StorageTable>>,\n );\n const valueSetSlot = ns.entries.valueSet;\n const entriesParts = [`readonly table: ${tablesType}`];\n if (valueSetSlot !== undefined && Object.keys(valueSetSlot).length > 0) {\n entriesParts.push(`readonly valueSet: ${generateNamespaceValueSetType(valueSetSlot)}`);\n }\n const entriesType = `{ ${entriesParts.join('; ')} }`;\n parts.push(\n `readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)}${kindSuffix}; readonly entries: ${entriesType} }`,\n );\n }\n return `{ ${parts.join('; ')} }`;\n}\n"],"mappings":";;;AAuBA,SAAS,2BAA2B,QAAqD;CACvF,IAAI,CAAC,UAAU,OAAO,KAAK,MAAM,CAAC,CAAC,WAAW,GAC5C,OAAO;CAGT,MAAM,UAAoB,CAAC;CAC3B,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,GAC9C,QAAQ,KAAK,YAAY,mBAAmB,GAAG,EAAE,IAAI,eAAe,KAAK,GAAG;CAG9E,OAAO,KAAK,QAAQ,KAAK,IAAI,EAAE;AACjC;AAEA,MAAa,cAAc;CACzB,IAAI;CAEJ,cAAc,UAAoB,MAA+B;EAC/D,MAAM,UAAU,SAAS;EACzB,IAAI,CAAC,SAAS,YACZ;EAGF,MAAM,cAAc;EAEpB,KAAK,MAAM,MAAM,OAAO,OAAO,QAAQ,UAAU,GAC/C,KAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,GAAG,QAAQ,SAAS,CAAC,CAAC,GACpE,KAAK,MAAM,CAAC,SAAS,eAAe,OAAO,QAAQ,MAAM,OAAO,GAAG;GAEjE,MAAM,UAAUA,WAAI;GACpB,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,WAAW,QAAQ,cAAc,UAAU,qBAAqB;GAIlF,IAAI,CADU,QAAQ,MAAM,WACnB,CAAC,GAAG,IACX,MAAM,IAAI,MACR,WAAW,QAAQ,cAAc,U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,QAAQ;EAChC,MAAM,eAAe,CAAC,mBAAmB,YAAY;EACrD,IAAI,iBAAiB,KAAA,KAAa,OAAO,KAAK,YAAY,CAAC,CAAC,SAAS,GACnE,aAAa,KAAK,sBAAsB,8BAA8B,YAAY,GAAG;EAEvF,MAAM,cAAc,KAAK,aAAa,KAAK,IAAI,EAAE;EACjD,MAAM,KACJ,YAAY,mBAAmB,IAAI,EAAE,mBAAmB,eAAe,GAAG,EAAE,IAAI,WAAW,sBAAsB,YAAY,GAC/H;CACF;CACA,OAAO,KAAK,MAAM,KAAK,IAAI,EAAE;AAC/B"}
{
"name": "@prisma-next/sql-contract-emitter",
"version": "0.13.0",
"version": "0.14.0-dev.1",
"license": "Apache-2.0",

@@ -9,12 +9,12 @@ "type": "module",

"dependencies": {
"@prisma-next/contract": "0.13.0",
"@prisma-next/emitter": "0.13.0",
"@prisma-next/framework-components": "0.13.0",
"@prisma-next/sql-contract": "0.13.0",
"@prisma-next/utils": "0.13.0"
"@prisma-next/contract": "0.14.0-dev.1",
"@prisma-next/emitter": "0.14.0-dev.1",
"@prisma-next/framework-components": "0.14.0-dev.1",
"@prisma-next/sql-contract": "0.14.0-dev.1",
"@prisma-next/utils": "0.14.0-dev.1"
},
"devDependencies": {
"@prisma-next/test-utils": "0.13.0",
"@prisma-next/tsconfig": "0.13.0",
"@prisma-next/tsdown": "0.13.0",
"@prisma-next/test-utils": "0.14.0-dev.1",
"@prisma-next/tsconfig": "0.14.0-dev.1",
"@prisma-next/tsdown": "0.14.0-dev.1",
"tsdown": "0.22.1",

@@ -21,0 +21,0 @@ "typescript": "5.9.3",

@@ -1,2 +0,2 @@

import type { Contract, ContractModel } from '@prisma-next/contract/types';
import type { Contract, ContractModel, ContractModelBase } from '@prisma-next/contract/types';
import {

@@ -11,12 +11,14 @@ serializeNamespaceId,

} from '@prisma-next/framework-components/emission';
import { type Namespace, UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
import {
isPostgresEnumStorageEntry,
type PostgresEnumStorageEntry,
type SqlModelStorage,
type SqlStorage,
type StorageTable,
type StorageTypeInstance,
entityAt,
type Namespace,
UNBOUND_NAMESPACE_ID,
} from '@prisma-next/framework-components/ir';
import type {
SqlModelStorage,
SqlStorage,
StorageTable,
StorageTypeInstance,
StorageValueSet,
} from '@prisma-next/sql-contract/types';
import { blindCast } from '@prisma-next/utils/casts';

@@ -48,4 +50,3 @@ function serializeTypeParamsLiteral(params: Record<string, unknown> | undefined): string {

for (const ns of Object.values(storage.namespaces)) {
for (const [tableName, tableUnknown] of Object.entries(ns.entries.table)) {
const table = tableUnknown as StorageTable;
for (const [tableName, table] of Object.entries(ns.entries.table ?? {})) {
for (const [colName, colUnknown] of Object.entries(table.columns)) {

@@ -81,3 +82,3 @@ const col = colUnknown as { codecId?: string };

for (const [nsId, ns] of Object.entries(storage.namespaces)) {
for (const tableName of Object.keys(ns.entries.table)) {
for (const tableName of Object.keys(ns.entries.table ?? {})) {
const existingNs = tableNamesSeenAcrossNamespaces.get(tableName);

@@ -95,3 +96,3 @@ if (existingNs !== undefined && existingNs !== nsId) {

for (const ns of Object.values(storage.namespaces)) {
for (const t of Object.keys(ns.entries.table)) {
for (const t of Object.keys(ns.entries.table ?? {})) {
tableNames.add(t);

@@ -118,5 +119,7 @@ }

const tableName = model.storage.table;
const table = storage.namespaces[namespaceId]?.entries.table[tableName] as
| StorageTable
| undefined;
const table = entityAt<StorageTable>(storage, {
namespaceId,
entityKind: 'table',
entityName: tableName,
});
if (!table) {

@@ -156,4 +159,3 @@ throw new Error(

for (const ns of Object.values(storage.namespaces)) {
for (const [tableName, tableUnknown] of Object.entries(ns.entries.table)) {
const table = tableUnknown as StorageTable;
for (const [tableName, table] of Object.entries(ns.entries.table ?? {})) {
const columnNames = new Set(Object.keys(table.columns));

@@ -216,5 +218,7 @@

const referencedTable = storage.namespaces[fk.target.namespaceId]?.entries.table[
fk.target.tableName
] as StorageTable | undefined;
const referencedTable = entityAt<StorageTable>(storage, {
namespaceId: fk.target.namespaceId,
entityKind: 'table',
entityName: fk.target.tableName,
});
if (!referencedTable) {

@@ -253,3 +257,3 @@ throw new Error(

generateModelStorageType(_modelName: string, model: ContractModel): string {
generateModelStorageType(_modelName: string, model: ContractModelBase): string {
const sqlModel = model as ContractModel<SqlModelStorage>;

@@ -279,3 +283,3 @@ const tableName = sqlModel.storage.table;

fieldName: string,
model: ContractModel,
model: ContractModelBase,
contract: Contract,

@@ -294,5 +298,7 @@ ): Record<string, unknown> | undefined {

const table = storage.namespaces[storageNamespaceId]?.entries.table[tableName] as
| StorageTable
| undefined;
const table = entityAt<StorageTable>(storage, {
namespaceId: storageNamespaceId,
entityKind: 'table',
entityName: tableName,
});
if (!table) return undefined;

@@ -304,16 +310,4 @@

if (column.typeRef) {
const ns = storage.namespaces[storageNamespaceId];
const nsEnums =
ns !== undefined
? blindCast<
{ readonly type?: Readonly<Record<string, PostgresEnumStorageEntry>> },
'postgres target namespace entries carry a type slot beyond the family-shared SqlNamespace.entries type'
>(ns.entries).type
: undefined;
const fromNamespace = nsEnums?.[column.typeRef];
const typeInstance = fromNamespace ?? storage.types?.[column.typeRef];
const typeInstance = storage.types?.[column.typeRef];
if (typeInstance === undefined) return undefined;
if (isPostgresEnumStorageEntry(typeInstance)) {
return { values: typeInstance.values };
}
const codecShape = typeInstance as Partial<StorageTypeInstance>;

@@ -374,7 +368,2 @@ return codecShape.typeParams;

for (const [typeName, typeInstance] of Object.entries(types)) {
if (isPostgresEnumStorageEntry(typeInstance)) {
throw new Error(
`Document-scoped storage.types entry "${typeName}" is a postgres-enum; enums belong under storage.namespaces[namespaceId].entries.type`,
);
}
const codecInstanceShape = typeInstance as Partial<StorageTypeInstance>;

@@ -400,26 +389,16 @@ if (

function generatePostgresNamespaceTypesType(
types: Readonly<Record<string, PostgresEnumStorageEntry | StorageTypeInstance>>,
function generateNamespaceValueSetType(
valueSet: Readonly<Record<string, StorageValueSet>>,
): string {
if (Object.keys(types).length === 0) {
if (Object.keys(valueSet).length === 0) {
return 'Record<string, never>';
}
const typeEntries: string[] = [];
for (const [typeName, typeInstance] of Object.entries(types)) {
if (isPostgresEnumStorageEntry(typeInstance)) {
const codecId = serializeValue(typeInstance.codecId);
const nativeType = serializeValue(typeInstance.nativeType);
const name = serializeValue(typeInstance.name);
const valuesLiteral = typeInstance.values.map((v) => serializeValue(v)).join(', ');
typeEntries.push(
`readonly ${serializeObjectKey(typeName)}: { readonly kind: 'postgres-enum'; readonly name: ${name}; readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly values: readonly [${valuesLiteral}] }`,
);
continue;
}
throw new Error(
`Unknown namespace storage type kind for "${typeName}"; expected postgres-enum in namespace.entries.type.`,
const entries: string[] = [];
for (const [name, vs] of Object.entries(valueSet)) {
const valuesLiteral = vs.values.map((v) => serializeValue(v)).join(', ');
entries.push(
`readonly ${serializeObjectKey(name)}: { readonly kind: 'valueSet'; readonly values: readonly [${valuesLiteral}] }`,
);
}
return `{ ${typeEntries.join('; ')} }`;
return `{ ${entries.join('; ')} }`;
}

@@ -545,17 +524,6 @@

);
const typeSlot =
ns.kind === 'schema'
? blindCast<
Readonly<Record<string, PostgresEnumStorageEntry | StorageTypeInstance>> | undefined,
'postgres schema namespace entries carry a type slot beyond the family-shared SqlNamespace.entries type'
>(
blindCast<
{ readonly type?: Readonly<Record<string, unknown>> },
'access opaque type slot on postgres target namespace entries'
>(ns.entries).type,
)
: undefined;
const valueSetSlot = ns.entries.valueSet;
const entriesParts = [`readonly table: ${tablesType}`];
if (typeSlot !== undefined) {
entriesParts.push(`readonly type: ${generatePostgresNamespaceTypesType(typeSlot)}`);
if (valueSetSlot !== undefined && Object.keys(valueSetSlot).length > 0) {
entriesParts.push(`readonly valueSet: ${generateNamespaceValueSetType(valueSetSlot)}`);
}

@@ -562,0 +530,0 @@ const entriesType = `{ ${entriesParts.join('; ')} }`;