Files
supabase/apps/studio/data/privileges/exposed-function-counts-query.ts
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

63 lines
1.9 KiB
TypeScript

import { getExposedFunctionCountsSql } from '@supabase/pg-meta'
import { queryOptions } from '@tanstack/react-query'
import { executeSql } from 'data/sql/execute-sql-query'
import type { ResponseError } from 'types'
import { privilegeKeys } from './keys'
export type ExposedFunctionCountsVariables = {
projectRef?: string
connectionString?: string | null
selectedSchemas: string[]
}
export type ExposedFunctionCountsResponse = {
total_count: number
grants_count: number
}
export async function getExposedFunctionCounts(
{ projectRef, connectionString, selectedSchemas }: ExposedFunctionCountsVariables,
signal?: AbortSignal
): Promise<ExposedFunctionCountsResponse> {
if (!projectRef) throw new Error('projectRef is required')
if (!selectedSchemas) throw new Error('selectedSchemas is required')
const sql = getExposedFunctionCountsSql({ selectedSchemas })
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['exposed-function-counts', selectedSchemas],
},
signal
)
return result[0] as ExposedFunctionCountsResponse
}
export type ExposedFunctionCountsData = Awaited<ReturnType<typeof getExposedFunctionCounts>>
export type ExposedFunctionCountsError = ResponseError
export const exposedFunctionCountsQueryOptions = (
{ projectRef, connectionString, selectedSchemas }: ExposedFunctionCountsVariables,
{ enabled = true }: { enabled?: boolean } = {}
) => {
return queryOptions({
// eslint-disable-next-line @tanstack/query/exhaustive-deps -- connection string doesn't change the result of the query
queryKey: privilegeKeys.exposedFunctionCounts(projectRef, selectedSchemas),
queryFn: ({ signal }) =>
getExposedFunctionCounts(
{
projectRef,
connectionString,
selectedSchemas,
},
signal
),
enabled: enabled && typeof projectRef !== 'undefined',
})
}