Files
supabase/studio/data/foreign-tables/foreign-table-query.ts
T
aaa0ba9685 Sync studio -> master (#17691)
* [hotfix studio] Update toast text (#17648)

Update ConsentToast.tsx

* fix: nav org redirect (#17651)

fix: nav v2 org redirect

* hotfix: wrappers tables bug (#17682)

Fix: Resolve null.length TypeError in WrapperRow.tsx (#17679)

* Fix: Resolved null.length TypeError in WrapperRow.tsx

* prettier

* switch to &&

---------

Co-authored-by: Monica Khoury <99693443+monicakh@users.noreply.github.com>

* Fix missing connection string when fetching foreign table (#17690)

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
Co-authored-by: Monica Khoury <99693443+monicakh@users.noreply.github.com>
2023-09-25 06:20:56 +00:00

58 lines
1.7 KiB
TypeScript

import { PostgresTable } from '@supabase/postgres-meta'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { foreignTableKeys } from './keys'
export type ForeignTableVariables = {
id?: number
projectRef?: string
connectionString?: string
}
export type ForeignTable = PostgresTable
export type ForeignTableResponse = ForeignTable | { error?: any }
export async function getForeignTable(
{ id, projectRef, connectionString }: ForeignTableVariables,
signal?: AbortSignal
) {
if (!projectRef) {
throw new Error('projectRef is required')
}
if (!id) {
throw new Error('id is required')
}
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const response = (await get(`${API_URL}/pg-meta/${projectRef}/foreign-tables?id=${id}`, {
headers: Object.fromEntries(headers),
signal,
})) as ForeignTableResponse
if ('error' in response) {
throw response.error
}
return response as PostgresTable
}
export type ForeignTableData = Awaited<ReturnType<typeof getForeignTable>>
export type ForeignTableError = unknown
export const useForeignTableQuery = <TData = ForeignTableData>(
{ projectRef, connectionString, id }: ForeignTableVariables,
{ enabled = true, ...options }: UseQueryOptions<ForeignTableData, ForeignTableError, TData> = {}
) =>
useQuery<ForeignTableData, ForeignTableError, TData>(
foreignTableKeys.foreignTable(projectRef, id),
({ signal }) => getForeignTable({ projectRef, connectionString, id }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined',
...options,
}
)