fix: update type generation

This commit is contained in:
2026-04-15 14:46:03 +08:00
parent 6eba70bb3b
commit 852a108c53
2 changed files with 59 additions and 22 deletions
+34 -12
View File
@@ -538,12 +538,22 @@ function generateTypeDefinition(
hasRefs?: boolean
): string {
const typeName = resourceName ? `${resourceName}Table` : 'Table';
const currentTableName = currentFilePath
? path.basename(currentFilePath, path.extname(currentFilePath))
: undefined;
const singularType = resourceName
? resourceName.charAt(0).toUpperCase() + resourceName.slice(1)
: `${typeName}[number]`;
// Generate import statements for referenced tables
const imports: string[] = [];
const resourceNames = new Map<string, string>();
references.forEach(tableName => {
if (tableName === currentTableName) {
resourceNames.set(tableName, singularType);
return;
}
// Convert table name to type name by capitalizing
const typeBase = tableName.charAt(0).toUpperCase() + tableName.slice(1);
resourceNames.set(tableName, typeBase);
@@ -551,10 +561,8 @@ function generateTypeDefinition(
// Generate import path based on current file path
let importPath: string;
if (currentFilePath) {
// Both files are in the same directory, use relative path
importPath = `./${tableName}.csv`;
} else {
// Fallback for unknown path
importPath = `../${tableName}.csv`;
}
imports.push(`import type { ${typeBase} } from '${importPath}';`);
@@ -793,15 +801,17 @@ function generateSchemaResolutionCode(
case 'union': {
const refMembers = schema.members.filter(m => hasNestedReferences(m));
const nonRefMembers = schema.members.filter(m => !hasNestedReferences(m));
const parts: string[] = [];
const resolveParts: string[] = [];
for (const member of refMembers) {
const resolveCode = generateSchemaResolutionCode(member, valueExpr, lookupVar, pkField);
parts.push(resolveCode);
resolveParts.push(resolveCode);
}
if (nonRefMembers.length > 0) {
parts.push(valueExpr);
resolveParts.push(valueExpr);
}
return parts[0] || valueExpr;
if (resolveParts.length === 0) return valueExpr;
if (resolveParts.length === 1) return resolveParts[0];
return `(${resolveParts.join(' ?? ')})`;
}
default:
return valueExpr;
@@ -834,14 +844,26 @@ export function csvToModule(
const lookupInits: string[] = [];
const lookupVarMap = new Map<string, string>();
const currentTableName = options.currentFilePath
? path.basename(options.currentFilePath, path.extname(options.currentFilePath))
: undefined;
const uniqueTables = new Set(result.referenceFields.map(f => f.tableName));
uniqueTables.forEach(tableName => {
const varName = `_${tableName}`;
lookupVarMap.set(tableName, `_${tableName}Lookup`);
imports.push(`import ${varName} from './${tableName}.csv';`);
lookupInits.push(
`const _${tableName}Lookup = new Map(${varName}().map(p => [String(p.${defaultPrimaryKey}), p]));`
);
const lookupVar = `_${tableName}Lookup`;
lookupVarMap.set(tableName, lookupVar);
if (tableName === currentTableName) {
lookupInits.push(
`const ${lookupVar} = new Map(_raw.map(p => [String(p.${defaultPrimaryKey}), p]));`
);
} else {
const varName = `_${tableName}`;
imports.push(`import ${varName} from './${tableName}.csv';`);
lookupInits.push(
`const ${lookupVar} = new Map(${varName}().map(p => [String(p.${defaultPrimaryKey}), p]));`
);
}
});
const lookupVar = (tableName: string) => lookupVarMap.get(tableName)!;