Files
supabase/apps/studio/data/auth/user-query.ts
Joshen Lim 1baaded0bb Consolidate execute-sql-query into execute-sql-mutation (#46944)
## Context

Just some clean up as I was going through stuff
- `useExecuteSqlQuery` is deprecated and not used at all
- As such `execute-sql-query` is technically irrelevant, the more
relevant file is `execute-sql-mutation`
- Hence opting to consolidate `execute-sql-query` into
`execute-sql-mutation`
- Also removing `ExecuteSqlError` since its just re-exporting the
`ResponseError` type

There's a lot of file changes but its essentially just updating the
importing statements across the files
2026-06-16 00:07:16 +08:00

47 lines
1.4 KiB
TypeScript

import { getUserSQL } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { authKeys } from './keys'
import { User } from './users-infinite-query'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { UUID_REGEX } from '@/lib/constants'
import { ResponseError, UseCustomQueryOptions } from '@/types'
type UserVariables = {
projectRef?: string
connectionString?: string | null
userId?: string | null
}
export async function getUser(
{ projectRef, connectionString, userId }: UserVariables,
signal?: AbortSignal
) {
if (!userId) throw new Error('UserID is required')
if (!UUID_REGEX.test(userId)) throw new Error('Invalid user ID format')
const sql = getUserSQL(userId)
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: [`user-${userId}`] },
signal
)
const user = result[0] as User | undefined
return user
}
export type UserData = Awaited<ReturnType<typeof getUser>>
export type UserError = ResponseError
export const useUserQuery = <TData = UserData>(
{ projectRef, connectionString, userId }: UserVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<UserData, UserError, TData> = {}
) =>
useQuery<UserData, UserError, TData>({
queryKey: authKeys.user(projectRef, userId),
queryFn: ({ signal }) => getUser({ projectRef, connectionString, userId }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})