mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 12:25:05 +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.
72 lines
1.9 KiB
TypeScript
72 lines
1.9 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 { databaseQueuesKeys } from './keys'
|
|
|
|
export type DatabaseQueueMessageSendVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
queueName: string
|
|
payload: string
|
|
delay: number
|
|
}
|
|
|
|
export async function sendDatabaseQueueMessage({
|
|
projectRef,
|
|
connectionString,
|
|
queueName,
|
|
payload,
|
|
delay,
|
|
}: DatabaseQueueMessageSendVariables) {
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: `select * from pgmq.send( '${queueName}', '${payload}', ${delay})`,
|
|
queryKey: databaseQueuesKeys.create(),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseQueueMessageSendData = Awaited<ReturnType<typeof sendDatabaseQueueMessage>>
|
|
|
|
export const useDatabaseQueueMessageSendMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseQueueMessageSendData,
|
|
ResponseError,
|
|
DatabaseQueueMessageSendVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<
|
|
DatabaseQueueMessageSendData,
|
|
ResponseError,
|
|
DatabaseQueueMessageSendVariables
|
|
>({
|
|
mutationFn: (vars) => sendDatabaseQueueMessage(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, queueName } = variables
|
|
await queryClient.invalidateQueries({
|
|
queryKey: databaseQueuesKeys.getMessagesInfinite(projectRef, queueName),
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to send database queue message: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|