The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Fix table editor to render empty strings and null values properly, was mixing them up previously
Joshen Lim70f356cced72
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsxindex 62da95d21f7..9fb22beec30 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx@@ -33,7 +33,7 @@ const ColumnDefaultValue: FC<Props> = ({<Selectlabel="Default Value"layout="horizontal"- value={columnFields.defaultValue}+ value={columnFields.defaultValue ?? ''}onChange={(event: any) => onUpdateField({ defaultValue: event.target.value })}><Select.Option key="empty-enum" value="">@@ -53,7 +53,11 @@ const ColumnDefaultValue: FC<Props> = ({label="Default Value"layout="horizontal"description="Can either be a literal or an expression (e.g uuid_generate_v4())"- placeholder="NULL"+ placeholder={+ typeof columnFields.defaultValue === 'string' && columnFields.defaultValue.length === 0+ ? 'Empty string'+ : 'NULL'+ }value={columnFields?.defaultValue ?? ''}format={columnFields?.format}suggestionsHeader="Suggested expressions"diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.tsindex 9db3ce26a5c..b20856fb6cd 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts@@ -33,7 +33,7 @@ export const generateColumnField = (field: any = {}): ColumnField => {name: name || '',comment: '',format: format || '',- defaultValue: '',+ defaultValue: null,foreignKey: undefined,isNullable: true,isUnique: false,@@ -53,13 +53,14 @@ export const generateColumnFieldFromPostgresColumn = (const primaryKeyColumns = primary_keys.map((key) => key.name)const foreignKey = getColumnForeignKey(column, table)const isArray = column?.data_type === 'ARRAY'+return {foreignKey,id: column?.id ?? uuidv4(),name: column.name,comment: column?.comment ?? '',format: isArray ? column.format.slice(1) : column.format,- defaultValue: unescapeLiteral(column?.default_value as string) ?? ('' as any),+ defaultValue: unescapeLiteral(column?.default_value as string),isArray: isArray,isNullable: column.is_nullable,isIdentity: column.is_identity,@@ -75,7 +76,7 @@ export const generateCreateColumnPayload = (field: ColumnField): CreateColumnPayload => {const isIdentity = field.format.includes('int') ? field.isIdentity : false- const defaultValue = field.defaultValue.length === 0 ? null : field.defaultValue+ const defaultValue = field.defaultValueconst payload: CreateColumnPayload = {tableId,isIdentity,@@ -105,7 +106,7 @@ export const generateUpdateColumnPayload = (field: ColumnField): Partial<UpdateColumnPayload> => {// Only append the properties which are getting updated- const defaultValue = field.defaultValue.length === 0 ? null : field.defaultValue+ const defaultValue = field.defaultValueconst type = field.isArray ? `${field.format}[]` : field.formatconst comment = (field.comment?.length ?? '') === 0 ? null : field.commentdiff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsxindex 3a8daa3c6fc..328ba81a1a3 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx@@ -82,18 +82,25 @@ const InputField: FC<Props> = ({if (includes(TEXT_TYPES, field.format)) {return (- <Input.TextArea- layout="horizontal"- label={field.name}- descriptionText={field.comment}- labelOptional={field.format}- disabled={!isEditable}- error={errors[field.name]}- rows={5}- value={field.value}- placeholder={field.defaultValue}- onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}- />+ <div className="text-area-text-sm">+ <Input.TextArea+ layout="horizontal"+ label={field.name}+ className="text-sm"+ descriptionText={field.comment}+ labelOptional={field.format}+ disabled={!isEditable}+ error={errors[field.name]}+ rows={5}+ value={field.value}+ placeholder={+ typeof field.defaultValue === 'string' && field.defaultValue.length === 0+ ? 'Default: Empty string'+ : field.defaultValue+ }+ onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}+ />+ </div>)}diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.tsindex 02052563e00..5af3bb3f45d 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts@@ -6,7 +6,6 @@ import { PostgresTable } from '@supabase/postgres-meta'import { uuidv4, minifyJSON, tryParseJson } from 'lib/helpers'import { RowField } from './RowEditor.types'import {- DATE_FORMAT,JSON_TYPES,NUMERICAL_TYPES,DATETIME_TYPES,@@ -24,9 +23,9 @@ export const generateRowFields = (const primaryKeyColumns = primary_keys.map((key) => key.name)return table.columns.map((column) => {- const defaultValue: string = (column?.default_value as string) ?? ''+ const defaultValue = column?.default_value as string | nullconst value =- isNewRecord && defaultValue.includes('now()')+ isNewRecord && defaultValue?.includes('now()')? nowDateTimeValue(column.format): isUndefined(row)? ''@@ -76,7 +75,7 @@ export const validateFields = (fields: RowField[]) => {}}if (field.isIdentity || field.defaultValue) return- if (!field.isNullable && !field.value) {+ if (!field.isNullable && !field.value && field.defaultValue === null) {errors[field.name] = `Please assign a value for this field`}})@@ -106,7 +105,7 @@ export const generateRowFieldsWithoutColumnMeta = (row: any): RowField[] => {const parseValue = (originalValue: string, format: string, dataType: string) => {try {if (originalValue === null || originalValue.length === 0) {- return ''+ return originalValue} else if (typeof originalValue === 'number' || !format) {return originalValue} else if (typeof originalValue === 'object') {diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.tsindex f68ef14587d..0a32830fa62 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts@@ -60,7 +60,7 @@ export interface ColumnField {name: stringcomment?: stringformat: string- defaultValue: string+ defaultValue: string | nullforeignKey: PostgresRelationship | undefinedisNullable: booleanisUnique: booleandiff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsxindex 96d2a4161ef..da3c7837bca 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx@@ -68,7 +68,7 @@ const Column: FC<Props> = ({column.isArray ? (settingsCount += 1) : nullreturn (- <div className="w-full flex items-center">+ <div className="flex w-full items-center"><div className={`w-[5%] ${!isNewRecord ? 'hidden' : ''}`}><div className="cursor-drag" {...dragHandleProps}><Typography>@@ -77,7 +77,7 @@ const Column: FC<Props> = ({</div></div><div className="w-[25%]">- <div className="flex items-center justify-between w-[95%]">+ <div className="flex w-[95%] items-center justify-between"><Inputvalue={column.name}size="small"@@ -115,11 +115,15 @@ const Column: FC<Props> = ({<div className={`${isNewRecord ? 'w-[25%]' : 'w-[30%]'}`}><div className="w-[90%]"><InputWithSuggestions- placeholder="NULL"+ placeholder={+ typeof column.defaultValue === 'string' && column.defaultValue.length === 0+ ? 'Empty string'+ : 'NULL'+ }size="small"- value={column.defaultValue}+ value={column.defaultValue ?? ''}disabled={column.format.includes('int') && column.isIdentity}- className={`bg-white dark:bg-transparent rounded lg:gap-0 ${+ className={`rounded bg-white dark:bg-transparent lg:gap-0 ${column.format.includes('int') && column.isIdentity ? 'opacity-50' : ''}`}suggestions={suggestions}@@ -140,7 +144,7 @@ const Column: FC<Props> = ({/></div><div className={`${hasImportContent ? 'w-[10%]' : 'w-[0%]'}`} />- <div className="w-[5%] flex justify-end">+ <div className="flex w-[5%] justify-end">{(!column.isPrimaryKey || column.format.includes('int')) && (<><Popover@@ -148,7 +152,7 @@ const Column: FC<Props> = ({className="pointer-events-auto"header={<div className="flex items-center justify-center">- <h5 className="text-sm text-scale-1200">Extra options</h5>+ <h5 className="text-scale-1200 text-sm">Extra options</h5></div>}overlay={[@@ -211,13 +215,13 @@ const Column: FC<Props> = ({</div>,]}>- <div className="group flex -space-x-1 items-center">+ <div className="group flex items-center -space-x-1">{settingsCount > 0 && (- <div className="py-0.5 px-2 text-xs rounded-full bg-scale-1200 dark:bg-scale-100 text-scale-100 dark:text-scale-1100">+ <div className="bg-scale-1200 dark:bg-scale-100 text-scale-100 dark:text-scale-1100 rounded-full py-0.5 px-2 text-xs">{settingsCount}</div>)}- <div className="text-scale-1100 transition-colors group-hover:text-scale-1200">+ <div className="text-scale-1100 group-hover:text-scale-1200 transition-colors"><IconSettings size={18} strokeWidth={1} /></div></div>@@ -226,7 +230,7 @@ const Column: FC<Props> = ({)}</div>{!hasImportContent && (- <div className="w-[5%] flex justify-end">+ <div className="flex w-[5%] justify-end"><div className="cursor-pointer" onClick={() => onRemoveColumn()}><Typography><IconX strokeWidth={1} />diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.constants.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.constants.tsindex 2bec836dff5..7a20ab648ab 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.constants.ts+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.constants.ts@@ -6,7 +6,7 @@ export const DEFAULT_COLUMNS: ColumnField[] = [id: uuidv4(),name: 'id',format: 'int8',- defaultValue: '',+ defaultValue: null,foreignKey: undefined,isNullable: false,isUnique: false,@@ -19,7 +19,7 @@ export const DEFAULT_COLUMNS: ColumnField[] = [id: uuidv4(),name: 'created_at',format: 'timestamptz',- defaultValue: 'NOW()',+ defaultValue: 'now()',foreignKey: undefined,isNullable: true,isUnique: false,diff --git a/studio/pages/project/[ref]/editor/[id].tsx b/studio/pages/project/[ref]/editor/[id].tsxindex 7e1375b3f1b..1f7f80e9ad1 100644--- a/studio/pages/project/[ref]/editor/[id].tsx+++ b/studio/pages/project/[ref]/editor/[id].tsx@@ -34,7 +34,8 @@ const TableEditorPage: NextPage = () => {const projectRef = ui.selectedProject?.refconst tables: PostgresTable[] = meta.tables.list()const selectedTable = !isNaN(Number(id))- ? tables.find((table) => table.id === Number(id))+ ? // @ts-ignore+ tables.find((table) => table.id === Number(id)): tryParseJson(Base64.decode(id))useEffect(() => {diff --git a/studio/styles/main.scss b/studio/styles/main.scssindex e716144439d..e6981dc0b24 100644--- a/studio/styles/main.scss+++ b/studio/styles/main.scss@@ -212,4 +212,10 @@ div.fixed.inset-0 {display: block;width: 100%;}+}++.text-area-text-sm {+ textarea {+ @apply text-sm;+ }}\ No newline at end of filediff --git a/studio/tests/components/Editor/ColumnEditor.utils.test.js b/studio/tests/components/Editor/ColumnEditor.utils.test.jsindex 9fc82d11801..91de069269e 100644--- a/studio/tests/components/Editor/ColumnEditor.utils.test.js+++ b/studio/tests/components/Editor/ColumnEditor.utils.test.js@@ -11,6 +11,16 @@ describe('ColumnEditor.utils: unescapeLiteral', () => {const value = unescapeLiteral(mockInput)expect(value).toStrictEqual('tomato')})+ test('should return null properly', () => {+ const mockInput = null+ const value = unescapeLiteral(mockInput)+ expect(value).toStrictEqual(null)+ })+ test('should return empty string properly', () => {+ const mockInput = `''::text`+ const value = unescapeLiteral(mockInput)+ expect(value).toStrictEqual('')+ })test('should return numerical arrays properly', () => {const mockInput = "'{1,2,3,4}'::integer[]"const value = unescapeLiteral(mockInput)