mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03: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
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databasePoliciesKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabasePolicyDeleteVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
originalPolicy: {
|
|
id: number
|
|
name: string
|
|
schema: string
|
|
table: string
|
|
}
|
|
}
|
|
|
|
export async function deleteDatabasePolicy({
|
|
projectRef,
|
|
connectionString,
|
|
originalPolicy,
|
|
}: DatabasePolicyDeleteVariables) {
|
|
let headers = new Headers()
|
|
if (connectionString) headers.set('x-connection-encrypted', connectionString)
|
|
|
|
const { sql } = pgMeta.policies.remove(originalPolicy)
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['policy', 'delete', originalPolicy.id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabasePolicyDeleteData = Awaited<ReturnType<typeof deleteDatabasePolicy>>
|
|
|
|
export const useDatabasePolicyDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabasePolicyDeleteData, ResponseError, DatabasePolicyDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabasePolicyDeleteData, ResponseError, DatabasePolicyDeleteVariables>({
|
|
mutationFn: (vars) => deleteDatabasePolicy(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databasePoliciesKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete database policy: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|