mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 17:30:25 -04:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { getKeywordsSql } from '@supabase/pg-meta'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { databaseKeys } from './keys'
|
|
import { executeSql, ExecuteSqlError } from '@/data/sql/execute-sql-query'
|
|
import { UseCustomQueryOptions } from '@/types'
|
|
|
|
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 }: UseCustomQueryOptions<KeywordsData, KeywordsError, TData> = {}
|
|
) =>
|
|
useQuery<KeywordsData, KeywordsError, TData>({
|
|
queryKey: databaseKeys.keywords(projectRef),
|
|
queryFn: ({ signal }) => getKeywords({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|