Files
supabase/apps/studio/data/table-editor/table-editor-query.ts
Joshen Lim 1baaded0bb Consolidate execute-sql-query into execute-sql-mutation (#46944)
## Context

Just some clean up as I was going through stuff
- `useExecuteSqlQuery` is deprecated and not used at all
- As such `execute-sql-query` is technically irrelevant, the more
relevant file is `execute-sql-mutation`
- Hence opting to consolidate `execute-sql-query` into
`execute-sql-mutation`
- Also removing `ExecuteSqlError` since its just re-exporting the
`ResponseError` type

There's a lot of file changes but its essentially just updating the
importing statements across the files
2026-06-16 00:07:16 +08:00

77 lines
2.2 KiB
TypeScript

import { getTableEditorSql } from '@supabase/pg-meta'
import { QueryClient, queryOptions, useQuery } from '@tanstack/react-query'
import { tableEditorKeys } from './keys'
import { Entity } from './table-editor-types'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { ResponseError, UseCustomQueryOptions } from '@/types'
type TableEditorArgs = {
id?: number
}
export type TableEditorVariables = TableEditorArgs & {
projectRef?: string
connectionString?: string | null
}
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 ?? null) as Entity | undefined
}
export type TableEditorData = Awaited<ReturnType<typeof getTableEditor>>
export type TableEditorError = ResponseError
export const useTableEditorQuery = <TData = TableEditorData>(
{ projectRef, connectionString, id }: TableEditorVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<TableEditorData, TableEditorError, TData> = {}
) =>
useQuery<TableEditorData, TableEditorError, TData>({
...tableEditorQueryOptions({ projectRef, connectionString, id }),
enabled:
enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined' && !isNaN(id),
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: 5 * 60 * 1000,
...options,
})
export function prefetchTableEditor(
client: QueryClient,
{ projectRef, connectionString, id }: TableEditorVariables
) {
return client.fetchQuery(tableEditorQueryOptions({ projectRef, connectionString, id }))
}
export const tableEditorQueryOptions = <TData = TableEditorData>({
projectRef,
connectionString,
id,
}: TableEditorVariables) => {
return queryOptions<TableEditorData, TableEditorError, TData>({
queryKey: tableEditorKeys.tableEditor(projectRef, id),
queryFn: ({ signal }) => getTableEditor({ projectRef, connectionString, id }, signal),
})
}