|
1 | | -import axios from "axios"; |
2 | 1 | import prettier from "prettier"; |
3 | 2 | import comment from "../comment"; |
4 | 3 | import type { File, HookFile } from "../types"; |
5 | | -import { DIRECTORY, log, parseNameFormats } from "../utils"; |
6 | | - |
7 | | -export type ColumnResponse = { |
8 | | - tableId: number; |
9 | | - schema: string; |
10 | | - table: string; |
11 | | - id: string; |
12 | | - ordinalPosition: number; |
13 | | - name: string; |
14 | | - defaultValue: unknown; |
15 | | - dataType: string; |
16 | | - format: string; |
17 | | - isIdentity: boolean; |
18 | | - identityGeneration: "ALWAYS" | "BY DEFAULT" | null; |
19 | | - isGenerated: boolean; |
20 | | - isNullable: boolean; |
21 | | - isUpdatable: boolean; |
22 | | - isUnique: boolean; |
23 | | - enums: string[]; |
24 | | - check: string | null; |
25 | | - comment: string | null; |
26 | | -}; |
27 | | - |
28 | | -export type TableResponse = { |
29 | | - id: number; |
30 | | - schema: string; |
31 | | - name: string; |
32 | | - rlsEnabled: boolean; |
33 | | - rlsForced: boolean; |
34 | | - replicaIdentity: "DEFAULT" | "INDEX" | "FULL" | "NOTHING"; |
35 | | - bytes: number; |
36 | | - size: string; |
37 | | - liveRowsEstimate: number; |
38 | | - deadRowsEstimate: number; |
39 | | - comment: string | null; |
40 | | - columns?: ColumnResponse[]; |
41 | | - primaryKeys: { |
42 | | - schema: string; |
43 | | - tableName: string; |
44 | | - name: string; |
45 | | - tableId: number; |
46 | | - }[]; |
47 | | - relationships: { |
48 | | - id: number; |
49 | | - constraintName: string; |
50 | | - sourceSchema: string; |
51 | | - sourceTableName: string; |
52 | | - sourceColumnName: string; |
53 | | - targetTableSchema: string; |
54 | | - targetTableName: string; |
55 | | - targetColumnName: string; |
56 | | - }[]; |
57 | | -}; |
| 4 | +import { DIRECTORY, parseNameFormats } from "../utils"; |
| 5 | +import { TableResponse, TablesResponse } from "../pgMeta/fetchTables"; |
58 | 6 |
|
59 | | -export type TablesResponse = TableResponse[]; |
60 | | - |
61 | | -const parseTableNames = async (): Promise<string[]> => { |
62 | | - const tablesResponse = await axios.get<TablesResponse>( |
63 | | - `${process.env.BACKENGINE_BASE_URL}/api/v1/projects/${process.env.BACKENGINE_PROJECT_ID}/pg-meta/tables`, |
64 | | - { |
65 | | - headers: { |
66 | | - Authorization: `Bearer ${process.env.BACKENGINE_API_KEY}`, |
67 | | - Accept: "application/json", |
68 | | - }, |
69 | | - } |
70 | | - ); |
71 | | - log("Fetched table metadata"); |
72 | | - |
73 | | - const publicTables = tablesResponse.data |
74 | | - .filter((table) => table.schema === "public") |
75 | | - .filter(({ primaryKeys }) => primaryKeys.some(({ name }) => name === "id")); |
76 | | - |
77 | | - return publicTables.map((table) => table.name); |
78 | | -}; |
79 | | - |
80 | | -const mapTableToFile = async (tableName: string): Promise<HookFile> => { |
| 7 | +const mapTableToFile = async (table: TableResponse): Promise<HookFile> => { |
| 8 | + const { name: tableName } = table; |
81 | 9 | const { pascalCase, pascalCasePlural, camelCase, camelCasePlural } = |
82 | 10 | parseNameFormats(tableName); |
83 | 11 |
|
@@ -190,9 +118,11 @@ const mapTableToFile = async (tableName: string): Promise<HookFile> => { |
190 | 118 | }; |
191 | 119 | }; |
192 | 120 |
|
193 | | -export const parseTableFiles = async (): Promise<HookFile[]> => { |
194 | | - const tableNames = await parseTableNames(); |
195 | | - const hookPromises = tableNames.map<Promise<HookFile>>(mapTableToFile); |
196 | | - const files = await Promise.all(hookPromises); |
197 | | - return files; |
| 121 | +export const parseTableFiles = async ( |
| 122 | + tables: TablesResponse |
| 123 | +): Promise<HookFile[]> => { |
| 124 | + const tableHookPromises = tables.map<Promise<HookFile>>(mapTableToFile); |
| 125 | + const tableFiles = await Promise.all(tableHookPromises); |
| 126 | + |
| 127 | + return tableFiles; |
198 | 128 | }; |
0 commit comments