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>
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useQuery, type UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError } from 'types'
|
|
import { tableKeys } from './keys'
|
|
|
|
export type TablesVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
name: string
|
|
schema: string
|
|
}
|
|
|
|
export async function getTable(
|
|
{ projectRef, connectionString, name, schema }: TablesVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
const { sql, zod } = pgMeta.tables.retrieve({ name, schema })
|
|
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: tableKeys.retrieve(projectRef, name, schema),
|
|
},
|
|
signal
|
|
)
|
|
return zod.parse(result[0])
|
|
}
|
|
|
|
export type RetrieveTableResult = Awaited<ReturnType<typeof getTable>>
|
|
export type RetrieveTableError = ResponseError
|
|
export type RetrievedTableColumn = NonNullable<RetrieveTableResult['columns']>[number]
|
|
|
|
export const useTablesQuery = <TData = RetrieveTableResult>(
|
|
{ projectRef, connectionString, name, schema }: TablesVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<RetrieveTableResult, RetrieveTableError, TData> = {}
|
|
) => {
|
|
return useQuery<RetrieveTableResult, RetrieveTableError, TData>(
|
|
tableKeys.retrieve(projectRef, name, schema),
|
|
({ signal }) => getTable({ projectRef, connectionString, name, schema }, signal),
|
|
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
|
|
)
|
|
}
|