Skip to content

Commit 3fa8b99

Browse files
committed
feat: add database tables inspection
1 parent cb30ffd commit 3fa8b99

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export default eventHandler(async () => {
2+
const db = useDatabaseClient()
3+
4+
const { results } = await db.prepare('PRAGMA table_list').all<{
5+
schema: string;
6+
name: string;
7+
type: string;
8+
ncol: number;
9+
wr: number;
10+
strict: number;
11+
}>()
12+
13+
if (!results) {
14+
throw createError({ statusCode: 404, message: 'No tables found' })
15+
}
16+
17+
const tables = results.filter(({ name }) => {
18+
const isInternal = name.startsWith('sqlite_') || name.startsWith('_cf_') || name.startsWith('d1_') || name.startsWith('__drizzle')
19+
20+
return !isInternal
21+
})
22+
23+
const [columns, count] = await Promise.all([
24+
db.batch(tables.map(({ name }) => db.prepare(`PRAGMA table_info("${name}")`)))
25+
.then(res => res.map(({ results }) => results as { name: string; type: string }[])),
26+
db.batch<{ c: number }>(tables.map(({ name }) => db.prepare(`SELECT COUNT(*) AS c FROM "${name}"`)))
27+
.then(res => res.map(({ results }) => results[0].c))
28+
])
29+
30+
31+
return tables.map(({ name }, i) => ({
32+
name,
33+
columns: columns[i],
34+
count: count[i]
35+
}))
36+
})

0 commit comments

Comments
 (0)