From e878b4d5753fbb1896ffe6176638b2d870cd8c82 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Mon, 8 May 2023 18:01:01 +0800 Subject: [PATCH] fix(nocodb): handle mysql xcdb api response --- packages/nocodb/src/db/BaseModelSqlv2.ts | 34 +++++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index c3164c15b6..9d37132c27 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -7,6 +7,7 @@ import { nocoExecute } from 'nc-help'; import { AuditOperationSubTypes, AuditOperationTypes, + BoolType, isSystemColumn, isVirtualCol, RelationTypes, @@ -3169,9 +3170,8 @@ class BaseModelSqlv2 { data = this.convertAttachmentType(data, childTable); // update date time fields - if (this.isMySQL && !(await this.isXcdbBase())) { - data = this.convertDateFormat(data, childTable); - } + const isXcdbBase = await this.isXcdbBase(); + data = this.convertDateFormat(data, isXcdbBase, childTable); return data; } @@ -3215,15 +3215,23 @@ class BaseModelSqlv2 { private _convertDateFormat( dateTimeColumns: Record[], d: Record, + isXcdbBase: BoolType, ) { try { if (d) { dateTimeColumns.forEach((col) => { if (d[col.title] && typeof d[col.title] === 'string') { - // e.g. 2022-01-01 04:30:00+00:00 - d[col.title] = dayjs(d[col.title]) - .utc() - .format('YYYY-MM-DD HH:mm:ssZ'); + if (isXcdbBase) { + // e.g. 01.01.2022 10:00:00+05:30 -> 2022-01-01 04:30:00+00:00 + d[col.title] = dayjs(d[col.title]) + .utc(true) + .format('YYYY-MM-DD HH:mm:ssZ'); + } else { + // e.g. 01.01.2022 10:00:00+05:30 -> 2022-01-01 04:30:00+00:00 + d[col.title] = dayjs(d[col.title]) + .utc() + .format('YYYY-MM-DD HH:mm:ssZ'); + } } }); } @@ -3231,7 +3239,11 @@ class BaseModelSqlv2 { return d; } - private convertDateFormat(data: Record, childTable?: Model) { + private convertDateFormat( + data: Record, + isXcdbBase: BoolType, + childTable?: Model, + ) { // MySQL converts TIMESTAMP values from the current time zone to UTC for storage. // Then, MySQL converts those values back from UTC to the current time zone for retrieval. // For xcdb base, to make it consistent with other DB types, we show the result in UTC instead @@ -3242,9 +3254,11 @@ class BaseModelSqlv2 { ).filter((c) => c.uidt === UITypes.DateTime); if (dateTimeColumns.length) { if (Array.isArray(data)) { - data = data.map((d) => this._convertDateFormat(dateTimeColumns, d)); + data = data.map((d) => + this._convertDateFormat(dateTimeColumns, d, isXcdbBase), + ); } else { - this._convertDateFormat(dateTimeColumns, data); + this._convertDateFormat(dateTimeColumns, data, isXcdbBase); } } }