Files
supabase/apps/studio/data/tables/table-create-mutation.ts
Andrew Valleteau 31aad403de fix(studio): early fail query when x-connection-encrypted is invalid (#35331)
* fix(studio): early fail query when x-connection-encrypted is invalid

* fix(studio): uniformize readDatabase and projectDetails connString handling

* chore: update api types

* chore: add connectionString null option

* fix: only enforce x-connection-encrypted on platform

* chore: refactor connString check in a single point

* chore: fix guard logic

* chore: fix pgMetaGuard

* chore: fix types
2025-05-08 12:11:03 +02:00

74 lines
2.2 KiB
TypeScript

import type { PostgresTable } from '@supabase/postgres-meta'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import type { components } from 'data/api'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { tableKeys } from './keys'
export type CreateTableBody = components['schemas']['CreateTableBody']
export type TableCreateVariables = {
projectRef: string
connectionString?: string | null
// the schema is required field
payload: CreateTableBody & { schema: string }
}
export async function createTable({ projectRef, connectionString, payload }: TableCreateVariables) {
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const { data, error } = await post('/platform/pg-meta/{ref}/tables', {
params: {
header: { 'x-connection-encrypted': connectionString! },
path: { ref: projectRef },
},
body: payload,
headers,
})
if (error) handleError(error)
// [Alaister] we have to manually cast the data to PostgresTable
// because the API types are slightly wrong
return data as PostgresTable
}
type TableCreateData = Awaited<ReturnType<typeof createTable>>
export const useTableCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<TableCreateData, ResponseError, TableCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<TableCreateData, ResponseError, TableCreateVariables>(
(vars) => createTable(vars),
{
async onSuccess(data, variables, context) {
const { projectRef, payload } = variables
await Promise.all([
queryClient.invalidateQueries(tableKeys.list(projectRef, payload.schema, true)),
queryClient.invalidateQueries(tableKeys.list(projectRef, payload.schema, false)),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create database table: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}