mirror of
https://github.com/supabase/supabase.git
synced 2026-09-22 13:37:53 +08:00
1baaded0bb
## 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
81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { privilegeKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type ColumnPrivilegesRevoke = {
|
|
column_id: string
|
|
grantee: string
|
|
privilege_type: 'ALL' | 'SELECT' | 'INSERT' | 'UPDATE' | 'REFERENCES'
|
|
}
|
|
|
|
export type ColumnPrivilegesRevokeVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
revokes: ColumnPrivilegesRevoke[]
|
|
}
|
|
|
|
export async function revokeColumnPrivileges({
|
|
projectRef,
|
|
connectionString,
|
|
revokes,
|
|
}: ColumnPrivilegesRevokeVariables) {
|
|
const { sql } = pgMeta.columnPrivileges.revoke(
|
|
revokes.map((r) => ({
|
|
columnId: r.column_id,
|
|
grantee: r.grantee,
|
|
privilegeType: r.privilege_type,
|
|
}))
|
|
)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['column-privileges', 'revoke'],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type ColumnPrivilegesRevokeData = Awaited<ReturnType<typeof revokeColumnPrivileges>>
|
|
|
|
export const useColumnPrivilegesRevokeMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
ColumnPrivilegesRevokeData,
|
|
ResponseError,
|
|
ColumnPrivilegesRevokeVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<ColumnPrivilegesRevokeData, ResponseError, ColumnPrivilegesRevokeVariables>({
|
|
mutationFn: (vars) => revokeColumnPrivileges(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: privilegeKeys.columnPrivilegesList(projectRef) }),
|
|
])
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to mutate: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|