mirror of
https://github.com/supabase/supabase.git
synced 2026-10-16 06:39:09 +03:00
* init * hovercard * adds button to install index advisor * hover card now now insert indexes * update * moved hook * align alert dialog to design syste, * Update index-advisor.utils.ts * shows all index statements now * Update query-performance.tsx * Some refactors * Clean up * Fix * One last nit refactor --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError } from 'types'
|
|
import { databaseIndexesKeys } from './keys'
|
|
|
|
export type DatabaseIndexCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string
|
|
payload: {
|
|
schema: string
|
|
entity: string
|
|
type: string
|
|
columns: string[]
|
|
}
|
|
}
|
|
|
|
export async function createDatabaseIndex({
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabaseIndexCreateVariables) {
|
|
const { schema, entity, type, columns } = payload
|
|
|
|
const sql = `
|
|
CREATE INDEX ON "${schema}"."${entity}" USING ${type} (${columns
|
|
.map((column) => `"${column}"`)
|
|
.join(', ')});
|
|
`.trim()
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['indexes', schema],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseIndexCreateData = Awaited<ReturnType<typeof createDatabaseIndex>>
|
|
|
|
export const useDatabaseIndexCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<DatabaseIndexCreateData, ResponseError, DatabaseIndexCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseIndexCreateData, ResponseError, DatabaseIndexCreateVariables>(
|
|
(vars) => createDatabaseIndex(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(databaseIndexesKeys.list(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database index: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|