mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 19:35:06 +03:00
## Context Just some clean up as I was going through stuff - `useExecuteSqlQuery` is deprecated and not used at all - As such `execute-sql-query` is technically irrelevant, the more relevant file is `execute-sql-mutation` - Hence opting to consolidate `execute-sql-query` into `execute-sql-mutation` - Also removing `ExecuteSqlError` since its just re-exporting the `ResponseError` type There's a lot of file changes but its essentially just updating the importing statements across the files
77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { tableKeys } from './keys'
|
|
import { getQueryClient } from '@/data/query-client'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { SafePostgresTable } from '@/lib/postgres-types'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type TablesVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
name: string
|
|
schema: string
|
|
}
|
|
|
|
export async function getTable(
|
|
{ projectRef, connectionString, name, schema }: TablesVariables,
|
|
signal?: AbortSignal
|
|
): Promise<SafePostgresTable> {
|
|
const { sql, zod } = pgMeta.tables.retrieve({ name, schema })
|
|
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: tableKeys.retrieve(projectRef, name, schema),
|
|
},
|
|
signal
|
|
)
|
|
// pg-meta sources `check` from pg_catalog; treat it as SafeSqlFragment for DDL composition.
|
|
return zod.parse(result[0]) as unknown as SafePostgresTable
|
|
}
|
|
|
|
export type RetrieveTableResult = Awaited<ReturnType<typeof getTable>>
|
|
export type RetrieveTableError = ResponseError
|
|
export type RetrievedTableColumn = NonNullable<RetrieveTableResult['columns']>[number]
|
|
|
|
export const useTableQuery = <TData = RetrieveTableResult>(
|
|
{ projectRef, connectionString, name, schema }: TablesVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<RetrieveTableResult, RetrieveTableError, TData> = {}
|
|
) => {
|
|
return useQuery<RetrieveTableResult, RetrieveTableError, TData>({
|
|
queryKey: tableKeys.retrieve(projectRef, name, schema),
|
|
queryFn: ({ signal }) => getTable({ projectRef, connectionString, name, schema }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Non-hook usage to fetch data + caching it into the store
|
|
*/
|
|
export const getTableQuery = async ({
|
|
projectRef,
|
|
name,
|
|
schema,
|
|
connectionString,
|
|
}: {
|
|
projectRef: string
|
|
name: string
|
|
schema: string
|
|
connectionString?: string | null
|
|
}) => {
|
|
const queryClient = getQueryClient()
|
|
const table = await queryClient.fetchQuery({
|
|
// eslint-disable-next-line @tanstack/query/exhaustive-deps
|
|
queryKey: tableKeys.retrieve(projectRef, name, schema),
|
|
queryFn: ({ signal }) => getTable({ projectRef, connectionString, name, schema }, signal),
|
|
})
|
|
return table
|
|
}
|