Skip to content

Commit

Permalink
feat: add primary keys and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Feb 5, 2024
1 parent 3fa8b99 commit 3364822
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions server/api/_hub/database/tables.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ export default eventHandler(async () => {
return !isInternal
})

const [columns, count] = await Promise.all([
const [columns, count, primaryKeys, indexes] = await Promise.all([
db.batch(tables.map(({ name }) => db.prepare(`PRAGMA table_info("${name}")`)))
.then(res => res.map(({ results }) => results as { name: string; type: string }[])),
.then(res => res.map(({ results }) => results as { name: string; type: string, notnull: number, dflt_value: null | string, pk: number }[])),
db.batch<{ c: number }>(tables.map(({ name }) => db.prepare(`SELECT COUNT(*) AS c FROM "${name}"`)))
.then(res => res.map(({ results }) => results[0].c))
.then(res => res.map(({ results }) => results[0].c)),
db.batch(tables.map(({ name }) => db.prepare(`PRAGMA foreign_key_list("${name}")`)))
.then(res => res.map(({ results }) => results as { name: string; type: string }[])),
db.batch(tables.map(({ name }) => db.prepare(`PRAGMA index_list("${name}")`)))
.then(res => res.map(({ results }) => results as { name: string; type: string }[])),
])


return tables.map(({ name }, i) => ({
name,
columns: columns[i],
primaryKeys: primaryKeys[i],
indexes: indexes[i],
count: count[i]
}))
})

0 comments on commit 3364822

Please sign in to comment.