mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Context Probably the final piece for this series of changes towards resource aware Cmd K. We currently only present results for tables when searching in Cmd K, this PR adds in other entity types like views, materialized views, foreign tables, and partitioned tables (only the parent) Note that the results excludes entities created from database extensions to reduce the chance of noise <img width="500" alt="image" src="https://github.com/user-attachments/assets/27bf5026-937a-4883-b365-75694d551a39" /> ## Related changes - Tweaked the entity icon for partitioned tables a little to be blue (previously gray) and - Added an entity icon for child partition tables in the table editor that's slightly lighter than the partition table icon, currently child partition tables just render with the same icon as tables which can be confusing <img width="286" height="128" alt="image" src="https://github.com/user-attachments/assets/638af7c3-512d-4264-bfae-0a8827b3ef94" /> - Note that i'm planning to look into this separately RE how to better render child partition tables so this might go away subsequently ## Performance As always, ran a claude benchmarking session - this runs on debounced keystrokes against a live catalog, so it's benchmarked at scale: **45,653 `pg_class` rows** - 12k tables - 3,143 views - 3,002 matviews - 3,001 foreign tables) Taking a median of 15 runs, all variants measured against the same catalog in one process. | scenario | query | master | this PR | worst | |---|---|---|---|---| | exact table name | `t_500` | 19.5ms | 24.3ms | 29.1ms | | view name | `v_10` | 19.3ms | 23.7ms | 25.8ms | | foreign table name | `ft_10` | 18.9ms | 24.1ms | 26.9ms | | no match | `zzz_nothing` | 19.4ms | 24.0ms | 27.5ms | | broad prefix (~3k matches) | `t_1` | 30.8ms | 38.7ms | 45.1ms | | broad matview prefix | `mv_1` | 26.6ms | 37.5ms | 41.7ms |
102 lines
3.7 KiB
TypeScript
102 lines
3.7 KiB
TypeScript
import { SEARCH_ENTITIES_MAX_QUERY_LENGTH } from '@supabase/pg-meta'
|
|
|
|
import type {
|
|
EntitySearchFunction,
|
|
EntitySearchPolicy,
|
|
EntitySearchTable,
|
|
EntitySearchTrigger,
|
|
} from '@/data/database/entity-search-query'
|
|
|
|
// Each search runs SQL against the user's database, so keep it to queries that
|
|
// are specific enough to be useful: short queries match too broadly to be worth
|
|
// the round-trip.
|
|
export const ENTITY_SEARCH_MIN_QUERY_LENGTH = 3
|
|
export const ENTITY_SEARCH_DEBOUNCE_MS = 300
|
|
|
|
export function getEntitySearchQuery(query: string) {
|
|
const trimmed = query.trim()
|
|
const isSearchable =
|
|
trimmed.length >= ENTITY_SEARCH_MIN_QUERY_LENGTH &&
|
|
trimmed.length <= SEARCH_ENTITIES_MAX_QUERY_LENGTH
|
|
return { query: trimmed, isSearchable }
|
|
}
|
|
|
|
/**
|
|
* Client-side mirror of the matching in getSearchEntitiesSql: the name contains
|
|
* the query, or for `schema.name` queries, the schema starts with the part
|
|
* before the first dot and the name contains the part after it. Used to narrow
|
|
* results from a previous search while the current one is still in flight.
|
|
*/
|
|
export function matchesEntitySearch(
|
|
{ schema, name }: Pick<EntitySearchTable, 'schema' | 'name'>,
|
|
query: string
|
|
) {
|
|
const lowerQuery = query.toLowerCase()
|
|
const lowerName = name.toLowerCase()
|
|
if (lowerName.includes(lowerQuery)) return true
|
|
|
|
const dotIndex = lowerQuery.indexOf('.')
|
|
if (dotIndex === -1) return false
|
|
|
|
const schemaPart = lowerQuery.slice(0, dotIndex)
|
|
const namePart = lowerQuery.slice(dotIndex + 1)
|
|
return schema.toLowerCase().startsWith(schemaPart) && lowerName.includes(namePart)
|
|
}
|
|
|
|
/**
|
|
* Client-side mirror of how policies and triggers are matched in
|
|
* getSearchEntitiesSql: the entity's own name contains the query, or its table
|
|
* matches it. Used to narrow results from a previous search while the current
|
|
* one is still in flight.
|
|
*/
|
|
export function matchesTableRelatedSearch(
|
|
{ name, schema, table }: Pick<EntitySearchPolicy, 'name' | 'schema' | 'table'>,
|
|
query: string
|
|
) {
|
|
if (name.toLowerCase().includes(query.toLowerCase())) return true
|
|
return matchesEntitySearch({ schema, name: table }, query)
|
|
}
|
|
|
|
export function formatTableSearchDetails(table: EntitySearchTable) {
|
|
if (table.type === 'r' || table.type === 'p') {
|
|
const policies = `${table.policy_count} ${table.policy_count === 1 ? 'policy' : 'policies'}`
|
|
return `${policies} • RLS ${table.rls_enabled ? 'enabled' : 'disabled'}`
|
|
}
|
|
if (table.type === 'v') return table.security_invoker ? 'Security invoker' : 'Security definer'
|
|
return ''
|
|
}
|
|
|
|
// Triggers found through their function don't match by name or table, so say which
|
|
// function they run instead of their timing
|
|
export function formatTriggerSearchDetails({
|
|
activation,
|
|
events,
|
|
schema,
|
|
table,
|
|
matched_function,
|
|
}: Pick<EntitySearchTrigger, 'activation' | 'events' | 'schema' | 'table' | 'matched_function'>) {
|
|
if (matched_function) return `Runs ${matched_function}`
|
|
return `${activation} ${events.join(' OR ')} on ${schema}.${table}`
|
|
}
|
|
|
|
/**
|
|
* Client-side mirror of how triggers are matched in getSearchEntitiesSql: like
|
|
* policies, by name or table, plus triggers found through the function they run.
|
|
*/
|
|
export function matchesTriggerSearch(
|
|
trigger: Pick<EntitySearchTrigger, 'name' | 'schema' | 'table' | 'matched_function'>,
|
|
query: string
|
|
) {
|
|
if (matchesTableRelatedSearch(trigger, query)) return true
|
|
return trigger.matched_function?.toLowerCase().includes(query.toLowerCase()) ?? false
|
|
}
|
|
|
|
export function formatFunctionSearchDetails({
|
|
type,
|
|
args,
|
|
}: Pick<EntitySearchFunction, 'type' | 'args'>) {
|
|
const procedure = type === 'procedure' ? 'procedure' : ''
|
|
if (args.length === 0) return procedure
|
|
return `${procedure} (${args})`.trim()
|
|
}
|