-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain.js
More file actions
64 lines (58 loc) · 1.86 KB
/
Copy pathmain.js
File metadata and controls
64 lines (58 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { callTsProxy, findAdminInstance } from "../callTsProxy.js";
import { toTitleCase } from '../utils.js';
import { generateResourceFile } from "./generateResourceFile.js";
import { injectResourceIntoIndex } from "./injectResourceIntoIndex.js";
import { search, Separator } from "@inquirer/prompts";
export default async function createResource(args) {
const instance = await findAdminInstance();
const tables = await callTsProxy(`
import { admin } from './${instance.file}.js';
export async function exec() {
await admin.discoverDatabases();
const allTables = await admin.getAllTables();
setTimeout(process.exit);
return allTables;
}
`);
const tableChoices = Object.entries(tables).flatMap(([db, tbls]) =>
tbls.map((t) => ({
name: `${db}.${t}`,
value: { db, table: t },
}))
);
const table = await search({
message: '🔍 Choose a table to generate a resource for:',
source: async (input = '') => {
const term = input.toLowerCase();
const choices = tableChoices
.filter(c =>
c.name.toLowerCase().includes(term)
)
.map(c => ({ name: c.name, value: c.value }));
return [
...choices,
new Separator(),
];
},
});
const columns = await callTsProxy(`
import { admin } from './${instance.file}.js';
export async function exec() {
await admin.discoverDatabases();
const columns = await admin.getAllColumnsInTable("${table.table}", "${table.db}");
setTimeout(process.exit);
return columns;
}
`);
const { resourceId } = await generateResourceFile({
table: table.table,
columns: columns[table.db],
dataSource: table.db,
});
await injectResourceIntoIndex({
table: resourceId,
resourceId: resourceId,
label: toTitleCase(table.table),
icon: "flowbite:user-solid",
});
}