Skip to content

Commit 8db4b82

Browse files
committed
feat: enhance resource file generation to handle existing files with the same dataSource and improve resourceId handling
1 parent 4c0e148 commit 8db4b82

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

adminforth/commands/createResource/generateResourceFile.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,61 @@ export async function generateResourceFile({
1717
dataSource = "maindb",
1818
resourcesDir = "resources"
1919
}) {
20-
const fileName = `${table}.ts`;
21-
const filePath = path.resolve(process.cwd(), resourcesDir, fileName);
20+
const baseFileName = `${table}.ts`;
21+
const baseFilePath = path.resolve(process.cwd(), resourcesDir, baseFileName);
2222

23-
if (fsSync.existsSync(filePath)) {
24-
console.log(chalk.yellow(`⚠️ File already exists: ${filePath}`));
25-
return { alreadyExists: true, path: filePath };
23+
if (fsSync.existsSync(baseFilePath)) {
24+
const content = await fs.readFile(baseFilePath, "utf-8");
25+
const match = content.match(/dataSource:\s*["'](.+?)["']/);
26+
const existingDataSource = match?.[1];
27+
console.log(existingDataSource, "123444444444");
28+
if (existingDataSource === dataSource) {
29+
console.log(chalk.yellow(`⚠️ File already exists with same dataSource: ${baseFilePath}`));
30+
return { alreadyExists: true, path: baseFilePath, fileName: baseFileName, resourceId: table };
31+
} else {
32+
const suffixedFileName = `${table}_${dataSource}.ts`;
33+
const suffixedFilePath = path.resolve(process.cwd(), resourcesDir, suffixedFileName);
34+
return await writeResourceFile(suffixedFilePath, suffixedFileName, {
35+
table,
36+
columns,
37+
dataSource,
38+
resourceId: `${table}_${dataSource}`,
39+
});
40+
}
2641
}
42+
43+
return await writeResourceFile(baseFilePath, baseFileName, {
44+
table,
45+
columns,
46+
dataSource,
47+
resourceId: table,
48+
});
49+
}
50+
51+
async function writeResourceFile(filePath, fileName, {
52+
table,
53+
columns,
54+
dataSource,
55+
resourceId,
56+
}) {
2757
const __filename = fileURLToPath(import.meta.url);
2858
const __dirname = path.dirname(__filename);
2959
const templatePath = path.resolve(__dirname, "templates/resource.ts.hbs");
3060
console.log(chalk.dim(`Using template: ${templatePath}`));
61+
3162
const context = {
3263
table,
3364
dataSource,
34-
resourceId: table,
65+
resourceId,
3566
label: table.charAt(0).toUpperCase() + table.slice(1),
3667
columns,
3768
};
3869

3970
const content = await renderHBSTemplate(templatePath, context);
40-
4171
await fs.mkdir(path.dirname(filePath), { recursive: true });
4272
await fs.writeFile(filePath, content, "utf-8");
4373

4474
console.log(chalk.green(`✅ Generated resource file: ${filePath}`));
4575

46-
return { alreadyExists: false, path: filePath };
76+
return { alreadyExists: false, path: filePath, fileName, resourceId };
4777
}

adminforth/commands/createResource/main.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { injectResourceIntoIndex } from "./injectResourceIntoIndex.js";
55
import { search, Separator } from "@inquirer/prompts";
66

77
export default async function createResource(args) {
8-
console.log("Bundling admin SPA...");
98
const instance = await findAdminInstance();
10-
console.log("🪲 Found admin instance:", instance.file);
11-
console.log("🪲 Found admin instance:", instance.file);
12-
console.log(JSON.stringify(instance));
139
const tables = await callTsProxy(`
1410
import { admin } from './${instance.file}.js';
1511
export async function exec() {
@@ -52,16 +48,16 @@ export default async function createResource(args) {
5248
return columns;
5349
}
5450
`);
55-
console.log("🪲 Found columns:", columns);
5651

57-
generateResourceFile({
52+
const { resourceId } = await generateResourceFile({
5853
table: table.table,
5954
columns: columns[table.db],
6055
dataSource: table.db,
6156
});
57+
6258
injectResourceIntoIndex({
63-
table: table.table,
64-
resourceId: table.table,
59+
table: resourceId,
60+
resourceId: resourceId,
6561
label: toTitleCase(table.table),
6662
icon: "flowbite:user-solid",
6763
});

adminforth/dataConnectors/mongo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
6363
function flattenObject(obj: any, prefix = '') {
6464
Object.entries(obj).forEach(([key, value]) => {
6565
const fullKey = prefix ? `${prefix}.${key}` : key;
66+
if (fullKey.startsWith('_id.') && typeof value !== 'string' && typeof value !== 'number') {
67+
return;
68+
}
6669
if (value && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date)) {
6770
flattenObject(value, fullKey);
6871
} else {

0 commit comments

Comments
 (0)