mirror of
https://github.com/supabase/supabase.git
synced 2026-05-27 06:03:44 +08: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>
92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { PGColumn } from '@supabase/pg-meta/src/pg-meta-columns'
|
|
import { useMutation, UseMutationOptions } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import type { components } from 'data/api'
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError } from 'types'
|
|
|
|
export type UpdateColumnBody = Omit<
|
|
components['schemas']['UpdateColumnBody'],
|
|
'check' | 'comment'
|
|
> & {
|
|
check?: string | null
|
|
comment?: string | null
|
|
}
|
|
|
|
export type DatabaseColumnUpdateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
originalColumn: Pick<
|
|
PGColumn,
|
|
| 'id'
|
|
| 'name'
|
|
| 'schema'
|
|
| 'table'
|
|
| 'table_id'
|
|
| 'ordinal_position'
|
|
| 'is_identity'
|
|
| 'is_unique'
|
|
>
|
|
payload: UpdateColumnBody
|
|
}
|
|
|
|
export async function updateDatabaseColumn({
|
|
projectRef,
|
|
connectionString,
|
|
originalColumn,
|
|
payload,
|
|
}: DatabaseColumnUpdateVariables) {
|
|
const { sql } = pgMeta.columns.update(originalColumn, {
|
|
name: payload.name,
|
|
type: payload.type,
|
|
drop_default: payload.dropDefault,
|
|
default_value: payload.defaultValue,
|
|
default_value_format: payload.defaultValueFormat,
|
|
is_identity: payload.isIdentity,
|
|
identity_generation: payload.identityGeneration,
|
|
is_nullable: payload.isNullable,
|
|
is_unique: payload.isUnique,
|
|
comment: payload.comment,
|
|
check: payload.check,
|
|
})
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['column', 'update', originalColumn.id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseColumnUpdateData = Awaited<ReturnType<typeof updateDatabaseColumn>>
|
|
|
|
export const useDatabaseColumnUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<DatabaseColumnUpdateData, ResponseError, DatabaseColumnUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<DatabaseColumnUpdateData, ResponseError, DatabaseColumnUpdateVariables>(
|
|
(vars) => updateDatabaseColumn(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update database column: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|