mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 18:05:11 +03:00
## Context Related to FE-2557 Part of shifting manually written dashboard queries into packages/pg-meta where - pg-meta can be code owners of - we can write tests for the queries This PR just shifts all the `.sql.ts` files that we previously created into packages/pg-meta There's still other areas where we need to shift over as well which I'll address in subsequent PRs ## Notable changes - `getTableRowsCountSql` -> Opted to shift `formatFilterValue` logic out before calling this method (ref `table-rows-count-query`) - `getDeleteOldCronJobRunDetailsByCtidSql` -> Opted to shift `validatePageNumber` logic out before calling this method (ref `CronJobsTab.useCleanupActions`)
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { getDatabaseExtensionDefaultSchemaSQL } from '@supabase/pg-meta'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { UseCustomQueryOptions } from 'types'
|
|
|
|
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
|
|
import { databaseExtensionsKeys } from './keys'
|
|
|
|
type DatabaseExtensionDefaultSchemaVariables = {
|
|
extension?: string
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
async function getDatabaseExtensionDefaultSchema(
|
|
{ extension, projectRef, connectionString }: DatabaseExtensionDefaultSchemaVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!extension) {
|
|
throw new Error('extension is required')
|
|
}
|
|
|
|
const sql = getDatabaseExtensionDefaultSchemaSQL({ extension })
|
|
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['database-extension-default-schema', extension],
|
|
},
|
|
signal
|
|
)
|
|
|
|
return result[0] as { name: string; version: string; schema: string | null }
|
|
}
|
|
|
|
type DatabaseExtensionDefaultSchemaData = Awaited<
|
|
ReturnType<typeof getDatabaseExtensionDefaultSchema>
|
|
>
|
|
type DatabaseExtensionDefaultSchemaError = ExecuteSqlError
|
|
|
|
export const useDatabaseExtensionDefaultSchemaQuery = <TData = DatabaseExtensionDefaultSchemaData>(
|
|
{ projectRef, connectionString, extension }: DatabaseExtensionDefaultSchemaVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<
|
|
DatabaseExtensionDefaultSchemaData,
|
|
DatabaseExtensionDefaultSchemaError,
|
|
TData
|
|
> = {}
|
|
) =>
|
|
useQuery<DatabaseExtensionDefaultSchemaData, DatabaseExtensionDefaultSchemaError, TData>({
|
|
queryKey: databaseExtensionsKeys.defaultSchema(projectRef, extension),
|
|
queryFn: ({ signal }) =>
|
|
getDatabaseExtensionDefaultSchema({ projectRef, connectionString, extension }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined' && typeof extension !== 'undefined',
|
|
...options,
|
|
})
|