Files
supabase/apps/studio/data/database-triggers/database-triggers-query.ts
Joshen Lim 4901f081e5 Migrate remaining requests to pg-meta API to use query endpoint (#47758)
## 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 -->
2026-07-09 17:09:03 +08:00

80 lines
2.6 KiB
TypeScript

import pgMeta, { type PGTrigger } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { executeSql } from '../sql/execute-sql-mutation'
import { databaseTriggerKeys } from './keys'
import type { PostgresTrigger } from '@/components/interfaces/Database/Triggers/TriggersList/TriggerList.utils'
import type { ResponseError, UseCustomQueryOptions } from '@/types'
function markSavedTriggerSafe(trigger: DatabaseTriggersData[number]): PostgresTrigger {
return trigger as PostgresTrigger
}
export type DatabaseTriggersVariables = {
projectRef?: string
connectionString?: string | null
schemas?: string[]
}
export async function getDatabaseTriggers(
{ projectRef, connectionString, schemas }: DatabaseTriggersVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { sql } = pgMeta.triggers.list({ includedSchemas: schemas })
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['triggers'],
},
signal
)
return result as PGTrigger[]
}
export type DatabaseTriggersData = Awaited<ReturnType<typeof getDatabaseTriggers>>
export type DatabaseTriggersError = ResponseError
export const useDatabaseHooksQuery = <TData = DatabaseTriggersData>(
{ projectRef, connectionString, schemas }: DatabaseTriggersVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<DatabaseTriggersData, DatabaseTriggersError, TData> = {}
) =>
useQuery<DatabaseTriggersData, DatabaseTriggersError, TData>({
queryKey: databaseTriggerKeys.list(projectRef, schemas),
queryFn: ({ signal }) => getDatabaseTriggers({ projectRef, connectionString }, signal),
select: (data) => {
return data.filter((trigger) => {
return (
trigger.function_schema === 'supabase_functions' &&
(trigger.schema !== 'net' || trigger.function_args.length === 0)
)
}) as any
},
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})
export const useDatabaseTriggersQuery = <TData = PostgresTrigger[]>(
{ projectRef, connectionString }: DatabaseTriggersVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<PostgresTrigger[], DatabaseTriggersError, TData> = {}
) =>
useQuery<PostgresTrigger[], DatabaseTriggersError, TData>({
queryKey: databaseTriggerKeys.list(projectRef),
queryFn: ({ signal }) =>
getDatabaseTriggers({ projectRef, connectionString }, signal).then((data) =>
data.map(markSavedTriggerSafe)
),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})