mirror of
https://github.com/supabase/supabase.git
synced 2026-07-03 12:14:26 +08:00
* 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
57 lines
1.6 KiB
TypeScript
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,
|
|
}
|
|
)
|
|
}
|