mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55: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>
78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import type { components } from 'data/api'
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import { lintKeys } from 'data/lint/keys'
|
|
import { tableEditorKeys } from 'data/table-editor/keys'
|
|
import type { ResponseError } from 'types'
|
|
import { tableKeys } from './keys'
|
|
|
|
export type UpdateTableBody = components['schemas']['UpdateTableBody']
|
|
|
|
export type TableUpdateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
id: number
|
|
name: string
|
|
schema: string
|
|
payload: UpdateTableBody
|
|
}
|
|
|
|
export async function updateTable({
|
|
projectRef,
|
|
connectionString,
|
|
id,
|
|
name,
|
|
schema,
|
|
payload,
|
|
}: TableUpdateVariables) {
|
|
const { sql } = pgMeta.tables.update({ id, name, schema }, payload)
|
|
|
|
const { result } = await executeSql<void>({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['table', 'update', id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableUpdateData = Awaited<ReturnType<typeof updateTable>>
|
|
|
|
export const useTableUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<TableUpdateData, ResponseError, TableUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<TableUpdateData, ResponseError, TableUpdateVariables>(
|
|
(vars) => updateTable(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, schema, id } = variables
|
|
await Promise.all([
|
|
queryClient.invalidateQueries(tableEditorKeys.tableEditor(projectRef, id)),
|
|
queryClient.invalidateQueries(tableKeys.list(projectRef, schema)),
|
|
queryClient.invalidateQueries(lintKeys.lint(projectRef)),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update database table: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|