Files
supabase/apps/studio/data/config/disk-breakdown-query.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

64 lines
1.6 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { executeSql } from 'data/sql/execute-sql-query'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { configKeys } from './keys'
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
}: UseCustomQueryOptions<DiskBreakdownData, DiskBreakdownError, TData> = {}
) =>
useQuery<DiskBreakdownData, DiskBreakdownError, TData>({
queryKey: configKeys.diskBreakdown(projectRef),
queryFn: ({ signal }) => getDiskBreakdown({ projectRef, connectionString }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})