mirror of
https://github.com/supabase/supabase.git
synced 2026-05-08 01:40:13 -04:00
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import { Query } from '@supabase/pg-meta/src/query'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { tableRowKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-query'
|
|
import { RoleImpersonationState, wrapWithRoleImpersonation } from '@/lib/role-impersonation'
|
|
import { isRoleImpersonationEnabled } from '@/state/role-impersonation-state'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type TableRowUpdateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
table: { id: number; name: string; schema?: string }
|
|
configuration: { identifiers: any }
|
|
payload: any
|
|
enumArrayColumns: string[]
|
|
returning?: boolean
|
|
roleImpersonationState?: RoleImpersonationState
|
|
}
|
|
|
|
export function getTableRowUpdateSql({
|
|
table,
|
|
configuration,
|
|
payload,
|
|
returning = false,
|
|
enumArrayColumns,
|
|
}: Pick<
|
|
TableRowUpdateVariables,
|
|
'table' | 'payload' | 'configuration' | 'enumArrayColumns' | 'returning'
|
|
>) {
|
|
return new Query()
|
|
.from(table.name, table.schema ?? undefined)
|
|
.update(payload, { returning, enumArrayColumns })
|
|
.match(configuration.identifiers)
|
|
.toSql()
|
|
}
|
|
|
|
export async function updateTableRow({
|
|
projectRef,
|
|
connectionString,
|
|
table,
|
|
payload,
|
|
configuration,
|
|
enumArrayColumns,
|
|
returning,
|
|
roleImpersonationState,
|
|
}: TableRowUpdateVariables) {
|
|
const sql = wrapWithRoleImpersonation(
|
|
getTableRowUpdateSql({ table, configuration, payload, enumArrayColumns, returning }),
|
|
roleImpersonationState
|
|
)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
isRoleImpersonationEnabled: isRoleImpersonationEnabled(roleImpersonationState?.role),
|
|
queryKey: ['table-row-update', table.id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableRowUpdateData = Awaited<ReturnType<typeof updateTableRow>>
|
|
|
|
export const useTableRowUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<TableRowUpdateData, ResponseError, TableRowUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<TableRowUpdateData, ResponseError, TableRowUpdateVariables>({
|
|
mutationFn: (vars) => updateTableRow(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, table } = variables
|
|
await queryClient.invalidateQueries({
|
|
queryKey: tableRowKeys.tableRows(projectRef, { table: { id: table.id } }),
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update table row: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|