Skip to content

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

Parse enum arrays as js arrays when fetching data in table editor

Joshen Lim8fefd43f50a3
diff --git a/studio/components/grid/components/grid/Grid.tsx b/studio/components/grid/components/grid/Grid.tsx
index 2f43c880290..e9feb3cde9f 100644
--- a/studio/components/grid/components/grid/Grid.tsx
+++ b/studio/components/grid/components/grid/Grid.tsx
@@ -39,6 +39,7 @@ export const Grid = memo(
const changedColumn = Object.keys(rowData).find(
(name) => rowData[name] !== originRowData![name]
)
+
if (changedColumn) {
const { error } = state.rowService!.update(rowData, changedColumn, (payload) => {
dispatch({
diff --git a/studio/components/grid/query/Query.utils.ts b/studio/components/grid/query/Query.utils.ts
index a9551201902..7d7d3384c46 100644
--- a/studio/components/grid/query/Query.utils.ts
+++ b/studio/components/grid/query/Query.utils.ts
@@ -35,18 +35,22 @@ export function deleteQuery(
filters?: Filter[],
options?: {
returning?: boolean
+ enumArrayColumns?: string[]
}
) {
if (!filters || filters.length === 0) {
throw { message: 'no filters for this delete query' }
}
let query = `delete from ${queryTable(table)}`
- const { returning } = options ?? {}
+ const { returning, enumArrayColumns } = options ?? {}
if (filters) {
query = applyFilters(query, filters)
}
if (returning) {
- query += ' returning *'
+ query +=
+ enumArrayColumns === undefined
+ ? ` returning *`
+ : ` returning *, ${enumArrayColumns.map((x) => `"${x}"::text[]`).join(',')}`
}
return query + ';'
}
@@ -56,12 +60,13 @@ export function insertQuery(
values: Dictionary<any>[],
options?: {
returning?: boolean
+ enumArrayColumns?: string[]
}
) {
if (!values || values.length === 0) {
throw { message: 'no value to insert' }
}
- const { returning } = options ?? {}
+ const { returning, enumArrayColumns } = options ?? {}
const queryColumns = Object.keys(values[0])
.map((x) => ident(x))
.join(',')
@@ -81,7 +86,10 @@ export function insertQuery(
)
}
if (returning) {
- query += ' returning *'
+ query +=
+ enumArrayColumns === undefined
+ ? ` returning *`
+ : ` returning *, ${enumArrayColumns.map((x) => `"${x}"::text[]`).join(',')}`
}
return query + ';'
}
@@ -119,9 +127,10 @@ export function updateQuery(
options?: {
filters?: Filter[]
returning?: boolean
+ enumArrayColumns?: string[]
}
) {
- const { filters, returning } = options ?? {}
+ const { filters, returning, enumArrayColumns } = options ?? {}
if (!filters || filters.length === 0) {
throw { message: 'no filters for this update query' }
}
@@ -138,8 +147,12 @@ export function updateQuery(
query = applyFilters(query, filters)
}
if (returning) {
- query += ' returning *'
+ query +=
+ enumArrayColumns === undefined
+ ? ` returning *`
+ : ` returning *, ${enumArrayColumns.map((x) => `"${x}"::text[]`).join(',')}`
}
+
return query + ';'
}
diff --git a/studio/components/grid/query/QueryAction.ts b/studio/components/grid/query/QueryAction.ts
index 0611321fb75..4689ad68cbf 100644
--- a/studio/components/grid/query/QueryAction.ts
+++ b/studio/components/grid/query/QueryAction.ts
@@ -25,7 +25,7 @@ export class QueryAction implements IQueryAction {
*
* @param options.returning If `true`, return the deleted row(s) in the response.
*/
- delete(options?: { returning: boolean }) {
+ delete(options?: { returning: boolean; enumArrayColumns?: string[] }) {
return new QueryFilter(this.table, 'delete', undefined, options)
}
@@ -35,7 +35,7 @@ export class QueryAction implements IQueryAction {
* @param values The values to insert.
* @param options.returning If `true`, return the inserted row(s) in the response.
*/
- insert(values: Dictionary<any>[], options?: { returning: boolean }) {
+ insert(values: Dictionary<any>[], options?: { returning: boolean; enumArrayColumns?: string[] }) {
return new QueryFilter(this.table, 'insert', values, options)
}
@@ -54,14 +54,14 @@ export class QueryAction implements IQueryAction {
* @param value The value to update.
* @param options.returning If `true`, return the updated row(s) in the response.
*/
- update(value: Dictionary<any>, options?: { returning: boolean }) {
+ update(value: Dictionary<any>, options?: { returning: boolean; enumArrayColumns?: string[] }) {
return new QueryFilter(this.table, 'update', value, options)
}
/**
* Performs a TRUNCATE on the table
*/
- truncate(options?: { returning: boolean }) {
+ truncate(options?: { returning: boolean; enumArrayColumns?: string[] }) {
return new QueryFilter(this.table, 'truncate', undefined, options)
}
}
diff --git a/studio/components/grid/query/QueryFilter.ts b/studio/components/grid/query/QueryFilter.ts
index 3a5976b2331..e266ff3d21a 100644
--- a/studio/components/grid/query/QueryFilter.ts
+++ b/studio/components/grid/query/QueryFilter.ts
@@ -15,7 +15,7 @@ export class QueryFilter implements IQueryFilter, IQueryModifier {
protected table: QueryTable,
protected action: 'count' | 'delete' | 'insert' | 'select' | 'update' | 'truncate',
protected actionValue?: string | string[] | Dictionary<any> | Dictionary<any>[],
- protected actionOptions?: { returning: boolean }
+ protected actionOptions?: { returning: boolean; enumArrayColumns?: string[] }
) {}
filter(column: string, operator: FilterOperator, value: any) {
diff --git a/studio/components/grid/query/QueryModifier.ts b/studio/components/grid/query/QueryModifier.ts
index 62fae3b0c78..e9feffb1d2b 100644
--- a/studio/components/grid/query/QueryModifier.ts
+++ b/studio/components/grid/query/QueryModifier.ts
@@ -21,7 +21,7 @@ export class QueryModifier implements IQueryModifier {
protected action: 'count' | 'delete' | 'insert' | 'select' | 'update' | 'truncate',
protected options?: {
actionValue?: string | string[] | Dictionary<any> | Dictionary<any>[]
- actionOptions?: { returning?: boolean; cascade?: boolean }
+ actionOptions?: { returning?: boolean; cascade?: boolean; enumArrayColumns?: string[] }
filters?: Filter[]
sorts?: Sort[]
}
@@ -51,11 +51,13 @@ export class QueryModifier implements IQueryModifier {
case 'delete': {
return deleteQuery(this.table, filters, {
returning: actionOptions?.returning,
+ enumArrayColumns: actionOptions?.enumArrayColumns,
})
}
case 'insert': {
return insertQuery(this.table, actionValue as Dictionary<any>[], {
returning: actionOptions?.returning,
+ enumArrayColumns: actionOptions?.enumArrayColumns,
})
}
case 'select': {
@@ -69,6 +71,7 @@ export class QueryModifier implements IQueryModifier {
return updateQuery(this.table, actionValue as Dictionary<any>, {
filters,
returning: actionOptions?.returning,
+ enumArrayColumns: actionOptions?.enumArrayColumns,
})
}
case 'truncate': {
diff --git a/studio/components/grid/services/row/SqlRowService.ts b/studio/components/grid/services/row/SqlRowService.ts
index 59ab4bc83c3..9d4263529ac 100644
--- a/studio/components/grid/services/row/SqlRowService.ts
+++ b/studio/components/grid/services/row/SqlRowService.ts
@@ -184,6 +184,11 @@ export class SqlRowService implements IRowService {
// remove primary key from updated value object
delete value[key]
})
+ const enumArrayColumns = this.table.columns
+ .filter((column) => {
+ return (column?.enum ?? []).length > 0 && column.dataType.toLowerCase() === 'array'
+ })
+ .map((column) => column.name)
const query = this.query
.from(this.table.name, this.table.schema ?? undefined)
.update(
@@ -192,7 +197,7 @@ export class SqlRowService implements IRowService {
[changedColumn]: value[changedColumn],
}
: value,
- { returning: true }
+ { returning: true, enumArrayColumns }
)
.match(matchValues)
.toSql()
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 3d9074eadaa..8a0d3ee57d3 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -63,11 +63,20 @@ const SidePanelEditor: FC<Props> = ({
configuration: { identifiers: any; rowIdx: number },
onComplete: Function
) => {
+ if (selectedTable === undefined) return
+
let saveRowError = false
+ // @ts-ignore
+ const enumArrayColumns = selectedTable.columns
+ .filter((column) => {
+ return (column?.enums ?? []).length > 0 && column.data_type.toLowerCase() === 'array'
+ })
+ .map((column) => column.name)
+
if (isNewRecord) {
const insertQuery = new Query()
- .from(selectedTable!.name, selectedTable!.schema)
- .insert([payload], { returning: true })
+ .from(selectedTable.name, selectedTable.schema)
+ .insert([payload], { returning: true, enumArrayColumns })
.toSql()
const res: any = await meta.query(insertQuery)
@@ -80,10 +89,10 @@ const SidePanelEditor: FC<Props> = ({
} else {
const hasChanges = !isEmpty(payload)
if (hasChanges) {
- if (selectedTable!.primary_keys.length > 0) {
+ if (selectedTable.primary_keys.length > 0) {
const updateQuery = new Query()
- .from(selectedTable!.name, selectedTable!.schema)
- .update(payload, { returning: true })
+ .from(selectedTable.name, selectedTable.schema)
+ .update(payload, { returning: true, enumArrayColumns })
.match(configuration.identifiers)
.toSql()
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 7ee7667b8b8..ef127b6ab46 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -1,7 +1,7 @@
import { v4 as _uuidV4 } from 'uuid'
+import { PostgresColumn } from '@supabase/postgres-meta'
import { post } from 'lib/common/fetch'
import { PASSWORD_STRENGTH, DEFAULT_MINIMUM_PASSWORD_STRENGTH, API_URL } from 'lib/constants'
-import { TEXT_TYPES } from 'components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.constants'
export const tryParseJson = (jsonString: any) => {
try {