Files
supabase/apps/studio/data/auth/user-query.ts
T
Joshen Lim fc7fb50aa2 Organize pg meta queries for studio (#43562)
## Context

We reached to a consensus that we'll keep manually written queries
within the `pg-meta` package

I'll shift queries over in several PRs but wanted to double check if the
changes here is a good folder structure

## Changes involved

- Am organizing dashboard queries in `pg-meta` based on product
  - So folder structure will be `/sql/studio/[product]/*.ts`
- Every constant will be exported directly from `pg-meta`'s index
- So consumption in dashboard will just be `import { xxx } from
'@supabase/pg-meta`

cc @avallete and @soedirgo on this one - lemme know if this is alright,
or if there's a preferred way to do this
2026-03-10 09:34:11 +07:00

47 lines
1.4 KiB
TypeScript

import { getUserSQL } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { executeSql, type ExecuteSqlError } from 'data/sql/execute-sql-query'
import { UseCustomQueryOptions } from 'types'
import { authKeys } from './keys'
import { User } from './users-infinite-query'
import { UUID_REGEX } from '@/lib/constants'
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 = ExecuteSqlError
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,
})