import { getQueuesExposePostgrestStatusSQL } from '@supabase/pg-meta' import { useQuery } from '@tanstack/react-query' import { databaseQueuesKeys } from './keys' import { unguarded } from '@/data/fetchers' import { executeSql } from '@/data/sql/execute-sql-mutation' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type DatabaseQueuesVariables = { projectRef?: string connectionString?: string | null } export async function getDatabaseQueuesExposePostgrestStatus({ projectRef, connectionString, }: DatabaseQueuesVariables) { if (!projectRef) throw new Error('Project ref is required') const sql = getQueuesExposePostgrestStatusSQL() const { result } = await executeSql({ projectRef, connectionString, sqlOrigin: 'studio', queryName: 'queues-postgrest-expose', sql, queryKey: ['queues-postgrest-expose'], grant: unguarded.migrating, }) return result[0].exists as boolean } export type DatabaseQueueData = boolean export type DatabaseQueueError = StudioError export const useQueuesExposePostgrestStatusQuery = ( { projectRef, connectionString }: DatabaseQueuesVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: databaseQueuesKeys.exposePostgrestStatus(projectRef), queryFn: () => getDatabaseQueuesExposePostgrestStatus({ projectRef, connectionString }), enabled: enabled && typeof projectRef !== 'undefined', ...options, })