Files
supabase/apps/studio/data/tables/table-query.ts
T
Alaister YoungandJoshen Lim 5781937739 chore: aggressive prefetching (#29987)
* chore: aggressive prefetching

* use abort signals in prefetchers

* move encrypted columns to react-query

* prefetching for filter and sort applied

* prefetch remaining entity types

* prefetch tables in more places

* prefetch editor page on project panel

* add feature flag

* fix typescript

* nit

* Nit

* fix imports

* remove views check on encrypted schemas

* use fetchQuery instead of prefetchQuery

* fix useEncryptedColumnsQuery ts error

* filter by schema on encrypted columns

* don't use pg_get_tabledef for foreign tables

* Remove unnecesary import

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-10-24 20:59:35 +08:00

67 lines
1.9 KiB
TypeScript

import type { PostgresTable } from '@supabase/postgres-meta'
import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { tableKeys } from './keys'
export type TableVariables = {
id?: number
projectRef?: string
connectionString?: string
}
export type Table = PostgresTable
export type TableResponse = Table | { error?: any }
export async function getTable(
{ id, projectRef, connectionString }: TableVariables,
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 { data, error } = await get('/platform/pg-meta/{ref}/tables', {
params: {
header: { 'x-connection-encrypted': connectionString! },
path: { ref: projectRef },
// @ts-ignore
query: { id: id.toString() },
},
headers,
signal,
})
if (error) handleError(error)
return data as unknown as PostgresTable
}
export type TableData = Awaited<ReturnType<typeof getTable>>
export type TableError = unknown
export const useTableQuery = <TData = TableData>(
{ projectRef, connectionString, id }: TableVariables,
{ enabled = true, ...options }: UseQueryOptions<TableData, TableError, TData> = {}
) =>
useQuery<TableData, TableError, TData>(
tableKeys.table(projectRef, id),
({ signal }) => getTable({ projectRef, connectionString, id }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined',
staleTime: 0,
...options,
}
)
export function prefetchTable(
client: QueryClient,
{ projectRef, connectionString, id }: TableVariables
) {
return client.fetchQuery(tableKeys.table(projectRef, id), ({ signal }) =>
getTable({ id, projectRef, connectionString }, signal)
)
}