mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
## Context Migrates the remaining API requests to the pg-meta endpoint to use the query endpoint directly with the SQL from the pg-meta package. This touches the following: - policies - publications - triggers - views - materialized views - types ## To test Just need to verify that we're still fetching the data correctly on these pages - Database policies - Database publications - Database triggers - Database tables (views + materialized views) - Database types <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved and stabilized loading of database metadata (views, triggers, RLS policies, publications, materialized views, and enum types), including more reliable schema-scoped filtering. * Updated policy loading behavior and related UI queries to consistently use schema arrays, improving cache correctness and consistency. * **Tests** * Updated end-to-end test synchronization to wait for the correct metadata responses using more specific request identifiers. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
import pgMeta, { type PGPublication } from '@supabase/pg-meta'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { executeSql } from '../sql/execute-sql-mutation'
|
|
import { databasePublicationsKeys } from './keys'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type DatabasePublicationsVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export async function getDatabasePublications(
|
|
{ projectRef, connectionString }: DatabasePublicationsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { sql } = pgMeta.publications.list()
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['publications'].filter(Boolean),
|
|
},
|
|
signal
|
|
)
|
|
|
|
return result as PGPublication[]
|
|
}
|
|
|
|
export type DatabasePublicationsData = Awaited<ReturnType<typeof getDatabasePublications>>
|
|
export type DatabasePublicationsError = ResponseError
|
|
|
|
export const useDatabasePublicationsQuery = <TData = DatabasePublicationsData>(
|
|
{ projectRef, connectionString }: DatabasePublicationsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabasePublicationsData, DatabasePublicationsError, TData> = {}
|
|
) =>
|
|
useQuery<DatabasePublicationsData, DatabasePublicationsError, TData>({
|
|
queryKey: databasePublicationsKeys.list(projectRef),
|
|
queryFn: ({ signal }) => getDatabasePublications({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
|
|
export const useIsTableRealtimeEnabled = ({ id }: { id: number }) => {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { data: publications } = useDatabasePublicationsQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
})
|
|
const realtimePublication = (publications ?? []).find(
|
|
(publication) => publication.name === 'supabase_realtime'
|
|
)
|
|
const realtimeEnabledTables = realtimePublication?.tables ?? []
|
|
const isRealtimeEnabled = realtimeEnabledTables.some((t) => t.id === id)
|
|
return isRealtimeEnabled
|
|
}
|