Files
supabase/apps/studio/data/config/disk-breakdown-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

60 lines
1.6 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import type { ResponseError } from 'types'
import { configKeys } from './keys'
import { executeSql } from 'data/sql/execute-sql-query'
export type DiskBreakdownVariables = {
projectRef?: string
connectionString?: string | null
}
type DiskBreakdownResult = {
db_size_bytes: number
wal_size_bytes: number
}
export async function getDiskBreakdown(
{ projectRef, connectionString }: DiskBreakdownVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('Project ref is required')
if (!connectionString) throw new Error('Connection string is required')
const { result } = await executeSql(
{
projectRef,
connectionString,
sql: `
SELECT
(
SELECT
SUM(pg_database_size(pg_database.datname)) AS db_size_bytes
FROM
pg_database
),
(
SELECT SUM(size)
FROM
pg_ls_waldir()
) AS wal_size_bytes`,
},
signal
)
return result[0] as DiskBreakdownResult
}
export type DiskBreakdownData = Awaited<ReturnType<typeof getDiskBreakdown>>
export type DiskBreakdownError = ResponseError
export const useDiskBreakdownQuery = <TData = DiskBreakdownData>(
{ projectRef, connectionString }: DiskBreakdownVariables,
{ enabled = true, ...options }: UseQueryOptions<DiskBreakdownData, DiskBreakdownError, TData> = {}
) =>
useQuery<DiskBreakdownData, DiskBreakdownError, TData>(
configKeys.diskBreakdown(projectRef),
({ signal }) => getDiskBreakdown({ projectRef, connectionString }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)