The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
move useDbQuery & useLogsQuery back to hooks
Alaister Young9178f22b5057
diff --git a/studio/components/interfaces/Reports/Reports.utils.tsx b/studio/components/interfaces/Reports/Reports.utils.tsxindex 91253b06b85..006ee0e17d5 100644--- a/studio/components/interfaces/Reports/Reports.utils.tsx+++ b/studio/components/interfaces/Reports/Reports.utils.tsx@@ -1,6 +1,6 @@import { IconRefreshCw } from '@supabase/ui'-import { LogsQueryData, LogsQueryHandlers, useLogsQuery } from 'data/analytics/logs-query'-import { useLogsSqlQuery } from 'data/analytics/logs-sql-query'+import useDbQuery from 'hooks/analytics/useDbQuery'+import useLogsQuery, { LogsQueryData, LogsQueryHandlers } from 'hooks/analytics/useLogsQuery'import React from 'react'import { Button } from 'ui'import { DatePickerToFrom } from '../Settings/Logs'@@ -24,7 +24,7 @@ export const hooksFactory = (projectRef: string, config: PresetConfig) => {if (queryType === 'db') {return {...acc,- [k]: () => useLogsSqlQuery(sql),+ [k]: () => useDbQuery(sql),}} else {return {diff --git a/studio/data/analytics/keys.ts b/studio/data/analytics/keys.tsindex 0bfe304ffae..b360a8f7132 100644--- a/studio/data/analytics/keys.ts+++ b/studio/data/analytics/keys.ts@@ -1,10 +1,6 @@import { LogsEndpointParams } from 'components/interfaces/Settings/Logs'export const analyticsKeys = {- logs: (projectRef: string | undefined, params: LogsEndpointParams) =>- ['projects', projectRef, 'logs', params] as const,- log: (projectRef: string | undefined, id: string | undefined) =>- ['projects', projectRef, 'log', id] as const,functionsInvStats: (projectRef: string | undefined,{ interval, functionId }: { functionId: string | undefined; interval: string | undefined }diff --git a/studio/data/analytics/logs-sql-query.ts b/studio/data/analytics/logs-sql-query.tsdeleted file mode 100644index 3a0a76253b5..00000000000--- a/studio/data/analytics/logs-sql-query.ts+++ /dev/null@@ -1,47 +0,0 @@-import { DEFAULT_QUERY_PARAMS } from 'components/interfaces/Reports/Reports.constants'-import {- BaseReportParams,- DbQuery,- DbQueryData,- DbQueryHandler,-} from 'components/interfaces/Reports/Reports.types'-import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'-import { useExecuteSqlQuery } from 'data/sql/execute-sql-query'--export const useLogsSqlQuery = (- sql: DbQuery['sql'],- params: BaseReportParams = DEFAULT_QUERY_PARAMS-): [DbQueryData, DbQueryHandler] => {- const resolvedSql = typeof sql === 'function' ? sql(params) : sql-- // Generally we don't want to use contexts inside query hooks,- // as keeping them as params makes them more reusable.- // But in this case we're doing it this way to maintain compatibility with the old SWR hook.- const { project } = useProjectContext()-- const {- data,- error: rqError,- isLoading,- isRefetching,- refetch,- } = useExecuteSqlQuery(- {- projectRef: project?.ref,- connectionString: project?.connectionString,- sql: resolvedSql,- },- {- // enabled: false,- refetchOnMount: false,- refetchOnWindowFocus: false,- refetchOnReconnect: false,- }- )-- const error = rqError || (typeof data === 'object' ? data?.result.error : '')- return [- { error, data: data?.result, isLoading: isLoading || isRefetching, params },- { runQuery: () => refetch() },- ]-}diff --git a/studio/hooks/analytics/useDbQuery.tsx b/studio/hooks/analytics/useDbQuery.tsxnew file mode 100644index 00000000000..0130ca85ed9--- /dev/null+++ b/studio/hooks/analytics/useDbQuery.tsx@@ -0,0 +1,51 @@+import { useQuery } from '@tanstack/react-query'+import { DEFAULT_QUERY_PARAMS } from 'components/interfaces/Reports/Reports.constants'+import {+ BaseReportParams,+ DbQueryData,+ DbQueryHandler,+ MetaQueryResponse,+ DbQuery,+} from 'components/interfaces/Reports/Reports.types'+import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'+import { executeSql } from 'data/sql/execute-sql-query'++type UseDbQuery = (sql: DbQuery['sql'], params?: BaseReportParams) => [DbQueryData, DbQueryHandler]+const useDbQuery: UseDbQuery = (sql, params = DEFAULT_QUERY_PARAMS) => {+ const { project } = useProjectContext()++ const resolvedSql = typeof sql === 'function' ? sql(params) : sql++ const {+ data,+ error: rqError,+ isLoading,+ isRefetching,+ refetch,+ } = useQuery(+ ['projects', project?.ref, 'db', { ...params, sql: resolvedSql }],+ ({ signal }) => {+ return executeSql(+ {+ projectRef: project?.ref,+ connectionString: project?.connectionString,+ sql: resolvedSql,+ },+ signal+ ).then((res) => res.result) as Promise<MetaQueryResponse>+ },+ {+ enabled: Boolean(resolvedSql),+ refetchOnWindowFocus: false,+ refetchOnReconnect: false,+ }+ )++ const error = rqError || (typeof data === 'object' ? data?.error : '')+ return [+ { error, data, isLoading: isLoading || isRefetching, params },+ { runQuery: () => refetch() },+ ]+}++export default useDbQuerydiff --git a/studio/data/analytics/logs-query.ts b/studio/hooks/analytics/useLogsQuery.tsxsimilarity index 74%rename from studio/data/analytics/logs-query.tsrename to studio/hooks/analytics/useLogsQuery.tsxindex 1ed4af70440..2b0ca83e11d 100644--- a/studio/data/analytics/logs-query.ts+++ b/studio/hooks/analytics/useLogsQuery.tsx@@ -1,3 +1,4 @@+import { useQuery } from '@tanstack/react-query'import {EXPLORER_DATEPICKER_HELPERS,genQueryParams,@@ -7,9 +8,6 @@ import { Dispatch, SetStateAction, useState } from 'react'import { LogsEndpointParams, Logs, LogData } from 'components/interfaces/Settings/Logs/Logs.types'import { API_URL } from 'lib/constants'import { get } from 'lib/common/fetch'-import { useQuery } from '@tanstack/react-query'-import { analyticsKeys } from './keys'-export interface LogsQueryData {params: LogsEndpointParamsisLoading: boolean@@ -23,8 +21,7 @@ export interface LogsQueryHandlers {setParams: Dispatch<SetStateAction<LogsEndpointParams>>}-// API differs from the other query hooks, as it maintains compatibility with the old SWR hook.-export const useLogsQuery = (+const useLogsQuery = (projectRef: string,initialParams: Partial<LogsEndpointParams> = {}): [LogsQueryData, LogsQueryHandlers] => {@@ -40,6 +37,8 @@ export const useLogsQuery = (: defaultHelper.calcTo(),})+ const enabled = typeof projectRef !== 'undefined' && Boolean(params.sql)+const queryParams = genQueryParams(params as any)const {data,@@ -47,28 +46,19 @@ export const useLogsQuery = (isLoading,isRefetching,refetch,- } = useQuery<Logs>(- analyticsKeys.logs(projectRef, params),- async ({ signal }) => {- const response = await get(- `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${queryParams}`,- {- signal,- }- )-- if (response.error) {- throw response.error- }-- return response- },+ } = useQuery(+ ['projects', projectRef, 'logs', params],+ ({ signal }) =>+ get<Logs>(`${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${queryParams}`, {+ signal,+ }),{- enabled: typeof projectRef !== 'undefined' && Boolean(params.sql),+ enabled,refetchOnWindowFocus: false,})- let error: null | string | object = rqError ? (rqError as any)?.message : null++ let error: null | string | object = rqError ? (rqError as any).message : nullif (!error && data?.error) {error = data?.error@@ -80,10 +70,11 @@ export const useLogsQuery = (return [{params,- isLoading: isLoading || isRefetching,+ isLoading: (enabled && isLoading) || isRefetching,logData: data?.result ? data?.result : [],error,},{ changeQuery, runQuery: () => refetch(), setParams },]}+export default useLogsQuerydiff --git a/studio/pages/project/[ref]/logs/explorer/index.tsx b/studio/pages/project/[ref]/logs/explorer/index.tsxindex f7d9968f61e..2a93f527629 100644--- a/studio/pages/project/[ref]/logs/explorer/index.tsx+++ b/studio/pages/project/[ref]/logs/explorer/index.tsx@@ -5,7 +5,7 @@ import { observer } from 'mobx-react-lite'import { Input, Modal, Form, Button } from 'ui'import { useParams, useStore } from 'hooks'-import { useLogsQuery } from 'data/analytics/logs-query'+import useLogsQuery from 'hooks/analytics/useLogsQuery'import { useProjectSubscriptionQuery } from 'data/subscriptions/project-subscription-query'import { NextPageWithLayout, UserContent } from 'types'import { uuidv4 } from 'lib/helpers'diff --git a/studio/tests/helpers.tsx b/studio/tests/helpers.tsxindex 6893fc6e2b7..6aacb05b4de 100644--- a/studio/tests/helpers.tsx+++ b/studio/tests/helpers.tsx@@ -46,7 +46,7 @@ export const clickDropdown = (elem: HTMLElement) => {* A custom render function for react testing library* https://testing-library.com/docs/react-testing-library/setup/#custom-render*/-const SwrTestConfig: React.FC = ({ children }) => {+const ReactQueryTestConfig: React.FC = ({ children }) => {const [rootStore] = useState(() => new RootStore())const [queryClient] = useState(@@ -74,4 +74,4 @@ const SwrTestConfig: React.FC = ({ children }) => {}type renderParams = Parameters<typeof originalRender>export const render = ((ui: renderParams[0], options: renderParams[1]) =>- originalRender(ui, { wrapper: SwrTestConfig, ...options })) as typeof originalRender+ originalRender(ui, { wrapper: ReactQueryTestConfig, ...options })) as typeof originalRender