Skip to content

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

Allow schema selection in database functions page, and show all available functions

Joshen Lim13125cfa6278
diff --git a/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx
index 7ce5476a2b5..2ab12bd3549 100644
--- a/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx
+++ b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx
@@ -12,6 +12,7 @@ import { useCheckPermissions, useStore } from 'hooks'
interface FunctionListProps {
schema: string
filterString: string
+ isLocked: boolean
editFunction: (fn: any) => void
deleteFunction: (fn: any) => void
}
@@ -19,13 +20,15 @@ interface FunctionListProps {
const FunctionList = ({
schema,
filterString,
+ isLocked,
editFunction = noop,
deleteFunction = noop,
}: FunctionListProps) => {
- const { project: selectedProject } = useProjectContext()
const router = useRouter()
const { meta } = useStore()
- const functions = meta.functions.list((fn: any) => !meta.excludedSchemas.includes(fn.schema))
+ const { project: selectedProject } = useProjectContext()
+
+ const functions = meta.functions.list()
const filteredFunctions = functions.filter((x: any) =>
includes(x.name.toLowerCase(), filterString.toLowerCase())
)
@@ -33,6 +36,8 @@ const FunctionList = ({
const isApiDocumentAvailable = schema == 'public'
const projectRef = selectedProject?.ref
+ console.log({ filteredFunctions })
+
const canUpdateFunctions = useCheckPermissions(
PermissionAction.TENANT_SQL_ADMIN_WRITE,
'functions'
@@ -46,6 +51,30 @@ const FunctionList = ({
deleteFunction(func)
}
+ if (_functions.length === 0 && filterString.length === 0) {
+ return (
+ <Table.tr key={schema}>
+ <Table.td colSpan={4}>
+ <p className="text-sm text-scale-1200">No functions created yet</p>
+ <p className="text-sm text-light">There are no indexes found in the schema "{schema}"</p>
+ </Table.td>
+ </Table.tr>
+ )
+ }
+
+ if (_functions.length === 0 && filterString.length > 0) {
+ return (
+ <Table.tr key={schema}>
+ <Table.td colSpan={4}>
+ <p className="text-sm text-scale-1200">No results found</p>
+ <p className="text-sm text-light">
+ Your search for "{filterString}" did not return any results
+ </p>
+ </Table.td>
+ </Table.tr>
+ )
+ }
+
return (
<>
{_functions.map((x) => (
@@ -60,59 +89,61 @@ const FunctionList = ({
<p>{x.return_type}</p>
</Table.td>
<Table.td className="text-right">
- <div className="flex items-center justify-end">
- {canUpdateFunctions ? (
- <Dropdown
- side="left"
- overlay={
- <>
- {isApiDocumentAvailable && (
+ {!isLocked && (
+ <div className="flex items-center justify-end">
+ {canUpdateFunctions ? (
+ <Dropdown
+ side="left"
+ overlay={
+ <>
+ {isApiDocumentAvailable && (
+ <Dropdown.Item
+ icon={<IconFileText size="tiny" />}
+ onClick={() => router.push(`/project/${projectRef}/api?rpc=${x.name}`)}
+ >
+ Client API docs
+ </Dropdown.Item>
+ )}
+ <Dropdown.Item icon={<IconEdit3 size="tiny" />} onClick={() => onEdit(x)}>
+ Edit function
+ </Dropdown.Item>
<Dropdown.Item
- icon={<IconFileText size="tiny" />}
- onClick={() => router.push(`/project/${projectRef}/api?rpc=${x.name}`)}
+ icon={<IconTrash stroke="red" size="tiny" />}
+ onClick={() => onDelete(x)}
>
- Client API docs
+ Delete function
</Dropdown.Item>
- )}
- <Dropdown.Item icon={<IconEdit3 size="tiny" />} onClick={() => onEdit(x)}>
- Edit function
- </Dropdown.Item>
- <Dropdown.Item
- icon={<IconTrash stroke="red" size="tiny" />}
- onClick={() => onDelete(x)}
- >
- Delete function
- </Dropdown.Item>
- </>
- }
- >
- <Button asChild type="default" icon={<IconMoreVertical />}>
- <span></span>
- </Button>
- </Dropdown>
- ) : (
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger asChild>
- <Button disabled type="default" icon={<IconMoreVertical />} />
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="left">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">
- You need additional permissions to update functions
- </span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- )}
- </div>
+ </>
+ }
+ >
+ <Button asChild type="default" icon={<IconMoreVertical />}>
+ <span></span>
+ </Button>
+ </Dropdown>
+ ) : (
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger asChild>
+ <Button disabled type="default" icon={<IconMoreVertical />} />
+ </Tooltip.Trigger>
+ <Tooltip.Portal>
+ <Tooltip.Content side="left">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">
+ You need additional permissions to update functions
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ </Tooltip.Root>
+ )}
+ </div>
+ )}
</Table.td>
</Table.tr>
))}
diff --git a/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx
index 92a90ea0d6e..0ec8addd118 100644
--- a/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx
+++ b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx
@@ -1,14 +1,15 @@
import * as Tooltip from '@radix-ui/react-tooltip'
-import { PostgresFunction } from '@supabase/postgres-meta'
+import { PostgresFunction, PostgresSchema } from '@supabase/postgres-meta'
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { includes, map as lodashMap, noop, uniqBy } from 'lodash'
+import { includes, map as lodashMap, noop, partition, uniqBy } from 'lodash'
import { observer } from 'mobx-react-lite'
import { useState } from 'react'
-import { Button, IconLoader, IconSearch, Input } from 'ui'
+import { Button, IconLoader, IconLock, IconSearch, Input, Listbox } from 'ui'
import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
+import Table from 'components/to-be-cleaned/Table'
import { useCheckPermissions, useStore } from 'hooks'
-import SchemaTable from './SchemaTable'
+import FunctionList from './FunctionList'
interface FunctionsListProps {
createFunction: () => void
@@ -22,20 +23,30 @@ const FunctionsList = ({
deleteFunction = noop,
}: FunctionsListProps) => {
const { meta } = useStore()
+ const [selectedSchema, setSelectedSchema] = useState<string>('public')
const [filterString, setFilterString] = useState<string>('')
- const functions = meta.functions.list(
- (fn: PostgresFunction) => !meta.excludedSchemas.includes(fn.schema)
- )
- const filteredFunctions = functions.filter((x: PostgresFunction) =>
- includes(x.name?.toLowerCase(), filterString.toLowerCase())
- )
- const filteredFunctionSchemas = lodashMap(uniqBy(filteredFunctions, 'schema'), 'schema')
const canCreateFunctions = useCheckPermissions(
PermissionAction.TENANT_SQL_ADMIN_WRITE,
'functions'
)
+ const schemas: PostgresSchema[] = meta.schemas.list()
+ const [protectedSchemas, openSchemas] = partition(schemas, (schema) =>
+ meta.excludedSchemas.includes(schema?.name ?? '')
+ )
+ const schema = schemas.find((schema) => schema.name === selectedSchema)
+ const isLocked = protectedSchemas.some((s) => s.id === schema?.id)
+
+ const functions = meta.functions.list()
+ const filteredFunctions = functions.filter(
+ (x: PostgresFunction) =>
+ x.schema === selectedSchema && includes(x.name?.toLowerCase(), filterString.toLowerCase())
+ )
+ const filteredFunctionSchemas = lodashMap(uniqBy(filteredFunctions, 'schema'), 'schema')
+
+ console.log({ functions, filteredFunctions })
+
if (meta.functions.isLoading) {
return (
<div className="flex h-full w-full items-center justify-center space-x-2">
@@ -76,14 +87,63 @@ const FunctionsList = ({
</div>
) : (
<div className="w-full space-y-4 py-4">
- <div className="flex items-center justify-between px-6">
- <Input
- placeholder="Filter by name"
- size="small"
- icon={<IconSearch size="tiny" />}
- value={filterString}
- onChange={(e) => setFilterString(e.target.value)}
- />
+ <div className="flex items-center justify-between px-4">
+ <div className="flex items-center space-x-4">
+ <div className="w-[260px]">
+ <Listbox
+ size="small"
+ value={selectedSchema}
+ onChange={setSelectedSchema}
+ icon={isLocked && <IconLock size={14} strokeWidth={2} />}
+ >
+ <Listbox.Option
+ disabled
+ key="normal-schemas"
+ value="normal-schemas"
+ label="Schemas"
+ >
+ <p className="text-sm">Schemas</p>
+ </Listbox.Option>
+ {/* @ts-ignore */}
+ {openSchemas.map((schema) => (
+ <Listbox.Option
+ key={schema.id}
+ value={schema.name}
+ label={schema.name}
+ addOnBefore={() => <span className="text-scale-900">schema</span>}
+ >
+ <span className="text-scale-1200 text-sm">{schema.name}</span>
+ </Listbox.Option>
+ ))}
+ <Listbox.Option
+ disabled
+ key="protected-schemas"
+ value="protected-schemas"
+ label="Protected schemas"
+ >
+ <p className="text-sm">Protected schemas</p>
+ </Listbox.Option>
+ {protectedSchemas.map((schema) => (
+ <Listbox.Option
+ key={schema.id}
+ value={schema.name}
+ label={schema.name}
+ addOnBefore={() => <span className="text-scale-900">schema</span>}
+ >
+ <span className="text-scale-1200 text-sm">{schema.name}</span>
+ </Listbox.Option>
+ ))}
+ </Listbox>
+ </div>
+ <Input
+ placeholder="Filter by name"
+ size="small"
+ icon={<IconSearch size="tiny" />}
+ value={filterString}
+ onChange={(e) => setFilterString(e.target.value)}
+ />
+ </div>
+
<Tooltip.Root delayDuration={0}>
<Tooltip.Trigger>
<Button disabled={!canCreateFunctions} onClick={() => createFunction()}>
@@ -111,23 +171,33 @@ const FunctionsList = ({
)}
</Tooltip.Root>
</div>
- {filteredFunctions.length <= 0 && (
- <div className="mx-auto flex max-w-lg items-center justify-center space-x-3 rounded border p-6 shadow-md dark:border-dark">
- <p>No results match your filter query</p>
- <Button type="outline" onClick={() => setFilterString('')}>
- Reset filter
- </Button>
- </div>
- )}
- {filteredFunctionSchemas.map((schema: any) => (
- <SchemaTable
- key={schema}
- filterString={filterString}
- schema={schema}
- editFunction={editFunction}
- deleteFunction={deleteFunction}
- />
- ))}
+
+ <Table
+ className="table-fixed px-4"
+ head={
+ <>
+ <Table.th key="name" className="w-1/3 space-x-4">
+ Name
+ </Table.th>
+ <Table.th key="arguments" className="hidden md:table-cell">
+ Arguments
+ </Table.th>
+ <Table.th key="return_type" className="hidden lg:table-cell">
+ Return type
+ </Table.th>
+ <Table.th key="buttons" className="w-1/6"></Table.th>
+ </>
+ }
+ body={
+ <FunctionList
+ schema={selectedSchema}
+ filterString={filterString}
+ isLocked={isLocked}
+ editFunction={editFunction}
+ deleteFunction={deleteFunction}
+ />
+ }
+ />
</div>
)}
</>
diff --git a/studio/components/interfaces/Database/Functions/FunctionsList/SchemaTable.tsx b/studio/components/interfaces/Database/Functions/FunctionsList/SchemaTable.tsx
deleted file mode 100644
index 242f14ac128..00000000000
--- a/studio/components/interfaces/Database/Functions/FunctionsList/SchemaTable.tsx
+++ /dev/null
@@ -1,57 +0,0 @@
-import { noop } from 'lodash'
-import { observer } from 'mobx-react-lite'
-
-import Table from 'components/to-be-cleaned/Table'
-import FunctionList from './FunctionList'
-
-interface SchemaTableProps {
- schema: string
- filterString: string
- editFunction: (fn: any) => void
- deleteFunction: (fn: any) => void
-}
-
-const SchemaTable = ({
- schema,
- filterString,
- editFunction = noop,
- deleteFunction = noop,
-}: SchemaTableProps) => {
- return (
- <div key={schema} className="">
- <div className="sticky top-0 backdrop-blur backdrop-filter">
- <div className="flex items-baseline space-x-1 py-2 px-6">
- <h5 className="text-scale-1000">schema</h5>
- <h4>{schema}</h4>
- </div>
- </div>
- <Table
- className="table-fixed px-6"
- head={
- <>
- <Table.th key="name" className="w-1/3 space-x-4">
- Name
- </Table.th>
- <Table.th key="arguments" className="hidden md:table-cell">
- Arguments
- </Table.th>
- <Table.th key="return_type" className="hidden lg:table-cell">
- Return type
- </Table.th>
- <Table.th key="buttons" className="w-1/6"></Table.th>
- </>
- }
- body={
- <FunctionList
- schema={schema}
- filterString={filterString}
- editFunction={editFunction}
- deleteFunction={deleteFunction}
- />
- }
- />
- </div>
- )
-}
-
-export default observer(SchemaTable)