mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
## Context Just some clean up as I was going through stuff - `useExecuteSqlQuery` is deprecated and not used at all - As such `execute-sql-query` is technically irrelevant, the more relevant file is `execute-sql-mutation` - Hence opting to consolidate `execute-sql-query` into `execute-sql-mutation` - Also removing `ExecuteSqlError` since its just re-exporting the `ResponseError` type There's a lot of file changes but its essentially just updating the importing statements across the files
101 lines
2.8 KiB
TypeScript
101 lines
2.8 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-mutation'
|
|
import { RoleImpersonationState, wrapWithRoleImpersonation } from '@/lib/role-impersonation'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import { isRoleImpersonationEnabled } from '@/state/role-impersonation-state'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type TableRowCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
table: { id: number; name: string; schema?: string }
|
|
payload: any
|
|
enumArrayColumns: string[]
|
|
returning?: boolean
|
|
roleImpersonationState?: RoleImpersonationState
|
|
}
|
|
|
|
export function getTableRowCreateSql({
|
|
table,
|
|
payload,
|
|
returning = false,
|
|
enumArrayColumns,
|
|
}: Pick<TableRowCreateVariables, 'table' | 'payload' | 'enumArrayColumns' | 'returning'>) {
|
|
return new Query()
|
|
.from(table.name, table.schema ?? undefined)
|
|
.insert([payload], { returning, enumArrayColumns })
|
|
.toSql()
|
|
}
|
|
|
|
export async function createTableRow({
|
|
projectRef,
|
|
connectionString,
|
|
table,
|
|
payload,
|
|
enumArrayColumns,
|
|
returning,
|
|
roleImpersonationState,
|
|
}: TableRowCreateVariables) {
|
|
const sql = wrapWithRoleImpersonation(
|
|
getTableRowCreateSql({ table, payload, enumArrayColumns, returning }),
|
|
roleImpersonationState
|
|
)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
isRoleImpersonationEnabled: isRoleImpersonationEnabled(roleImpersonationState?.role),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableRowCreateData = Awaited<ReturnType<typeof createTableRow>>
|
|
|
|
export const useTableRowCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<TableRowCreateData, ResponseError, TableRowCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
const track = useTrack()
|
|
|
|
return useMutation<TableRowCreateData, ResponseError, TableRowCreateVariables>({
|
|
mutationFn: (vars) => createTableRow(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, table } = variables
|
|
|
|
track(
|
|
'table_data_added',
|
|
{
|
|
method: 'table_editor',
|
|
schema_name: table.schema,
|
|
table_name: table.name,
|
|
},
|
|
{ project: projectRef }
|
|
)
|
|
|
|
await queryClient.invalidateQueries({
|
|
queryKey: tableRowKeys.tableRowsAndCount(projectRef, table.id),
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(data.message)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|