Skip to content

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

1.5 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'

import {
  getExposedTables,
  type ExposedTable,
  type ExposedTablesError,
  type ExposedTablesVariables,
} from './exposed-tables-infinite-query'
import { privilegeKeys } from './keys'
import type { UseCustomQueryOptions } from '@/types'

// The API Docs panel renders every table without virtualization, so we fetch
// the full grant-status list in a single request rather than paginating.
const ALL_EXPOSED_ENTITIES_LIMIT = 100_000

export type ExposedTablesAllData = ExposedTable[]

async function getAllExposedTables(
  { projectRef, connectionString }: ExposedTablesVariables,
  signal?: AbortSignal
): Promise<ExposedTablesAllData> {
  const { tables } = await getExposedTables(
    { projectRef, connectionString, page: 0, limit: ALL_EXPOSED_ENTITIES_LIMIT },
    signal
  )
  return tables
}

/**
 * Returns the grant status (`granted` | `custom` | `revoked`) for every table,
 * used to filter unexposed tables out of the autogenerated Data API docs.
 */
export const useExposedTablesQuery = <TData = ExposedTablesAllData>(
  { projectRef, connectionString }: ExposedTablesVariables,
  {
    enabled = true,
    ...options
  }: UseCustomQueryOptions<ExposedTablesAllData, ExposedTablesError, TData> = {}
) =>
  useQuery<ExposedTablesAllData, ExposedTablesError, TData>({
    queryKey: privilegeKeys.exposedTablesAll(projectRef),
    queryFn: ({ signal }) => getAllExposedTables({ projectRef, connectionString }, signal),
    enabled: enabled && typeof projectRef !== 'undefined',
    ...options,
  })