mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Refactor / dependency cleanup. ## What is the current behavior? `apps/studio` lists both `@supabase/pg-meta` (workspace package) as a runtime dep and `@supabase/postgres-meta` (external npm package, `^0.64.4`) as a devDependency. The external package is used only for type imports across 44 files — there is no runtime usage and no codegen pipeline that needs it. ## What is the new behavior? Every `Postgres*` type import (`PostgresTable`, `PostgresColumn`, `PostgresPolicy`, `PostgresTrigger`, `PostgresView`, `PostgresMaterializedView`, `PostgresForeignTable`, `PostgresSchema`, `PostgresPublication`, `PostgresRelationship`, `PostgresPrimaryKey`) is replaced with its `PG*` counterpart from `@supabase/pg-meta`, and the external dep is removed from \`apps/studio/package.json\`. Top-level type re-exports were added to \`packages/pg-meta/src/index.ts\` so consumers can import directly from the package root. Two latent issues surfaced by the stricter pg-meta types are also fixed: - \`data/foreign-tables/foreign-tables-query.ts\` was casting foreign-table results as \`PostgresView[]\`; corrected to \`PGForeignTable[]\`. - \`pg-meta\`'s \`PGTrigger\` Zod schema declared \`orientation\`/\`activation\` as \`z.string()\`, inconsistent with pg-meta's own \`getDatabaseTriggerUpdateSQL\` helper that requires the narrow literal unions; tightened to \`z.enum\`. ## Additional context <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Updated internal TypeScript type definitions across the codebase to use the latest type system from `@supabase/pg-meta`. * Removed `@supabase/postgres-meta` dependency. * Enhanced type validation for database triggers and schemas to enforce stricter constraints. [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45844) <!-- end of auto-generated comment: release notes by coderabbit.ai -->
125 lines
3.7 KiB
TypeScript
125 lines
3.7 KiB
TypeScript
import type { PGTable } from '@supabase/pg-meta'
|
|
import { DEFAULT_PLATFORM_APPLICATION_NAME } from '@supabase/pg-meta/src/constants'
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { sortBy } from 'lodash'
|
|
import { useCallback } from 'react'
|
|
|
|
import { tableKeys } from './keys'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type TablesVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
schema?: string
|
|
/**
|
|
* Defaults to false
|
|
*/
|
|
includeColumns?: boolean
|
|
sortByProperty?: keyof PGTable
|
|
}
|
|
|
|
export async function getTables(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
schema,
|
|
includeColumns = false,
|
|
sortByProperty = 'name',
|
|
}: TablesVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) {
|
|
throw new Error('projectRef is required')
|
|
}
|
|
|
|
let headers = new Headers()
|
|
if (connectionString) headers.set('x-connection-encrypted', connectionString)
|
|
|
|
let queryParams: Record<string, string> = {
|
|
//include_columns is a string, even though it's true or false
|
|
include_columns: `${includeColumns}`,
|
|
}
|
|
if (schema) {
|
|
queryParams.included_schemas = schema
|
|
}
|
|
|
|
const { data, error } = await get('/platform/pg-meta/{ref}/tables', {
|
|
params: {
|
|
header: {
|
|
'x-connection-encrypted': connectionString!,
|
|
'x-pg-application-name': DEFAULT_PLATFORM_APPLICATION_NAME,
|
|
},
|
|
path: { ref: projectRef },
|
|
query: queryParams as any,
|
|
},
|
|
headers,
|
|
signal,
|
|
})
|
|
|
|
if (!Array.isArray(data) && error) handleError(error)
|
|
|
|
// Sort the data if the sortByName option is true
|
|
if (Array.isArray(data) && sortByProperty) {
|
|
return sortBy(data, (t) => t[sortByProperty]) as PGTable[]
|
|
}
|
|
|
|
return data as PGTable[]
|
|
}
|
|
|
|
export type TablesData = Awaited<ReturnType<typeof getTables>>
|
|
export type TablesError = ResponseError
|
|
|
|
export const useTablesQuery = <TData = TablesData>(
|
|
{ projectRef, connectionString, schema, includeColumns }: TablesVariables,
|
|
{ enabled = true, ...options }: UseCustomQueryOptions<TablesData, TablesError, TData> = {}
|
|
) => {
|
|
return useQuery<TablesData, TablesError, TData>({
|
|
queryKey: tableKeys.list(projectRef, schema, includeColumns),
|
|
queryFn: ({ signal }) =>
|
|
getTables({ projectRef, connectionString, schema, includeColumns }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* useGetTables
|
|
* Tries to get tables from the react-query cache, or loads it from the server if it's not cached.
|
|
*/
|
|
export function useGetTables({
|
|
projectRef,
|
|
connectionString,
|
|
}: Pick<TablesVariables, 'projectRef' | 'connectionString'>) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useCallback(
|
|
(schema?: TablesVariables['schema'], includeColumns?: TablesVariables['includeColumns']) => {
|
|
return queryClient.fetchQuery({
|
|
queryKey: tableKeys.list(projectRef, schema, includeColumns),
|
|
queryFn: ({ signal }) =>
|
|
getTables({ projectRef, connectionString, schema, includeColumns }, signal),
|
|
})
|
|
},
|
|
[connectionString, projectRef, queryClient]
|
|
)
|
|
}
|
|
|
|
export function usePrefetchTables({
|
|
projectRef,
|
|
connectionString,
|
|
}: Pick<TablesVariables, 'projectRef' | 'connectionString'>) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useCallback(
|
|
(schema?: TablesVariables['schema'], includeColumns?: TablesVariables['includeColumns']) => {
|
|
return queryClient.prefetchQuery({
|
|
queryKey: tableKeys.list(projectRef, schema, includeColumns),
|
|
queryFn: ({ signal }) =>
|
|
getTables({ projectRef, connectionString, schema, includeColumns }, signal),
|
|
})
|
|
},
|
|
[connectionString, projectRef, queryClient]
|
|
)
|
|
}
|