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
83 lines
2.1 KiB
TypeScript
83 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 ColumnPrivilegesGrant = {
|
|
column_id: string
|
|
grantee: string
|
|
privilege_type: 'ALL' | 'SELECT' | 'INSERT' | 'UPDATE' | 'REFERENCES'
|
|
is_grantable?: boolean
|
|
}
|
|
|
|
export type ColumnPrivilegesGrantVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
grants: ColumnPrivilegesGrant[]
|
|
}
|
|
|
|
export async function grantColumnPrivileges({
|
|
projectRef,
|
|
connectionString,
|
|
grants,
|
|
}: ColumnPrivilegesGrantVariables) {
|
|
const { sql } = pgMeta.columnPrivileges.grant(
|
|
grants.map((g) => ({
|
|
columnId: g.column_id,
|
|
grantee: g.grantee,
|
|
privilegeType: g.privilege_type,
|
|
isGrantable: g.is_grantable,
|
|
}))
|
|
)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['column-privileges', 'grant'],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type ColumnPrivilegesGrantData = Awaited<ReturnType<typeof grantColumnPrivileges>>
|
|
|
|
export const useColumnPrivilegesGrantMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
ColumnPrivilegesGrantData,
|
|
ResponseError,
|
|
ColumnPrivilegesGrantVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<ColumnPrivilegesGrantData, ResponseError, ColumnPrivilegesGrantVariables>({
|
|
mutationFn: (vars) => grantColumnPrivileges(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,
|
|
})
|
|
}
|