From f92791954a70fb261ebf1813759345fb733b958b Mon Sep 17 00:00:00 2001 From: Luca Restagno <59067245+lucarestagno@users.noreply.github.com> Date: Thu, 9 Mar 2023 14:52:03 +0100 Subject: [PATCH] Convert mutation payload values according to the column data type PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8210 GitOrigin-RevId: 18a0909f3b99b5ef93891baf03454b88cade2055 --- .../DataSource/gdc/introspection/utils.tsx | 12 ++++++------ .../lib/features/InsertRow/InsertRowForm.tsx | 17 +++++++++++++++-- .../InsertRow/InsertRowForm.utils.test.ts | 19 +++++++++++++++++++ .../features/InsertRow/InsertRowForm.utils.ts | 18 ++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.test.ts create mode 100644 frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.ts diff --git a/frontend/libs/console/legacy-ce/src/lib/features/DataSource/gdc/introspection/utils.tsx b/frontend/libs/console/legacy-ce/src/lib/features/DataSource/gdc/introspection/utils.tsx index e6bed97dc9e..0e23e1de672 100644 --- a/frontend/libs/console/legacy-ce/src/lib/features/DataSource/gdc/introspection/utils.tsx +++ b/frontend/libs/console/legacy-ce/src/lib/features/DataSource/gdc/introspection/utils.tsx @@ -61,15 +61,15 @@ export function adaptAgentDataType( const DataTypeToSQLTypeMap: Record = { bool: ['bool'], string: ['string'], - number: ['number'], - datetime: [], - timestamp: [], - xml: [], - json: [], + number: ['number', 'integer', 'float'], + datetime: ['datetime'], + timestamp: ['timestamp'], + xml: ['xml'], + json: ['json', 'jsonb'], }; const [dataType] = getEntries(DataTypeToSQLTypeMap).find(([, value]) => - value.includes(sqlDataType) + value.includes(sqlDataType.toLowerCase()) ) ?? ['string', []]; return dataType; diff --git a/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.tsx b/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.tsx index 715f46fca17..4e6294c59da 100644 --- a/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.tsx +++ b/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.tsx @@ -4,6 +4,7 @@ import { InsertRowArgs } from '../DataSource'; import { ColumnRow } from './components/ColumnRow'; import { FormData } from '../Data/hooks/useInsertRow'; import Skeleton from 'react-loading-skeleton'; +import { convertTableValue } from './InsertRowForm.utils'; import { ListAllTableColumn } from '../Data/hooks/useListAllTableColumns'; export type InsertRowFormProps = { @@ -23,10 +24,22 @@ export const InsertRowForm: React.VFC = ({ const onInsert = () => { const adaptedValues = values.reduce((acc, value) => { + const columnName = Object.keys(value)[0]; + const columnValue = Object.values(value)[0]; + + const columnDefinition = columns.find( + column => column.name === columnName + ); + + const finalColumnValue = convertTableValue( + columnValue, + columnDefinition?.dataType + ); + const formData: FormData = { - [Object.keys(value)[0]]: { + [columnName]: { option: 'value', - value: Object.values(value)[0], + value: finalColumnValue, }, }; diff --git a/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.test.ts b/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.test.ts new file mode 100644 index 00000000000..025cd0e8f05 --- /dev/null +++ b/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.test.ts @@ -0,0 +1,19 @@ +import { convertTableValue } from './InsertRowForm.utils'; + +describe('convertTableValue', () => { + it.each` + columnValue | dataType | expected + ${'foo'} | ${'string'} | ${'foo'} + ${'13.2'} | ${'number'} | ${13.2} + ${'11'} | ${'number'} | ${11} + ${'true'} | ${'boolean'} | ${true} + ${'false'} | ${'boolean'} | ${false} + ${'true'} | ${'bool'} | ${true} + ${'false'} | ${'bool'} | ${false} + `( + 'given $dataType it returns $expected', + ({ columnValue, dataType, expected }) => { + expect(convertTableValue(columnValue, dataType)).toEqual(expected); + } + ); +}); diff --git a/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.ts b/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.ts new file mode 100644 index 00000000000..d113d034188 --- /dev/null +++ b/frontend/libs/console/legacy-ce/src/lib/features/InsertRow/InsertRowForm.utils.ts @@ -0,0 +1,18 @@ +import { TableColumn } from '../DataSource'; + +export const convertTableValue = ( + value: unknown, + dataType: TableColumn['dataType'] | undefined +): string | number | boolean | unknown => { + if (typeof value === 'string') { + if (dataType === 'number') { + return parseFloat(value); + } + + if (dataType === 'boolean' || dataType === 'bool') { + return value === 'true' ? true : false; + } + } + + return value; +};