Files
supabase/apps/studio/data/table-rows/table-rows-count-query.ts
T
e6006b9653 feat: user impersonation (#18945)
* feat: user impersonation

* refactor

* add anon impersonation

* add user impersonation to graphiql

* thanks Ivan for this one

* add radio buttons

* progress

* working user selector

* adds loading and error states to user selector

* nicer popover button

* default graphiql to use service role key

* styling updates

* fix padding in graphiql

* add service role warning to graphiql

* add user impersonation to realtime inspector

* add feature flag

* use Alert_Shadcn_ instead of old Alert

* Update apps/studio/lib/role-impersonation.ts

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* update title

* Update apps/studio/components/interfaces/RoleImpersonationSelector/UserImpersonationSelector.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/UserImpersonationSelector.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* add constant padding at the bottom of user selector

* update graphiql buttons font size

* make rls warning dismissable

* use text-foreground on rls warning title

* fix no results while impersonating a role

* fix error line number when impersonating a role

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2023-12-04 04:45:11 +00:00

116 lines
3.0 KiB
TypeScript

import { QueryKey, UseQueryOptions } from '@tanstack/react-query'
import { useCallback } from 'react'
import { Filter, Query, SupaTable } from 'components/grid'
import { ImpersonationRole, wrapWithRoleImpersonation } from 'lib/role-impersonation'
import { ExecuteSqlData, useExecuteSqlPrefetch, useExecuteSqlQuery } from '../sql/execute-sql-query'
import { formatFilterValue } from './utils'
type GetTableRowsCountArgs = {
table?: SupaTable
filters?: Filter[]
impersonatedRole?: ImpersonationRole
}
export const getTableRowsCountSqlQuery = ({ table, filters = [] }: GetTableRowsCountArgs) => {
const query = new Query()
if (!table) {
return ``
}
let queryChains = query.from(table.name, table.schema ?? undefined).count()
filters
.filter((x) => x.value && x.value != '')
.forEach((x) => {
const value = formatFilterValue(table, x)
queryChains = queryChains.filter(x.column, x.operator, value)
})
const sql = queryChains.toSql()
return sql
}
export type TableRowsCount = {
count: number
}
export type TableRowsCountVariables = GetTableRowsCountArgs & {
projectRef?: string
connectionString?: string
queryKey?: QueryKey
}
export type TableRowsCountData = TableRowsCount
export type TableRowsCountError = unknown
export const useTableRowsCountQuery = <TData extends TableRowsCountData = TableRowsCountData>(
{
projectRef,
connectionString,
queryKey,
table,
impersonatedRole,
...args
}: TableRowsCountVariables,
options: UseQueryOptions<ExecuteSqlData, TableRowsCountError, TData> = {}
) =>
useExecuteSqlQuery(
{
projectRef,
connectionString,
sql: wrapWithRoleImpersonation(getTableRowsCountSqlQuery({ table, ...args }), {
projectRef: projectRef ?? 'ref',
role: impersonatedRole,
}),
queryKey: [
...(queryKey ?? []),
{
table: { name: table?.name, schema: table?.schema },
impersonatedRole,
...args,
},
],
},
{
select(data) {
return {
count: data.result[0].count,
} as TData
},
enabled: typeof projectRef !== 'undefined' && typeof table !== 'undefined',
...options,
}
)
/**
* useTableRowsCountPrefetch is used for prefetching the table rows count. For example, starting a query loading before a page is navigated to.
*
* @example
* const prefetch = useTableRowsCountPrefetch()
*
* return (
* <Link onMouseEnter={() => prefetch({ ...args })}>
* Start loading on hover
* </Link>
* )
*/
export const useTableRowsCountPrefetch = () => {
const prefetch = useExecuteSqlPrefetch()
return useCallback(
({ projectRef, connectionString, queryKey, table, ...args }: TableRowsCountVariables) =>
prefetch({
projectRef,
connectionString,
sql: getTableRowsCountSqlQuery({ table, ...args }),
queryKey: [
...(queryKey ?? []),
{ table: { name: table?.name, schema: table?.schema }, ...args },
],
}),
[prefetch]
)
}