Skip to content

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

Chore/column editor show types from extensions schema (#19580)

Joshen Lim18840b5e3b80
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 2a752289161..bf57225add1 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -1,4 +1,4 @@
-import type { PostgresColumn, PostgresTable, PostgresType } from '@supabase/postgres-meta'
+import type { PostgresColumn, PostgresTable } from '@supabase/postgres-meta'
import { useParams } from 'common'
import { isEmpty, noop } from 'lodash'
import Link from 'next/link'
@@ -21,8 +21,9 @@ import { Dictionary } from 'components/grid'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { FormSection, FormSectionContent, FormSectionLabel } from 'components/ui/Forms'
import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'
+import { usePostgresTypesQuery } from 'data/database/types-query'
import { useStore } from 'hooks'
-import { EXCLUDED_SCHEMAS } from 'lib/constants/schemas'
+import { EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS } from 'lib/constants/schemas'
import { ForeignKeySelector } from '..'
import ActionBar from '../ActionBar'
import { TEXT_TYPES } from '../SidePanelEditor.constants'
@@ -71,13 +72,21 @@ const ColumnEditor = ({
updateEditorDirty = noop,
}: ColumnEditorProps) => {
const { ref } = useParams()
- const { meta, vault } = useStore()
+ const { vault } = useStore()
const { project } = useProjectContext()
const [errors, setErrors] = useState<Dictionary<any>>({})
const [columnFields, setColumnFields] = useState<ColumnField>()
const [isEditingRelation, setIsEditingRelation] = useState<boolean>(false)
+ const { data: types } = usePostgresTypesQuery({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ })
+ const enumTypes = (types ?? []).filter(
+ (type) => !EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS.includes(type.schema)
+ )
+
const { data } = useForeignKeyConstraintsQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
@@ -86,7 +95,6 @@ const ColumnEditor = ({
const foreignKeyMeta = data || []
const keys = vault.listKeys()
- const enumTypes = meta.types.list((type: PostgresType) => !EXCLUDED_SCHEMAS.includes(type.schema))
const isNewRecord = column === undefined
const originalForeignKey = column
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx
index 38e3bcd0732..ada75e45011 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnType.tsx
@@ -121,7 +121,7 @@ const ColumnType = ({
*/}
{enumTypes.length > 0 ? (
<Listbox.Option disabled key="header-1" value="header-1" label="header-1">
- Enumerated Types
+ Other Data Types
</Listbox.Option>
) : (
<></>
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
index 35d05e2f264..d33a33781e5 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
@@ -1,4 +1,4 @@
-import type { PostgresTable, PostgresType } from '@supabase/postgres-meta'
+import type { PostgresTable } from '@supabase/postgres-meta'
import { isEmpty, isUndefined, noop } from 'lodash'
import Link from 'next/link'
import { useEffect, useState } from 'react'
@@ -6,8 +6,9 @@ import { useEffect, useState } from 'react'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import ConfirmationModal from 'components/ui/ConfirmationModal'
import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'
+import { usePostgresTypesQuery } from 'data/database/types-query'
import { useIsFeatureEnabled, useStore } from 'hooks'
-import { EXCLUDED_SCHEMAS } from 'lib/constants/schemas'
+import { EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS } from 'lib/constants/schemas'
import { useTableEditorStateSnapshot } from 'state/table-editor'
import { Alert, Badge, Button, Checkbox, IconBookOpen, Input, Modal, SidePanel } from 'ui'
import { SpreadsheetImport } from '../'
@@ -60,8 +61,13 @@ const TableEditor = ({
const isNewRecord = isUndefined(table)
const realtimeEnabled = useIsFeatureEnabled('realtime:all')
-
- const enumTypes = meta.types.list((type: PostgresType) => !EXCLUDED_SCHEMAS.includes(type.schema))
+ const { data: types } = usePostgresTypesQuery({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ })
+ const enumTypes = (types ?? []).filter(
+ (type) => !EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS.includes(type.schema)
+ )
const publications = meta.publications.list()
const realtimePublication = publications.find(
diff --git a/apps/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/apps/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
index 05d3e3f7a35..af69babd757 100644
--- a/apps/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
+++ b/apps/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
@@ -18,7 +18,6 @@ const TableEditorLayout = ({ children }: PropsWithChildren<{}>) => {
useEffect(() => {
if (ui.selectedProjectRef) {
- meta.types.load()
meta.policies.load()
meta.publications.load()
meta.extensions.load()
diff --git a/apps/studio/data/database/keys.ts b/apps/studio/data/database/keys.ts
index dbaccaee8f9..8135460f3f3 100644
--- a/apps/studio/data/database/keys.ts
+++ b/apps/studio/data/database/keys.ts
@@ -1,5 +1,6 @@
export const databaseKeys = {
schemaList: (projectRef: string | undefined) => ['projects', projectRef, 'schemas'] as const,
+ postgresTypes: (projectRef: string | undefined) => ['projects', projectRef, 'types'] as const,
backups: (projectRef: string | undefined) => [projectRef, 'database', 'backups'] as const,
poolingConfiguration: (projectRef: string | undefined) =>
[projectRef, 'database', 'pooling-configuration'] as const,
diff --git a/apps/studio/data/database/types-query.ts b/apps/studio/data/database/types-query.ts
new file mode 100644
index 00000000000..caf65f226d0
--- /dev/null
+++ b/apps/studio/data/database/types-query.ts
@@ -0,0 +1,57 @@
+import { PostgresType } from '@supabase/postgres-meta'
+import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
+
+import { get } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { databaseKeys } from './keys'
+
+export type PostgresTypesVariables = {
+ projectRef?: string
+ connectionString?: string
+}
+
+export async function getPostgresTypes(
+ { projectRef, connectionString }: PostgresTypesVariables,
+ signal?: AbortSignal
+) {
+ if (!projectRef) {
+ throw new Error('projectRef is required')
+ }
+
+ let headers = new Headers()
+ if (connectionString) headers.set('x-connection-encrypted', connectionString)
+
+ const { data, error } = await get('/platform/pg-meta/{ref}/types', {
+ // @ts-ignore [Joshen] query param is not required
+ params: {
+ header: {
+ 'x-connection-encrypted': connectionString!,
+ },
+ path: {
+ ref: projectRef,
+ },
+ },
+ headers: Object.fromEntries(headers),
+ signal,
+ })
+
+ if (error) throw error
+ return data as PostgresType[]
+}
+
+export type PostgresTypesData = Awaited<ReturnType<typeof getPostgresTypes>>
+export type PostgresTypesError = ResponseError
+
+export const usePostgresTypesQuery = <TData = PostgresTypesData>(
+ { projectRef, connectionString }: PostgresTypesVariables,
+ { enabled = true, ...options }: UseQueryOptions<PostgresTypesData, PostgresTypesError, TData> = {}
+) =>
+ useQuery<PostgresTypesData, PostgresTypesError, TData>(
+ databaseKeys.postgresTypes(projectRef),
+ ({ signal }) => getPostgresTypes({ projectRef, connectionString }, signal),
+ { enabled: enabled && typeof projectRef !== 'undefined', ...options }
+ )
+
+export function invalidatePostgresTypesQuery(client: QueryClient, projectRef: string | undefined) {
+ return client.invalidateQueries(databaseKeys.postgresTypes(projectRef))
+}
diff --git a/apps/studio/lib/constants/schemas.ts b/apps/studio/lib/constants/schemas.ts
index 5f773e13b88..7f6504cf986 100644
--- a/apps/studio/lib/constants/schemas.ts
+++ b/apps/studio/lib/constants/schemas.ts
@@ -18,3 +18,7 @@ export const EXCLUDED_SCHEMAS = [
'graphql',
'graphql_public',
]
+
+export const EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS = EXCLUDED_SCHEMAS.filter(
+ (x) => x !== 'extensions'
+)
diff --git a/apps/studio/pages/project/[ref]/database/tables.tsx b/apps/studio/pages/project/[ref]/database/tables.tsx
index 329b39895fb..ca627652cb4 100644
--- a/apps/studio/pages/project/[ref]/database/tables.tsx
+++ b/apps/studio/pages/project/[ref]/database/tables.tsx
@@ -1,6 +1,5 @@
import { useParams } from 'common'
-import { observer } from 'mobx-react-lite'
-import { useEffect, useState } from 'react'
+import { useState } from 'react'
import { ColumnList, TableList } from 'components/interfaces/Database'
import { SidePanelEditor } from 'components/interfaces/TableGridEditor'
@@ -8,12 +7,10 @@ import DeleteConfirmationDialogs from 'components/interfaces/TableGridEditor/Del
import { DatabaseLayout } from 'components/layouts'
import { ScaffoldContainer, ScaffoldSection } from 'components/layouts/Scaffold'
import { Table } from 'data/tables/table-query'
-import { useStore } from 'hooks'
import { useTableEditorStateSnapshot } from 'state/table-editor'
import { NextPageWithLayout } from 'types'
const DatabaseTables: NextPageWithLayout = () => {
- const { ui, meta } = useStore()
const { ref: projectRef } = useParams()
const snap = useTableEditorStateSnapshot()
@@ -23,12 +20,6 @@ const DatabaseTables: NextPageWithLayout = () => {
// since selectedTable above handles the state for ColumnList
const [selectedTableToEdit, setSelectedTableToEdit] = useState<Table>()
- useEffect(() => {
- if (ui.selectedProjectRef) {
- meta.types.load()
- }
- }, [ui.selectedProjectRef])
-
return (
<>
<ScaffoldContainer>
@@ -68,4 +59,4 @@ const DatabaseTables: NextPageWithLayout = () => {
DatabaseTables.getLayout = (page) => <DatabaseLayout title="Database">{page}</DatabaseLayout>
-export default observer(DatabaseTables)
+export default DatabaseTables
diff --git a/apps/studio/stores/pgmeta/MetaStore.ts b/apps/studio/stores/pgmeta/MetaStore.ts
index 4ffa8eee86b..55ba1460d40 100644
--- a/apps/studio/stores/pgmeta/MetaStore.ts
+++ b/apps/studio/stores/pgmeta/MetaStore.ts
@@ -47,7 +47,6 @@ import PoliciesStore from './PoliciesStore'
import PublicationStore from './PublicationStore'
import RolesStore, { IRolesStore } from './RolesStore'
import TriggersStore from './TriggersStore'
-import TypesStore from './TypesStore'
import ViewStore, { IViewStore } from './ViewStore'
const BATCH_SIZE = 1000
@@ -68,7 +67,6 @@ export interface IMetaStore {
functions: IPostgresMetaInterface<any>
extensions: IPostgresMetaInterface<any>
publications: IPostgresMetaInterface<any>
- types: IPostgresMetaInterface<any>
projectRef?: string
@@ -160,7 +158,6 @@ export default class MetaStore implements IMetaStore {
functions: FunctionsStore
extensions: ExtensionsStore
publications: PublicationStore
- types: TypesStore
projectRef?: string
connectionString?: string
@@ -215,7 +212,6 @@ export default class MetaStore implements IMetaStore {
`${this.baseUrl}/publications`,
this.headers
)
- this.types = new TypesStore(this.rootStore, `${this.baseUrl}/types`, this.headers)
makeObservable(this, {})
}
@@ -1019,8 +1015,5 @@ export default class MetaStore implements IMetaStore {
this.publications.setUrl(`${this.baseUrl}/publications`)
this.publications.setHeaders(this.headers)
-
- this.types.setUrl(`${this.baseUrl}/types`)
- this.types.setHeaders(this.headers)
}
}
diff --git a/apps/studio/stores/pgmeta/TypesStore.ts b/apps/studio/stores/pgmeta/TypesStore.ts
deleted file mode 100644
index 1b507f1be03..00000000000
--- a/apps/studio/stores/pgmeta/TypesStore.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import PostgresMetaInterface from '../common/PostgresMetaInterface'
-import type { 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)
- }
-}