mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
151 lines
4.7 KiB
TypeScript
151 lines
4.7 KiB
TypeScript
import { getTableRowsCountSql } from '@supabase/pg-meta'
|
|
import { formatPatternMatchFilterValue } from '@supabase/pg-meta/src/query/table-row-query'
|
|
import { QueryClient, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { IS_PLATFORM } from 'common'
|
|
|
|
import { tableRowKeys } from './keys'
|
|
import { parseSupaTable } from '@/components/grid/SupabaseGrid.utils'
|
|
import type { Filter, SupaTable } from '@/components/grid/types'
|
|
import { unguarded } from '@/data/fetchers'
|
|
import { useConnectionStringForReadOps } from '@/data/read-replicas/replicas-query'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { prefetchTableEditor } from '@/data/table-editor/table-editor-query'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { RoleImpersonationState, wrapWithRoleImpersonation } from '@/lib/role-impersonation'
|
|
import { isRoleImpersonationEnabled } from '@/state/role-impersonation-state'
|
|
import { UseCustomQueryOptions } from '@/types'
|
|
|
|
export type GetTableRowsCountArgs = {
|
|
table?: SupaTable
|
|
filters?: Filter[]
|
|
enforceExactCount?: boolean
|
|
}
|
|
|
|
export type TableRowsCount = {
|
|
count?: number
|
|
is_estimate?: boolean
|
|
}
|
|
|
|
export type TableRowsCountVariables = Omit<GetTableRowsCountArgs, 'table'> & {
|
|
queryClient: QueryClient
|
|
tableId?: number
|
|
roleImpersonationState?: RoleImpersonationState
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export type TableRowsCountData = TableRowsCount
|
|
export type TableRowsCountError = StudioError
|
|
|
|
export async function getTableRowsCount(
|
|
{
|
|
queryClient,
|
|
projectRef,
|
|
connectionString,
|
|
tableId,
|
|
filters,
|
|
roleImpersonationState,
|
|
enforceExactCount,
|
|
isReadOnlyContext = false,
|
|
}: TableRowsCountVariables & { isReadOnlyContext?: boolean },
|
|
signal?: AbortSignal
|
|
) {
|
|
const entity = await prefetchTableEditor(queryClient, {
|
|
projectRef,
|
|
connectionString,
|
|
id: tableId,
|
|
})
|
|
if (!entity) {
|
|
throw new Error('Table not found')
|
|
}
|
|
|
|
const table = parseSupaTable(entity)
|
|
|
|
const formattedFilters = filters?.map((x) => ({
|
|
...x,
|
|
value: formatPatternMatchFilterValue(x.value, x.operator),
|
|
}))
|
|
const sql = wrapWithRoleImpersonation(
|
|
getTableRowsCountSql({
|
|
table,
|
|
filters: formattedFilters,
|
|
enforceExactCount,
|
|
isReadOnlyContext,
|
|
}),
|
|
roleImpersonationState
|
|
)
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['table-rows-count', table.id],
|
|
isRoleImpersonationEnabled: isRoleImpersonationEnabled(roleImpersonationState?.role),
|
|
grant: unguarded.migrating,
|
|
},
|
|
signal
|
|
)
|
|
|
|
return {
|
|
count: result?.[0]?.count,
|
|
is_estimate: result?.[0]?.is_estimate ?? false,
|
|
} as TableRowsCount
|
|
}
|
|
|
|
export const useTableRowsCountQuery = <TData = TableRowsCountData>(
|
|
{
|
|
projectRef,
|
|
tableId,
|
|
...args
|
|
}: Omit<TableRowsCountVariables, 'queryClient' | 'connectionString'>,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<TableRowsCountData, TableRowsCountError, TData> = {}
|
|
) => {
|
|
const queryClient = useQueryClient()
|
|
const {
|
|
connectionString,
|
|
identifier: readReplicaIdentifier,
|
|
type,
|
|
} = useConnectionStringForReadOps()
|
|
const { can: canSQLAdminWrite, isLoading: isPermissionsLoading } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.DATABASE_WRITE
|
|
)
|
|
|
|
return useQuery<TableRowsCountData, TableRowsCountError, TData>({
|
|
queryKey: tableRowKeys.tableRowsCount(projectRef, {
|
|
table: { id: tableId },
|
|
readReplicaIdentifier,
|
|
...args,
|
|
}),
|
|
queryFn: ({ signal }) =>
|
|
getTableRowsCount(
|
|
{
|
|
queryClient,
|
|
projectRef,
|
|
connectionString,
|
|
tableId,
|
|
isReadOnlyContext: type === 'replica' || !canSQLAdminWrite,
|
|
...args,
|
|
},
|
|
signal
|
|
),
|
|
enabled:
|
|
enabled &&
|
|
typeof projectRef !== 'undefined' &&
|
|
typeof tableId !== 'undefined' &&
|
|
(!IS_PLATFORM || typeof connectionString !== 'undefined') &&
|
|
// isReadOnlyContext resolves to `type === 'replica' || !canSQLAdminWrite`: for
|
|
// read replicas it's already known synchronously, but otherwise it depends on
|
|
// canSQLAdminWrite, which starts out `false` while permissions are loading.
|
|
// Firing while that's still in flight would cache a transient
|
|
// isReadOnlyContext:true (and, on a never-analyzed table,
|
|
// count:-1/is_estimate:true) for what may actually be a writable user. Wait
|
|
// for the permission check to settle before firing in that case.
|
|
(type === 'replica' || !isPermissionsLoading),
|
|
...options,
|
|
})
|
|
}
|