Files
supabase/apps/studio/data/database/primary-keys-exists-query.ts
T
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

56 lines
1.8 KiB
TypeScript

import { getCheckPrimaryKeysExistsSQL } from '@supabase/pg-meta/src/sql/studio/check-primary-keys-exists'
import { useQuery } from '@tanstack/react-query'
import { executeSql } from 'data/sql/execute-sql-query'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { databaseKeys } from './keys'
type CheckPrimaryKeysExistsVariables = {
projectRef?: string
connectionString?: string | null
tables: { name: string; schema: string }[]
}
type CheckPrimaryKeysExistResponse = {
id: string
name: string
schema: string
has_primary_key: boolean
}[]
export async function checkPrimaryKeysExists({
projectRef,
connectionString,
tables,
}: CheckPrimaryKeysExistsVariables) {
if (!projectRef) throw new Error('Project ref is required')
const { result } = await executeSql({
projectRef,
connectionString,
sql: getCheckPrimaryKeysExistsSQL(tables),
})
return {
offendingTables: (result as CheckPrimaryKeysExistResponse).filter((x) => !x.has_primary_key),
}
}
export type CheckPrimaryKeysExistsData = Awaited<ReturnType<typeof checkPrimaryKeysExists>>
export type CheckPrimaryKeysExistsError = ResponseError
export const useCheckPrimaryKeysExists = <TData = CheckPrimaryKeysExistsData>(
{ projectRef, connectionString, tables }: CheckPrimaryKeysExistsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<CheckPrimaryKeysExistsData, CheckPrimaryKeysExistsError, TData> = {}
) =>
useQuery<CheckPrimaryKeysExistsData, CheckPrimaryKeysExistsError, TData>({
queryKey: databaseKeys.checkPrimaryKeysExists(projectRef, tables),
queryFn: () => checkPrimaryKeysExists({ projectRef, connectionString, tables }),
retry: false,
enabled: enabled && typeof projectRef !== 'undefined' && tables.length > 0,
...options,
})