Files
supabase/apps/studio/data/database-indexes/indexes-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

56 lines
1.6 KiB
TypeScript

import { getIndexesSQL } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { databaseIndexesKeys } from './keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { ResponseError, UseCustomQueryOptions } from '@/types'
type GetIndexesArgs = {
schema?: string
}
export type DatabaseIndex = {
name: string
schema: string
table: string
definition: string
columns: string // Comma-separated strings
}
export type IndexesVariables = GetIndexesArgs & {
projectRef?: string
connectionString?: string | null
}
export async function getIndexes(
{ schema, projectRef, connectionString }: IndexesVariables,
signal?: AbortSignal
) {
if (!schema) {
throw new Error('schema is required')
}
const sql = getIndexesSQL({ schema })
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['indexes', schema] },
signal
)
return result as DatabaseIndex[]
}
export type IndexesData = Awaited<ReturnType<typeof getIndexes>>
export type IndexesError = ResponseError
export const useIndexesQuery = <TData = IndexesData>(
{ projectRef, connectionString, schema }: IndexesVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<IndexesData, IndexesError, TData> = {}
) =>
useQuery<IndexesData, IndexesError, TData>({
queryKey: databaseIndexesKeys.list(projectRef, schema),
queryFn: ({ signal }) => getIndexes({ projectRef, connectionString, schema }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof schema !== 'undefined',
...options,
})