fix: all tables generate accessors
This commit is contained in:
+19
-43
@@ -534,8 +534,7 @@ function generateTypeDefinition(
|
||||
resourceName: string,
|
||||
propertyConfigs: PropertyConfig[],
|
||||
references: Set<string>,
|
||||
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<string, string>();
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user