Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.6 KB
Raw Blame
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,
  })