Files
supabase/apps/studio/data/replication/create-tenant-source-mutation.ts
Raminder Singh 520428680d feat: call the new api endpoints to handle partial failures in pg_replicate (#35418)
* feat: use the new tenant-source api to atomically create the tenant and source in pg_replicate

* feat: use the new sink-pipeline api to atomically create the sink and pipeline in pg_replicate

* fix: remove some unused imports

* feat: use the new sink-pipeline api to atomically update the sink and pipeline in pg_replicate

* feat: deleting the sink cascades to delete the pipeline

* chore: update api types

* fix: revert accidental update to types

* remove unused code

* add a comment explaining why deleting only sink is enough
2025-05-10 12:04:37 +05:30

57 lines
1.6 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import type { ResponseError } from 'types'
import { replicationKeys } from './keys'
import { handleError, post } from 'data/fetchers'
export type CreateTenantSourceParams = {
projectRef: string
}
async function createTenantSource({ projectRef }: CreateTenantSourceParams, signal?: AbortSignal) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await post('/platform/replication/{ref}/tenants-sources', {
params: { path: { ref: projectRef } },
signal,
})
if (error) {
handleError(error)
}
return data
}
type CreateTenantSourceData = Awaited<ReturnType<typeof createTenantSource>>
export const useCreateTenantSourceMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<CreateTenantSourceData, ResponseError, CreateTenantSourceParams>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<CreateTenantSourceData, ResponseError, CreateTenantSourceParams>(
(vars) => createTenantSource(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(replicationKeys.sources(projectRef))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create tenant or source: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}