mirror of
https://github.com/supabase/supabase.git
synced 2026-05-10 17:11:21 +08: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.
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import { tableKeys } from 'data/tables/keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
import { databaseQueuesKeys } from './keys'
|
|
|
|
export type DatabaseQueueCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
name: string
|
|
type: 'basic' | 'partitioned' | 'unlogged'
|
|
enableRls: boolean
|
|
configuration?: {
|
|
partitionInterval?: number
|
|
retentionInterval?: number
|
|
}
|
|
}
|
|
|
|
export async function createDatabaseQueue({
|
|
projectRef,
|
|
connectionString,
|
|
name,
|
|
type,
|
|
enableRls,
|
|
configuration,
|
|
}: DatabaseQueueCreateVariables) {
|
|
const { partitionInterval, retentionInterval } = configuration ?? {}
|
|
|
|
const query =
|
|
type === 'partitioned'
|
|
? `select from pgmq.create_partitioned('${name}', '${partitionInterval}', '${retentionInterval}');`
|
|
: type === 'unlogged'
|
|
? `SELECT pgmq.create_unlogged('${name}');`
|
|
: `SELECT pgmq.create('${name}');`
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: `${query} ${enableRls ? `alter table pgmq."q_${name}" enable row level security;` : ''}`.trim(),
|
|
queryKey: databaseQueuesKeys.create(),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseQueueCreateData = Awaited<ReturnType<typeof createDatabaseQueue>>
|
|
|
|
export const useDatabaseQueueCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>({
|
|
mutationFn: (vars) => createDatabaseQueue(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseQueuesKeys.list(projectRef) })
|
|
queryClient.invalidateQueries({ queryKey: tableKeys.list(projectRef, 'pgmq') })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database queue: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|