The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Chore/protected schemas in database pages (#17636)
Joshen Limf80d92fd6faf
diff --git a/studio/components/interfaces/Database/Functions/CreateFunction.tsx b/studio/components/interfaces/Database/Functions/CreateFunction.tsxindex 1a32bb22545..a1e34e697cd 100644--- a/studio/components/interfaces/Database/Functions/CreateFunction.tsx+++ b/studio/components/interfaces/Database/Functions/CreateFunction.tsx@@ -15,6 +15,8 @@ import { useSchemasQuery } from 'data/database/schemas-query'import { useStore } from 'hooks'import { isResponseOk } from 'lib/common/fetch'import { SupaResponse } from 'types'+import { convertArgumentTypes, convertConfigParams, hasWhitespace } from './Functions.utils'+import { EXCLUDED_SCHEMAS } from 'lib/constants/schemas'// [Refactor] Remove local state, just use the Form component@@ -89,37 +91,6 @@ class CreateFunctionFormState {}}-/**- * convert argument_types = "a integer, b integer"- * to args = {value: [{name:'a', type:'integer'}, {name:'b', type:'integer'}]}- */-function convertArgumentTypes(value: string) {- const items = value?.split(',')- if (isEmpty(value) || !items || items?.length == 0) return { value: [] }- const temp = items.map((x) => {- const str = x.trim()- const space = str.indexOf(' ')- const name = str.slice(0, space !== 1 ? space : 0)- const type = str.slice(space + 1)- return { name, type }- })- return { value: temp }-}--/**- * convert config_params = {search_path: "auth, public"}- * to {value: [{name: 'search_path', value: 'auth, public'}]}- */-function convertConfigParams(value: Dictionary<any>) {- const temp = []- if (value) {- for (var key in value) {- temp.push({ name: key, value: value[key] })- }- }- return { value: temp }-}-interface ICreateFunctionStore {loading: booleanformState: CreateFunctionFormState@@ -283,10 +254,6 @@ class CreateFunctionStore implements ICreateFunctionStore {}}-function hasWhitespace(value: string) {- return /\s/.test(value)-}-const CreateFunctionContext = createContext<ICreateFunctionStore | null>(null)interface CreateFunctionProps {@@ -296,10 +263,9 @@ interface CreateFunctionProps {}const CreateFunction = ({ func, visible, setVisible }: CreateFunctionProps) => {- const { project } = useProjectContext()const { ui, meta } = useStore()+ const { project } = useProjectContext()const _localState = useLocalObservable(() => new CreateFunctionStore())- _localState.meta = meta as anyconst [isClosingPanel, setIsClosingPanel] = useState<boolean>(false)@@ -327,8 +293,8 @@ const CreateFunction = ({ func, visible, setVisible }: CreateFunctionProps) => {const body = _localState.formState.requestBodyconst response: SupaResponse<Function> = body.id- ? await (_localState!.meta as any).functions.update(body.id, body)- : await (_localState!.meta as any).functions.create(body)+ ? await (meta as any).functions.update(body.id, body)+ : await (meta as any).functions.create(body)if (!isResponseOk(response)) {ui.setNotification({@@ -757,6 +723,13 @@ const InputDefinition = observer(({}) => {const SelectSchema = observer(({}) => {const _localState = useContext(CreateFunctionContext)+ const { project } = useProjectContext()+ const { data } = useSchemasQuery({+ projectRef: project?.ref,+ connectionString: project?.connectionString,+ })+ const schemas = data?.filter((schema) => !EXCLUDED_SCHEMAS.includes(schema.name)) ?? []+return (<Listboxid="schema"@@ -768,7 +741,7 @@ const SelectSchema = observer(({}) => {descriptionText="Tables made in the table editor will be in 'public'"onChange={(value) => _localState!.onFormChange({ key: 'schema', value })}>- {_localState!.schemas.map((x) => (+ {schemas.map((x) => (<Listbox.Option key={x.name} label={x.name} value={x.name}>{x.name}</Listbox.Option>diff --git a/studio/components/interfaces/Database/Functions/Functions.utils.ts b/studio/components/interfaces/Database/Functions/Functions.utils.tsnew file mode 100644index 00000000000..5c09aa3d99c--- /dev/null+++ b/studio/components/interfaces/Database/Functions/Functions.utils.ts@@ -0,0 +1,37 @@+import { Dictionary } from 'components/grid'+import { isEmpty } from 'lodash'++/**+ * convert argument_types = "a integer, b integer"+ * to args = {value: [{name:'a', type:'integer'}, {name:'b', type:'integer'}]}+ */+export function convertArgumentTypes(value: string) {+ const items = value?.split(',')+ if (isEmpty(value) || !items || items?.length == 0) return { value: [] }+ const temp = items.map((x) => {+ const str = x.trim()+ const space = str.indexOf(' ')+ const name = str.slice(0, space !== 1 ? space : 0)+ const type = str.slice(space + 1)+ return { name, type }+ })+ return { value: temp }+}++/**+ * convert config_params = {search_path: "auth, public"}+ * to {value: [{name: 'search_path', value: 'auth, public'}]}+ */+export function convertConfigParams(value: Dictionary<any>) {+ const temp = []+ if (value) {+ for (var key in value) {+ temp.push({ name: key, value: value[key] })+ }+ }+ return { value: temp }+}++export function hasWhitespace(value: string) {+ return /\s/.test(value)+}diff --git a/studio/components/interfaces/Database/Tables/TableList.tsx b/studio/components/interfaces/Database/Tables/TableList.tsxindex 1e190ba7eb8..a6a015a0be4 100644--- a/studio/components/interfaces/Database/Tables/TableList.tsx+++ b/studio/components/interfaces/Database/Tables/TableList.tsx@@ -7,7 +7,7 @@ import { useState } from 'react'import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'import NoSearchResults from 'components/to-be-cleaned/NoSearchResults'import Table from 'components/to-be-cleaned/Table'-import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'+import ShimmeringLoader, { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'import { useSchemasQuery } from 'data/database/schemas-query'import { useTablesQuery } from 'data/tables/tables-query'import { useCheckPermissions, useStore } from 'hooks'@@ -47,7 +47,11 @@ const TableList = ({const [filterString, setFilterString] = useState<string>('')const canUpdateTables = useCheckPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'tables')- const { data: schemas } = useSchemasQuery({+ const {+ data: schemas,+ isLoading: isLoadingSchemas,+ isSuccess: isSuccessSchemas,+ } = useSchemasQuery({projectRef: project?.ref,connectionString: project?.connectionString,})@@ -93,44 +97,52 @@ const TableList = ({<div className="flex items-center justify-between"><div className="flex items-center space-x-4"><div className="w-[260px]">- <Listbox- size="small"- value={snap.selectedSchemaName}- onChange={snap.setSelectedSchemaName}- 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>- {openSchemas.map((schema) => (+ {isLoadingSchemas && <ShimmeringLoader className="!py-4" />}+ {isSuccessSchemas && (+ <Listbox+ size="small"+ value={snap.selectedSchemaName}+ onChange={snap.setSelectedSchemaName}+ icon={isLocked && <IconLock size={14} strokeWidth={2} />}+ ><Listbox.Option- key={schema.id}- value={schema.name}- label={schema.name}- addOnBefore={() => <span className="text-scale-900">schema</span>}+ disabled+ key="normal-schemas"+ value="normal-schemas"+ label="Schemas">- <span className="text-scale-1200 text-sm">{schema.name}</span>+ <p className="text-sm">Schemas</p></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) => (+ {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- key={schema.id}- value={schema.name}- label={schema.name}- addOnBefore={() => <span className="text-scale-900">schema</span>}+ disabled+ key="protected-schemas"+ value="protected-schemas"+ label="Protected schemas">- <span className="text-scale-1200 text-sm">{schema.name}</span>+ <p className="text-sm">Protected schemas</p></Listbox.Option>- ))}- </Listbox>+ {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><div><Inputdiff --git a/studio/components/interfaces/Database/Triggers/CreateTrigger.tsx b/studio/components/interfaces/Database/Triggers/CreateTrigger.tsxindex 91eebf02de4..ab6475a5c3f 100644--- a/studio/components/interfaces/Database/Triggers/CreateTrigger.tsx+++ b/studio/components/interfaces/Database/Triggers/CreateTrigger.tsx@@ -25,6 +25,7 @@ import {SidePanel,} from 'ui'import ChooseFunctionForm from './ChooseFunctionForm'+import { EXCLUDED_SCHEMAS } from 'lib/constants/schemas'class CreateTriggerFormState {id: number | undefined@@ -112,7 +113,7 @@ interface ICreateTriggerStore {setChooseFunctionFormVisible: (value: boolean) => voidsetDefaultSelectedTable: () => voidsetLoading: (value: boolean) => void- setTables: (value: Dictionary<any>[]) => void+ setTables: (value: any[]) => voidsetTriggerFunctions: (value: Dictionary<any>[]) => voidvalidateForm: () => boolean}@@ -168,8 +169,10 @@ class CreateTriggerStore implements ICreateTriggerStore {this.isDirty = value}- setTables = (value: Dictionary<any>[]) => {- this.tables = value as any+ setTables = (value: any[]) => {+ this.tables = value+ .sort((a, b) => a.schema.localeCompare(b.schema))+ .filter((a) => !EXCLUDED_SCHEMAS.includes(a.schema)) as anythis.setDefaultSelectedTable()}@@ -261,7 +264,6 @@ const CreateTrigger = ({ trigger, visible, setVisible }: CreateTriggerProps) =>const { ui, meta } = useStore()const [isClosingPanel, setIsClosingPanel] = useState(false)const _localState = useLocalObservable(() => new CreateTriggerStore())- _localState.meta = meta as anyuseTablesQuery({@@ -279,13 +281,13 @@ const CreateTrigger = ({ trigger, visible, setVisible }: CreateTriggerProps) =>useEffect(() => {const fetchFunctions = async () => {- await (_localState.meta as any).functions.load()- const triggerFuncs = (_localState!.meta as any)!.functions.listTriggerFunctions()+ await meta.functions.load()+ const triggerFuncs = (meta as any)!.functions.listTriggerFunctions()_localState.setTriggerFunctions(triggerFuncs)}- fetchFunctions()- }, [])+ if (ui.selectedProjectRef) fetchFunctions()+ }, [ui.selectedProjectRef])useEffect(() => {_localState.setisDirty(false)@@ -304,8 +306,8 @@ const CreateTrigger = ({ trigger, visible, setVisible }: CreateTriggerProps) =>const body = _localState.formState.requestBodyconst response: any = _localState.isEditing- ? await (_localState.meta as any).triggers.update(body.id, body)- : await (_localState.meta as any).triggers.create(body)+ ? await (meta as any).triggers.update(body.id, body)+ : await (meta as any).triggers.create(body)if (response.error) {ui.setNotification({@@ -593,8 +595,8 @@ const ListboxTable = observer(({}) => {)}><div className="flex flex-row items-center space-x-1">- <p>{x.name}</p><p className="text-sm text-scale-1000">{x.schema}</p>+ <p className="text">{x.name}</p></div></Listbox.Option>)diff --git a/studio/pages/project/[ref]/database/hooks.tsx b/studio/pages/project/[ref]/database/hooks.tsxindex 121004351a1..099950eda65 100644--- a/studio/pages/project/[ref]/database/hooks.tsx+++ b/studio/pages/project/[ref]/database/hooks.tsx@@ -113,8 +113,8 @@ const HooksPage: NextPageWithLayout = () => {return (<>- <ScaffoldContainer>- <ScaffoldSection>+ <ScaffoldContainer className="h-full">+ <ScaffoldSection className="h-full"><div className="col-span-12"><HooksList createHook={createHook} editHook={editHook} deleteHook={deleteHook} /></div>diff --git a/studio/pages/project/[ref]/database/triggers.tsx b/studio/pages/project/[ref]/database/triggers.tsxindex 8bbdac52605..ff1abd91597 100644--- a/studio/pages/project/[ref]/database/triggers.tsx+++ b/studio/pages/project/[ref]/database/triggers.tsx@@ -12,7 +12,7 @@ import { NextPageWithLayout } from 'types'import { ScaffoldContainer, ScaffoldSection } from 'components/layouts/Scaffold'const TriggersPage: NextPageWithLayout = () => {- const { meta } = useStore()+ const { meta, ui } = useStore()const { project } = useProjectContext()const [selectedTrigger, setSelectedTrigger] = useState<any>()@@ -22,10 +22,8 @@ const TriggersPage: NextPageWithLayout = () => {const canReadTriggers = useCheckPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'triggers')useEffect(() => {- if (project?.ref) {- fetchTriggers()- }- }, [project?.ref])+ if (ui.selectedProjectRef) fetchTriggers()+ }, [ui.selectedProjectRef])const fetchTriggers = async () => {meta.triggers.load()