mirror of
https://github.com/supabase/supabase.git
synced 2026-05-08 18:00:20 -04:00
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { PGTriggerUpdate } from '@supabase/pg-meta/src/pg-meta-triggers'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseTriggerKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseTriggerUpdateVariables = {
|
|
originalTrigger: {
|
|
id: number
|
|
name: string
|
|
schema: string
|
|
table: string
|
|
}
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
payload: PGTriggerUpdate
|
|
}
|
|
|
|
export async function updateDatabaseTrigger({
|
|
originalTrigger,
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabaseTriggerUpdateVariables) {
|
|
const { sql } = pgMeta.triggers.update(originalTrigger, payload)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['trigger', 'update', originalTrigger.id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseTriggerUpdateData = Awaited<ReturnType<typeof updateDatabaseTrigger>>
|
|
|
|
export const useDatabaseTriggerUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseTriggerUpdateData,
|
|
ResponseError,
|
|
DatabaseTriggerUpdateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseTriggerUpdateData, ResponseError, DatabaseTriggerUpdateVariables>({
|
|
mutationFn: (vars) => updateDatabaseTrigger(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseTriggerKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update database trigger: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|