Files
supabase/apps/studio/data/database/max-connections-query.ts
Andrew Valleteau 31aad403de fix(studio): early fail query when x-connection-encrypted is invalid (#35331)
* fix(studio): early fail query when x-connection-encrypted is invalid

* fix(studio): uniformize readDatabase and projectDetails connString handling

* chore: update api types

* chore: add connectionString null option

* fix: only enforce x-connection-encrypted on platform

* chore: refactor connString check in a single point

* chore: fix guard logic

* chore: fix pgMetaGuard

* chore: fix types
2025-05-08 12:11:03 +02:00

52 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
import { databaseKeys } from './keys'
export const getMaxConnectionsSql = () => {
const sql = /* SQL */ `show max_connections`
return sql
}
export type MaxConnectionsVariables = {
projectRef?: string
connectionString?: string | null
table?: string
schema?: string
}
export async function getMaxConnections(
{ projectRef, connectionString }: MaxConnectionsVariables,
signal?: AbortSignal
) {
const sql = getMaxConnectionsSql()
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['max-connections'] },
signal
)
const connections = parseInt(result[0].max_connections)
return { maxConnections: connections }
}
export type MaxConnectionsData = Awaited<ReturnType<typeof getMaxConnections>>
export type MaxConnectionsError = ExecuteSqlError
export const useMaxConnectionsQuery = <TData = MaxConnectionsData>(
{ projectRef, connectionString }: MaxConnectionsVariables,
{
enabled = true,
...options
}: UseQueryOptions<MaxConnectionsData, MaxConnectionsError, TData> = {}
) =>
useQuery<MaxConnectionsData, MaxConnectionsError, TData>(
databaseKeys.maxConnections(projectRef),
({ signal }) => getMaxConnections({ projectRef, connectionString }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)