Files
supabase/apps/studio/data/table-editor/table-editor-query.ts
T
Alaister Young 0e3bc5804b chore: remove useExecuteSqlQuery (part 1) (#30437)
* chore: remove useExecuteSqlQuery part 1

* fix invalidation

* fixes

* more fixes

* I fixed it, but tested on preview instead of local 🤦🏻

* only refetch table rows and not count on row updates

* removed unneeded invalidations and prefetched new table after create
2024-11-14 15:21:29 +08:00

72 lines
1.9 KiB
TypeScript

import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
import { tableEditorKeys } from './keys'
import { getTableEditorSql } from './table-editor-query-sql'
import { Entity } from './table-editor-types'
type TableEditorArgs = {
id?: number
}
export type TableEditorVariables = TableEditorArgs & {
projectRef?: string
connectionString?: string
}
export async function getTableEditor(
{ projectRef, connectionString, id }: TableEditorVariables,
signal?: AbortSignal
) {
if (!id) {
throw new Error('id is required')
}
const sql = getTableEditorSql(id)
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['table-editor', id],
},
signal
)
return (result[0]?.entity ?? undefined) as Entity | undefined
}
export type TableEditorData = Awaited<ReturnType<typeof getTableEditor>>
export type TableEditorError = ExecuteSqlError
export const useTableEditorQuery = <TData = TableEditorData>(
{ projectRef, connectionString, id }: TableEditorVariables,
{ enabled = true, ...options }: UseQueryOptions<TableEditorData, TableEditorError, TData> = {}
) =>
useQuery<TableEditorData, TableEditorError, TData>(
tableEditorKeys.tableEditor(projectRef, id),
({ signal }) => getTableEditor({ projectRef, connectionString, id }, signal),
{
enabled:
enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined' && !isNaN(id),
staleTime: 5 * 60 * 1000, // 5 minutes
...options,
}
)
export function prefetchTableEditor(
client: QueryClient,
{ projectRef, connectionString, id }: TableEditorVariables
) {
return client.fetchQuery(tableEditorKeys.tableEditor(projectRef, id), ({ signal }) =>
getTableEditor(
{
projectRef,
connectionString,
id,
},
signal
)
)
}