Files
supabase/apps/studio/data/read-replicas/replica-lag-query.ts
Joshen Lim 1baaded0bb Consolidate execute-sql-query into execute-sql-mutation (#46944)
## Context

Just some clean up as I was going through stuff
- `useExecuteSqlQuery` is deprecated and not used at all
- As such `execute-sql-query` is technically irrelevant, the more
relevant file is `execute-sql-mutation`
- Hence opting to consolidate `execute-sql-query` into
`execute-sql-mutation`
- Also removing `ExecuteSqlError` since its just re-exporting the
`ResponseError` type

There's a lot of file changes but its essentially just updating the
importing statements across the files
2026-06-16 00:07:16 +08:00

44 lines
1.4 KiB
TypeScript

import { replicationLagSql } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { replicaKeys } from './keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { ResponseError, UseCustomQueryOptions } from '@/types'
export type ReplicationLagVariables = {
id: string
projectRef?: string
connectionString?: string | null
}
export async function getReplicationLag(
{ projectRef, connectionString, id }: ReplicationLagVariables,
signal?: AbortSignal
) {
const sql = replicationLagSql()
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['replica-lag', id] },
signal
)
return Number((result[0] ?? null)?.physical_replica_lag_second ?? 0)
}
export type ReplicationLagData = Awaited<ReturnType<typeof getReplicationLag>>
export type ReplicationLagError = ResponseError
export const useReplicationLagQuery = <TData = ReplicationLagData>(
{ projectRef, connectionString, id }: ReplicationLagVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<ReplicationLagData, ReplicationLagError, TData> = {}
) =>
useQuery<ReplicationLagData, ReplicationLagError, TData>({
queryKey: replicaKeys.replicaLag(projectRef, id),
queryFn: ({ signal }) => getReplicationLag({ projectRef, connectionString, id }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined',
...options,
})