mirror of
https://github.com/supabase/supabase.git
synced 2026-05-18 19:06:50 +08:00
* Add custom types for queries, mutations and infinite queries. * Migrate all queries to use the new type. * Migrate all infinite queries to useCustomInfiniteQueryOptions. * Migrate all mutations to use useCustomMutationOptions. * Add type to all imports in `types` folder.
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
import { invalidateRolesQuery } from './database-roles-query'
|
|
|
|
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,
|
|
})
|
|
}
|