mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +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
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { invalidateRolesQuery } from './database-roles-query'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
type CreateRoleBody = Parameters<typeof pgMeta.roles.create>[0]
|
|
|
|
export type DatabaseRoleCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
payload: CreateRoleBody
|
|
}
|
|
|
|
export async function createDatabaseRole({
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabaseRoleCreateVariables) {
|
|
const sql = pgMeta.roles.create(payload).sql
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['roles', 'create'],
|
|
})
|
|
return result
|
|
}
|
|
|
|
type DatabaseRoleCreateData = Awaited<ReturnType<typeof createDatabaseRole>>
|
|
|
|
export const useDatabaseRoleCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseRoleCreateData, ResponseError, DatabaseRoleCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseRoleCreateData, ResponseError, DatabaseRoleCreateVariables>({
|
|
mutationFn: (vars) => createDatabaseRole(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await invalidateRolesQuery(queryClient, projectRef)
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database role: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|