mirror of
https://github.com/supabase/supabase.git
synced 2026-09-09 03:19:36 +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
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { getCreatePublicationSQL } from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from '../sql/execute-sql-mutation'
|
|
import { replicationKeys } from './keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type CreatePublicationParams = {
|
|
projectRef: string
|
|
sourceId: number
|
|
name: string
|
|
tables: { schema: string; name: string }[]
|
|
connectionString?: string | null
|
|
}
|
|
|
|
async function createPublication(
|
|
{ projectRef, connectionString, name, tables }: CreatePublicationParams,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const sql = getCreatePublicationSQL({ name, tables })
|
|
const { result } = await executeSql({ projectRef, connectionString, sql }, signal)
|
|
|
|
return result
|
|
}
|
|
|
|
type CreatePublicationData = Awaited<ReturnType<typeof createPublication>>
|
|
|
|
export const useCreatePublicationMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<CreatePublicationData, ResponseError, CreatePublicationParams>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<CreatePublicationData, ResponseError, CreatePublicationParams>({
|
|
mutationFn: (vars) => createPublication(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, sourceId } = variables
|
|
await queryClient.invalidateQueries({
|
|
queryKey: replicationKeys.publications(projectRef, sourceId),
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create publication: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|