mirror of
https://github.com/supabase/supabase.git
synced 2026-09-09 03:19:36 +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
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { invalidateRolesQuery } from './database-roles-query'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
type DropRoleBody = Parameters<typeof pgMeta.roles.remove>[1]
|
|
|
|
export type DatabaseRoleDeleteVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
id: number
|
|
payload?: DropRoleBody
|
|
}
|
|
|
|
export async function deleteDatabaseRole({
|
|
projectRef,
|
|
connectionString,
|
|
id,
|
|
payload,
|
|
}: DatabaseRoleDeleteVariables) {
|
|
const sql = pgMeta.roles.remove({ id }, payload).sql
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['roles', 'delete'],
|
|
})
|
|
return result
|
|
}
|
|
|
|
type DatabaseRoleDeleteData = Awaited<ReturnType<typeof deleteDatabaseRole>>
|
|
|
|
export const useDatabaseRoleDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseRoleDeleteData, ResponseError, DatabaseRoleDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseRoleDeleteData, ResponseError, DatabaseRoleDeleteVariables>({
|
|
mutationFn: (vars) => deleteDatabaseRole(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await invalidateRolesQuery(queryClient, projectRef)
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete database role: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|