From 07c2e90ab3ef7b14c828eecea4f4b76fe56f2992 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 27 Apr 2023 19:56:37 +0800 Subject: [PATCH] fix(nocodb): handle datetime timezone in backend --- packages/nocodb-nest/src/models/Model.ts | 28 +++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/nocodb-nest/src/models/Model.ts b/packages/nocodb-nest/src/models/Model.ts index abb6ec5268..818c3eb067 100644 --- a/packages/nocodb-nest/src/models/Model.ts +++ b/packages/nocodb-nest/src/models/Model.ts @@ -1,4 +1,5 @@ import { isVirtualCol, ModelTypes, UITypes, ViewTypes } from 'nocodb-sdk'; +import dayjs from 'dayjs'; import { BaseModelSqlv2 } from '../db/BaseModelSqlv2'; import Noco from '../Noco'; import { parseMetaProp } from '../utils/modelUtils'; @@ -15,6 +16,7 @@ import { sanitize } from '../helpers/sqlSanitize'; import { extractProps } from '../helpers/extractProps'; import Audit from './Audit'; import View from './View'; +import Base from './Base'; import Column from './Column'; import type { BoolType, TableReqType, TableType } from 'nocodb-sdk'; import type { XKnex } from '../db/CustomKnex'; @@ -430,8 +432,15 @@ export default class Model implements TableType { return true; } - async mapAliasToColumn(data) { + async mapAliasToColumn( + data, + clientMeta = { + isMySQL: false, + isSqlite: false, + }, + ) { const insertObj = {}; + const base = await Base.get(this.base_id); for (const col of await this.getColumns()) { if (isVirtualCol(col)) continue; let val = @@ -442,6 +451,23 @@ export default class Model implements TableType { if (col.uidt === UITypes.Attachment && typeof val !== 'string') { val = JSON.stringify(val); } + if (col.uidt === UITypes.DateTime && dayjs(val).isValid()) { + const { isMySQL, isSqlite } = clientMeta; + if (base.is_meta) { + if (isMySQL) { + val = dayjs(val)?.format('YYYY-MM-DD HH:mm:ss'); + } else if (isSqlite) { + val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss'); + } else { + val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ'); + } + } else { + // TODO(timezone): keep ext db as it is + val = dayjs(val).format( + isMySQL ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ', + ); + } + } insertObj[sanitize(col.column_name)] = val; } }