mirror of
https://github.com/supabase/supabase.git
synced 2026-06-29 11:57:37 -04:00
1baaded0bb
## 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
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import type { PGFunctionCreate } from '@supabase/pg-meta/src/pg-meta-functions'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
import { z } from 'zod'
|
|
|
|
import { databaseKeys } from '@/data/database/keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseFunctionCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
payload: PGFunctionCreate
|
|
}
|
|
|
|
export async function createDatabaseFunction({
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabaseFunctionCreateVariables) {
|
|
const { sql, zod } = pgMeta.functions.create(payload)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['functions', 'create'],
|
|
})
|
|
|
|
return result as z.infer<typeof zod>
|
|
}
|
|
|
|
type DatabaseFunctionCreateData = Awaited<ReturnType<typeof createDatabaseFunction>>
|
|
|
|
export const useDatabaseFunctionCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseFunctionCreateData,
|
|
ResponseError,
|
|
DatabaseFunctionCreateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseFunctionCreateData, ResponseError, DatabaseFunctionCreateVariables>({
|
|
mutationFn: (vars) => createDatabaseFunction(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseKeys.databaseFunctions(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database function: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|