-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrexDB.tsx
More file actions
189 lines (170 loc) · 5.27 KB
/
TrexDB.tsx
File metadata and controls
189 lines (170 loc) · 5.27 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { useState, useEffect } from "react";
import { useQuery } from "urql";
import { DataTable, type Column } from "@/components/DataTable";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { RefreshCwIcon } from "lucide-react";
const TREX_DATABASES_QUERY = `
query TrexDatabases {
trexDatabases { databaseName databaseOid path internal type }
}
`;
const TREX_SCHEMAS_QUERY = `
query TrexSchemas {
trexSchemas { databaseName schemaName schemaOid internal }
}
`;
const TREX_TABLES_QUERY = `
query TrexTables {
trexTables { databaseName schemaName tableName }
}
`;
interface DatabaseRow {
databaseName: string;
databaseOid: string;
path: string | null;
internal: boolean;
type: string;
}
interface SchemaRow {
databaseName: string;
schemaName: string;
schemaOid: string;
internal: boolean;
}
interface TableRow {
databaseName: string;
schemaName: string;
tableName: string;
}
export function TrexDB() {
const [selectedDatabase, setSelectedDatabase] = useState<string | null>(null);
const [dbResult, reexecuteDbs] = useQuery({ query: TREX_DATABASES_QUERY });
const [schemaResult, reexecuteSchemas] = useQuery({ query: TREX_SCHEMAS_QUERY });
const [tableResult, reexecuteTables] = useQuery({ query: TREX_TABLES_QUERY });
const databases: DatabaseRow[] = dbResult.data?.trexDatabases || [];
const allSchemas: SchemaRow[] = schemaResult.data?.trexSchemas || [];
const allTables: TableRow[] = tableResult.data?.trexTables || [];
const schemas = selectedDatabase
? allSchemas.filter((s) => s.databaseName === selectedDatabase)
: allSchemas;
const schemaCountByDb = new Map<string, number>();
for (const s of allSchemas) {
schemaCountByDb.set(s.databaseName, (schemaCountByDb.get(s.databaseName) || 0) + 1);
}
const tableCountBySchema = new Map<string, number>();
for (const t of allTables) {
const key = `${t.databaseName}.${t.schemaName}`;
tableCountBySchema.set(key, (tableCountBySchema.get(key) || 0) + 1);
}
function refetchAll() {
reexecuteDbs({ requestPolicy: "network-only" });
reexecuteSchemas({ requestPolicy: "network-only" });
reexecuteTables({ requestPolicy: "network-only" });
}
useEffect(() => {
const interval = setInterval(refetchAll, 30000);
return () => clearInterval(interval);
}, []);
const databaseColumns: Column<DatabaseRow>[] = [
{
header: "Name",
cell: (row) => <code className="text-xs font-mono">{row.databaseName}</code>,
},
{
header: "Type",
cell: (row) => <Badge variant="secondary">{row.type}</Badge>,
},
{
header: "Path",
cell: (row) => (
<span className="text-sm text-muted-foreground">{row.path || "-"}</span>
),
},
{
header: "Schemas",
cell: (row) => (
<span className="text-sm">{schemaCountByDb.get(row.databaseName) || 0}</span>
),
},
{
header: "Internal",
cell: (row) => (
<Badge variant={row.internal ? "secondary" : "default"}>
{row.internal ? "Yes" : "No"}
</Badge>
),
},
];
const schemaColumns: Column<SchemaRow>[] = [
{
header: "Database",
cell: (row) => <code className="text-xs font-mono">{row.databaseName}</code>,
},
{
header: "Schema",
cell: (row) => <span className="text-sm font-medium">{row.schemaName}</span>,
},
{
header: "Tables",
cell: (row) => (
<span className="text-sm">{tableCountBySchema.get(`${row.databaseName}.${row.schemaName}`) || 0}</span>
),
},
{
header: "Internal",
cell: (row) => (
<Badge variant={row.internal ? "secondary" : "default"}>
{row.internal ? "Yes" : "No"}
</Badge>
),
},
];
return (
<div className="space-y-6">
<div className="flex items-start justify-between">
<div>
<h2 className="text-2xl font-bold">Databases</h2>
<p className="text-muted-foreground">
{selectedDatabase
? `Browsing database: ${selectedDatabase}`
: `${databases.length} database${databases.length === 1 ? "" : "s"} attached`}
</p>
</div>
<div className="flex gap-2">
{selectedDatabase && (
<Button variant="outline" onClick={() => setSelectedDatabase(null)}>
Clear Filter
</Button>
)}
<Button variant="outline" onClick={refetchAll}>
<RefreshCwIcon className="h-4 w-4" />
</Button>
</div>
</div>
<div className="space-y-2">
<h3 className="text-lg font-semibold">Databases</h3>
<DataTable
columns={databaseColumns}
data={databases}
loading={dbResult.fetching}
onRowClick={(row) =>
setSelectedDatabase(
row.databaseName === selectedDatabase ? null : row.databaseName
)
}
emptyMessage="No databases found."
/>
</div>
<div className="space-y-2">
<h3 className="text-lg font-semibold">Schemas</h3>
<DataTable
columns={schemaColumns}
data={schemas}
loading={schemaResult.fetching}
emptyMessage="No schemas found."
/>
</div>
</div>
);
}