Skip to content

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

Deprecate the use of unescapeLiteral in RowEditor

Joshen Lim7ce1e343d309
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
index 8c3818e61ce..61bb58f92e2 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
@@ -60,8 +60,9 @@ const ColumnDefaultValue: FC<Props> = ({
}
value={columnFields?.defaultValue ?? ''}
format={columnFields?.format}
- suggestionsHeader="Suggested expressions"
suggestions={suggestions}
+ suggestionsHeader="Suggested expressions"
+ suggestionsTooltip="Suggested expressions"
onChange={(event: any) => onUpdateField({ defaultValue: event.target.value })}
onSelectSuggestion={(suggestion: Suggestion) =>
onUpdateField({ defaultValue: suggestion.value })
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index 64bc70e4eab..299dbfad1d8 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -62,7 +62,7 @@ export const generateColumnFieldFromPostgresColumn = (
name: column.name,
comment: column?.comment ?? '',
format: isArray ? column.format.slice(1) : column.format,
- defaultValue: (column?.default_value ?? '') as string,
+ defaultValue: column?.default_value as string | null,
isArray: isArray,
isNullable: column.is_nullable,
isIdentity: column.is_identity,
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/InputWithSuggestions.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/InputWithSuggestions.tsx
index b73579ecfb0..f541c04bd02 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/InputWithSuggestions.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/InputWithSuggestions.tsx
@@ -6,11 +6,11 @@
// the component over to the UI library
import { FC, useEffect, useRef, useState } from 'react'
+import * as Tooltip from '@radix-ui/react-tooltip'
import { Button, Dropdown, IconList, Input } from '@supabase/ui'
import { Suggestion } from './ColumnEditor.types'
const MAX_SUGGESTIONS = 3
-const DEFAULT_SUGGESTIONS_WIDTH = 400
interface Props {
label?: string
@@ -22,9 +22,9 @@ interface Props {
className?: string
value: string
format?: string
- suggestionsHeader?: string
- suggestionsWidth?: number
suggestions: Suggestion[]
+ suggestionsTooltip?: string
+ suggestionsHeader?: string
onChange: (event: any) => void
onSelectSuggestion: (suggestion: Suggestion) => void
}
@@ -40,6 +40,8 @@ const InputWithSuggestions: FC<Props> = ({
value = '',
format,
suggestions = [],
+ suggestionsTooltip,
+ suggestionsHeader,
onChange = () => {},
onSelectSuggestion = () => {},
}) => {
@@ -75,7 +77,7 @@ const InputWithSuggestions: FC<Props> = ({
size={size}
layout={layout}
disabled={disabled}
- className={`${className} ${format?.includes('json') ? 'input-mono' : ''}`}
+ className={className}
type="text"
value={value}
onChange={onInputChange}
@@ -87,7 +89,7 @@ const InputWithSuggestions: FC<Props> = ({
side="bottom"
overlay={
<>
- <Dropdown.Label>Suggestions</Dropdown.Label>
+ <Dropdown.Label>{suggestionsHeader || 'Suggestions'}</Dropdown.Label>
<Dropdown.Seperator />
{filteredSuggestions.map((suggestion: Suggestion) => (
<Dropdown.Item
@@ -101,7 +103,24 @@ const InputWithSuggestions: FC<Props> = ({
</>
}
>
- <Button as="span" type="default" icon={<IconList strokeWidth={1.5} />}></Button>
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button as="span" type="default" icon={<IconList strokeWidth={1.5} />}></Button>
+ </Tooltip.Trigger>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'bg-scale-100 rounded py-1 px-2 leading-none shadow',
+ 'border-scale-200 border',
+ ].join(' ')}
+ >
+ <span className="text-scale-1200 text-xs">
+ {suggestionsTooltip || 'Suggestions'}
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Root>
</Dropdown>
)
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
index 699e8e99ae0..82382c5c210 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
@@ -100,9 +100,11 @@ const InputField: FC<Props> = ({
rows={5}
value={field.value ?? ''}
placeholder={
- typeof field.defaultValue === 'string' && field.defaultValue.length === 0
+ field.defaultValue === null
+ ? ''
+ : typeof field.defaultValue === 'string' && field.defaultValue.length === 0
? 'Default: Empty string'
- : field.defaultValue
+ : `Default: ${field.defaultValue}`
}
onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}
/>
@@ -119,7 +121,7 @@ const InputField: FC<Props> = ({
descriptionText={field.comment}
labelOptional={field.format}
disabled={!isEditable}
- placeholder={field.defaultValue}
+ placeholder={field?.defaultValue ?? ''}
error={errors[field.name]}
onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}
actions={
@@ -156,7 +158,13 @@ const InputField: FC<Props> = ({
labelOptional={field.format}
error={errors[field.name]}
value={field.value ?? ''}
- placeholder={field.isIdentity ? 'Automatically generated as identity' : field.defaultValue}
+ placeholder={
+ field.isIdentity
+ ? 'Automatically generated as identity'
+ : field.defaultValue !== null
+ ? `Default: ${field.defaultValue}`
+ : ''
+ }
disabled={!isEditable}
onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}
/>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
index 5b3bfb9d459..a6b710ededf 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
@@ -57,6 +57,7 @@ const RowEditor: FC<Props> = ({
useEffect(() => {
if (visible) {
setErrors({})
+ console.log('Row', row)
const rowFields = generateRowFields(row, selectedTable, isNewRecord)
setRowFields(rowFields)
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.types.ts
index 2dd7f8d5b70..495ea21e5d1 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.types.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.types.ts
@@ -18,7 +18,7 @@ export interface RowField {
format: string
enums: string[]
value: string | null
- defaultValue: string
+ defaultValue: string | null
foreignKey?: PostgresRelationship
isNullable: boolean
isIdentity: boolean
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
index 942be27d3d3..ce88aa6dfbe 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
@@ -5,13 +5,7 @@ import { PostgresTable } from '@supabase/postgres-meta'
import { uuidv4, minifyJSON, tryParseJson } from 'lib/helpers'
import { RowField } from './RowEditor.types'
-import {
- JSON_TYPES,
- NUMERICAL_TYPES,
- DATETIME_TYPES,
- TIME_TYPES,
- TIMESTAMP_TYPES,
-} from '../SidePanelEditor.constants'
+import { DATETIME_TYPES, TIME_TYPES, TIMESTAMP_TYPES } from '../SidePanelEditor.constants'
export const generateRowFields = (
row: Dictionary<any> | undefined,
@@ -24,14 +18,13 @@ export const generateRowFields = (
return table.columns.map((column) => {
const defaultValue = column?.default_value as string | null
- const value =
- isNewRecord && defaultValue?.includes('now()')
- ? nowDateTimeValue(column.format)
- : isUndefined(row)
- ? ''
- : DATETIME_TYPES.includes(column.format)
- ? convertPostgresDatetimeToInputDatetime(column.format, row[column.name])
- : parseValue(row[column.name], column.format, column.data_type)
+ const value = isUndefined(row)
+ ? ''
+ : isNewRecord && defaultValue?.includes('now()')
+ ? nowDateTimeValue(column.format)
+ : DATETIME_TYPES.includes(column.format)
+ ? convertPostgresDatetimeToInputDatetime(column.format, row[column.name])
+ : parseValue(row[column.name], column.format, column.data_type)
const foreignKey = find(relationships, (relationship) => {
return (
@@ -49,7 +42,7 @@ export const generateRowFields = (
comment: parseDescription(column.comment),
format: column.format,
enums: column.enums as any,
- defaultValue: parseValue(column.default_value as string, column.format, column.data_type),
+ defaultValue: column?.default_value as string | null,
isNullable: column.is_nullable,
isIdentity: column.is_identity,
isPrimaryKey: primaryKeyColumns.includes(column.name),
@@ -109,26 +102,6 @@ const parseValue = (originalValue: string, format: string, dataType: string) =>
return JSON.stringify(originalValue)
} else if (typeof originalValue === 'boolean') {
return (originalValue as any).toString()
- } else if (includes(JSON_TYPES, format)) {
- const value = _unescapeLiteral(dataType, originalValue, format)
- return minifyJSON(value)
- }
-
- // escape literal format from postgres-meta
- const value = _unescapeLiteral(dataType, originalValue, format)
- if (dataType && dataType.toLowerCase() == 'array') {
- if (originalValue && originalValue.includes("}'::") && originalValue.endsWith('[]')) {
- // for array default value, we need to use this method to parse literal format
- // TODO: should merge with above method... if we can
- return _unescapeLiteralArray(originalValue)
- } else if (typeof value === 'string') {
- const parsedValue = JSON.parse(value)
- return JSON.parse(parsedValue)
- } else {
- return JSON.stringify(value)
- }
- } else {
- return value
}
} catch (error) {
return originalValue
@@ -201,68 +174,6 @@ const convertInputDatetimeToPostgresDatetime = (format: string, value: string |
}
}
-/**
- * postgres-meta can return default value with format like
- * 'hello world'::character varying
- * '232.34'::double precision
- * 'USER'::"Role" user-defined type
- * this method will help convert them to valid default value
- */
-const _unescapeLiteral = (dataType: string, value: string, format: string): any => {
- // unEscape format literal
- let temp = `::${dataType}`
- let tempWithQuotes = `::"${dataType}"`
- // for user-defined type, need to use format instead
- if (dataType?.toLowerCase() == 'user-defined') {
- temp = `::${format}`
- tempWithQuotes = `::"${format}"`
- }
-
- if (value && value.includes(temp)) {
- value = value.replace(temp, '')
- // remove quotes
- value = value.slice(1, value.length - 1)
- } else if (value && value.includes(tempWithQuotes)) {
- value = value.replace(tempWithQuotes, '')
- // remove quotes
- value = value.slice(1, value.length - 1)
- }
- return value
-}
-
-/**
- * postgres-meta can return default value for array like
- * ex: '{apple,banana}'::text[] => ["apple","banana"]
- * ex: '{1,2,3,4,5,6}'::integer[] => [1,2,3,4,5,6]
- * ex: '{{meeting,lunch},{training,presentation}}'::character varying[]
- * this method will help convert them to valid default value
- */
-const _unescapeLiteralArray = (value: any) => {
- const splits = value.split("'::")
- if (splits.length < 1) return value
-
- let temp = splits[0].replace("'{", '{')
- if (value.endsWith('integer[]') || value.endsWith('real[]')) {
- temp = temp.replaceAll('{', '[')
- temp = temp.replaceAll('}', ']')
- return temp
- } else {
- const matches = temp.match(/\{([^{}]+)\}/g)
- if (matches) {
- const array = [...matches]
- array.forEach((x) => {
- let _x = x.replaceAll('{', '{"')
- _x = _x.replaceAll('}', '"}')
- _x = _x.replaceAll(',', '","')
- temp = temp.replace(x, _x)
- })
- temp = temp.replaceAll('{', '[')
- temp = temp.replaceAll('}', ']')
- }
- return temp
- }
-}
-
export const generateRowObjectFromFields = (
fields: RowField[],
includeNullProperties = false