import { getFDWsSql } from '@supabase/pg-meta' import { useQuery } from '@tanstack/react-query' import { fdwKeys } from './keys' import { executeSql } from '@/data/sql/execute-sql-mutation' import { EMPTY_ARR } from '@/lib/void' import { ResponseError, UseCustomQueryOptions } from '@/types' 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 (Array.isArray(result) ? result : EMPTY_ARR) as FDW[] } export type FDWsData = Awaited> export type FDWsError = ResponseError export const useFDWsQuery = ( { projectRef, connectionString }: FDWsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: fdwKeys.list(projectRef), queryFn: ({ signal }) => getFDWs({ projectRef, connectionString }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })