mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03: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
85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databasePublicationsKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabasePublicationUpdateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
id: number
|
|
tables?: string[]
|
|
publish_insert?: boolean
|
|
publish_update?: boolean
|
|
publish_delete?: boolean
|
|
publish_truncate?: boolean
|
|
}
|
|
|
|
export async function updateDatabasePublication({
|
|
projectRef,
|
|
connectionString,
|
|
id,
|
|
tables,
|
|
publish_insert,
|
|
publish_update,
|
|
publish_delete,
|
|
publish_truncate,
|
|
}: DatabasePublicationUpdateVariables) {
|
|
const { sql } = pgMeta.publications.update(id, {
|
|
tables,
|
|
publish_insert,
|
|
publish_update,
|
|
publish_delete,
|
|
publish_truncate,
|
|
})
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['publication', 'update', id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabasePublicationUpdateData = Awaited<ReturnType<typeof updateDatabasePublication>>
|
|
|
|
export const useDatabasePublicationUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabasePublicationUpdateData,
|
|
ResponseError,
|
|
DatabasePublicationUpdateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<
|
|
DatabasePublicationUpdateData,
|
|
ResponseError,
|
|
DatabasePublicationUpdateVariables
|
|
>({
|
|
mutationFn: (vars) => updateDatabasePublication(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databasePublicationsKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update database publication: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|