mirror of
https://github.com/supabase/supabase.git
synced 2026-10-18 23:59:08 +03:00
* chore: cleanup packages - Avoid circular imports - Export API-types as types - pg-format without depending on Node internal Buffer (not browser-compatible) - Avoid importing from barrel files in ui dir * chore: avoid barrel file imports in studio (#27771) * chore: avoid barrel file imports - Removes some unused imports - Avoids barrel file import for faster builds + less memory * add eslint rule * type fixes * delete layouts barrel * delete components/grid barrel file * delete components/grid/utils barrel file * delete components/grid/components/common barrel file * delete components/grid/components/editor barrel file * delete components/grid/components/formatter barrel file * delete components/grid/components/grid barrel file * delete components/grid/components/header/filter barrel file * remote components/grid/store barrel file * remove components/interfaces/Auth/Policies barrel file * delete components/interfaces/Settings/Logs barrel file * delete components/ui/CodeEditor barrel file * delete components/ui/Forms barrel file * delete components/ui/Shimmers barrel file * delete data/analytics barrel file * delete hooks barrel file * cleanup lib/common/fetch barrel file * final * barral files cleanup * global react-data-grid styles * remove console.log --------- Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com> * fix build --------- Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'react-hot-toast'
|
|
|
|
import { Query } from 'components/grid/query/Query'
|
|
import type { Filter, SupaTable } from 'components/grid/types'
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import { sqlKeys } from 'data/sql/keys'
|
|
import { ImpersonationRole, wrapWithRoleImpersonation } from 'lib/role-impersonation'
|
|
import { isRoleImpersonationEnabled } from 'state/role-impersonation-state'
|
|
import type { ResponseError } from 'types'
|
|
import { formatFilterValue } from './utils'
|
|
|
|
export type TableRowDeleteAllVariables = {
|
|
projectRef: string
|
|
connectionString?: string
|
|
table: SupaTable
|
|
filters: Filter[]
|
|
impersonatedRole?: ImpersonationRole
|
|
}
|
|
|
|
export function getTableRowDeleteAllSql({
|
|
table,
|
|
filters,
|
|
}: Pick<TableRowDeleteAllVariables, 'table' | 'filters'>) {
|
|
let queryChains = new Query().from(table.name, table.schema ?? undefined).delete()
|
|
|
|
filters
|
|
.filter((x) => x.value && x.value !== '')
|
|
.forEach((x) => {
|
|
const value = formatFilterValue(table, x)
|
|
queryChains = queryChains.filter(x.column, x.operator, value)
|
|
})
|
|
|
|
return queryChains.toSql()
|
|
}
|
|
|
|
export async function deleteAllTableRow({
|
|
projectRef,
|
|
connectionString,
|
|
table,
|
|
filters,
|
|
impersonatedRole,
|
|
}: TableRowDeleteAllVariables) {
|
|
const sql = wrapWithRoleImpersonation(getTableRowDeleteAllSql({ table, filters }), {
|
|
projectRef,
|
|
role: impersonatedRole,
|
|
})
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
isRoleImpersonationEnabled: isRoleImpersonationEnabled(impersonatedRole),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableRowDeleteAllData = Awaited<ReturnType<typeof deleteAllTableRow>>
|
|
|
|
/**
|
|
* For deleting all rows based on a given filter
|
|
*/
|
|
export const useTableRowDeleteAllMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<TableRowDeleteAllData, ResponseError, TableRowDeleteAllVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<TableRowDeleteAllData, ResponseError, TableRowDeleteAllVariables>(
|
|
(vars) => deleteAllTableRow(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 all table rows: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|