Files
supabase/apps/studio/data/tables/table-retrieve-query.ts
T
b09440bd47 fix: move table create update delete to query route (#35662)
* 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>
2025-05-20 10:34:59 +08:00

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 }
)
}