Files
supabase/apps/studio/data/foreign-tables/foreign-tables-query.ts
T
Alaister YoungandJoshen Lim 6c592dec99 chore: remove useExecuteSqlQuery() part 2 (#30467)
* foreign-key-constraints

* update entity-types stale time

* schemas query

* deprecate useExecuteSqlQuery

* users count query

* database size query

* indexes query

* keywords query

* migrations query

* table columns

* database functions

* database roles query

* fdws query

* replication lag query

* ongoing queries query

* vault secrets query

* remove unneeded staleTime: 0

* max connections query

* fix entity types key in tests

* Some fixes

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-11-18 05:15:37 +00:00

55 lines
1.7 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { PostgresView } from '@supabase/postgres-meta'
import { get, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { foreignTableKeys } from './keys'
export type ForeignTablesVariables = {
projectRef?: string
connectionString?: string
schema?: string
}
export async function getForeignTables(
{ projectRef, connectionString, schema }: ForeignTablesVariables,
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}/foreign-tables', {
params: {
header: { 'x-connection-encrypted': connectionString! },
path: { ref: projectRef },
query: {
included_schemas: schema || '',
include_columns: true,
} as any,
},
headers,
signal,
})
if (error) handleError(error)
return data as PostgresView[]
}
export type ForeignTablesData = Awaited<ReturnType<typeof getForeignTables>>
export type ForeignTablesError = ResponseError
export const useForeignTablesQuery = <TData = ForeignTablesData>(
{ projectRef, connectionString, schema }: ForeignTablesVariables,
{ enabled = true, ...options }: UseQueryOptions<ForeignTablesData, ForeignTablesError, TData> = {}
) =>
useQuery<ForeignTablesData, ForeignTablesError, TData>(
schema ? foreignTableKeys.listBySchema(projectRef, schema) : foreignTableKeys.list(projectRef),
({ signal }) => getForeignTables({ projectRef, connectionString, schema }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)