@prisma-next/sql-contract-emitter 0.5.0-dev.9 → 0.5.0

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/README.md CHANGED
@@ -13,7 +13,7 @@ This package provides the SQL-specific emitter hook implementation for the Prism
13
13
  - `validateStructure()`: Validates SQL-specific logical consistency (foreign key references, model-to-table mappings, constraint consistency). **Note**: Structural properties (required fields, types) are validated by Arktype schema validation - this function focuses on logical validation that schema validators can't perform.
14
14
 
15
15
  - **Type Generation**: Generates TypeScript type definitions for SQL contracts
16
- - `generateContractTypes()`: Generates `contract.d.ts` file content (receives separate `codecTypeImports` and `operationTypeImports` arrays)
16
+ - `generateContractTypes()`: Generates `contract.d.ts` file content (receives a `codecTypeImports` array)
17
17
 
18
18
  ## Dependencies
19
19
 
package/dist/index.d.mts CHANGED
@@ -1,6 +1,27 @@
1
1
  import { Contract, ContractModel } from "@prisma-next/contract/types";
2
- import { GenerateContractTypesOptions, ValidationContext } from "@prisma-next/framework-components/emission";
3
2
 
3
+ //#region ../../../1-framework/1-core/framework-components/dist/types-import-spec-BxI5cSQy.d.mts
4
+ //#region src/shared/types-import-spec.d.ts
5
+ /**
6
+ * Specifies how to import TypeScript types from a package.
7
+ * Used in extension pack manifests to declare codec and operation type imports.
8
+ */
9
+ interface TypesImportSpec {
10
+ readonly package: string;
11
+ readonly named: string;
12
+ readonly alias: string;
13
+ } //#endregion
14
+ //#endregion
15
+ //#region ../../../1-framework/1-core/framework-components/dist/emission-types-CMv_053d.d.mts
16
+ //#region src/control/emission-types.d.ts
17
+ interface GenerateContractTypesOptions {
18
+ readonly queryOperationTypeImports?: ReadonlyArray<TypesImportSpec>;
19
+ }
20
+ interface ValidationContext {
21
+ readonly codecTypeImports?: ReadonlyArray<TypesImportSpec>;
22
+ readonly extensionIds?: ReadonlyArray<string>;
23
+ }
24
+ //#endregion
4
25
  //#region src/index.d.ts
