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
78 lines
2.9 KiB
TypeScript
78 lines
2.9 KiB
TypeScript
import { SEARCH_ENTITIES_MAX_QUERY_LENGTH } from '@supabase/pg-meta'
|
|
|
|
import type { EntitySearchPolicy, 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)
|
|
}
|
|
|
|
/**
|
|
* Client-side mirror of how policies are matched in getSearchEntitiesSql: the
|
|
* policy name contains the query, or the policy's table matches it. Used to
|
|
* narrow results from a previous search while the current one is still in
|
|
* flight.
|
|
*/
|
|
export function matchesPolicySearch(
|
|
{ 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({
|
|
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)}`
|
|
}
|
|
|
|
// The policies page lists policies per schema, so the schema must be set for
|
|
// the policy to be found and opened for editing.
|
|
export function getPolicyEditorRoute(
|
|
projectRef: string,
|
|
policy: Pick<EntitySearchPolicy, 'id' | 'schema'>
|
|
): `/${string}` {
|
|
return `/project/${projectRef}/database/policies?schema=${encodeURIComponent(policy.schema)}&edit=${policy.id}`
|
|
}
|