Skip to content

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

Feat/sortby on tables (#19841)

Terry Suttonc685d654b6b4
diff --git a/apps/studio/components/interfaces/Database/Tables/TableList.tsx b/apps/studio/components/interfaces/Database/Tables/TableList.tsx
index c8e6ccfa045..0e726a36861 100644
--- a/apps/studio/components/interfaces/Database/Tables/TableList.tsx
+++ b/apps/studio/components/interfaces/Database/Tables/TableList.tsx
@@ -72,6 +72,7 @@ const TableList = ({
projectRef: project?.ref,
connectionString: project?.connectionString,
schema: snap.selectedSchemaName,
+ sortByProperty: 'name',
},
{
select(tables) {
diff --git a/apps/studio/data/tables/tables-query.ts b/apps/studio/data/tables/tables-query.ts
index eefe5c92879..eb4ad2846a3 100644
--- a/apps/studio/data/tables/tables-query.ts
+++ b/apps/studio/data/tables/tables-query.ts
@@ -1,22 +1,20 @@
import { PostgresTable } from '@supabase/postgres-meta'
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { get } from 'data/fetchers'
+import { sortBy } from 'lodash'
import { useCallback } from 'react'
import { ResponseError } from 'types'
import { tableKeys } from './keys'
-import { Table } from './table-query'
export type TablesVariables = {
projectRef?: string
connectionString?: string
schema?: string
+ sortByProperty?: keyof PostgresTable
}
-export type TablesResponse = Table[] | { error?: any }
-
export async function getTables(
- { projectRef, connectionString, schema }: TablesVariables,
+ { projectRef, connectionString, schema, sortByProperty = 'name' }: TablesVariables,
signal?: AbortSignal
) {
if (!projectRef) {
@@ -26,23 +24,34 @@ export async function getTables(
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- let queryParams = new URLSearchParams()
- if (schema) queryParams.set('included_schemas', schema)
- const searchStr = queryParams.toString()
+ let queryParams = {}
+ if (schema) {
+ queryParams = { included_schemas: schema }
+ }
- const response = (await get(
- `${API_URL}/pg-meta/${projectRef}/tables${searchStr ? `?${searchStr}` : ''}`,
- {
- headers: Object.fromEntries(headers),
- signal,
- }
- )) as TablesResponse
+ const { data, error } = await get('/platform/pg-meta/{ref}/tables', {
+ params: {
+ header: {
+ 'x-connection-encrypted': connectionString!,
+ },
+ path: {
+ ref: projectRef,
+ },
+ query: queryParams as any,
+ },
+ headers,
+ signal,
+ })
- if (!Array.isArray(response) && response.error) {
- throw response.error
+ if (!Array.isArray(data) && error) {
+ throw error
}
- return response as PostgresTable[]
+ // Sort the data if the sortByName option is true
+ if (Array.isArray(data) && sortByProperty) {
+ return sortBy(data, (t) => t[sortByProperty]) as PostgresTable[]
+ }
+ return data as PostgresTable[]
}
export type TablesData = Awaited<ReturnType<typeof getTables>>