mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +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
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import type { SafeSqlFragment } from '@supabase/pg-meta/src/pg-format'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseEventTriggerKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseEventTriggerCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
sql: SafeSqlFragment
|
|
}
|
|
|
|
export async function createDatabaseEventTrigger({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
}: DatabaseEventTriggerCreateVariables) {
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['event-trigger', 'create'],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseEventTriggerCreateData = Awaited<ReturnType<typeof createDatabaseEventTrigger>>
|
|
|
|
export const useDatabaseEventTriggerCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseEventTriggerCreateData,
|
|
ResponseError,
|
|
DatabaseEventTriggerCreateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<
|
|
DatabaseEventTriggerCreateData,
|
|
ResponseError,
|
|
DatabaseEventTriggerCreateVariables
|
|
>({
|
|
mutationFn: (vars) => createDatabaseEventTrigger(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseEventTriggerKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create event trigger: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|