Files
supabase/apps/studio/data/auth/user-query.ts
T
Joshen LimandAli Waseem b94538268b Joshen/fe 2558 shift sql queries to contextual folders update codeowners (#42770)
## Context

Related to Dashboard Scalability, specifically having Postgres team as
CODEOWNERS for dashboard queries

This is just a clean up as we're currently piling manual queries into
one folder under `data/sql/queries`, whereas I reckon it'll be better
for each query to sit within their RQ folder for better context.

Am opting the naming format for files housing queries to be `*.sql.ts`,
and also updating CODEOWNERS to reflect as such

Next step will also be to shift all the dashboard queries within pg-meta
into studio itself as requested by the pg-meta team

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Consolidated and reorganized internal module structure for the data
layer to improve maintainability and reduce redundancy.
* Streamlined import paths across components to align with new
consolidated module organization.

* **Chores**
* Updated code ownership patterns to reflect reorganized file structure.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Ali Waseem <waseema393@gmail.com>
2026-02-16 18:34:08 +00:00

46 lines
1.4 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { executeSql, type ExecuteSqlError } from 'data/sql/execute-sql-query'
import { UseCustomQueryOptions } from 'types'
import { getUserSQL } from './auth.sql'
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,
})