Files
supabase/apps/studio/data/database/keywords-query.ts
Andrew Valleteau 31aad403de fix(studio): early fail query when x-connection-encrypted is invalid (#35331)
* 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
2025-05-08 12:11:03 +02:00

47 lines
1.3 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
import { databaseKeys } from './keys'
export const getKeywordsSql = () => {
const sql = /* SQL */ `
SELECT word FROM pg_get_keywords();
`.trim()
return sql
}
export type KeywordsVariables = {
projectRef?: string
connectionString?: string | null
}
export async function getKeywords(
{ projectRef, connectionString }: KeywordsVariables,
signal?: AbortSignal
) {
const sql = getKeywordsSql()
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['keywords'] },
signal
)
return result.map((x: { word: string }) => x.word.toLocaleLowerCase()) as string[]
}
export type KeywordsData = Awaited<ReturnType<typeof getKeywords>>
export type KeywordsError = ExecuteSqlError
export const useKeywordsQuery = <TData = KeywordsData>(
{ projectRef, connectionString }: KeywordsVariables,
{ enabled = true, ...options }: UseQueryOptions<KeywordsData, KeywordsError, TData> = {}
) =>
useQuery<KeywordsData, KeywordsError, TData>(
databaseKeys.keywords(projectRef),
({ signal }) => getKeywords({ projectRef, connectionString }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)