mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 20:35:07 +03: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.
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
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 { databaseIndexesKeys } from './keys'
|
|
|
|
export type DatabaseIndexDeleteVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
name: string
|
|
schema: string
|
|
}
|
|
|
|
export async function deleteDatabaseIndex({
|
|
projectRef,
|
|
connectionString,
|
|
name,
|
|
schema,
|
|
}: DatabaseIndexDeleteVariables) {
|
|
const sql = `drop index if exists "${schema}"."${name}"`
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['indexes'],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseIndexDeleteData = Awaited<ReturnType<typeof deleteDatabaseIndex>>
|
|
|
|
export const useDatabaseIndexDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseIndexDeleteData, ResponseError, DatabaseIndexDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseIndexDeleteData, ResponseError, DatabaseIndexDeleteVariables>({
|
|
mutationFn: (vars) => deleteDatabaseIndex(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseIndexesKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete database index: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|