Files
Joshen Lim 241f7bb721 Chore/shift manual queries into pg meta (#43692)
## Context

Related to FE-2557

Part of shifting manually written dashboard queries into
packages/pg-meta where
- pg-meta can be code owners of
- we can write tests for the queries 

This PR just shifts all the `.sql.ts` files that we previously created
into packages/pg-meta

There's still other areas where we need to shift over as well which I'll
address in subsequent PRs

## Notable changes

- `getTableRowsCountSql` -> Opted to shift `formatFilterValue` logic out
before calling this method (ref `table-rows-count-query`)
- `getDeleteOldCronJobRunDetailsByCtidSql` -> Opted to shift
`validatePageNumber` logic out before calling this method (ref
`CronJobsTab.useCleanupActions`)
2026-03-16 16:14:48 +07:00

56 lines
1.6 KiB
TypeScript

import { getIndexesSQL } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { UseCustomQueryOptions } from 'types'
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
import { databaseIndexesKeys } from './keys'
type GetIndexesArgs = {
schema?: string
}
export type DatabaseIndex = {
name: string
schema: string
table: string
definition: string
columns: string // Comma-separated strings
}
export type IndexesVariables = GetIndexesArgs & {
projectRef?: string
connectionString?: string | null
}
export async function getIndexes(
{ schema, projectRef, connectionString }: IndexesVariables,
signal?: AbortSignal
) {
if (!schema) {
throw new Error('schema is required')
}
const sql = getIndexesSQL({ schema })
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['indexes', schema] },
signal
)
return result as DatabaseIndex[]
}
export type IndexesData = Awaited<ReturnType<typeof getIndexes>>
export type IndexesError = ExecuteSqlError
export const useIndexesQuery = <TData = IndexesData>(
{ projectRef, connectionString, schema }: IndexesVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<IndexesData, IndexesError, TData> = {}
) =>
useQuery<IndexesData, IndexesError, TData>({
queryKey: databaseIndexesKeys.list(projectRef, schema),
queryFn: ({ signal }) => getIndexes({ projectRef, connectionString, schema }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof schema !== 'undefined',
...options,
})