fix: all tables generate accessors

This commit is contained in:
hypercross 2026-04-15 14:52:41 +08:00
parent 852a108c53
commit 1f3a812728
2 changed files with 26 additions and 50 deletions

View File

@ -745,7 +745,7 @@ describe('parseCsv - resolveReferences: false', () => {
}); });
describe('csvToModule - accessor-based output', () => { 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 = [ const csv = [
'name,age', 'name,age',
'string,number', 'string,number',
@ -754,9 +754,9 @@ describe('csvToModule - accessor-based output', () => {
const result = csvToModule(csv, { emitTypes: false }); 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('import ');
expect(result.js).not.toContain('getData'); expect(result.js).not.toContain('Lookup');
}); });
it('should emit accessor function for tables with references', () => { 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'); 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 = [ const csv = [
'name,age', 'name,age',
'string,number', 'string,number',
@ -826,9 +826,9 @@ describe('csvToModule - accessor-based output', () => {
const result = csvToModule(csv, { emitTypes: true, resourceName: 'people' }); const result = csvToModule(csv, { emitTypes: true, resourceName: 'people' });
expect(result.dts).toContain('declare const data: peopleTable'); expect(result.dts).toContain('declare function getData(): peopleTable');
expect(result.dts).toContain('export default data'); expect(result.dts).toContain('export default getData');
expect(result.dts).not.toContain('declare function getData'); expect(result.dts).not.toContain('declare const data');
}); });
it('should handle nested references in tuples', () => { it('should handle nested references in tuples', () => {

View File

@ -534,8 +534,7 @@ function generateTypeDefinition(
resourceName: string, resourceName: string,
propertyConfigs: PropertyConfig[], propertyConfigs: PropertyConfig[],
references: Set<string>, references: Set<string>,
currentFilePath?: string, currentFilePath?: string
hasRefs?: boolean
): string { ): string {
const typeName = resourceName ? `${resourceName}Table` : 'Table'; const typeName = resourceName ? `${resourceName}Table` : 'Table';
const currentTableName = currentFilePath const currentTableName = currentFilePath
@ -580,24 +579,13 @@ function generateTypeDefinition(
exportAlias = `\nexport type ${singularType} = ${typeName}[number];`; exportAlias = `\nexport type ${singularType} = ${typeName}[number];`;
} }
if (hasRefs) { return `${importSection}type ${typeName} = readonly {
return `${importSection}type ${typeName} = readonly {
${properties} ${properties}
}[]; }[];
${exportAlias} ${exportAlias}
declare function getData(): ${typeName}; declare function getData(): ${typeName};
export default getData; 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 || '', options.resourceName || '',
propertyConfigs, propertyConfigs,
references, references,
options.currentFilePath, options.currentFilePath
referenceFields.length > 0
); );
} }
@ -832,14 +819,6 @@ export function csvToModule(
const hasRefs = result.referenceFields.length > 0; const hasRefs = result.referenceFields.length > 0;
const defaultPrimaryKey = options.defaultPrimaryKey ?? 'id'; 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 imports: string[] = [];
const lookupInits: string[] = []; const lookupInits: string[] = [];
const lookupVarMap = new Map<string, string>(); const lookupVarMap = new Map<string, string>();
@ -883,29 +862,26 @@ export function csvToModule(
const rawJson = JSON.stringify(result.data, null, 2); const rawJson = JSON.stringify(result.data, null, 2);
let js: string; const js = [
if (rowResolvers.length > 0) { ...imports,
js = [ '',
...imports, `const _raw = ${rawJson};`,
'', '',
`const _raw = ${rawJson};`, 'let _resolved = null;',
'', '',
'let _resolved = null;', 'export default function getData() {',
'', ' if (_resolved) return _resolved;',
'export default function getData() {', ' _resolved = _raw;',
' if (_resolved) return _resolved;', ...lookupInits.map(l => ` ${l}`),
' _resolved = _raw;', ...rowResolvers.length > 0 ? [
...lookupInits.map(l => ` ${l}`),
' _resolved = _raw.map(row => ({', ' _resolved = _raw.map(row => ({',
' ...row,', ' ...row,',
...rowResolvers, ...rowResolvers,
' }));', ' }));',
' return _resolved;', ] : [],
'}', ' return _resolved;',
].join('\n'); '}',
} else { ].join('\n');
js = `export default ${rawJson};`;
}
return { return {
js, js,