mirror of
https://github.com/supabase/supabase.git
synced 2026-05-24 12:47:48 +08:00
* Update database-triggers-query to use get from data/fetchers * Update database triggers mutation RQs to use methods from data/fetchers * Don't cd to the directory, use the --dir parameter of pnpm. --------- Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { handleError, post } from 'data/fetchers'
|
|
import type { ResponseError } from 'types'
|
|
import { databaseTriggerKeys } from './keys'
|
|
|
|
export type DatabaseTriggerCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string
|
|
payload: any
|
|
}
|
|
|
|
export async function createDatabaseTrigger({
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabaseTriggerCreateVariables) {
|
|
let headers = new Headers()
|
|
if (connectionString) headers.set('x-connection-encrypted', connectionString)
|
|
|
|
const { data, error } = await post('/platform/pg-meta/{ref}/triggers', {
|
|
params: {
|
|
header: { 'x-connection-encrypted': connectionString! },
|
|
path: { ref: projectRef },
|
|
},
|
|
body: payload,
|
|
headers,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type DatabaseTriggerCreateData = Awaited<ReturnType<typeof createDatabaseTrigger>>
|
|
|
|
export const useDatabaseTriggerCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<DatabaseTriggerCreateData, ResponseError, DatabaseTriggerCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseTriggerCreateData, ResponseError, DatabaseTriggerCreateVariables>(
|
|
(vars) => createDatabaseTrigger(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(databaseTriggerKeys.list(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database trigger: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|