5
26
  declare const sqlEmission: {
6
27
  readonly id: "sql";
@@ -8,6 +29,7 @@ declare const sqlEmission: {
8
29
  readonly validateStructure: (contract: Contract) => void;
9
30
  readonly generateStorageType: (contract: Contract, storageHashTypeName: string) => string;
10
31
  readonly generateModelStorageType: (_modelName: string, model: ContractModel) => string;
32
+ readonly resolveFieldTypeParams: (_modelName: string, fieldName: string, model: ContractModel, contract: Contract) => Record<string, unknown> | undefined;
11
33
  readonly getFamilyImports: () => string[];
12
34
  readonly getFamilyTypeAliases: (options?: GenerateContractTypesOptions) => string;
13
35
  readonly getTypeMapsExpression: () => string;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;cA0Ba;;EAAA,SAAA,aAwTH,EAAA,CAAA,QAAA,EArTgB,QAqThB,EAAA,IAAA,EArTgC,iBAqThC,EAAA,GAAA,IAAA;EArTgB,SAAA,iBAAA,EAAA,CAAA,QAAA,EA2BI,QA3BJ,EAAA,GAAA,IAAA;EAAgB,SAAA,mBAAA,EAAA,CAAA,QAAA,EA6KV,QA7KU,EAAA,mBAAA,EAAA,MAAA,EAAA,GAAA,MAAA;EA2BZ,SAAA,wBAAA,EAAA,CAAA,UAAA,EAAA,MAAA,EAAA,KAAA,EAiOwB,aAjOxB,EAAA,GAAA,MAAA;EAkJE,SAAA,gBAAA,EAAA,GAAA,GAAA,MAAA,EAAA;EA+EsB,SAAA,oBAAA,EAAA,CAAA,OAAA,CAAA,EA4BrB,4BA5BqB,EAAA,GAAA,MAAA;EA4BrB,SAAA,qBAAA,EAAA,GAAA,GAAA,MAAA;EAA4B,SAAA,kBAAA,EAAA,CAAA,gBAAA,EAAA,MAAA,EAAA,YAAA,EAAA,MAAA,EAAA,GAAA,MAAA"}
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;;;cCab,WAAA;EAAA;qCAGa,QAAA,EAAQ,IAAA,EAAQ,iBAAA;EAAA,uCA2BZ,QAAA;EAAA,yCA8IE,QAAA,EAAQ,mBAAA;EAAA,wDAgFK,KAAA,EAAS,aAAA;EAAA,sDAoBhC,SAAA,UACD,KAAA,EACV,aAAA,EAAa,QAAA,EACV,QAAA,KACT,MAAA;EAAA;4CA+B4B,4BAAA;EAAA;0DAwBY,YAAA;AAAA"}
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
- import { generateCodecTypeIntersection, serializeObjectKey, serializeValue } from "@prisma-next/emitter/domain-type-generation";
1
+ import { serializeObjectKey, serializeValue } from "@prisma-next/emitter/domain-type-generation";
2
2
  import { assertDefined } from "@prisma-next/utils/assertions";
3
-
4
3
  //#region src/index.ts
5
4
  function serializeTypeParamsLiteral(params) {
6
5
  if (!params || Object.keys(params).length === 0) return "Record<string, never>";
@@ -36,7 +35,6 @@ const sqlEmission = {
36
35
  if (!tableNames.has(tableName)) throw new Error(`Model "${modelName}" references non-existent table "${tableName}"`);
37
36
  const table = storage.tables[tableName];
38
37
  assertDefined(table, `Model "${modelName}" references non-existent table "${tableName}"`);
39
- if (!table.primaryKey) throw new Error(`Model "${modelName}" table "${tableName}" is missing a primary key`);
40
38
  const columnNames = new Set(Object.keys(table.columns));
41
39
  const storageFields = model.storage.fields;
42
40
  if (!storageFields || Object.keys(storageFields).length === 0) throw new Error(`Model "${modelName}" is missing storage.fields`);
@@ -93,7 +91,7 @@ const sqlEmission = {
93
91
  }).join(", ");
94
92
  tableParts.push(`uniques: readonly [${uniques}]`);
95
93
  const indexes = table.indexes.map((i) => {
96
- return `{ readonly columns: readonly [${i.columns.map((c) => serializeValue(c)).join(", ")}]${i.name ? `; readonly name: ${serializeValue(i.name)}` : ""}${i.using !== void 0 ? `; readonly using: ${serializeValue(i.using)}` : ""}${i.config !== void 0 ? `; readonly config: ${serializeValue(i.config)}` : ""} }`;
94
+ return `{ readonly columns: readonly [${i.columns.map((c) => serializeValue(c)).join(", ")}]${i.name !== void 0 ? `; readonly name: ${serializeValue(i.name)}` : ""}${i.type !== void 0 ? `; readonly type: ${serializeValue(i.type)}` : ""}${i.options !== void 0 ? `; readonly options: ${serializeValue(i.options)}` : ""} }`;
97
95
  }).join(", ");
98
96
  tableParts.push(`indexes: readonly [${indexes}]`);
99
97
  const fks = table.foreignKeys.map((fk) => {
@@ -120,6 +118,20 @@ const sqlEmission = {
120
118
  }
121
119
  return `{ ${storageParts.join("; ")} }`;
122
120
  },
121
+ resolveFieldTypeParams(_modelName, fieldName, model, contract) {
122
+ const sqlModel = model;
123
+ const storageField = sqlModel.storage?.fields?.[fieldName];
124
+ if (!storageField) return void 0;
125
+ const storage = contract.storage;
126
+ if (!storage) return void 0;
127
+ const tableName = sqlModel.storage.table;
128
+ const table = storage.tables[tableName];
129
+ if (!table) return void 0;
130
+ const column = table.columns[storageField.column];
131
+ if (!column) return void 0;
132
+ if (column.typeRef) return (storage.types?.[column.typeRef])?.typeParams;
133
+ return column.typeParams;
134
+ },
123
135
  getFamilyImports() {
124
136
  return [
125
137
  "import type {",
@@ -129,9 +141,10 @@ const sqlEmission = {
129
141
  ];
130
142
  },
131
143
  getFamilyTypeAliases(options) {
144
+ const queryOperationAliases = (options?.queryOperationTypeImports ?? []).filter((imp) => imp.named === "QueryOperationTypes").map((imp) => `${imp.alias}<CodecTypes>`);
132
145
  return [
133
146
  "export type LaneCodecTypes = CodecTypes;",
134
- `export type QueryOperationTypes = ${generateCodecTypeIntersection(options?.queryOperationTypeImports ?? [], "QueryOperationTypes")};`,
147
+ `export type QueryOperationTypes = ${queryOperationAliases.length > 0 ? queryOperationAliases.join(" & ") : "Record<string, never>"};`,
135
148
  "type DefaultLiteralValue<CodecId extends string, _Encoded> =",
136
149
  " CodecId extends keyof CodecTypes",
137
150
  " ? CodecTypes[CodecId]['output']",
@@ -139,7 +152,7 @@ const sqlEmission = {
139
152
  ].join("\n");
140
153
  },
141
154
  getTypeMapsExpression() {
142
- return "TypeMapsType<CodecTypes, OperationTypes, QueryOperationTypes, FieldOutputTypes, FieldInputTypes>";
155
+ return "TypeMapsType<CodecTypes, QueryOperationTypes, FieldOutputTypes, FieldInputTypes>";
143
156
  },
144
157
  getContractWrapper(contractBaseName, typeMapsName) {
145
158
  return [
@@ -161,7 +174,7 @@ function generateStorageTypesType(types) {
161
174
  }
162
175
  return `{ ${typeEntries.join("; ")} }`;
163
176
  }
164
-
165
177
  //#endregion
166
178
  export { sqlEmission };
179
+
167
180
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["entries: string[]","table: StorageTable | undefined","referencedTable: StorageTable | undefined","tables: string[]","columns: string[]","tableParts: string[]","fieldParts: string[]","typeEntries: string[]"],"sources":["../src/index.ts"],"sourcesContent":["import type { Contract, ContractModel } from '@prisma-next/contract/types';\nimport {\n generateCodecTypeIntersection,\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 { 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 || !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 || !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 || !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 if (!table.primaryKey) {\n throw new Error(`Model \"${modelName}\" table \"${tableName}\" is missing a primary key`);\n }\n\n const columnNames = new Set(Object.keys(table.columns));\n 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 ? `; readonly name: ${serializeValue(i.name)}` : '';\n const using = i.using !== undefined ? `; readonly using: ${serializeValue(i.using)}` : '';\n const config =\n i.config !== undefined ? `; readonly config: ${serializeValue(i.config)}` : '';\n return `{ readonly columns: readonly [${cols}]${name}${using}${config} }`;\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 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 queryOperationTypes = generateCodecTypeIntersection(\n queryOperationTypeImports,\n 'QueryOperationTypes',\n );\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, OperationTypes, 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":";;;;AAaA,SAAS,2BAA2B,QAAqD;AACvF,KAAI,CAAC,UAAU,OAAO,KAAK,OAAO,CAAC,WAAW,EAC5C,QAAO;CAGT,MAAMA,UAAoB,EAAE;AAC5B,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,CAC/C,SAAQ,KAAK,YAAY,mBAAmB,IAAI,CAAC,IAAI,eAAe,MAAM,GAAG;AAG/E,QAAO,KAAK,QAAQ,KAAK,KAAK,CAAC;;AAGjC,MAAa,cAAc;CACzB,IAAI;CAEJ,cAAc,UAAoB,MAA+B;EAC/D,MAAM,UAAU,SAAS;AACzB,MAAI,CAAC,WAAW,CAAC,QAAQ,OACvB;EAGF,MAAM,cAAc;AAEpB,OAAK,MAAM,CAAC,WAAW,iBAAiB,OAAO,QAAQ,QAAQ,OAAO,EAAE;GACtE,MAAM,QAAQ;AACd,QAAK,MAAM,CAAC,SAAS,eAAe,OAAO,QAAQ,MAAM,QAAQ,EAAE;IAEjE,MAAM,UADM,WACQ;AACpB,QAAI,CAAC,QACH,OAAM,IAAI,MAAM,WAAW,QAAQ,cAAc,UAAU,sBAAsB;IAGnF,MAAM,QAAQ,QAAQ,MAAM,YAAY;AACxC,QAAI,CAAC,SAAS,CAAC,MAAM,GACnB,OAAM,IAAI,MACR,WAAW,QAAQ,cAAc,UAAU,iCAAiC,QAAQ,qCACrF;;;;CAMT,kBAAkB,UAA0B;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AED,SAAS,yBAAyB,OAAoC;AACpE,KAAI,CAAC,SAAS,OAAO,KAAK,MAAM,CAAC,WAAW,EAC1C,QAAO;CAGT,MAAMC,cAAwB,EAAE;AAChC,MAAK,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;AACzE,cAAY,KACV,YAAY,SAAS,wBAAwB,QAAQ,yBAAyB,WAAW,yBAAyB,cAAc,IACjI;;AAGH,QAAO,KAAK,YAAY,KAAK,KAAK,CAAC"}
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 || !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 || !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 || !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,WAAW,CAAC,QAAQ,QACvB;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;IAGnF,MAAM,QAAQ,QAAQ,MAAM,YAAY;IACxC,IAAI,CAAC,SAAS,CAAC,MAAM,IACnB,MAAM,IAAI,MACR,WAAW,QAAQ,cAAc,UAAU,iCAAiC,QAAQ,qCACrF;;;;CAMT,kBAAkB,UAA0B;EAC1C,IAAI,SAAS,iBAAiB,OAC5B,MAAM,IAAI,MAAM,qCAAqC,SAAS,aAAa,GAAG;EAGhF,MAAM,UAAU,SAAS;EACzB,IAAI,CAAC,WAAW,CAAC,QAAQ,QACvB,MAAM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AQ,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"}
package/package.json CHANGED
@@ -1,23 +1,24 @@
1
1
  {
2
2
  "name": "@prisma-next/sql-contract-emitter",
3
- "version": "0.5.0-dev.9",
3
+ "version": "0.5.0",
4
+ "license": "Apache-2.0",
4
5
  "type": "module",
5
6
  "sideEffects": false,
6
7
  "description": "SQL emitter hook for Prisma Next",
7
8
  "dependencies": {
8
- "@prisma-next/contract": "0.5.0-dev.9",
9
- "@prisma-next/emitter": "0.5.0-dev.9",
10
- "@prisma-next/sql-contract": "0.5.0-dev.9",
11
- "@prisma-next/utils": "0.5.0-dev.9"
9
+ "@prisma-next/contract": "0.5.0",
10
+ "@prisma-next/emitter": "0.5.0",
11
+ "@prisma-next/utils": "0.5.0",
12
+ "@prisma-next/sql-contract": "0.5.0"
12
13
  },
13
14
  "devDependencies": {
14
- "tsdown": "0.18.4",
15
+ "tsdown": "0.22.0",
15
16
  "typescript": "5.9.3",
16
- "vitest": "4.0.17",
17
- "@prisma-next/framework-components": "0.5.0-dev.9",
18
- "@prisma-next/tsconfig": "0.0.0",
17
+ "vitest": "4.1.5",
18
+ "@prisma-next/framework-components": "0.5.0",
19
19
  "@prisma-next/test-utils": "0.0.1",
20
- "@prisma-next/tsdown": "0.0.0"
20
+ "@prisma-next/tsdown": "0.0.0",
21
+ "@prisma-next/tsconfig": "0.0.0"
21
22
  },
22
23
  "files": [
23
24
  "dist",
package/src/index.ts CHANGED
@@ -1,9 +1,5 @@
1
1
  import type { Contract, ContractModel } from '@prisma-next/contract/types';
2
- import {
3
- generateCodecTypeIntersection,
4
- serializeObjectKey,
5
- serializeValue,
6
- } from '@prisma-next/emitter/domain-type-generation';
2
+ import { serializeObjectKey, serializeValue } from '@prisma-next/emitter/domain-type-generation';
7
3
  import type {
8
4
  GenerateContractTypesOptions,
9
5
  ValidationContext,
@@ -81,10 +77,6 @@ export const sqlEmission = {
81
77
  const table: StorageTable | undefined = storage.tables[tableName];
82
78
  assertDefined(table, `Model "${modelName}" references non-existent table "${tableName}"`);
83
79
 
84
- if (!table.primaryKey) {
85
- throw new Error(`Model "${modelName}" table "${tableName}" is missing a primary key`);
86
- }
87
-
88
80
  const columnNames = new Set(Object.keys(table.columns));
89
81
  const storageFields = model.storage.fields;
90
82
  if (!storageFields || Object.keys(storageFields).length === 0) {
@@ -252,11 +244,12 @@ export const sqlEmission = {
252
244
  const indexes = table.indexes
253
245
  .map((i) => {
254
246
  const cols = i.columns.map((c: string) => serializeValue(c)).join(', ');
255
- const name = i.name ? `; readonly name: ${serializeValue(i.name)}` : '';
256
- const using = i.using !== undefined ? `; readonly using: ${serializeValue(i.using)}` : '';
257
- const config =
258
- i.config !== undefined ? `; readonly config: ${serializeValue(i.config)}` : '';
259
- return `{ readonly columns: readonly [${cols}]${name}${using}${config} }`;
247
+ const name = i.name !== undefined ? `; readonly name: ${serializeValue(i.name)}` : '';
248
+ const indexType =
249
+ i.type !== undefined ? `; readonly type: ${serializeValue(i.type)}` : '';
250
+ const indexOptions =
251
+ i.options !== undefined ? `; readonly options: ${serializeValue(i.options)}` : '';
252
+ return `{ readonly columns: readonly [${cols}]${name}${indexType}${indexOptions} }`;
260
253
  })
261
254
  .join(', ');
262
255
  tableParts.push(`indexes: readonly [${indexes}]`);
@@ -298,6 +291,33 @@ export const sqlEmission = {
298
291
  return `{ ${storageParts.join('; ')} }`;
299
292
  },
300
293
 
294
+ resolveFieldTypeParams(
295
+ _modelName: string,
296
+ fieldName: string,
297
+ model: ContractModel,
298
+ contract: Contract,
299
+ ): Record<string, unknown> | undefined {
300
+ const sqlModel = model as ContractModel<SqlModelStorage>;
301
+ const storageField = sqlModel.storage?.fields?.[fieldName];
302
+ if (!storageField) return undefined;
303
+
304
+ const storage = contract.storage as unknown as SqlStorage | undefined;
305
+ if (!storage) return undefined;
306
+
307
+ const tableName = sqlModel.storage.table;
308
+ const table: StorageTable | undefined = storage.tables[tableName];
309
+ if (!table) return undefined;
310
+
311
+ const column = table.columns[storageField.column];
312
+ if (!column) return undefined;
313
+
314
+ if (column.typeRef) {
315
+ const typeInstance = storage.types?.[column.typeRef];
316
+ return typeInstance?.typeParams;
317
+ }
318
+ return column.typeParams;
319
+ },
320
+
301
321
  getFamilyImports(): string[] {
302
322
  return [
303
323
  'import type {',
@@ -309,10 +329,13 @@ export const sqlEmission = {
309
329
 
310
330
  getFamilyTypeAliases(options?: GenerateContractTypesOptions): string {
311
331
  const queryOperationTypeImports = options?.queryOperationTypeImports ?? [];
312
- const queryOperationTypes = generateCodecTypeIntersection(
313
- queryOperationTypeImports,
314
- 'QueryOperationTypes',
315
- );
332
+ const queryOperationAliases = queryOperationTypeImports
333
+ .filter((imp) => imp.named === 'QueryOperationTypes')
334
+ .map((imp) => `${imp.alias}<CodecTypes>`);
335
+ const queryOperationTypes =
336
+ queryOperationAliases.length > 0
337
+ ? queryOperationAliases.join(' & ')
338
+ : 'Record<string, never>';
316
339
 
317
340
  return [
318
341
  'export type LaneCodecTypes = CodecTypes;',
@@ -325,7 +348,7 @@ export const sqlEmission = {
325
348
  },
326
349
 
327
350
  getTypeMapsExpression(): string {
328
- return 'TypeMapsType<CodecTypes, OperationTypes, QueryOperationTypes, FieldOutputTypes, FieldInputTypes>';
351
+ return 'TypeMapsType<CodecTypes, QueryOperationTypes, FieldOutputTypes, FieldInputTypes>';
329
352
  },
330
353
 
331
354
  getContractWrapper(contractBaseName: string, typeMapsName: string): string {