Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update database-triggers react queries to use methods from data/fetchers (#33546)

Joshen Lim8d527f7f9e8d
diff --git a/apps/studio/data/database-triggers/database-trigger-create-mutation.ts b/apps/studio/data/database-triggers/database-trigger-create-mutation.ts
index 1ee800521b4..5b4e7d682b2 100644
--- a/apps/studio/data/database-triggers/database-trigger-create-mutation.ts
+++ b/apps/studio/data/database-triggers/database-trigger-create-mutation.ts
@@ -1,9 +1,7 @@
-import type { PostgresTrigger } from '@supabase/postgres-meta'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { post } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databaseTriggerKeys } from './keys'
@@ -13,8 +11,6 @@ export type DatabaseTriggerCreateVariables = {
payload: any
}
-type CreateDatabaseTriggerResponse = PostgresTrigger & { error?: any }
-
export async function createDatabaseTrigger({
projectRef,
connectionString,
@@ -23,12 +19,17 @@ export async function createDatabaseTrigger({
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- const response = (await post(`${API_URL}/pg-meta/${projectRef}/triggers`, payload, {
- headers: Object.fromEntries(headers),
- })) as CreateDatabaseTriggerResponse
+ const { data, error } = await post('/platform/pg-meta/{ref}/triggers', {
+ params: {
+ header: { 'x-connection-encrypted': connectionString! },
+ path: { ref: projectRef },
+ },
+ body: payload,
+ headers,
+ })
- if (response.error) throw response.error
- return response as PostgresTrigger
+ if (error) handleError(error)
+ return data
}
type DatabaseTriggerCreateData = Awaited<ReturnType<typeof createDatabaseTrigger>>
diff --git a/apps/studio/data/database-triggers/database-trigger-delete-mutation.ts b/apps/studio/data/database-triggers/database-trigger-delete-mutation.ts
index a23620fe3b8..3404c4a5461 100644
--- a/apps/studio/data/database-triggers/database-trigger-delete-mutation.ts
+++ b/apps/studio/data/database-triggers/database-trigger-delete-mutation.ts
@@ -2,8 +2,7 @@ import type { PostgresTrigger } from '@supabase/postgres-meta'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { delete_ } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { del, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databaseTriggerKeys } from './keys'
@@ -23,12 +22,17 @@ export async function deleteDatabaseTrigger({
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- const response = (await delete_(`${API_URL}/pg-meta/${projectRef}/triggers?id=${id}`, undefined, {
- headers: Object.fromEntries(headers),
- })) as DeleteDatabaseTriggerResponse
+ const { data, error } = await del('/platform/pg-meta/{ref}/triggers', {
+ params: {
+ header: { 'x-connection-encrypted': connectionString! },
+ path: { ref: projectRef },
+ query: { id },
+ },
+ headers,
+ })
- if (response?.error) throw response.error
- return response as PostgresTrigger
+ if (error) handleError(error)
+ return data
}
type DatabaseTriggerDeleteData = Awaited<ReturnType<typeof deleteDatabaseTrigger>>
diff --git a/apps/studio/data/database-triggers/database-trigger-update-mutation.ts b/apps/studio/data/database-triggers/database-trigger-update-mutation.ts
index ca53c246e34..fccccbfdaeb 100644
--- a/apps/studio/data/database-triggers/database-trigger-update-mutation.ts
+++ b/apps/studio/data/database-triggers/database-trigger-update-mutation.ts
@@ -1,9 +1,7 @@
-import type { PostgresTrigger } from '@supabase/postgres-meta'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { patch } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { handleError, patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databaseTriggerKeys } from './keys'
@@ -14,8 +12,6 @@ export type DatabaseTriggerUpdateVariables = {
payload: any
}
-type UpdateDatabaseTriggerResponse = PostgresTrigger & { error?: any }
-
export async function updateDatabaseTrigger({
id,
projectRef,
@@ -25,12 +21,18 @@ export async function updateDatabaseTrigger({
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- const response = (await patch(`${API_URL}/pg-meta/${projectRef}/triggers?id=${id}`, payload, {
- headers: Object.fromEntries(headers),
- })) as UpdateDatabaseTriggerResponse
+ const { data, error } = await patch('/platform/pg-meta/{ref}/triggers', {
+ params: {
+ header: { 'x-connection-encrypted': connectionString! },
+ path: { ref: projectRef },
+ query: { id },
+ },
+ body: payload,
+ headers,
+ })
- if (response.error) throw response.error
- return response as PostgresTrigger
+ if (error) handleError(error)
+ return data
}
type DatabaseTriggerUpdateData = Awaited<ReturnType<typeof updateDatabaseTrigger>>
diff --git a/apps/studio/data/database-triggers/database-triggers-query.ts b/apps/studio/data/database-triggers/database-triggers-query.ts
index 065ec129d40..a3c9a10db18 100644
--- a/apps/studio/data/database-triggers/database-triggers-query.ts
+++ b/apps/studio/data/database-triggers/database-triggers-query.ts
@@ -1,9 +1,7 @@
-import type { PostgresTrigger } from '@supabase/postgres-meta'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
-import { databaseTriggerKeys } from './keys'
+import { get, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
+import { databaseTriggerKeys } from './keys'
export type DatabaseTriggersVariables = {
projectRef?: string
@@ -19,13 +17,18 @@ export async function getDatabaseTriggers(
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- const response = (await get(`${API_URL}/pg-meta/${projectRef}/triggers`, {
- headers: Object.fromEntries(headers),
+ const { data, error } = await get('/platform/pg-meta/{ref}/triggers', {
+ params: {
+ header: { 'x-connection-encrypted': connectionString! },
+ path: { ref: projectRef },
+ query: undefined as any,
+ },
+ headers,
signal,
- })) as PostgresTrigger[] | { error?: any }
+ })
- if (!Array.isArray(response) && response.error) throw response.error
- return response as PostgresTrigger[]
+ if (error) handleError(error)
+ return data
}
export type DatabaseTriggersData = Awaited<ReturnType<typeof getDatabaseTriggers>>
@@ -42,13 +45,13 @@ export const useDatabaseHooksQuery = <TData = DatabaseTriggersData>(
databaseTriggerKeys.list(projectRef),
({ signal }) => getDatabaseTriggers({ projectRef, connectionString }, signal),
{
- // @ts-ignore
- select(data) {
- return (data as PostgresTrigger[]).filter(
- (trigger) =>
+ select: (data) => {
+ return data.filter((trigger) => {
+ return (
trigger.function_schema === 'supabase_functions' &&
(trigger.schema !== 'net' || trigger.function_args.length === 0)
- )
+ )
+ }) as any
},
enabled: enabled && typeof projectRef !== 'undefined',
...options,
diff --git a/apps/studio/data/sql/execute-sql-mutation.ts b/apps/studio/data/sql/execute-sql-mutation.ts
index a34f2e592ee..9486c6c8eb0 100644
--- a/apps/studio/data/sql/execute-sql-mutation.ts
+++ b/apps/studio/data/sql/execute-sql-mutation.ts
@@ -8,6 +8,7 @@ import { databaseTriggerKeys } from 'data/database-triggers/keys'
import { databaseKeys } from 'data/database/keys'
import { entityTypeKeys } from 'data/entity-types/keys'
import { enumeratedTypesKeys } from 'data/enumerated-types/keys'
+import { tableKeys } from 'data/tables/keys'
import { executeSql, ExecuteSqlData, ExecuteSqlVariables } from './execute-sql-query'
export type QueryResponseError = {
@@ -42,6 +43,7 @@ export const useExecuteSqlMutation = ({
// [Joshen] Default to false for now, only used for SQL editor to dynamically invalidate
if (contextualInvalidation && projectRef) {
const invalidationKeys = inferInvalidationKeys(projectRef, sql)
+ console.log({ invalidationKeys })
await Promise.all(invalidationKeys.map((key) => queryClient.invalidateQueries(key)))
}
await onSuccess?.(data, variables, context)
@@ -69,6 +71,7 @@ const inferInvalidationKeys = (ref: string, sql: string) => {
sqlLower.includes('drop table')
) {
keys.push(entityTypeKeys.list(ref))
+ keys.push(tableKeys.list(ref))
}
if (
sqlLower.includes('create schema') ||
diff --git a/apps/studio/data/tables/keys.ts b/apps/studio/data/tables/keys.ts
index 4b6bbca70fc..688655a6078 100644
--- a/apps/studio/data/tables/keys.ts
+++ b/apps/studio/data/tables/keys.ts
@@ -1,4 +1,4 @@
export const tableKeys = {
- list: (projectRef: string | undefined, schema: string | undefined, includeColumns?: boolean) =>
+ list: (projectRef: string | undefined, schema?: string, includeColumns?: boolean) =>
['projects', projectRef, 'tables', schema, includeColumns].filter(Boolean),
}
diff --git a/apps/studio/lib/common/fetch/base.ts b/apps/studio/lib/common/fetch/base.ts
index 38f4eda7dcd..f9bf3c87aa7 100644
--- a/apps/studio/lib/common/fetch/base.ts
+++ b/apps/studio/lib/common/fetch/base.ts
@@ -2,6 +2,7 @@ import { getAccessToken } from 'lib/gotrue'
import { isUndefined } from 'lodash'
import type { SupaResponse } from 'types/base'
+/* @deprecated Use handleError from data/fetchers instead */
export function handleError<T>(e: any, requestId: string): SupaResponse<T> {
const message = e?.message ? `An error has occurred: ${e.message}` : 'An error has occurred'
const error = { code: 500, message, requestId }
diff --git a/package.json b/package.json
index b088280717b..f719b75a8a3 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,7 @@
"dev": "turbo run dev --parallel",
"dev:reference": "turbo run dev --filter=reference --parallel",
"dev:studio": "turbo run dev --filter=studio --parallel",
+ "dev:studio-local": "pnpm run --dir tests/local-studio-tests codegen:setup",
"dev:docs": "turbo run dev --filter=docs --parallel",
"dev:www": "turbo run dev --filter=www --parallel",
"dev:design-system": "turbo run dev --filter=design-system --parallel",