Tables.utils.ts73 lines · main
1import { PGForeignTable, PGMaterializedView, PGView } from '@supabase/pg-meta'
2
3import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
4import type { SafePostgresTable } from '@/lib/postgres-types'
5
6// [Joshen] We just need name, schema, description, rows, size, and the number of columns
7// Just missing partitioned tables as missing pg-meta support
8export const formatAllEntities = ({
9 tables = [],
10 views = [],
11 materializedViews = [],
12 foreignTables = [],
13}: {
14 tables?: SafePostgresTable[]
15 views?: PGView[]
16 materializedViews?: PGMaterializedView[]
17 foreignTables?: PGForeignTable[]
18}) => {
19 const formattedTables = tables.map((x) => {
20 return {
21 ...x,
22 type: ENTITY_TYPE.TABLE as const,
23 rows: x.live_rows_estimate,
24 columns: x.columns ?? [],
25 }
26 })
27
28 const formattedViews = views.map((x) => {
29 return {
30 type: ENTITY_TYPE.VIEW as const,
31 id: x.id,
32 name: x.name,
33 comment: x.comment,
34 schema: x.schema,
35 rows: undefined,
36 size: undefined,
37 columns: x.columns ?? [],
38 }
39 })
40
41 const formattedMaterializedViews = materializedViews.map((x) => {
42 return {
43 type: ENTITY_TYPE.MATERIALIZED_VIEW as const,
44 id: x.id,
45 name: x.name,
46 comment: x.comment,
47 schema: x.schema,
48 rows: undefined,
49 size: undefined,
50 columns: x.columns ?? [],
51 }
52 })
53
54 const formattedForeignTables = foreignTables.map((x) => {
55 return {
56 type: ENTITY_TYPE.FOREIGN_TABLE as const,
57 id: x.id,
58 name: x.name,
59 comment: x.comment,
60 schema: x.schema,
61 rows: undefined,
62 size: undefined,
63 columns: x.columns ?? [],
64 }
65 })
66
67 return [
68 ...formattedTables,
69 ...formattedViews,
70 ...formattedMaterializedViews,
71 ...formattedForeignTables,
72 ].sort((a, b) => a.name.localeCompare(b.name))
73}