The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
adds some fdw validation
Alaister Young4b23d42c672e
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx b/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsxindex 10c29dd410b..50bf744243a 100644--- a/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx+++ b/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx@@ -1,11 +1,13 @@import { useState } from 'react'import { Button, Input, SidePanel } from 'ui'+import { isEmpty } from 'lodash'import ActionBar from 'components/interfaces/TableGridEditor/SidePanelEditor/ActionBar'import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'import { useFDWCreateMutation } from 'data/fdw/fdw-create-mutation'import { useStore } from 'hooks'import { Wrapper } from './types'+import { makeValidateRequired } from './utils'import WrapperTableEditor from './WrapperTableEditor'export type WrapperEditorProps = {@@ -17,11 +19,7 @@ export type WrapperEditorProps = {const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {const { ui } = useStore()- const validate = (values: any) => {- const errors: any = {}- if (values.schema === 'custom' && !values.name) errors.name = 'Required field'- return errors- }+ const validate = makeValidateRequired(wrapper.server.options)const getInitialFormState = () =>Object.fromEntries(@@ -29,6 +27,7 @@ const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {)const [formState, setFormState] = useState(getInitialFormState)+ const [formErrors, setFormErrors] = useState<{ [k: string]: string }>({})const [newTables, setNewTables] = useState<any[]>([])const [isAddTableOpen, setIsAddTableOpen] = useState(false)@@ -38,10 +37,28 @@ const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {setIsAddTableOpen(false)}+ const resetForm = () => {+ setFormState(getInitialFormState())+ setNewTables([])+ }++ const handleCancel = () => {+ resetForm()+ onCancel()+ }+const { project } = useProjectContext()const { mutateAsync: createFDW } = useFDWCreateMutation()const onSaveChanges = async (done: () => void) => {+ // Validate form+ const errors = validate(formState)+ if (!isEmpty(errors)) {+ setFormErrors(errors)+ done()+ return+ }+const toastId = ui.setNotification({category: 'loading',message: `Creating foreign data wrapper...`,@@ -56,15 +73,15 @@ const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {newTables,})- // Reset state- setFormState(getInitialFormState())- setNewTables([])+ resetForm()ui.setNotification({id: toastId,category: 'success',message: `Created foreign data wrapper successfully!`,})++ onCancel()} catch (error: any) {ui.setNotification({id: toastId,@@ -74,7 +91,6 @@ const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {}done()- onCancel()}return (@@ -88,14 +104,14 @@ const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {<h5 className="text-sm text-scale-1200">{wrapper.label} Foreign Data Wrapper</h5></div>}- className={`transition-all duration-100 ease-in`}- onCancel={onCancel}+ className="transition-all duration-100 ease-in"+ onCancel={handleCancel}onConfirm={() => (resolve: () => void) => onSaveChanges(resolve)}customFooter={<ActionBarbackButtonLabel="Cancel"applyButtonLabel="Save"- closePanel={onCancel}+ closePanel={handleCancel}applyFunction={(resolve: () => void) => onSaveChanges(resolve)}/>}@@ -119,9 +135,11 @@ const WrapperEditor = ({ visible, wrapper, onCancel }: WrapperEditorProps) => {required={option.required ?? false}layout="horizontal"value={formState[option.name]}- onChange={(e) =>+ onChange={(e) => {setFormState((prev) => ({ ...prev, [option.name]: e.target.value }))- }+ setFormErrors((prev) => ({ ...prev, [option.name]: '' }))+ }}+ error={formErrors[option.name]}/>))}diff --git a/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx b/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsxindex f3e65756af4..291413239bd 100644--- a/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx+++ b/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx@@ -2,6 +2,7 @@ import ActionBar from 'components/interfaces/TableGridEditor/SidePanelEditor/Actimport { useState } from 'react'import { Accordion, Checkbox, Form, Input, Listbox, SidePanel } from 'ui'import { Table, TableOption } from './types'+import { makeValidateRequired } from './utils'export type WrapperTableEditorProps = {visible: boolean@@ -73,11 +74,10 @@ const Option = ({ option }: { option: TableOption }) => {<Inputkey={option.name}id={option.name}- name={`options.${option.name}`}+ name={option.name}label={option.label}placeholder={option.placeholder ?? ''}defaultValue={option.defaultValue ?? ''}- required={option.required ?? false}/>)}@@ -94,27 +94,30 @@ const TableForm = ({ table, onSubmit }: { table: Table; onSubmit: OnSubmitFn })const initialValues = {table_name: '',columns: [],- options: Object.fromEntries(- table.options.map((option) => [option.name, option.defaultValue ?? ''])- ),+ ...Object.fromEntries(table.options.map((option) => [option.name, option.defaultValue ?? ''])),}+ const validate = makeValidateRequired([+ ...table.options,+ { name: 'table_name', required: true },+ { name: 'columns', required: true },+ ])+return (<Formid="wrapper-table-editor-form"initialValues={initialValues}- // validate={validate}+ validate={validate}onSubmit={onSubmit}enableReinitialize={true}>- {() => (+ {({ errors }: any) => (<div className="space-y-4"><Input- id="table-name"+ id="table_name"name="table_name"label="Table name"descriptionText="The name of the local table table you will query after the wrapper is enabled."- required/>{requiredOptions.map((option) => (@@ -135,6 +138,12 @@ const TableForm = ({ table, onSubmit }: { table: Table; onSubmit: OnSubmitFn })/>))}</div>++ {errors.columns && (+ <p className="text-sm text-red-900 transition-all data-show:mt-2 data-show:animate-slide-down-normal data-hide:animate-slide-up-normal">+ {errors.columns}+ </p>+ )}</div>{optionalOptions.length > 0 && (diff --git a/studio/components/interfaces/Database/Wrappers/utils.ts b/studio/components/interfaces/Database/Wrappers/utils.tsnew file mode 100644index 00000000000..6bf0b68c509--- /dev/null+++ b/studio/components/interfaces/Database/Wrappers/utils.ts@@ -0,0 +1,17 @@+export const makeValidateRequired =+ (options: { name: string; required: boolean }[]) => (values: any) => {+ const requiredOptionsSet = new Set(+ options.filter((option) => option.required).map((option) => option.name)+ )++ const errors = Object.fromEntries(+ Object.entries(values)+ .filter(+ ([key, value]) =>+ requiredOptionsSet.has(key) && (Array.isArray(value) ? value.length < 1 : !value)+ )+ .map(([key]) => [key, 'Required'])+ )++ return errors+ }diff --git a/studio/data/fdw/fdw-create-mutation.ts b/studio/data/fdw/fdw-create-mutation.tsindex 5cd08b6d7c6..e298841df21 100644--- a/studio/data/fdw/fdw-create-mutation.ts+++ b/studio/data/fdw/fdw-create-mutation.ts@@ -89,7 +89,8 @@ export function getFDWCreateSql({)server ${wrapper.server.name}options (- ${Object.entries(newTable.options)+ ${Object.entries(newTable)+ .filter(([key]) => key !== 'table_name' && key !== 'columns' && key !== 'index').map(([key, value]) => `${key} '${value}'`).join(',\n ')});