Files
supabase/apps/studio/data/privileges/default-privileges-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

59 lines
1.7 KiB
TypeScript

import { getDefaultPrivilegesStateSql } 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 DefaultPrivilegesVariables = {
projectRef?: string
connectionString?: string | null
schema?: string
}
export async function getDefaultPrivilegesState(
{ projectRef, connectionString, schema }: DefaultPrivilegesVariables,
signal?: AbortSignal
): Promise<boolean> {
if (!projectRef) throw new Error('projectRef is required')
const sql = getDefaultPrivilegesStateSql({ schema })
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['default-privileges-state'],
},
signal
)
const grantCount = (result[0] as { grant_count: number }).grant_count
return grantCount === 3
}
export type DefaultPrivilegesData = Awaited<ReturnType<typeof getDefaultPrivilegesState>>
export type DefaultPrivilegesError = ResponseError
export const defaultPrivilegesQueryOptions = (
{ projectRef, connectionString, schema }: DefaultPrivilegesVariables,
{ 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.defaultPrivileges(projectRef, schema),
queryFn: ({ signal }) =>
getDefaultPrivilegesState(
{
projectRef,
connectionString,
schema,
},
signal
),
enabled: enabled && typeof projectRef !== 'undefined',
})
}