mirror of
https://github.com/supabase/supabase.git
synced 2026-09-07 02:20:52 +08:00
## 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
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { QueryClient, useQuery } from '@tanstack/react-query'
|
|
import { z } from 'zod'
|
|
|
|
import { databaseRoleKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type DatabaseRolesVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export type PgRole = z.infer<typeof pgMeta.roles.zod>
|
|
|
|
const pgMetaRolesList = pgMeta.roles.list()
|
|
|
|
export async function getDatabaseRoles(
|
|
{ projectRef, connectionString }: DatabaseRolesVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
const { result } = await executeSql(
|
|
{ projectRef, connectionString, sql: pgMetaRolesList.sql, queryKey: ['database-roles'] },
|
|
signal
|
|
)
|
|
|
|
return result as PgRole[]
|
|
}
|
|
|
|
export type DatabaseRolesData = z.infer<typeof pgMetaRolesList.zod>
|
|
export type DatabaseRolesError = ResponseError
|
|
|
|
export const useDatabaseRolesQuery = <TData = DatabaseRolesData>(
|
|
{ projectRef, connectionString }: DatabaseRolesVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabaseRolesData, DatabaseRolesError, TData> = {}
|
|
) =>
|
|
useQuery<DatabaseRolesData, DatabaseRolesError, TData>({
|
|
queryKey: databaseRoleKeys.databaseRoles(projectRef),
|
|
queryFn: ({ signal }) => getDatabaseRoles({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
|
|
export function invalidateRolesQuery(client: QueryClient, projectRef: string | undefined) {
|
|
return client.invalidateQueries({ queryKey: databaseRoleKeys.databaseRoles(projectRef) })
|
|
}
|