mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 20:35:07 +03:00
## Context Related to dashboard scalability Previous PR: https://github.com/supabase/supabase/pull/42856 Note: Changes are all feature flagged still and I'm still not entirely convinced with the current UX Will iterate as as go along, and only make this publicly available when we're satisfied with the behaviour Adds a "Dashboard preference" section to the project settings <img width="265" height="740" alt="image" src="https://github.com/user-attachments/assets/6ce1aa19-26c2-47c6-a9c4-595137266631" /> In which users can then select which database they'd like to use for read queries run from the dashboard Note: Everything is local storage for now, but we'd need middleware support if we want to make this setting persist for all users on the project <img width="791" height="434" alt="image" src="https://github.com/user-attachments/assets/e651d6d9-fed4-4da4-b552-c9f93f8d46d3" /> Added a dialog as well to further explain what this implies <img width="610" height="312" alt="image" src="https://github.com/user-attachments/assets/0aa957af-cb51-476f-aa79-8948a7cbe5ae" /> ## To test - Choosing a replica in dashboard preferences will only affect the table editor as thats the only place that's set up so far to use a replica for read queries (I'll need to follow up for other parts of the dashboard in subsequent PRs)
110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { LOCAL_STORAGE_KEYS, useFeatureFlags, useFlag, useParams } from 'common'
|
|
import type { components } from 'data/api'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
|
|
import { replicaKeys } from './keys'
|
|
import { DashboardPreference } from '@/components/interfaces/Settings/General/DashboardPreferences'
|
|
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
|
|
export const MAX_REPLICAS_BELOW_XL = 2
|
|
export const MAX_REPLICAS_ABOVE_XL = 5
|
|
|
|
export type ReadReplicasVariables = {
|
|
projectRef?: string
|
|
}
|
|
|
|
export type Database = components['schemas']['DatabaseDetailResponse']
|
|
|
|
export async function getReadReplicas({ projectRef }: ReadReplicasVariables, signal?: AbortSignal) {
|
|
if (!projectRef) throw new Error('Project ref is required')
|
|
|
|
const { data, error } = await get(`/platform/projects/{ref}/databases`, {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type ReadReplicasData = Awaited<ReturnType<typeof getReadReplicas>>
|
|
export type ReadReplicasError = ResponseError
|
|
|
|
export const useReadReplicasQuery = <TData = ReadReplicasData>(
|
|
{ projectRef }: ReadReplicasVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<ReadReplicasData, ReadReplicasError, TData> = {}
|
|
) => {
|
|
return useQuery<ReadReplicasData, ReadReplicasError, TData>({
|
|
queryKey: replicaKeys.list(projectRef),
|
|
queryFn: ({ signal }) => getReadReplicas({ projectRef }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const usePrimaryDatabase = ({ projectRef }: { projectRef?: string }) => {
|
|
const {
|
|
data: databases = [],
|
|
error,
|
|
isPending: isLoading,
|
|
isError,
|
|
isSuccess,
|
|
} = useReadReplicasQuery({ projectRef })
|
|
const primaryDatabase = databases.find((x) => x.identifier === projectRef)
|
|
return { database: primaryDatabase, error, isLoading, isError, isSuccess }
|
|
}
|
|
|
|
/**
|
|
* [Joshen] JFYI this logic here can and should be optimized
|
|
* Returns the connection string of read replica if available, otherwise default to project's (primary)
|
|
* If multiple read replicas available, (naively) prioritise replica in the same region as primary
|
|
* to minimize any latency. Otherwise just use the first available read replica
|
|
*/
|
|
export const useConnectionStringForReadOps = (): {
|
|
type: 'replica' | 'primary' | undefined
|
|
identifier: string | undefined
|
|
connectionString: string | undefined | null
|
|
} => {
|
|
const { ref: projectRef } = useParams()
|
|
const { hasLoaded: flagsLoaded } = useFeatureFlags()
|
|
const showDashboardPreferences = useFlag('dashboardPreferences')
|
|
|
|
const [dashboardPreferences, , { isLoading: isLoadingLocalStorage }] =
|
|
useLocalStorageQuery<DashboardPreference>(
|
|
LOCAL_STORAGE_KEYS.DASHBOARD_PREFERENCES(projectRef ?? '_'),
|
|
{}
|
|
)
|
|
|
|
const { data: project, isSuccess: isSuccessProject } = useSelectedProjectQuery()
|
|
const { data: databases = [], isLoading: isLoadingDatabases } = useReadReplicasQuery({
|
|
projectRef: project?.ref,
|
|
})
|
|
|
|
if (!isSuccessProject || isLoadingDatabases || !flagsLoaded || isLoadingLocalStorage) {
|
|
return { connectionString: undefined, type: undefined, identifier: undefined }
|
|
}
|
|
|
|
if (!showDashboardPreferences) {
|
|
return { type: 'primary', identifier: project.ref, connectionString: project.connectionString }
|
|
}
|
|
|
|
const readReplicas = databases.filter(
|
|
(x) => x.identifier !== project?.ref && x.status === 'ACTIVE_HEALTHY'
|
|
)
|
|
const readReplica = readReplicas.find(
|
|
(x) => x.identifier === dashboardPreferences.defaultDatabase
|
|
)
|
|
|
|
return {
|
|
type: !!readReplica ? 'replica' : 'primary',
|
|
identifier: !!readReplica ? readReplica.identifier : project.ref,
|
|
connectionString: !!readReplica ? readReplica.connectionString : project.connectionString,
|
|
}
|
|
}
|