Files
supabase/apps/studio/components/interfaces/App/CommandMenu/DatabaseEntitySearch.utils.ts
T
Joshen Lim 7d63d764fc Joshenlim/fe 4712 cmd k support searching for database tables (#52205)
## Context

Looking into some kaizen improvements for Cmd+K, with the overarching
idea 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 the foundation here
by starting with just the tables.
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/65054781-2385-416c-aed9-f142380e4454"
/>

This might be similar to how we currently support searching for database
entities via these "Queries" section, but these only search within the
context of the entity category, not across (e.g only tables, only
policies)
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/66ca16c6-9551-4134-b1e1-0b0c4b004085"
/>

The idea isn't to replace this, but provide another way to find your
database entities so that
- You can see related entities to what you're searching at a glance
- You don't need to go to a 2nd level nav in Cmd K 

Also fwiw the bulk of the LOC changes in here are mainly tests actually,
actual LOC changes are +385, -22
2026-10-23 23:24:56 +08:00

55 lines
2.0 KiB
TypeScript

import { SEARCH_ENTITIES_MAX_QUERY_LENGTH } from '@supabase/pg-meta'
import type { EntitySearchTable } 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)
}
export function formatTableSearchDetails({
policy_count,
rls_enabled,
}: Pick<EntitySearchTable, 'policy_count' | 'rls_enabled'>) {
const policies = `${policy_count} ${policy_count === 1 ? 'policy' : 'policies'}`
return `${policies} • RLS ${rls_enabled ? 'enabled' : 'disabled'}`
}
export function getTableEditorRoute(
projectRef: string,
table: Pick<EntitySearchTable, 'id' | 'schema'>
): `/${string}` {
return `/project/${projectRef}/editor/${table.id}?schema=${encodeURIComponent(table.schema)}`
}