mirror of
https://github.com/supabase/supabase.git
synced 2026-07-04 16:24:25 +08:00
* 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
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
import { z } from 'zod'
|
|
|
|
import { executeSql, ExecuteSqlError } from 'data/sql/execute-sql-query'
|
|
import { databaseKeys } from './keys'
|
|
|
|
export type SchemasVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export type Schema = z.infer<typeof pgMeta.schemas.zod>
|
|
|
|
const pgMetaSchemasList = pgMeta.schemas.list()
|
|
|
|
export type SchemasData = z.infer<typeof pgMetaSchemasList.zod>
|
|
export type SchemasError = ExecuteSqlError
|
|
|
|
export async function getSchemas(
|
|
{ projectRef, connectionString }: SchemasVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql: pgMetaSchemasList.sql,
|
|
queryKey: ['schemas'],
|
|
},
|
|
signal
|
|
)
|
|
|
|
return result
|
|
}
|
|
|
|
export const useSchemasQuery = <TData = SchemasData>(
|
|
{ projectRef, connectionString }: SchemasVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<SchemasData, SchemasError, TData> = {}
|
|
) =>
|
|
useQuery<SchemasData, SchemasError, TData>(
|
|
databaseKeys.schemas(projectRef),
|
|
({ signal }) => getSchemas({ projectRef, connectionString }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
}
|
|
)
|
|
|
|
export function invalidateSchemasQuery(client: QueryClient, projectRef: string | undefined) {
|
|
return client.invalidateQueries(databaseKeys.schemas(projectRef))
|
|
}
|
|
|
|
export function prefetchSchemas(
|
|
client: QueryClient,
|
|
{ projectRef, connectionString }: SchemasVariables
|
|
) {
|
|
return client.fetchQuery(databaseKeys.schemas(projectRef), ({ signal }) =>
|
|
getSchemas({ projectRef, connectionString }, signal)
|
|
)
|
|
}
|