Files
supabase/apps/studio/data/table-rows/table-row-delete-mutation.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

84 lines
2.4 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import { Query, SupaRow } from 'components/grid'
import { executeSql } from 'data/sql/execute-sql-query'
import { sqlKeys } from 'data/sql/keys'
import { Table } from 'data/tables/table-query'
import { ImpersonationRole, wrapWithRoleImpersonation } from 'lib/role-impersonation'
import { ResponseError } from 'types'
import { getPrimaryKeys } from './utils'
export type TableRowDeleteVariables = {
projectRef: string
connectionString?: string
table: Table
rows: SupaRow[]
impersonatedRole?: ImpersonationRole
}
export function getTableRowDeleteSql({
table,
rows,
}: Pick<TableRowDeleteVariables, 'table' | 'rows'>) {
const { primaryKeys, error } = getPrimaryKeys({ table })
if (error) throw error
let queryChains = new Query().from(table.name, table.schema ?? undefined).delete()
primaryKeys!.forEach((key) => {
const primaryKeyValues = rows.map((x) => x[key])
queryChains = queryChains.filter(key, 'in', primaryKeyValues)
})
return queryChains.toSql()
}
export async function deleteTableRow({
projectRef,
connectionString,
table,
rows,
impersonatedRole,
}: TableRowDeleteVariables) {
const sql = wrapWithRoleImpersonation(getTableRowDeleteSql({ table, rows }), {
projectRef,
role: impersonatedRole,
})
const { result } = await executeSql({ projectRef, connectionString, sql })
return result
}
type TableRowDeleteData = Awaited<ReturnType<typeof deleteTableRow>>
export const useTableRowDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<TableRowDeleteData, ResponseError, TableRowDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<TableRowDeleteData, ResponseError, TableRowDeleteVariables>(
(vars) => deleteTableRow(vars),
{
async onSuccess(data, variables, context) {
const { projectRef, table } = variables
await queryClient.invalidateQueries(sqlKeys.query(projectRef, [table.schema, table.name]))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete table row: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}