Tables.utils.ts 1.8 KB

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