Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Implement enum types store and hide system enum types from table editor

Joshen Limd889b46ebfb3
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
index c7f02815118..460b4fb811d 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
@@ -1,17 +1,18 @@
import React, { FC } from 'react'
import { isNil } from 'lodash'
import { Select } from '@supabase/ui'
+import { PostgresType } from '@supabase/postgres-meta'
import InputWithSuggestions from './InputWithSuggestions'
import { POSTGRES_DATA_TYPES } from '../SidePanelEditor.constants'
-import { ColumnField, EnumType } from '../SidePanelEditor.types'
+import { ColumnField } from '../SidePanelEditor.types'
import { Suggestion } from './ColumnEditor.types'
import { getSelectedEnumValues } from './ColumnEditor.utils'
import { typeExpressionSuggestions } from './ColumnEditor.constants'
interface Props {
columnFields: ColumnField
- enumTypes: EnumType[]
+ enumTypes: PostgresType[]
onUpdateField: (changes: Partial<ColumnField>) => void
}
@@ -27,6 +28,7 @@ const ColumnDefaultValue: FC<Props> = ({
isNil(columnFields.format) && !POSTGRES_DATA_TYPES.includes(columnFields.format)
if (isUserDefinedEnum) {
+ const enumValues = getSelectedEnumValues(columnFields.format, enumTypes)
return (
<Select
label="Default Value"
@@ -37,7 +39,7 @@ const ColumnDefaultValue: FC<Props> = ({
<Select.Option key="empty-enum" value="">
---
</Select.Option>
- {getSelectedEnumValues(columnFields.format, enumTypes).map((value: string) => (
+ {enumValues.map((value: string) => (
<Select.Option key={value} value={value}>
{value}
</Select.Option>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 6b8b9e08842..b2f33cc3129 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -2,7 +2,12 @@ import { FC, useEffect, useState } from 'react'
import { isUndefined, isEmpty } from 'lodash'
import { Dictionary } from '@supabase/grid'
import { Checkbox, SidePanel, Space, Input, Divider } from '@supabase/ui'
-import { PostgresColumn, PostgresRelationship, PostgresTable } from '@supabase/postgres-meta'
+import {
+ PostgresColumn,
+ PostgresRelationship,
+ PostgresTable,
+ PostgresType,
+} from '@supabase/postgres-meta'
import ActionBar from '../ActionBar'
import HeaderTitle from './HeaderTitle'
@@ -18,18 +23,13 @@ import {
generateCreateColumnPayload,
generateUpdateColumnPayload,
} from './ColumnEditor.utils'
-import {
- EnumType,
- ColumnField,
- CreateColumnPayload,
- UpdateColumnPayload,
-} from '../SidePanelEditor.types'
+import { ColumnField, CreateColumnPayload, UpdateColumnPayload } from '../SidePanelEditor.types'
interface Props {
column?: PostgresColumn
selectedTable: PostgresTable
tables: PostgresTable[]
- enumTypes: EnumType[]
+ enumTypes: PostgresType[]
visible: boolean
closePanel: () => void
saveChanges: (
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index 78482346551..10fd709c644 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -1,14 +1,14 @@
import { find, isUndefined, isEqual, isNull } from 'lodash'
import { Dictionary } from '@supabase/grid'
-import { PostgresColumn, PostgresRelationship, PostgresTable } from '@supabase/postgres-meta'
+import {
+ PostgresColumn,
+ PostgresRelationship,
+ PostgresTable,
+ PostgresType,
+} from '@supabase/postgres-meta'
import { uuidv4 } from 'lib/helpers'
-import {
- ColumnField,
- EnumType,
- CreateColumnPayload,
- UpdateColumnPayload,
-} from '../SidePanelEditor.types'
+import { ColumnField, CreateColumnPayload, UpdateColumnPayload } from '../SidePanelEditor.types'
const isSQLExpression = (input: string) => {
if (input[0] === '(' && input[input.length - 1] === ')') {
@@ -138,9 +138,9 @@ export const generateUpdateColumnPayload = (
return payload
}
-export const getSelectedEnumValues = (type: string, enums: EnumType[]) => {
+export const getSelectedEnumValues = (type: string, enums: PostgresType[]) => {
const enumType = find(enums, { name: type })
- return enumType?.values ?? []
+ return (enumType?.enums ?? []) as string[]
}
export const validateFields = (field: ColumnField) => {
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx
index af761a0447d..1be64e209a8 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx
@@ -1,11 +1,12 @@
import React, { FC } from 'react'
import { IconCalendar, IconType, IconHash, Listbox, IconToggleRight } from '@supabase/ui'
+import { PostgresType } from '@supabase/postgres-meta'
import { POSTGRES_DATA_TYPE_OPTIONS } from '../SidePanelEditor.constants'
-import { PostgresDataTypeOption, EnumType } from '../SidePanelEditor.types'
+import { PostgresDataTypeOption } from '../SidePanelEditor.types'
interface Props {
value: string
- enumTypes: EnumType[]
+ enumTypes: PostgresType[]
size?: 'tiny' | 'small' | 'medium' | 'large' | 'xlarge'
className?: string
error?: any
@@ -66,7 +67,7 @@ const ColumnType: FC<Props> = ({
</Listbox.Option>
{enumTypes.length > 0 ? (
- enumTypes.map((enumType: EnumType) => (
+ enumTypes.map((enumType: PostgresType) => (
<Listbox.Option
key={enumType.name}
value={enumType.name}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 85eed831540..8ebe4b389de 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -45,24 +45,11 @@ const SidePanelEditor: FC<Props> = ({
}) => {
const { meta, ui } = useStore()
- const [enumTypes, setEnumTypes] = useState<any[]>([])
const [isEdited, setIsEdited] = useState<boolean>(false)
const [isClosingPanel, setIsClosingPanel] = useState<boolean>(false)
const tables = meta.tables.list()
-
- useEffect(() => {
- let cancel = false
- const fetchEnumTypes = async () => {
- const enumTypes = await meta.schemas.getEnums()
- if (!cancel) setEnumTypes(enumTypes)
- }
- fetchEnumTypes()
-
- return () => {
- cancel = true
- }
- }, [])
+ const enumTypes = meta.types.list()
const saveRow = async (
payload: any,
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
index dd7eb1332e2..f68ef14587d 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
@@ -75,12 +75,6 @@ export interface ColumnField {
// column: PostgresColumn
// }
-export interface EnumType {
- id: number
- name: string
- values: any[]
-}
-
export interface PostgresDataTypeOption {
name: string
description: string
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
index fbc150d3ab0..96d2a4161ef 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
@@ -11,8 +11,9 @@ import {
IconSettings,
Button,
} from '@supabase/ui'
+import { PostgresType } from '@supabase/postgres-meta'
-import { ColumnField, EnumType } from '../SidePanelEditor.types'
+import { ColumnField } from '../SidePanelEditor.types'
import ColumnType from '../ColumnEditor/ColumnType'
import InputWithSuggestions from '../ColumnEditor/InputWithSuggestions'
import { Suggestion } from '../ColumnEditor/ColumnEditor.types'
@@ -38,7 +39,7 @@ import { typeExpressionSuggestions } from '../ColumnEditor/ColumnEditor.constant
interface Props {
column: ColumnField
- enumTypes: EnumType[]
+ enumTypes: PostgresType[]
isNewRecord: boolean
hasImportContent: boolean
dragHandleProps?: any
@@ -49,7 +50,7 @@ interface Props {
const Column: FC<Props> = ({
column = {} as ColumnField,
- enumTypes = [] as EnumType[],
+ enumTypes = [] as PostgresType[],
isNewRecord = false,
hasImportContent = false,
dragHandleProps = {},
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ColumnManagement.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ColumnManagement.tsx
index 87c662797cd..b57774198c1 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ColumnManagement.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ColumnManagement.tsx
@@ -9,7 +9,12 @@ import {
IconTrash,
Typography,
} from '@supabase/ui'
-import { PostgresTable, PostgresColumn, PostgresRelationship } from '@supabase/postgres-meta'
+import {
+ PostgresTable,
+ PostgresColumn,
+ PostgresRelationship,
+ PostgresType,
+} from '@supabase/postgres-meta'
import {
DragDropContext,
Droppable,
@@ -22,7 +27,7 @@ import * as Tooltip from '@radix-ui/react-tooltip'
import Column from './Column'
import InformationBox from 'components/ui/InformationBox'
import ForeignKeySelector from '../ForeignKeySelector/ForeignKeySelector'
-import { ColumnField, EnumType } from '../SidePanelEditor.types'
+import { ColumnField } from '../SidePanelEditor.types'
import { ImportContent } from './TableEditor.types'
import { generateColumnField } from '../ColumnEditor/ColumnEditor.utils'
@@ -30,7 +35,7 @@ interface Props {
table?: Partial<PostgresTable>
tables: PostgresTable[]
columns?: ColumnField[]
- enumTypes: EnumType[]
+ enumTypes: PostgresType[]
importContent?: ImportContent
isNewRecord: boolean
onColumnsUpdated: (columns: ColumnField[]) => void
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
index fcf02df4032..e03b613e545 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
@@ -1,19 +1,14 @@
import { FC, useEffect, useState } from 'react'
import { isUndefined, isEmpty } from 'lodash'
import { Badge, Checkbox, SidePanel, Input } from '@supabase/ui'
-import { PostgresTable } from '@supabase/postgres-meta'
+import { PostgresTable, PostgresType } from '@supabase/postgres-meta'
import { useStore } from 'hooks'
import ActionBar from '../ActionBar'
import HeaderTitle from './HeaderTitle'
import ColumnManagement from './ColumnManagement'
import SpreadsheetImport from './SpreadsheetImport/SpreadsheetImport'
-import {
- ColumnField,
- EnumType,
- CreateTablePayload,
- UpdateTablePayload,
-} from '../SidePanelEditor.types'
+import { ColumnField, CreateTablePayload, UpdateTablePayload } from '../SidePanelEditor.types'
import { DEFAULT_COLUMNS } from './TableEditor.constants'
import { TableField, ImportContent } from './TableEditor.types'
import {
@@ -26,7 +21,7 @@ import {
interface Props {
table?: PostgresTable
tables: PostgresTable[]
- enumTypes: EnumType[]
+ enumTypes: PostgresType[]
selectedSchema: string
isDuplicating: boolean
visible: boolean
@@ -49,7 +44,7 @@ interface Props {
const TableEditor: FC<Props> = ({
table,
tables = [],
- enumTypes = [] as EnumType[],
+ enumTypes = [] as PostgresType[],
selectedSchema,
isDuplicating,
visible = false,
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
index 787635589bd..7a73bb73d18 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
@@ -36,6 +36,7 @@ const TableEditorLayout: FC<Props> = ({
if (ui.selectedProject) {
meta.schemas.load()
meta.tables.load()
+ meta.types.load()
}
}, [ui.selectedProject])
diff --git a/studio/pages/api/pg-meta/[ref]/types.ts b/studio/pages/api/pg-meta/[ref]/types.ts
new file mode 100644
index 00000000000..821152ed9cf
--- /dev/null
+++ b/studio/pages/api/pg-meta/[ref]/types.ts
@@ -0,0 +1,38 @@
+import { NextApiRequest, NextApiResponse } from 'next'
+import { PG_META_URL } from 'lib/constants'
+import apiWrapper from 'lib/api/apiWrapper'
+import { constructHeaders } from 'lib/api/apiHelpers'
+import { get } from 'lib/common/fetch'
+
+export default (req: NextApiRequest, res: NextApiResponse) =>
+ apiWrapper(req, res, handler, { withAuth: true })
+
+async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const { method } = req
+
+ switch (method) {
+ case 'GET':
+ return handleGetAll(req, res)
+ default:
+ res.setHeader('Allow', ['GET'])
+ res.status(405).json({ error: { message: `Method ${method} Not Allowed` } })
+ }
+}
+
+const handleGetAll = async (req: NextApiRequest, res: NextApiResponse) => {
+ const headers = constructHeaders(req.headers)
+ const response = await get(`${PG_META_URL}/types`, { headers })
+
+ const includeSystemSchemas = req.query['include_system_schemas'] === 'true'
+
+ if (response.error) {
+ return res.status(400).json({ error: response.error })
+ }
+
+ if (!includeSystemSchemas) {
+ const types = response?.filter((x: any) => x.schema === 'public')
+ return res.status(200).json(types)
+ }
+
+ return res.status(200).json(response)
+}
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index c625ac9339b..ad47c091ae7 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -1,6 +1,6 @@
import Papa from 'papaparse'
import { makeObservable, observable } from 'mobx'
-import { find, isUndefined, isEqual, isEmpty, chunk, maxBy } from 'lodash'
+import { find, isUndefined, isEqual, isEmpty, chunk } from 'lodash'
import { Query } from '@supabase/grid'
import {
PostgresColumn,
@@ -38,6 +38,7 @@ import PublicationStore, { IPublicationStore } from './PublicationStore'
import FunctionsStore from './FunctionsStore'
import HooksStore from './HooksStore'
import ExtensionsStore from './ExtensionsStore'
+import TypesStore from './TypesStore'
const BATCH_SIZE = 1000
const CHUNK_SIZE = 1024 * 1024 * 0.25 // 0.25MB
@@ -57,6 +58,7 @@ export interface IMetaStore {
functions: IPostgresMetaInterface<any>
extensions: IPostgresMetaInterface<any>
publications: IPublicationStore
+ types: IPostgresMetaInterface<any>
query: (value: string) => Promise<any | { error: ResponseError }>
validateQuery: (value: string) => Promise<any | { error: ResponseError }>
@@ -113,6 +115,7 @@ export default class MetaStore implements IMetaStore {
functions: FunctionsStore
extensions: ExtensionsStore
publications: PublicationStore
+ types: TypesStore
connectionString?: string
baseUrl: string
@@ -155,6 +158,7 @@ export default class MetaStore implements IMetaStore {
identifier: 'name',
})
this.publications = new PublicationStore(rootStore, `${this.baseUrl}/publications`, headers)
+ this.types = new TypesStore(rootStore, `${this.baseUrl}/types`, headers)
makeObservable(this, {
excludedSchemas: observable,
diff --git a/studio/stores/pgmeta/SchemaStore.ts b/studio/stores/pgmeta/SchemaStore.ts
index 208aa26e5c1..b8762efec64 100644
--- a/studio/stores/pgmeta/SchemaStore.ts
+++ b/studio/stores/pgmeta/SchemaStore.ts
@@ -6,8 +6,6 @@ import { ResponseError } from 'types'
export interface ISchemaStore extends IPostgresMetaInterface<PostgresSchema> {
getViews: (schema: string) => Promise<any | { error: ResponseError }>
- getEnums: () => Promise<any | { error: ResponseError }>
- // getEnumValues: (id: string) => Promise<any | { error: ResponseError }>
}
export default class SchemaStore extends PostgresMetaInterface<PostgresSchema> {
constructor(
@@ -29,15 +27,4 @@ export default class SchemaStore extends PostgresMetaInterface<PostgresSchema> {
`
return await this.rootStore.meta.query(query)
}
-
- async getEnums() {
- const query = `SELECT "oid" as "id", "typname" as "name" FROM pg_type WHERE typcategory = 'E';`
- return await this.rootStore.meta.query(query)
- }
-
- // [Joshen] I realised we probably don't need this (yet) for table editor
- // async getEnumValues(id: string) {
- // const query = `SELECT enumlabel FROM pg_enum WHERE enumtypid = ${id};`
- // return await this.rootStore.meta.query(query)
- // }
}
diff --git a/studio/stores/pgmeta/TypesStore.ts b/studio/stores/pgmeta/TypesStore.ts
new file mode 100644
index 00000000000..c7057dbc333
--- /dev/null
+++ b/studio/stores/pgmeta/TypesStore.ts
@@ -0,0 +1,16 @@
+import PostgresMetaInterface from '../common/PostgresMetaInterface'
+import { PostgresType } from '@supabase/postgres-meta'
+import { IRootStore } from '../RootStore'
+
+export default class TypesStore extends PostgresMetaInterface<PostgresType> {
+ constructor(
+ rootStore: IRootStore,
+ dataUrl: string,
+ headers?: {
+ [prop: string]: any
+ },
+ options?: { identifier: string }
+ ) {
+ super(rootStore, dataUrl, headers, options)
+ }
+}