Skip to content

Commit 7b9dcd2

Browse files
committed
fix: refactor model generation for generate-models command to use callTsProxy
1 parent 1d65c21 commit 7b9dcd2

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

adminforth/commands/generateModels.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import { toPascalCase, mapToTypeScriptType, getInstance } from "./utils.js";
44
import dotenv from "dotenv";
5+
import { callTsProxy } from "./callTsProxy.js";
56

67
const envFileArg = process.argv.find((arg) => arg.startsWith("--env-file="));
78
const envFilePath = envFileArg ? envFileArg.split("=")[1] : ".env";
@@ -25,28 +26,40 @@ async function generateModels() {
2526
let modelContent = "// Generated model file\n\n";
2627
const files = fs.readdirSync(currentDirectory);
2728
let instanceFound = false;
28-
2929
for (const file of files) {
3030
if (file.endsWith(".js") || file.endsWith(".ts")) {
3131
const instance = await getInstance(file, currentDirectory);
3232
if (instance) {
3333
await instance.discoverDatabases();
3434
instanceFound = true;
35-
instance.config.resources.forEach((resource) => {
35+
for (const resource of instance.config.resources) {
3636
if (resource.columns) {
37-
modelContent += `export type ${toPascalCase(
38-
resource.resourceId
39-
)} = {\n`;
40-
resource.columns.forEach((column) => {
41-
if (column.name && column.type) {
42-
modelContent += ` ${column.name}: ${mapToTypeScriptType(
43-
column.type
44-
)};\n`;
37+
const typeName = toPascalCase(resource.resourceId);
38+
39+
const tsCode = `
40+
export async function exec() {
41+
const columns = ${JSON.stringify(resource.columns)};
42+
const typeName = "${typeName}";
43+
function mapToTypeScriptType(type) {
44+
const map = { "int": "number", "varchar": "string", "boolean": "boolean" };
45+
return map[type] || "any";
46+
}
47+
48+
let typeStr = \`export type \${typeName} = {\\n\`;
49+
for (const col of columns) {
50+
if (col.name && col.type) {
51+
typeStr += \` \${col.name}: \${mapToTypeScriptType(col.type)};\\n\`;
52+
}
53+
}
54+
typeStr += "}\\n\\n";
55+
return typeStr;
4556
}
46-
});
47-
modelContent += `}\n\n`;
57+
`;
58+
59+
const result = await callTsProxy(tsCode);
60+
modelContent += result;
4861
}
49-
});
62+
};
5063
}
5164
}
5265
}
@@ -55,7 +68,6 @@ async function generateModels() {
5568
console.error("Error: No valid instance found to generate models.");
5669
return;
5770
}
58-
5971
fs.writeFileSync(modelFilePath, modelContent, "utf-8");
6072
console.log(`Generated TypeScript model file: ${modelFilePath}`);
6173
return true;

0 commit comments

Comments
 (0)