Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

adds search to table editor entities

Alaister Young3e720574b220
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
index 9664b9700de..f1af1edd7ba 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
@@ -52,13 +52,26 @@ const TableEditorMenu = ({
const [searchText, setSearchText] = useState<string>('')
const { project } = useProjectContext()
- const { data, isLoading, refetch, isRefetching, hasNextPage, isFetchingNextPage, fetchNextPage } =
- useEntityTypesQuery({
+ const {
+ data,
+ isLoading,
+ refetch,
+ isRefetching,
+ hasNextPage,
+ isFetchingNextPage,
+ fetchNextPage,
+ isPreviousData: isSearching,
+ } = useEntityTypesQuery(
+ {
projectRef: project?.ref,
connectionString: project?.connectionString,
schema: selectedSchema,
- // search: searchText.trim().toLowerCase()
- })
+ search: searchText || undefined,
+ },
+ {
+ keepPreviousData: true,
+ }
+ )
const totalCount = data?.pages?.[0].data.count
const entityTypes = useMemo(
@@ -183,9 +196,15 @@ const TableEditorMenu = ({
<div className="mb-2 block px-3">
<Input
className="table-editor-search border-none"
- icon={<IconSearch className="text-scale-900" size={12} strokeWidth={1.5} />}
+ icon={
+ isSearching ? (
+ <IconLoader className="animate-spin text-scale-900" size={12} strokeWidth={1.5} />
+ ) : (
+ <IconSearch className="text-scale-900" size={12} strokeWidth={1.5} />
+ )
+ }
placeholder="Search tables"
- onChange={(e) => setSearchText(e.target.value)}
+ onChange={(e) => setSearchText(e.target.value.trim())}
value={searchText}
size="tiny"
actions={
@@ -209,6 +228,11 @@ const TableEditorMenu = ({
<p className="text-xs">No entities available</p>
<p className="text-xs text-scale-1100">This schema has no entities available yet</p>
</div>
+ ) : searchText.length > 0 && (entityTypes?.length ?? 0) === 0 ? (
+ <div className="mx-7 space-y-1 rounded-md border border-scale-400 bg-scale-300 py-3 px-4">
+ <p className="text-xs">No results found</p>
+ <p className="text-xs text-scale-1100">There are no entities that match your search</p>
+ </div>
) : (
<Menu
type="pills"
@@ -254,13 +278,6 @@ const TableEditorMenu = ({
</div>
</Menu>
)}
-
- {searchText.length > 0 && (entityTypes?.length ?? 0) === 0 && (
- <div className="!mt-0 mx-7 space-y-1 rounded-md border border-scale-400 bg-scale-300 py-3 px-4">
- <p className="text-xs">No results found</p>
- <p className="text-xs text-scale-1100">There are no entities that match your search</p>
- </div>
- )}
</div>
)
}
diff --git a/studio/data/entity-types/entity-types-infinite-query.ts b/studio/data/entity-types/entity-types-infinite-query.ts
index 1a0d9e7f82f..1c7b6b1b7be 100644
--- a/studio/data/entity-types/entity-types-infinite-query.ts
+++ b/studio/data/entity-types/entity-types-infinite-query.ts
@@ -6,6 +6,7 @@ import { entityTypeKeys } from './keys'
export type EntityTypesVariables = {
projectRef?: string
schema?: string
+ search?: string
limit?: number
page?: number
} & Pick<ExecuteSqlVariables, 'connectionString'>
@@ -18,7 +19,14 @@ export type EntityTypesResponse = {
}
export async function getEntityTypes(
- { projectRef, connectionString, schema = 'public', limit = 100, page = 0 }: EntityTypesVariables,
+ {
+ projectRef,
+ connectionString,
+ schema = 'public',
+ search,
+ limit = 100,
+ page = 0,
+ }: EntityTypesVariables,
signal?: AbortSignal
) {
const sql = /* SQL */ `
@@ -51,6 +59,7 @@ export async function getEntityTypes(
or has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES')
)
and nc.nspname = '${schema}'
+ ${search ? `and c.relname ilike '%${search}%'` : ''}
order by "name" asc
limit ${limit}
offset ${page * limit}
@@ -77,6 +86,7 @@ export async function getEntityTypes(
projectRef,
connectionString,
sql,
+ queryKey: ['public', 'entity-types'],
},
signal
)
@@ -92,6 +102,7 @@ export const useEntityTypesQuery = <TData = EntityTypesData>(
projectRef,
connectionString,
schema = 'public',
+ search,
limit = 100,
}: Omit<EntityTypesVariables, 'page'>,
{
@@ -100,9 +111,12 @@ export const useEntityTypesQuery = <TData = EntityTypesData>(
}: UseInfiniteQueryOptions<EntityTypesData, EntityTypesError, TData> = {}
) =>
useInfiniteQuery<EntityTypesData, EntityTypesError, TData>(
- entityTypeKeys.list(projectRef),
+ entityTypeKeys.list(projectRef, search),
({ signal, pageParam }) =>
- getEntityTypes({ projectRef, connectionString, schema, limit, page: pageParam }, signal),
+ getEntityTypes(
+ { projectRef, connectionString, schema, search, limit, page: pageParam },
+ signal
+ ),
{
enabled:
enabled && typeof projectRef !== 'undefined' && typeof connectionString !== 'undefined',
diff --git a/studio/data/entity-types/keys.ts b/studio/data/entity-types/keys.ts
index b58ad8e80a1..7444a19f161 100644
--- a/studio/data/entity-types/keys.ts
+++ b/studio/data/entity-types/keys.ts
@@ -1,3 +1,4 @@
export const entityTypeKeys = {
- list: (projectRef: string | undefined) => ['projects', projectRef, 'entity-types'] as const,
+ list: (projectRef: string | undefined, search: string | undefined) =>
+ ['projects', projectRef, 'entity-types', { search }] as const,
}
diff --git a/studio/data/sql/execute-sql-query.ts b/studio/data/sql/execute-sql-query.ts
index be2f598d8a5..1eb86b56373 100644
--- a/studio/data/sql/execute-sql-query.ts
+++ b/studio/data/sql/execute-sql-query.ts
@@ -23,7 +23,8 @@ export async function executeSql(
projectRef,
connectionString,
sql,
- }: Pick<ExecuteSqlVariables, 'projectRef' | 'connectionString' | 'sql'>,
+ queryKey,
+ }: Pick<ExecuteSqlVariables, 'projectRef' | 'connectionString' | 'sql' | 'queryKey'>,
signal?: AbortSignal
) {
if (!projectRef) {
@@ -37,7 +38,9 @@ export async function executeSql(
}
const response = await post(
- `${API_URL}/pg-meta/${projectRef}/query`,
+ `${API_URL}/pg-meta/${projectRef}/query${
+ queryKey ? `?key=${queryKey.filter((seg) => typeof seg === 'string').join('-')}` : ''
+ }`,
{ query: sql },
{ headers: Object.fromEntries(headers), signal }
)
@@ -57,7 +60,7 @@ export const useExecuteSqlQuery = <TData = ExecuteSqlData>(
) =>
useQuery<ExecuteSqlData, ExecuteSqlError, TData>(
sqlKeys.query(projectRef, queryKey ?? [md5(sql)]),
- ({ signal }) => executeSql({ projectRef, connectionString, sql }, signal),
+ ({ signal }) => executeSql({ projectRef, connectionString, sql, queryKey }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)
@@ -66,7 +69,7 @@ export const prefetchExecuteSql = (
{ projectRef, connectionString, sql, queryKey }: ExecuteSqlVariables
) => {
return client.prefetchQuery(sqlKeys.query(projectRef, queryKey ?? [md5(sql)]), ({ signal }) =>
- executeSql({ projectRef, connectionString, sql }, signal)
+ executeSql({ projectRef, connectionString, sql, queryKey }, signal)
)
}