From 1f3a81272852405552b907319538b133874886ba Mon Sep 17 00:00:00 2001 From: hypercross Date: Wed, 15 Apr 2026 14:52:41 +0800 Subject: [PATCH] fix: all tables generate accessors --- src/csv-loader/loader.test.ts | 14 ++++---- src/csv-loader/loader.ts | 62 +++++++++++------------------------ 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/src/csv-loader/loader.test.ts b/src/csv-loader/loader.test.ts index 9acdbee..2558b3b 100644 --- a/src/csv-loader/loader.test.ts +++ b/src/csv-loader/loader.test.ts @@ -745,7 +745,7 @@ describe('parseCsv - resolveReferences: false', () => { }); describe('csvToModule - accessor-based output', () => { - it('should emit static JSON for tables without references', () => { + it('should emit accessor function for tables without references', () => { const csv = [ 'name,age', 'string,number', @@ -754,9 +754,9 @@ describe('csvToModule - accessor-based output', () => { const result = csvToModule(csv, { emitTypes: false }); - expect(result.js).toContain('export default'); + expect(result.js).toContain('export default function getData()'); expect(result.js).not.toContain('import '); - expect(result.js).not.toContain('getData'); + expect(result.js).not.toContain('Lookup'); }); it('should emit accessor function for tables with references', () => { @@ -817,7 +817,7 @@ describe('csvToModule - accessor-based output', () => { expect(result.dts).not.toContain('declare const data'); }); - it('should generate const type in dts for tables without references', () => { + it('should generate function type in dts for tables without references', () => { const csv = [ 'name,age', 'string,number', @@ -826,9 +826,9 @@ describe('csvToModule - accessor-based output', () => { const result = csvToModule(csv, { emitTypes: true, resourceName: 'people' }); - expect(result.dts).toContain('declare const data: peopleTable'); - expect(result.dts).toContain('export default data'); - expect(result.dts).not.toContain('declare function getData'); + expect(result.dts).toContain('declare function getData(): peopleTable'); + expect(result.dts).toContain('export default getData'); + expect(result.dts).not.toContain('declare const data'); }); it('should handle nested references in tuples', () => { diff --git a/src/csv-loader/loader.ts b/src/csv-loader/loader.ts index 2b04e03..a0bb8f9 100644 --- a/src/csv-loader/loader.ts +++ b/src/csv-loader/loader.ts @@ -534,8 +534,7 @@ function generateTypeDefinition( resourceName: string, propertyConfigs: PropertyConfig[], references: Set, - currentFilePath?: string, - hasRefs?: boolean + currentFilePath?: string ): string { const typeName = resourceName ? `${resourceName}Table` : 'Table'; const currentTableName = currentFilePath @@ -580,24 +579,13 @@ function generateTypeDefinition( exportAlias = `\nexport type ${singularType} = ${typeName}[number];`; } - if (hasRefs) { - return `${importSection}type ${typeName} = readonly { + return `${importSection}type ${typeName} = readonly { ${properties} }[]; ${exportAlias} declare function getData(): ${typeName}; export default getData; -`; - } - - return `${importSection}type ${typeName} = readonly { -${properties} -}[]; -${exportAlias} - -declare const data: ${typeName}; -export default data; `; } @@ -756,8 +744,7 @@ export function parseCsv( options.resourceName || '', propertyConfigs, references, - options.currentFilePath, - referenceFields.length > 0 + options.currentFilePath ); } @@ -832,14 +819,6 @@ export function csvToModule( const hasRefs = result.referenceFields.length > 0; const defaultPrimaryKey = options.defaultPrimaryKey ?? 'id'; - if (!hasRefs) { - const json = JSON.stringify(result.data, null, 2); - return { - js: `export default ${json};`, - dts: result.typeDefinition, - }; - } - const imports: string[] = []; const lookupInits: string[] = []; const lookupVarMap = new Map(); @@ -883,29 +862,26 @@ export function csvToModule( const rawJson = JSON.stringify(result.data, null, 2); - let js: string; - if (rowResolvers.length > 0) { - js = [ - ...imports, - '', - `const _raw = ${rawJson};`, - '', - 'let _resolved = null;', - '', - 'export default function getData() {', - ' if (_resolved) return _resolved;', - ' _resolved = _raw;', - ...lookupInits.map(l => ` ${l}`), + const js = [ + ...imports, + '', + `const _raw = ${rawJson};`, + '', + 'let _resolved = null;', + '', + 'export default function getData() {', + ' if (_resolved) return _resolved;', + ' _resolved = _raw;', + ...lookupInits.map(l => ` ${l}`), + ...rowResolvers.length > 0 ? [ ' _resolved = _raw.map(row => ({', ' ...row,', ...rowResolvers, ' }));', - ' return _resolved;', - '}', - ].join('\n'); - } else { - js = `export default ${rawJson};`; - } + ] : [], + ' return _resolved;', + '}', + ].join('\n'); return { js,