mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Context Continue to build off from [here](https://github.com/supabase/supabase/pull/52205) to make Cmd+K resource-aware so finding and inspecting anything in a project doesn't depend on knowing where it lives in the dashboard. Specifically looking into database entities for now - end state would be that you could search for a table name for example, and Cmd K will pull up everything related to that table for you to skim (e.g Policies, Functions, Triggers, etc). Changes in this PR sets up searching for RLS policies. <img width="609" height="525" alt="image" src="https://github.com/user-attachments/assets/212a430a-314d-48c4-a27c-2f9bb06e8457" /> Policies are matched on, in priority, its name and the target table. ## Performance checking Just ran a check with Claude just to make sure that the added SQL doesn't cause any performance degradation on the database. Plan guard passes at 12,000 tables, under its 500ms budget. Also benchmarked locally with a 3-character query matching every policy, results look good so should be alright: | Catalog | Worst-case search | No-match baseline | |---|---|---| | 36k policies | ~52–64ms | ~16ms | | 120k policies | ~118–134ms | ~63ms | ## To test - [x] Search a table name: Tables first, then its policies, under Policies - [x] Search a policy name: it appears under Policies and opens the editor panel in database/policies - [x] Policy outside `public`: schema filter switches and the policy opens
119 lines
4.6 KiB
TypeScript
119 lines
4.6 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { useDebounce } from '@uidotdev/usehooks'
|
|
import { DatabaseIcon, ShieldIcon } from 'lucide-react'
|
|
import type { ICommand } from 'ui-patterns/CommandMenu'
|
|
import {
|
|
useCommandMenuOpen,
|
|
useQuery as useCommandMenuQuery,
|
|
useRegisterCommands,
|
|
} from 'ui-patterns/CommandMenu'
|
|
|
|
import { COMMAND_MENU_SECTIONS } from './CommandMenu.utils'
|
|
import {
|
|
ENTITY_SEARCH_DEBOUNCE_MS,
|
|
formatTableSearchDetails,
|
|
getEntitySearchQuery,
|
|
getPolicyEditorRoute,
|
|
getTableEditorRoute,
|
|
matchesEntitySearch,
|
|
matchesPolicySearch,
|
|
} from './DatabaseEntitySearch.utils'
|
|
import { orderCommandSectionsByPriority } from './ordering'
|
|
import { entitySearchQueryOptions } from '@/data/database/entity-search-query'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
|
|
// Tables and policies are ranked as one unit by the command menu's relevance
|
|
// sort, so a policy that matches better than its table doesn't jump above it
|
|
const ENTITY_SEARCH_RELEVANCE_GROUP = 'database-entity-search'
|
|
|
|
/**
|
|
* Searches the project's database entities by name as the user types in the
|
|
* command menu, and registers matches as their own sections (tables,
|
|
* policies). Filtering happens in the database (see getSearchEntitiesSql), so
|
|
* results are force-mounted to stop cmdk from filtering them a second time.
|
|
*/
|
|
export function useDatabaseEntitySearchCommands() {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const isOpen = useCommandMenuOpen()
|
|
const commandMenuQuery = useCommandMenuQuery()
|
|
const debouncedQuery = useDebounce(commandMenuQuery, ENTITY_SEARCH_DEBOUNCE_MS)
|
|
const currentSearch = getEntitySearchQuery(commandMenuQuery)
|
|
const debouncedSearch = getEntitySearchQuery(debouncedQuery)
|
|
|
|
const { data, isPlaceholderData } = useQuery(
|
|
entitySearchQueryOptions(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
query: debouncedSearch.query,
|
|
},
|
|
{ enabled: isOpen && debouncedSearch.isSearchable }
|
|
)
|
|
)
|
|
|
|
// Results lag behind the input (debounce, request in flight, placeholder data
|
|
// from the previous search). Until they catch up, only keep the ones that
|
|
// still match what's typed, so e.g. `blog_posts` results never show for
|
|
// `colors`, while refining `blog` to `blog_p` narrows smoothly.
|
|
const canShowResults = isOpen && currentSearch.isSearchable
|
|
const isStale = isPlaceholderData || debouncedSearch.query !== currentSearch.query
|
|
const getVisibleResults = <T,>(results: T[], matches: (result: T, query: string) => boolean) => {
|
|
if (!canShowResults) return []
|
|
return isStale ? results.filter((result) => matches(result, currentSearch.query)) : results
|
|
}
|
|
const tables = getVisibleResults(data?.tables ?? [], matchesEntitySearch)
|
|
const policies = getVisibleResults(data?.policies ?? [], matchesPolicySearch)
|
|
const projectRef = project?.ref ?? '_'
|
|
|
|
const tableCommands: ICommand[] = tables.map((table) => {
|
|
const displayName = `${table.schema}.${table.name}`
|
|
return {
|
|
id: `entity-search-table-${table.id}`,
|
|
name: displayName,
|
|
value: `entity-search-table-${table.id} ${displayName}`,
|
|
forceMount: true,
|
|
icon: () => <DatabaseIcon />,
|
|
badge: () => (
|
|
<span className="text-xs text-foreground-lighter truncate pl-4 shrink-0">
|
|
{formatTableSearchDetails(table)}
|
|
</span>
|
|
),
|
|
route: getTableEditorRoute(projectRef, table),
|
|
}
|
|
})
|
|
|
|
const policyCommands: ICommand[] = policies.map((policy) => {
|
|
const tableName = `${policy.schema}.${policy.table}`
|
|
return {
|
|
id: `entity-search-policy-${policy.id}`,
|
|
name: policy.name,
|
|
value: `entity-search-policy-${policy.id} ${policy.name} ${tableName}`,
|
|
forceMount: true,
|
|
icon: () => <ShieldIcon />,
|
|
badge: () => (
|
|
<span className="text-xs text-foreground-lighter truncate pl-4 shrink-0">
|
|
{policy.command} on {tableName}
|
|
</span>
|
|
),
|
|
route: getPolicyEditorRoute(projectRef, policy),
|
|
}
|
|
})
|
|
|
|
useRegisterCommands(COMMAND_MENU_SECTIONS.DATABASE_TABLES, tableCommands, {
|
|
deps: [tables, projectRef],
|
|
enabled: tableCommands.length > 0,
|
|
forceMountSection: true,
|
|
orderSection: orderCommandSectionsByPriority,
|
|
sectionMeta: { priority: 2, relevanceGroup: ENTITY_SEARCH_RELEVANCE_GROUP },
|
|
})
|
|
|
|
useRegisterCommands(COMMAND_MENU_SECTIONS.DATABASE_POLICIES, policyCommands, {
|
|
deps: [policies, projectRef],
|
|
enabled: policyCommands.length > 0,
|
|
forceMountSection: true,
|
|
orderSection: orderCommandSectionsByPriority,
|
|
// Ranked together with Tables, and listed after it
|
|
sectionMeta: { priority: 3, relevanceGroup: ENTITY_SEARCH_RELEVANCE_GROUP },
|
|
})
|
|
}
|