mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 10:25:06 +03:00
* fix: move table create update delete to query route * chore: implement query to fetch a single table * fix: retrieve table after update * chore: assign type to update table payload * chore: use updated table columns for edit * chore: make executeSql castable with generic (#35685) * Chore/refactor derivate more types from queries (#35687) * chore: make executeSql castable with generic * chore: derivate types from performed queries - It allows to decouple more the frontend logic and the pg-meta/sql-query logic allowing to reduce the number of cast and get closer types between what we do fetch and what we expect in our components * fix: remove existing check * chore: handle null comment and check * fix: format check name as identifier --------- Co-authored-by: avallete <andrew.valleteau@supabase.io> Co-authored-by: Andrew Valleteau <avallete@users.noreply.github.com>
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { entityTypeKeys } from 'data/entity-types/keys'
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import { tableEditorKeys } from 'data/table-editor/keys'
|
|
import { viewKeys } from 'data/views/keys'
|
|
import type { ResponseError } from 'types'
|
|
import { tableKeys } from './keys'
|
|
|
|
export type TableDeleteVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
id: number
|
|
name: string
|
|
schema: string
|
|
cascade?: boolean
|
|
}
|
|
|
|
export async function deleteTable({
|
|
projectRef,
|
|
connectionString,
|
|
id,
|
|
name,
|
|
schema,
|
|
cascade = false,
|
|
}: TableDeleteVariables) {
|
|
const { sql } = pgMeta.tables.remove({ name, schema }, { cascade })
|
|
|
|
const { result } = await executeSql<void>({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['table', 'delete', id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableDeleteData = Awaited<ReturnType<typeof deleteTable>>
|
|
|
|
export const useTableDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<TableDeleteData, ResponseError, TableDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<TableDeleteData, ResponseError, TableDeleteVariables>(
|
|
(vars) => deleteTable(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { id, projectRef, schema } = variables
|
|
await Promise.all([
|
|
queryClient.invalidateQueries(tableEditorKeys.tableEditor(projectRef, id)),
|
|
queryClient.invalidateQueries(tableKeys.list(projectRef, schema)),
|
|
queryClient.invalidateQueries(entityTypeKeys.list(projectRef)),
|
|
// invalidate all views from this schema
|
|
queryClient.invalidateQueries(viewKeys.listBySchema(projectRef, schema)),
|
|
])
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete database table: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|