mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 09:20:21 -04:00
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { invalidateSchemasQuery } from './schemas-query'
|
|
import { executeSql } from '@/data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type SchemaCreateVariables = {
|
|
name: string
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export async function createSchema({ name, projectRef, connectionString }: SchemaCreateVariables) {
|
|
const sql = pgMeta.schemas.create({ name, owner: 'postgres' }).sql
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['schema', 'create'],
|
|
})
|
|
return result
|
|
}
|
|
|
|
type SchemaCreateData = Awaited<ReturnType<typeof createSchema>>
|
|
|
|
export const useSchemaCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<SchemaCreateData, ResponseError, SchemaCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<SchemaCreateData, ResponseError, SchemaCreateVariables>({
|
|
mutationFn: (vars) => createSchema(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await invalidateSchemasQuery(queryClient, projectRef)
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create schema: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|