Files
supabase/apps/studio/data/fdw/fdws-query.ts
Joshen Lim 98b1b79909 Chore/shift manual queries into pg meta 04 (#43956)
## Context

Shifts all remaining dashboard queries into pg-meta so that we
centralize all manually written queries in one place
Having them in packages/pg-meta also allows us to write tests for them

## To test

Just needs a smoke test on
- Role Impersonation
- Lints
- Data API
- Database
  - Enumerated Types
- Integrations
  - Foreign Data Wrappers
  - Vault
2026-03-24 16:23:13 +08:00

62 lines
1.4 KiB
TypeScript

import { getFDWsSql } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { UseCustomQueryOptions } from 'types'
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
import { fdwKeys } from './keys'
export type FDWColumn = {
name: string
type: string
}
export type FDWTable = {
id: string
name: string
schema: string
columns: FDWColumn[]
options: string[]
}
export type FDW = {
id: number
name: string
handler: string
server_name: string
server_options: string[] | null
tables: FDWTable[]
}
export type FDWsVariables = {
projectRef?: string
connectionString?: string | null
}
export async function getFDWs(
{ projectRef, connectionString }: FDWsVariables,
signal?: AbortSignal
) {
const sql = getFDWsSql()
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['fdws'] },
signal
)
return result as FDW[]
}
export type FDWsData = Awaited<ReturnType<typeof getFDWs>>
export type FDWsError = ExecuteSqlError
export const useFDWsQuery = <TData = FDWsData>(
{ projectRef, connectionString }: FDWsVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<FDWsData, FDWsError, TData> = {}
) =>
useQuery<FDWsData, FDWsError, TData>({
queryKey: fdwKeys.list(projectRef),
queryFn: ({ signal }) => getFDWs({ projectRef, connectionString }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})