Browse Source

Merge pull request #5689 from nocodb/refactor/timezone-locale

refactor: timezone locale
pull/5752/head
աɨռɢӄաօռɢ 2 years ago committed by GitHub
parent
commit
b15dbcc835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13860
      package-lock.json
  2. 52
      packages/nc-gui/components/cell/DateTimePicker.vue
  3. 7
      packages/nc-gui/components/smartsheet/Cell.vue
  4. 6
      packages/nc-gui/components/smartsheet/Grid.vue
  5. 6
      packages/nc-gui/components/virtual-cell/Formula.vue
  6. 4
      packages/nc-gui/components/virtual-cell/components/ItemChip.vue
  7. 4
      packages/nc-gui/components/virtual-cell/components/ListChildItems.vue
  8. 6
      packages/nc-gui/components/virtual-cell/components/ListItems.vue
  9. 3
      packages/nc-gui/composables/useMultiSelect/convertCellData.ts
  10. 58
      packages/nc-gui/composables/useMultiSelect/index.ts
  11. 2
      packages/nc-gui/lib/types.ts
  12. 2
      packages/nc-gui/plugins/a.dayjs.ts
  13. 7
      packages/nc-gui/store/project.ts
  14. 23
      packages/nc-gui/utils/cell.ts
  15. 8
      packages/nc-gui/utils/dateTimeUtils.ts
  16. 1
      packages/nc-gui/utils/index.ts
  17. 294
      packages/nocodb/src/db/BaseModelSqlv2.ts
  18. 14
      packages/nocodb/src/db/CustomKnex.ts
  19. 45
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  20. 8
      packages/nocodb/src/db/functionMappings/mssql.ts
  21. 2
      packages/nocodb/src/db/functionMappings/mysql.ts
  22. 6
      packages/nocodb/src/db/functionMappings/sqlite.ts
  23. 34
      packages/nocodb/src/meta/meta.service.ts
  24. 65
      packages/nocodb/src/models/Model.ts
  25. 51
      packages/nocodb/tests/unit/model/tests/baseModelSql.test.ts
  26. 4
      packages/nocodb/tests/unit/rest/tests/tableRow.test.ts
  27. 4576
      tests/playwright/package-lock.json
  28. 3
      tests/playwright/package.json
  29. 9
      tests/playwright/pages/Dashboard/ExpandedForm/index.ts
  30. 24
      tests/playwright/pages/Dashboard/TreeView.ts
  31. 29
      tests/playwright/pages/Dashboard/common/Cell/DateTimeCell.ts
  32. 13
      tests/playwright/pages/Dashboard/common/Cell/index.ts
  33. 2
      tests/playwright/pages/Dashboard/common/Toolbar/Filter.ts
  34. 1132
      tests/playwright/tests/db/timezone.spec.ts
  35. 2
      tests/playwright/tests/db/viewGridShare.spec.ts
  36. 48
      tests/playwright/tests/utils/config.ts
  37. 12
      tests/playwright/tests/utils/general.ts

13860
package-lock.json generated

File diff suppressed because it is too large Load Diff

52
packages/nc-gui/components/cell/DateTimePicker.vue

@ -18,13 +18,14 @@ import {
interface Props {
modelValue?: string | null
isPk?: boolean
isUpdatedFromCopyNPaste: Record<string, boolean>
}
const { modelValue, isPk } = defineProps<Props>()
const { modelValue, isPk, isUpdatedFromCopyNPaste } = defineProps<Props>()
const emit = defineEmits(['update:modelValue'])
const { isMysql } = useProject()
const { isMssql, isXcdbBase } = useProject()
const { showNull } = useGlobal()
@ -44,6 +45,8 @@ const dateTimeFormat = $computed(() => {
return `${dateFormat} ${timeFormat}`
})
let localModelValue = modelValue ? dayjs(modelValue).utc().local() : undefined
let localState = $computed({
get() {
if (!modelValue) {
@ -55,7 +58,45 @@ let localState = $computed({
return undefined
}
return /^\d+$/.test(modelValue) ? dayjs(+modelValue) : dayjs(modelValue)
const isXcDB = isXcdbBase(column.value.base_id)
// cater copy and paste
// when copying a datetime cell, the copied value would be local time
// when pasting a datetime cell, UTC (xcdb) will be saved in DB
// we convert back to local time
if (column.value.title! in (isUpdatedFromCopyNPaste ?? {})) {
localModelValue = dayjs(modelValue).utc().local()
return localModelValue
}
// ext db
if (!isXcDB) {
return /^\d+$/.test(modelValue) ? dayjs(+modelValue) : dayjs(modelValue)
}
if (isMssql(column.value.base_id)) {
// e.g. 2023-04-29T11:41:53.000Z
return dayjs(modelValue)
}
// if cdf is defined, that means the value is auto-generated
// hence, show the local time
if (column?.value?.cdf) {
return dayjs(modelValue).utc().local()
}
// if localModelValue is defined, show localModelValue instead
// localModelValue is set in setter below
if (localModelValue) {
const res = localModelValue
// resetting localModelValue here
// e.g. save in expanded form -> render the correct modelValue
localModelValue = undefined
return res
}
// empty cell - use modelValue in local time
return dayjs(modelValue).utc().local()
},
set(val?: dayjs.Dayjs) {
if (!val) {
@ -64,7 +105,10 @@ let localState = $computed({
}
if (val.isValid()) {
emit('update:modelValue', val?.format(isMysql(column.value.base_id) ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'))
// setting localModelValue to cater NOW function in date picker
localModelValue = dayjs(val)
// send the payload in UTC format
emit('update:modelValue', dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ'))
}
},
})

7
packages/nc-gui/components/smartsheet/Cell.vue

@ -209,7 +209,12 @@ onUnmounted(() => {
<LazyCellMultiSelect v-else-if="isMultiSelect(column)" v-model="vModel" :row-index="props.rowIndex" />
<LazyCellDatePicker v-else-if="isDate(column, abstractType)" v-model="vModel" :is-pk="isPrimaryKey(column)" />
<LazyCellYearPicker v-else-if="isYear(column, abstractType)" v-model="vModel" :is-pk="isPrimaryKey(column)" />
<LazyCellDateTimePicker v-else-if="isDateTime(column, abstractType)" v-model="vModel" :is-pk="isPrimaryKey(column)" />
<LazyCellDateTimePicker
v-else-if="isDateTime(column, abstractType)"
v-model="vModel"
:is-pk="isPrimaryKey(column)"
:is-updated-from-copy-n-paste="currentRow.rowMeta.isUpdatedFromCopyNPaste"
/>
<LazyCellTimePicker v-else-if="isTime(column, abstractType)" v-model="vModel" :is-pk="isPrimaryKey(column)" />
<LazyCellRating v-else-if="isRating(column)" v-model="vModel" />
<LazyCellDuration v-else-if="isDuration(column)" v-model="vModel" />

6
packages/nc-gui/components/smartsheet/Grid.vue

@ -316,6 +316,12 @@ const {
return
}
// See DateTimePicker.vue for details
data.value[ctx.row].rowMeta.isUpdatedFromCopyNPaste = {
...data.value[ctx.row].rowMeta.isUpdatedFromCopyNPaste,
[ctx.updatedColumnTitle || columnObj.title]: true,
}
// update/save cell value
await updateOrSaveRow(rowObj, ctx.updatedColumnTitle || columnObj.title)
},

6
packages/nc-gui/components/virtual-cell/Formula.vue

@ -1,7 +1,7 @@
<script lang="ts" setup>
import type { ColumnType } from 'nocodb-sdk'
import type { Ref } from 'vue'
import { CellValueInj, ColumnInj, computed, handleTZ, inject, replaceUrlsWithLink, useProject } from '#imports'
import { CellValueInj, ColumnInj, computed, handleTZ, inject, renderValue, replaceUrlsWithLink, useProject } from '#imports'
// todo: column type doesn't have required property `error` - throws in typecheck
const column = inject(ColumnInj) as Ref<ColumnType & { colOptions: { error: any } }>
@ -10,7 +10,9 @@ const cellValue = inject(CellValueInj)
const { isPg } = useProject()
const result = computed(() => (isPg(column.value.base_id) ? handleTZ(cellValue?.value) : cellValue?.value))
const result = computed(() =>
isPg(column.value.base_id) ? renderValue(handleTZ(cellValue?.value)) : renderValue(cellValue?.value),
)
const urls = computed(() => replaceUrlsWithLink(result.value))

4
packages/nc-gui/components/virtual-cell/components/ItemChip.vue

@ -7,9 +7,9 @@ import {
iconMap,
inject,
ref,
renderValue,
useExpandedFormDetached,
useLTARStoreOrThrow,
useUIPermission,
} from '#imports'
interface Props {
@ -60,7 +60,7 @@ export default {
:class="{ active }"
@click="openExpandedForm"
>
<span class="name">{{ value }}</span>
<span class="name">{{ renderValue(value) }}</span>
<div v-show="active || isForm" v-if="!readOnly && !isLocked && isUIAllowed('xcDatatableEditable')" class="flex items-center">
<component

4
packages/nc-gui/components/virtual-cell/components/ListChildItems.vue

@ -13,10 +13,10 @@ import {
iconMap,
inject,
ref,
renderValue,
useLTARStoreOrThrow,
useSmartsheetRowStoreOrThrow,
useVModel,
watch,
} from '#imports'
const props = defineProps<{ modelValue?: boolean; cellValue: any }>()
@ -148,7 +148,7 @@ const onClick = (row: Row) => {
>
<div class="flex items-center">
<div class="flex-1 overflow-hidden min-w-0">
{{ row[relatedTableDisplayValueProp] }}
{{ renderValue(row[relatedTableDisplayValueProp]) }}
<span class="text-gray-400 text-[11px] ml-1">(Primary key : {{ getRelatedTableRowId(row) }})</span>
</div>

6
packages/nc-gui/components/virtual-cell/components/ListItems.vue

@ -12,11 +12,11 @@ import {
inject,
isDrawerExist,
ref,
renderValue,
useLTARStoreOrThrow,
useSelectedCellKeyupListener,
useSmartsheetRowStoreOrThrow,
useVModel,
watch,
} from '#imports'
const props = defineProps<{ modelValue: boolean }>()
@ -213,7 +213,7 @@ watch(vModel, (nextVal) => {
<component :is="iconMap.reload" class="cursor-pointer text-gray-500 nc-reload" @click="loadChildrenExcludedList" />
<!-- Add new record -->
<!-- Add new record -->
<a-button v-if="!isPublic" type="primary" size="small" @click="expandedFormDlg = true">
{{ $t('activity.addNewRecord') }}
</a-button>
@ -229,7 +229,7 @@ watch(vModel, (nextVal) => {
:class="{ 'nc-selected-row': selectedRowIndex === i }"
@click="linkRow(refRow)"
>
{{ refRow[relatedTableDisplayValueProp] }}
{{ renderValue(refRow[relatedTableDisplayValueProp]) }}
<span class="hidden group-hover:(inline) text-gray-400 text-[11px] ml-1">
({{ $t('labels.primaryKey') }} : {{ getRelatedTableRowId(refRow) }})
</span>

3
packages/nc-gui/composables/useMultiSelect/convertCellData.ts

@ -7,6 +7,7 @@ import { parseProp } from '#imports'
export default function convertCellData(
args: { from: UITypes; to: UITypes; value: any; column: ColumnType; appInfo: AppInfo },
isMysql = false,
isXcdbBase = false,
) {
const { from, to, value } = args
if (from === to && ![UITypes.Attachment, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year].includes(to)) {
@ -42,7 +43,7 @@ export default function convertCellData(
if (!parsedDateTime.isValid()) {
throw new Error('Not a valid datetime value')
}
return parsedDateTime.format(dateFormat)
return parsedDateTime.utc().format('YYYY-MM-DD HH:mm:ssZ')
}
case UITypes.Time: {
let parsedTime = dayjs(value)

58
packages/nc-gui/composables/useMultiSelect/index.ts

@ -1,3 +1,4 @@
import dayjs from 'dayjs'
import type { MaybeRef } from '@vueuse/core'
import type { ColumnType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk'
import { RelationTypes, UITypes, isVirtualCol } from 'nocodb-sdk'
@ -7,6 +8,7 @@ import convertCellData from './convertCellData'
import type { Nullable, Row } from '~/lib'
import {
copyTable,
dateFormats,
extractPkFromRow,
extractSdkResponseErrorMsg,
isMac,
@ -14,6 +16,7 @@ import {
message,
reactive,
ref,
timeFormats,
unref,
useCopy,
useEventListener,
@ -52,7 +55,7 @@ export function useMultiSelect(
const { appInfo } = useGlobal()
const { isMysql } = useProject()
const { isMysql, isXcdbBase } = useProject()
let clipboardContext = $ref<{ value: any; uidt: UITypes } | null>(null)
@ -79,6 +82,20 @@ export function useMultiSelect(
activeCell.col = col
}
function constructDateTimeFormat(column: ColumnType) {
const dateFormat = constructDateFormat(column)
const timeFormat = constructTimeFormat(column)
return `${dateFormat} ${timeFormat}`
}
function constructDateFormat(column: ColumnType) {
return parseProp(column?.meta)?.date_format ?? dateFormats[0]
}
function constructTimeFormat(column: ColumnType) {
return parseProp(column?.meta)?.time_format ?? timeFormats[0]
}
async function copyValue(ctx?: Cell) {
try {
if (selectedRange.start !== null && selectedRange.end !== null && !selectedRange.isSingleCell()) {
@ -106,6 +123,43 @@ export function useMultiSelect(
if (typeof textToCopy === 'object') {
textToCopy = JSON.stringify(textToCopy)
}
if (columnObj.uidt === UITypes.Formula) {
textToCopy = textToCopy.replace(/\b(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2})\b/g, (d: string) => {
// TODO(timezone): retrieve the format from the corresponding column meta
// assume hh:mm at this moment
return dayjs(d).utc().local().format('YYYY-MM-DD HH:mm')
})
}
if (columnObj.uidt === UITypes.DateTime || columnObj.uidt === UITypes.Time) {
// remove `"`
// e.g. "2023-05-12T08:03:53.000Z" -> 2023-05-12T08:03:53.000Z
textToCopy = textToCopy.replace(/["']/g, '')
const isMySQL = isMysql(columnObj.base_id)
let d = dayjs(textToCopy)
if (!d.isValid()) {
// insert a datetime value, copy the value without refreshing
// e.g. textToCopy = 2023-05-12T03:49:25.000Z
// feed custom parse format
d = dayjs(textToCopy, isMySQL ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ')
}
// users can change the datetime format in UI
// `textToCopy` would be always in YYYY-MM-DD HH:mm:ss(Z / +xx:yy) format
// therefore, here we reformat to the correct datetime format based on the meta
textToCopy = d.format(
columnObj.uidt === UITypes.DateTime ? constructDateTimeFormat(columnObj) : constructTimeFormat(columnObj),
)
if (columnObj.uidt === UITypes.DateTime && !dayjs(textToCopy).isValid()) {
throw new Error('Invalid DateTime')
}
}
await copy(textToCopy)
message.success(t('msg.info.copiedToClipboard'))
}
@ -305,6 +359,7 @@ export function useMultiSelect(
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
isXcdbBase(meta.value?.base_id),
)
e.preventDefault()
@ -339,6 +394,7 @@ export function useMultiSelect(
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
isXcdbBase(meta.value?.base_id),
)
e.preventDefault()
syncCellData?.(activeCell)

2
packages/nc-gui/lib/types.ts

@ -60,6 +60,8 @@ export interface Row {
commentCount?: number
changed?: boolean
saving?: boolean
// use in datetime picker component
isUpdatedFromCopyNPaste?: Record<string, boolean>
}
}

2
packages/nc-gui/plugins/a.dayjs.ts

@ -4,6 +4,7 @@ import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import duration from 'dayjs/plugin/duration.js'
import utc from 'dayjs/plugin/utc.js'
import weekday from 'dayjs/plugin/weekday.js'
import timezone from 'dayjs/plugin/timezone.js'
import { defineNuxtPlugin } from '#imports'
export default defineNuxtPlugin(() => {
@ -12,4 +13,5 @@ export default defineNuxtPlugin(() => {
extend(customParseFormat)
extend(duration)
extend(weekday)
extend(timezone)
})

7
packages/nc-gui/store/project.ts

@ -82,6 +82,10 @@ export const useProject = defineStore('projectStore', () => {
return ['mysql', ClientType.MYSQL].includes(getBaseType(baseId))
}
function isSqlite(baseId?: string) {
return getBaseType(baseId) === ClientType.SQLITE
}
function isMssql(baseId?: string) {
return getBaseType(baseId) === 'mssql'
}
@ -91,7 +95,7 @@ export const useProject = defineStore('projectStore', () => {
}
function isXcdbBase(baseId?: string) {
return bases.value.find((base) => base.id === baseId)?.is_meta
return (bases.value.find((base) => base.id === baseId)?.is_meta as boolean) || false
}
const isSharedBase = computed(() => projectType === 'base')
@ -209,6 +213,7 @@ export const useProject = defineStore('projectStore', () => {
isMysql,
isMssql,
isPg,
isSqlite,
sqlUis,
isSharedBase,
loadProjectMetaInfo,

23
packages/nc-gui/utils/cell.ts

@ -1,5 +1,6 @@
import type { ColumnType } from 'nocodb-sdk'
import { UITypes } from 'nocodb-sdk'
import dayjs from 'dayjs'
export const dataTypeLow = (column: ColumnType) => column.dt?.toLowerCase()
export const isBoolean = (column: ColumnType, abstractType?: any) =>
@ -53,3 +54,25 @@ export const isManualSaved = (column: ColumnType) => [UITypes.Currency].includes
export const isPrimary = (column: ColumnType) => !!column.pv
export const isPrimaryKey = (column: ColumnType) => !!column.pk
// used for LTAR and Formula
export const renderValue = (result?: any) => {
if (!result || typeof result !== 'string') {
return result
}
// convert ISO string (e.g. in MSSQL) to YYYY-MM-DD hh:mm:ssZ
// e.g. 2023-05-18T05:30:00.000Z -> 2023-05-18 11:00:00+05:30
result = result.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/g, (d: string) => {
return dayjs(d).isValid() ? dayjs(d).format('YYYY-MM-DD HH:mm:ssZ') : d
})
// convert all date time values to local time
// the datetime is either YYYY-MM-DD hh:mm:ss (xcdb)
// or YYYY-MM-DD hh:mm:ss+/-xx:yy (ext)
return result.replace(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:[+-]\d{2}:\d{2})?/g, (d: string) => {
// TODO(timezone): retrieve the format from the corresponding column meta
// assume HH:mm at this moment
return dayjs(d).isValid() ? dayjs(d).format('YYYY-MM-DD HH:mm') : d
})
}

8
packages/nc-gui/utils/dateTimeUtils.ts

@ -1,7 +1,13 @@
import dayjs from 'dayjs'
export const timeAgo = (date: any) => {
return dayjs.utc(date).fromNow()
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(date)) {
// if there is no timezone info, consider as UTC
// e.g. 2023-01-01 08:00:00 (MySQL)
date += '+00:00'
}
// show in local time
return dayjs(date).fromNow()
}
export const dateFormats = [

1
packages/nc-gui/utils/index.ts

@ -23,3 +23,4 @@ export * from './browserUtils'
export * from './geoDataUtils'
export * from './mimeTypeUtils'
export * from './parseUtils'
export * from './cell'

294
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -1,6 +1,9 @@
import autoBind from 'auto-bind';
import groupBy from 'lodash/groupBy';
import DataLoader from 'dataloader';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc.js';
import timezone from 'dayjs/plugin/timezone';
import { nocoExecute } from 'nc-help';
import {
AuditOperationSubTypes,
@ -16,8 +19,16 @@ import DOMPurify from 'isomorphic-dompurify';
import { v4 as uuidv4 } from 'uuid';
import { NcError } from '../helpers/catchError';
import getAst from '../helpers/getAst';
import { Audit, Column, Filter, Model, Project, Sort, View } from '../models';
import {
Audit,
Base,
Column,
Filter,
Model,
Project,
Sort,
View,
} from '../models';
import { sanitize, unsanitize } from '../helpers/sqlSanitize';
import {
COMPARISON_OPS,
@ -46,7 +57,10 @@ import type {
SelectOption,
} from '../models';
import type { Knex } from 'knex';
import type { SortType } from 'nocodb-sdk';
import type { BoolType, SortType } from 'nocodb-sdk';
dayjs.extend(utc);
dayjs.extend(timezone);
const GROUP_COL = '__nc_group_id';
@ -1607,6 +1621,67 @@ class BaseModelSqlv2 {
if (!checkColumnRequired(column, fields, extractPkAndPv)) continue;
switch (column.uidt) {
case UITypes.DateTime:
if (this.isMySQL) {
// MySQL stores timestamp in UTC but display in timezone
// To verify the timezone, run `SELECT @@global.time_zone, @@session.time_zone;`
// If it's SYSTEM, then the timezone is read from the configuration file
// if a timezone is set in a DB, the retrieved value would be converted to the corresponding timezone
// for example, let's say the global timezone is +08:00 in DB
// the value 2023-01-01 10:00:00 (UTC) would display as 2023-01-01 18:00:00 (UTC+8)
// our existing logic is based on UTC, during the query, we need to take the UTC value
// hence, we use CONVERT_TZ to convert back to UTC value
res[sanitize(column.title || column.column_name)] =
this.dbDriver.raw(
`CONVERT_TZ(??, @@GLOBAL.time_zone, '+00:00')`,
[
`${sanitize(alias || this.model.table_name)}.${
column.column_name
}`,
],
);
break;
} else if (this.isPg) {
// if there is no timezone info,
// convert to database timezone,
// then convert to UTC
if (
column.dt !== 'timestamp with time zone' &&
column.dt !== 'timestamptz'
) {
res[sanitize(column.title || column.column_name)] = this.dbDriver
.raw(
`?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`,
[
`${sanitize(alias || this.model.table_name)}.${
column.column_name
}`,
],
)
.wrap('(', ')');
break;
}
} else if (this.isMssql) {
// if there is no timezone info,
// convert to database timezone,
// then convert to UTC
if (column.dt !== 'datetimeoffset') {
res[sanitize(column.title || column.column_name)] =
this.dbDriver.raw(
`CONVERT(DATETIMEOFFSET, ?? AT TIME ZONE 'UTC')`,
[
`${sanitize(alias || this.model.table_name)}.${
column.column_name
}`,
],
);
break;
}
}
res[sanitize(column.title || column.column_name)] = sanitize(
`${alias || this.model.table_name}.${column.column_name}`,
);
break;
case 'LinkToAnotherRecord':
case 'Lookup':
break;
@ -1734,7 +1809,11 @@ class BaseModelSqlv2 {
await populatePk(this.model, data);
// todo: filter based on view
const insertObj = await this.model.mapAliasToColumn(data);
const insertObj = await this.model.mapAliasToColumn(
data,
this.clientMeta,
this.dbDriver,
);
await this.validate(insertObj);
@ -1872,7 +1951,11 @@ class BaseModelSqlv2 {
async updateByPk(id, data, trx?, cookie?) {
try {
const updateObj = await this.model.mapAliasToColumn(data);
const updateObj = await this.model.mapAliasToColumn(
data,
this.clientMeta,
this.dbDriver,
);
await this.validate(data);
@ -1920,6 +2003,16 @@ class BaseModelSqlv2 {
return this.getTnPath(this.model);
}
public get clientMeta() {
return {
isSqlite: this.isSqlite,
isMssql: this.isMssql,
isPg: this.isPg,
isMySQL: this.isMySQL,
// isSnowflake: this.isSnowflake,
};
}
get isSqlite() {
return this.clientType === 'sqlite3';
}
@ -1948,7 +2041,11 @@ class BaseModelSqlv2 {
// const driver = trx ? trx : await this.dbDriver.transaction();
try {
await populatePk(this.model, data);
const insertObj = await this.model.mapAliasToColumn(data);
const insertObj = await this.model.mapAliasToColumn(
data,
this.clientMeta,
this.dbDriver,
);
let rowId = null;
const postInsertOps = [];
@ -2106,7 +2203,11 @@ class BaseModelSqlv2 {
: await Promise.all(
datas.map(async (d) => {
await populatePk(this.model, d);
return this.model.mapAliasToColumn(d);
return this.model.mapAliasToColumn(
d,
this.clientMeta,
this.dbDriver,
);
}),
);
@ -2170,7 +2271,11 @@ class BaseModelSqlv2 {
const updateDatas = raw
? datas
: await Promise.all(datas.map((d) => this.model.mapAliasToColumn(d)));
: await Promise.all(
datas.map((d) =>
this.model.mapAliasToColumn(d, this.clientMeta, this.dbDriver),
),
);
const prevData = [];
const newData = [];
@ -2222,7 +2327,11 @@ class BaseModelSqlv2 {
) {
try {
let count = 0;
const updateData = await this.model.mapAliasToColumn(data);
const updateData = await this.model.mapAliasToColumn(
data,
this.clientMeta,
this.dbDriver,
);
await this.validate(updateData);
const pkValues = await this._extractPksValues(updateData);
if (pkValues) {
@ -2268,7 +2377,9 @@ class BaseModelSqlv2 {
let transaction;
try {
const deleteIds = await Promise.all(
ids.map((d) => this.model.mapAliasToColumn(d)),
ids.map((d) =>
this.model.mapAliasToColumn(d, this.clientMeta, this.dbDriver),
),
);
const deleted = [];
@ -3154,16 +3265,23 @@ class BaseModelSqlv2 {
} else {
query = sanitize(query);
}
return this.convertAttachmentType(
let data =
this.isPg || this.isSnowflake
? (await this.dbDriver.raw(query))?.rows
: query.slice(0, 6) === 'select' && !this.isMssql
? await this.dbDriver.from(
this.dbDriver.raw(query).wrap('(', ') __nc_alias'),
)
: await this.dbDriver.raw(query),
childTable,
);
: await this.dbDriver.raw(query);
// update attachment fields
data = this.convertAttachmentType(data, childTable);
// update date time fields
data = this.convertDateFormat(data, childTable);
return data;
}
private _convertAttachmentType(
@ -3195,7 +3313,153 @@ class BaseModelSqlv2 {
this._convertAttachmentType(attachmentColumns, d),
);
} else {
this._convertAttachmentType(attachmentColumns, data);
data = this._convertAttachmentType(attachmentColumns, data);
}
}
}
return data;
}
// TODO(timezone): retrieve the format from the corresponding column meta
private _convertDateFormat(
dateTimeColumns: Record<string, any>[],
d: Record<string, any>,
) {
if (!d) return d;
for (const col of dateTimeColumns) {
if (!d[col.title]) continue;
if (col.uidt === UITypes.Formula) {
if (!d[col.title] || typeof d[col.title] !== 'string') {
continue;
}
// remove milliseconds
if (this.isMySQL) {
d[col.title] = d[col.title].replace(/\.000000/g, '');
} else if (this.isMssql) {
d[col.title] = d[col.title].replace(/\.0000000 \+00:00/g, '');
}
if (/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/g.test(d[col.title])) {
// convert ISO string (e.g. in MSSQL) to YYYY-MM-DD hh:mm:ssZ
// e.g. 2023-05-18T05:30:00.000Z -> 2023-05-18 11:00:00+05:30
d[col.title] = d[col.title].replace(
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/g,
(d: string) => {
if (!dayjs(d).isValid()) return d;
if (this.isSqlite) {
// e.g. DATEADD formula
return dayjs(d).utc().format('YYYY-MM-DD HH:mm:ssZ');
}
return dayjs(d).utc(true).format('YYYY-MM-DD HH:mm:ssZ');
},
);
continue;
}
// convert all date time values to utc
// the datetime is either YYYY-MM-DD hh:mm:ss (xcdb)
// or YYYY-MM-DD hh:mm:ss+/-xx:yy (ext)
d[col.title] = d[col.title].replace(
/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:[+-]\d{2}:\d{2})?/g,
(d: string) => {
if (!dayjs(d).isValid()) {
return d;
}
if (this.isSqlite) {
// if there is no timezone info,
// we assume the input is on NocoDB server timezone
// then we convert to UTC from server timezone
// example: datetime without timezone
// we need to display 2023-04-27 10:00:00 (in HKT)
// we convert d (e.g. 2023-04-27 18:00:00) to utc, i.e. 2023-04-27 02:00:00+00:00
// if there is timezone info,
// we simply convert it to UTC
// example: datetime with timezone
// e.g. 2023-04-27 10:00:00+05:30 -> 2023-04-27 04:30:00+00:00
return dayjs(d)
.tz(Intl.DateTimeFormat().resolvedOptions().timeZone)
.utc()
.format('YYYY-MM-DD HH:mm:ssZ');
}
// set keepLocalTime to true if timezone info is not found
const keepLocalTime = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/g.test(
d,
);
return dayjs(d).utc(keepLocalTime).format('YYYY-MM-DD HH:mm:ssZ');
},
);
continue;
}
let keepLocalTime = true;
if (this.isSqlite) {
if (!col.cdf) {
if (
d[col.title].indexOf('-') === -1 &&
d[col.title].indexOf('+') === -1 &&
d[col.title].slice(-1) !== 'Z'
) {
// if there is no timezone info,
// we assume the input is on NocoDB server timezone
// then we convert to UTC from server timezone
// e.g. 2023-04-27 10:00:00 (IST) -> 2023-04-27 04:30:00+00:00
d[col.title] = dayjs(d[col.title])
.tz(Intl.DateTimeFormat().resolvedOptions().timeZone)
.utc()
.format('YYYY-MM-DD HH:mm:ssZ');
continue;
} else {
// otherwise, we convert from the given timezone to UTC
keepLocalTime = false;
}
}
}
if (
this.isPg &&
(col.dt === 'timestamp with time zone' || col.dt === 'timestamptz')
) {
// postgres - timezone already attached to input
// e.g. 2023-05-11 16:16:51+08:00
keepLocalTime = false;
}
if (d[col.title] instanceof Date) {
// e.g. MSSQL
// Wed May 10 2023 17:47:46 GMT+0800 (Hong Kong Standard Time)
keepLocalTime = false;
}
// e.g. 01.01.2022 10:00:00+05:30 -> 2022-01-01 04:30:00+00:00
// e.g. 2023-05-09 11:41:49 -> 2023-05-09 11:41:49+00:00
d[col.title] = dayjs(d[col.title])
// keep the local time
.utc(keepLocalTime)
// show the timezone even for Mysql
.format('YYYY-MM-DD HH:mm:ssZ');
}
return d;
}
private convertDateFormat(data: Record<string, any>, childTable?: Model) {
// Show the date time in UTC format in API response
// e.g. 2022-01-01 04:30:00+00:00
if (data) {
const dateTimeColumns = (
childTable ? childTable.columns : this.model.columns
).filter(
(c) => c.uidt === UITypes.DateTime || c.uidt === UITypes.Formula,
);
if (dateTimeColumns.length) {
if (Array.isArray(data)) {
data = data.map((d) => this._convertDateFormat(dateTimeColumns, d));
} else {
data = this._convertDateFormat(dateTimeColumns, data);
}
}
}

14
packages/nocodb/src/db/CustomKnex.ts

@ -1,12 +1,24 @@
import { Knex, knex } from 'knex';
import { SnowflakeClient } from 'nc-help';
import { types } from 'pg';
import pg, { types } from 'pg';
import dayjs from 'dayjs';
import Filter from '../models/Filter';
import type { FilterType } from 'nocodb-sdk';
import type { BaseModelSql } from './BaseModelSql';
// For the code, check out
// https://raw.githubusercontent.com/brianc/node-pg-types/master/lib/builtins.js
// override parsing date column to Date()
types.setTypeParser(1082, (val) => val);
// override timestamp
types.setTypeParser(1114, (val) => {
return dayjs(val).format('YYYY-MM-DD HH:mm:ss');
});
// override timestampz
types.setTypeParser(1184, (val) => {
return dayjs(val).format('YYYY-MM-DD HH:mm:ssZ');
});
const opMappingGen = {
eq: '=',

45
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -537,6 +537,51 @@ async function _formulaQueryBuilder(
};
};
break;
case UITypes.DateTime:
if (knex.clientType().startsWith('mysql')) {
aliasToColumn[col.id] = async (): Promise<any> => {
return {
// convert from DB timezone to UTC
builder: knex.raw(
`CONVERT_TZ(??, @@GLOBAL.time_zone, '+00:00')`,
[col.column_name],
),
};
};
} else if (
knex.clientType() === 'pg' &&
col.dt !== 'timestamp with time zone' &&
col.dt !== 'timestamptz'
) {
aliasToColumn[col.id] = async (): Promise<any> => {
return {
// convert from DB timezone to UTC
builder: knex
.raw(
`?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`,
[col.column_name],
)
.wrap('(', ')'),
};
};
} else if (
knex.clientType() === 'mssql' &&
col.dt !== 'datetimeoffset'
) {
// convert from DB timezone to UTC
aliasToColumn[col.id] = async (): Promise<any> => {
return {
builder: knex.raw(
`CONVERT(DATETIMEOFFSET, ?? AT TIME ZONE 'UTC')`,
[col.column_name],
),
};
};
} else {
aliasToColumn[col.id] = () =>
Promise.resolve({ builder: col.column_name });
}
break;
default:
aliasToColumn[col.id] = () =>
Promise.resolve({ builder: col.column_name });

8
packages/nocodb/src/db/functionMappings/mssql.ts

@ -130,15 +130,15 @@ const mssql = {
)},
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${
(await fn(pt.arguments[0])).builder
}), 'yyyy-MM-dd HH:mm')
}), 'yyyy-MM-dd HH:mm:ss')
ELSE
FORMAT(DATEADD(${String((await fn(pt.arguments[2])).builder).replace(
/["']/g,
'',
)},
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${fn(
pt.arguments[0],
)}), 'yyyy-MM-dd')
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${
(await fn(pt.arguments[0])).builder
}), 'yyyy-MM-dd')
END${colAlias}`,
),
};

2
packages/nocodb/src/db/functionMappings/mysql.ts

@ -62,7 +62,7 @@ const mysql2 = {
DATE_FORMAT(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL
${(await fn(pt.arguments[1])).builder} ${String(
(await fn(pt.arguments[2])).builder,
).replace(/["']/g, '')}), '%Y-%m-%d %H:%i')
).replace(/["']/g, '')}), '%Y-%m-%d %H:%i:%s')
ELSE
DATE(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL
${(await fn(pt.arguments[1])).builder} ${String(

6
packages/nocodb/src/db/functionMappings/sqlite.ts

@ -95,9 +95,9 @@ const sqlite3 = {
builder: knex.raw(
`CASE
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${
STRFTIME('%Y-%m-%dT%H:%M:%fZ', DATETIME(DATETIME(${
(await fn(pt.arguments[0])).builder
}, 'localtime'),
}, 'utc'),
${dateIN > 0 ? '+' : ''}${
(await fn(pt.arguments[1])).builder
} || ' ${String((await fn(pt.arguments[2])).builder).replace(
@ -105,7 +105,7 @@ const sqlite3 = {
'',
)}'))
ELSE
DATE(DATETIME(${(await fn(pt.arguments[0])).builder}, 'localtime'),
DATE(DATETIME(${(await fn(pt.arguments[0])).builder}),
${dateIN > 0 ? '+' : ''}${
(await fn(pt.arguments[1])).builder
} || ' ${String((await fn(pt.arguments[2])).builder).replace(

34
packages/nocodb/src/meta/meta.service.ts

@ -6,7 +6,9 @@ import {
OnModuleInit,
Optional,
} from '@nestjs/common';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { customAlphabet } from 'nanoid';
import CryptoJS from 'crypto-js';
import { Connection } from '../connection/connection';
@ -16,6 +18,9 @@ import XcMigrationSourcev2 from './migrations/XcMigrationSourcev2';
import XcMigrationSource from './migrations/XcMigrationSource';
import type { Knex } from 'knex';
dayjs.extend(utc);
dayjs.extend(timezone);
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 4);
// todo: tobe fixed
@ -256,8 +261,8 @@ export class MetaService {
await this.knexConnection(target).insert({
...insertObj,
created_at: data?.created_at || this.knexConnection?.fn?.now(),
updated_at: data?.updated_at || this.knexConnection?.fn?.now(),
created_at: this.now(),
updated_at: this.now(),
});
return insertObj;
}
@ -539,9 +544,9 @@ export class MetaService {
return this.knexConnection(target).insert({
db_alias: dbAlias,
project_id,
created_at: this.knexConnection?.fn?.now(),
updated_at: this.knexConnection?.fn?.now(),
...data,
created_at: this.now(),
updated_at: this.now(),
});
}
@ -689,7 +694,7 @@ export class MetaService {
delete data.created_at;
query.update({ ...data, updated_at: this.knexConnection?.fn?.now() });
query.update({ ...data, updated_at: this.now() });
if (typeof idOrCondition !== 'object') {
query.where('id', idOrCondition);
} else if (idOrCondition) {
@ -810,8 +815,8 @@ export class MetaService {
// todo: check project name used or not
await this.knexConnection('nc_projects').insert({
...project,
created_at: this.knexConnection?.fn?.now(),
updated_at: this.knexConnection?.fn?.now(),
created_at: this.now(),
updated_at: this.now(),
});
// todo
@ -1030,6 +1035,19 @@ export class MetaService {
return nanoid();
}
private isMySQL(): boolean {
return (
this.connection.clientType() === 'mysql' ||
this.connection.clientType() === 'mysql2'
);
}
private now(): any {
return dayjs()
.utc()
.format(this.isMySQL() ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ');
}
public async audit(
project_id: string,
dbAlias: string,

65
packages/nocodb/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';
@ -458,8 +460,18 @@ export default class Model implements TableType {
return true;
}
async mapAliasToColumn(data) {
async mapAliasToColumn(
data,
clientMeta = {
isMySQL: false,
isSqlite: false,
isMssql: false,
isPg: false,
},
knex,
) {
const insertObj = {};
const base = await Base.get(this.base_id);
for (const col of await this.getColumns()) {
if (isVirtualCol(col)) continue;
let val =
@ -470,6 +482,57 @@ 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, isMssql, isPg } = clientMeta;
if (
val.indexOf('-') < 0 &&
val.indexOf('+') < 0 &&
val.slice(-1) !== 'Z'
) {
// if no timezone is given,
// then append +00:00 to make it as UTC
val += '+00:00';
}
if (isMySQL) {
// first convert the value to utc
// from UI
// e.g. 2022-01-01 20:00:00Z -> 2022-01-01 20:00:00
// from API
// e.g. 2022-01-01 20:00:00+08:00 -> 2022-01-01 12:00:00
// if timezone info is not found - considered as utc
// e.g. 2022-01-01 20:00:00 -> 2022-01-01 20:00:00
// if timezone info is found
// e.g. 2022-01-01 20:00:00Z -> 2022-01-01 20:00:00
// e.g. 2022-01-01 20:00:00+00:00 -> 2022-01-01 20:00:00
// e.g. 2022-01-01 20:00:00+08:00 -> 2022-01-01 12:00:00
// then we use CONVERT_TZ to convert that in the db timezone
val = knex.raw(`CONVERT_TZ(?, '+00:00', @@GLOBAL.time_zone)`, [
dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss'),
]);
} else if (isSqlite) {
// convert to UTC
// e.g. 2022-01-01T10:00:00.000Z -> 2022-01-01 04:30:00+00:00
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
} else if (isPg) {
// convert to UTC
// e.g. 2023-01-01T12:00:00.000Z -> 2023-01-01 12:00:00+00:00
// then convert to db timezone
val = knex.raw(`? AT TIME ZONE CURRENT_SETTING('timezone')`, [
dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ'),
]);
} else if (isMssql) {
// convert ot UTC
// e.g. 2023-05-10T08:49:32.000Z -> 2023-05-10 08:49:32-08:00
// then convert to db timezone
val = knex.raw(
`SWITCHOFFSET(CONVERT(datetimeoffset, ?), DATENAME(TzOffset, SYSDATETIMEOFFSET()))`,
[dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ')],
);
} else {
// e.g. 2023-01-01T12:00:00.000Z -> 2023-01-01 12:00:00+00:00
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
}
}
insertObj[sanitize(col.column_name)] = val;
}
}

51
packages/nocodb/tests/unit/model/tests/baseModelSql.test.ts

@ -1,20 +1,20 @@
import 'mocha';
import { BaseModelSqlv2 } from '../../../../src/db/BaseModelSqlv2'
import NcConnectionMgrv2 from '../../../../src/utils/common/NcConnectionMgrv2'
import { expect } from 'chai';
import { BaseModelSqlv2 } from '../../../../src/db/BaseModelSqlv2';
import NcConnectionMgrv2 from '../../../../src/utils/common/NcConnectionMgrv2';
import init from '../../init';
import { createProject } from '../../factory/project';
import { createTable } from '../../factory/table';
import Base from '../../../../src/models/Base';
import Model from '../../../../src/models/Model';
import Project from '../../../../src/models/Project';
import View from '../../../../src/models/View';
import { createRow, generateDefaultRowAttributes } from '../../factory/row';
import Audit from '../../../../src/models/Audit';
import { expect } from 'chai';
import Filter from '../../../../src/models/Filter';
import { createLtarColumn } from '../../factory/column';
import LinkToAnotherRecordColumn from '../../../../src/models/LinkToAnotherRecordColumn';
import { isPg, isSqlite } from '../../init/db';
import type View from '../../../../src/models/View';
import type Project from '../../../../src/models/Project';
import type Model from '../../../../src/models/Model';
import type LinkToAnotherRecordColumn from '../../../../src/models/LinkToAnotherRecordColumn';
function baseModelSqlTests() {
let context;
@ -44,11 +44,11 @@ function baseModelSqlTests() {
};
const columns = await table.getColumns();
let inputData: any = generateDefaultRowAttributes({ columns });
const inputData: any = generateDefaultRowAttributes({ columns });
const response = await baseModelSql.insert(
generateDefaultRowAttributes({ columns }),
undefined,
request
request,
);
const insertedRow = (await baseModelSql.list())[0];
@ -61,6 +61,10 @@ function baseModelSqlTests() {
response.CreatedAt = new Date(response.CreatedAt).toISOString();
response.UpdatedAt = new Date(response.UpdatedAt).toISOString();
} else if (isSqlite(context)) {
// append +00:00 to the date string
inputData.CreatedAt = `${inputData.CreatedAt}+00:00`;
inputData.UpdatedAt = `${inputData.UpdatedAt}+00:00`;
}
expect(insertedRow).to.include(inputData);
@ -106,6 +110,10 @@ function baseModelSqlTests() {
if (isPg(context)) {
inputData.CreatedAt = new Date(inputData.CreatedAt).toISOString();
inputData.UpdatedAt = new Date(inputData.UpdatedAt).toISOString();
} else if (isSqlite(context)) {
// append +00:00 to the date string
inputData.CreatedAt = `${inputData.CreatedAt}+00:00`;
inputData.UpdatedAt = `${inputData.UpdatedAt}+00:00`;
}
expect(insertedRows[index]).to.include(inputData);
});
@ -145,7 +153,7 @@ function baseModelSqlTests() {
expect(updatedRow).to.include({ Id: rowId, Title: 'test' });
const rowUpdatedAudit = (await Audit.projectAuditList(project.id, {})).find(
(audit) => audit.op_sub_type === 'UPDATE'
(audit) => audit.op_sub_type === 'UPDATE',
);
expect(rowUpdatedAudit).to.include({
user: 'test@example.com',
@ -156,7 +164,8 @@ function baseModelSqlTests() {
row_id: '1',
op_type: 'DATA',
op_sub_type: 'UPDATE',
description: 'Record with ID 1 has been updated in Table Table1_Title.\nColumn "Title" got changed from "test-0" to "test"',
description:
'Record with ID 1 has been updated in Table Table1_Title.\nColumn "Title" got changed from "test-0" to "test"',
});
});
@ -178,7 +187,7 @@ function baseModelSqlTests() {
await baseModelSql.bulkUpdate(
insertedRows.map((row) => ({ ...row, Title: `new-${row['Title']}` })),
{ cookie: request }
{ cookie: request },
);
const updatedRows = await baseModelSql.list();
@ -229,7 +238,7 @@ function baseModelSqlTests() {
],
},
{ Title: 'new-1' },
{ cookie: request }
{ cookie: request },
);
const updatedRows = await baseModelSql.list();
@ -277,7 +286,7 @@ function baseModelSqlTests() {
console.log('Delete record', await Audit.projectAuditList(project.id, {}));
const rowDeletedAudit = (await Audit.projectAuditList(project.id, {})).find(
(audit) => audit.op_sub_type === 'DELETE'
(audit) => audit.op_sub_type === 'DELETE',
);
expect(rowDeletedAudit).to.include({
user: 'test@example.com',
@ -309,7 +318,7 @@ function baseModelSqlTests() {
insertedRows
.filter((row) => row['Id'] < 5)
.map((row) => ({ id: row['Id'] })),
{ cookie: request }
{ cookie: request },
);
const remainingRows = await baseModelSql.list();
@ -359,7 +368,7 @@ function baseModelSqlTests() {
}),
],
},
{ cookie: request }
{ cookie: request },
);
const remainingRows = await baseModelSql.list();
@ -414,7 +423,7 @@ function baseModelSqlTests() {
[ltarColumn.title]: [{ Id: childRow['Id'] }],
},
undefined,
request
request,
);
const childBaseModel = new BaseModelSqlv2({
@ -470,7 +479,7 @@ function baseModelSqlTests() {
await baseModelSql.insert(
generateDefaultRowAttributes({ columns }),
undefined,
request
request,
);
const insertedRow = await baseModelSql.readByPk(1);
@ -487,7 +496,7 @@ function baseModelSqlTests() {
view,
});
const updatedChildRow = await childBaseModel.readByPk(
insertedChildRow['Id']
insertedChildRow['Id'],
);
expect(updatedChildRow[childCol.column_name]).to.equal(insertedRow['Id']);
@ -538,7 +547,7 @@ function baseModelSqlTests() {
await baseModelSql.insert(
generateDefaultRowAttributes({ columns }),
undefined,
request
request,
);
const insertedRow = await baseModelSql.readByPk(1);
@ -562,7 +571,7 @@ function baseModelSqlTests() {
view,
});
const updatedChildRow = await childBaseModel.readByPk(
insertedChildRow['Id']
insertedChildRow['Id'],
);
expect(updatedChildRow[childCol.column_name]).to.be.null;

4
packages/nocodb/tests/unit/rest/tests/tableRow.test.ts

@ -878,7 +878,9 @@ function tableTest() {
}
});
it('Sorted Formula column on rollup customer table', async function () {
// rollup usage in formula is currently not supported
// work in progress
it.skip('Sorted Formula column on rollup customer table', async function () {
const rollupColumnTitle = 'Number of rentals';
const rollupColumn = await createRollupColumn(context, {
project: sakilaProject,

4576
tests/playwright/package-lock.json generated

File diff suppressed because it is too large Load Diff

3
tests/playwright/package.json

@ -11,6 +11,7 @@
"test:repeat": "TRACE=true npx playwright test --workers=4 --repeat-each=12",
"test:quick": "TRACE=true PW_QUICK_TEST=1 npx playwright test --workers=4",
"test:debug": "./startPlayWrightServer.sh; PW_TEST_REUSE_CONTEXT=1 PW_TEST_CONNECT_WS_ENDPOINT=ws://127.0.0.1:31000/ PWDEBUG=console npx playwright test -c playwright.config.ts --headed --project=chromium --retries 0 --timeout 0 --workers 1 --max-failures=1",
"test:debug:intelliJ": "TRACE=true PWDEBUG=console npx playwright test --trace on -c playwright.config.ts --headed --project=chromium --retries 0 --workers 1 --max-failures=1",
"test:debug:watch": "npx nodemon -e ts -w ./ -x \"npm run test:debug\"",
"test:debug:quick:sqlite": "./startPlayWrightServer.sh; PW_QUICK_TEST=1 PW_TEST_REUSE_CONTEXT=1 PW_TEST_CONNECT_WS_ENDPOINT=ws://127.0.0.1:31000/ PWDEBUG=console npx playwright test -c playwright.config.ts --headed --project=chromium --retries 0 --timeout 5 --workers 1 --max-failures=1",
"ci:test": "npx playwright test --workers=2",
@ -46,7 +47,9 @@
"body-parser": "^1.20.1",
"dayjs": "^1.11.7",
"express": "^4.18.2",
"knex": "^2.4.2",
"nocodb-sdk": "file:../../packages/nocodb-sdk",
"sqlite3": "^5.1.6",
"xlsx": "^0.18.5"
}
}

9
tests/playwright/pages/Dashboard/ExpandedForm/index.ts

@ -1,6 +1,7 @@
import { expect, Locator } from '@playwright/test';
import BasePage from '../../Base';
import { DashboardPage } from '..';
import { DateTimeCellPageObject } from '../common/Cell/DateTimeCell';
export class ExpandedFormPage extends BasePage {
readonly dashboard: DashboardPage;
@ -93,6 +94,14 @@ export class ExpandedFormPage extends BasePage {
await field.locator(`[data-testid="nc-child-list-button-link-to"]`).click();
await this.dashboard.linkRecord.select(value);
break;
case 'dateTime':
await field.locator('.nc-cell').click();
// eslint-disable-next-line no-case-declarations
const dateTimeObj = new DateTimeCellPageObject(this.dashboard.grid.cell);
await dateTimeObj.selectDate({ date: value.slice(0, 10) });
await dateTimeObj.selectTime({ hour: +value.slice(11, 13), minute: +value.slice(14, 16) });
await dateTimeObj.save();
break;
}
}

24
tests/playwright/pages/Dashboard/TreeView.ts

@ -48,6 +48,24 @@ export class TreeViewPage extends BasePage {
await this.get().locator(`.nc-project-tree-tbl-${title}`).focus();
}
async openBase({ title }: { title: string }) {
const nodes = await this.get().locator(`.ant-collapse`);
// loop through nodes.count() to find the node with title
for (let i = 0; i < (await nodes.count()); i++) {
const node = nodes.nth(i);
const nodeTitle = await node.innerText();
// check if nodeTitle contains title
if (nodeTitle.includes(title)) {
// click on node
await node.waitFor({ state: 'visible' });
await node.click();
break;
}
}
await this.rootPage.waitForTimeout(2000);
}
// assumption: first view rendered is always GRID
//
async openTable({
@ -72,6 +90,8 @@ export class TreeViewPage extends BasePage {
}
}
await this.get().locator(`.nc-project-tree-tbl-${title}`).waitFor({ state: 'visible' });
if (networkResponse === true) {
await this.waitForResponse({
uiAction: () => this.get().locator(`.nc-project-tree-tbl-${title}`).click(),
@ -86,7 +106,7 @@ export class TreeViewPage extends BasePage {
}
}
async createTable({ title, skipOpeningModal }: { title: string; skipOpeningModal?: boolean }) {
async createTable({ title, skipOpeningModal, mode }: { title: string; skipOpeningModal?: boolean; mode?: string }) {
if (!skipOpeningModal) await this.get().locator('.nc-add-new-table').click();
await this.dashboard.get().locator('.nc-modal-table-create').locator('.ant-modal-body').waitFor();
@ -101,7 +121,7 @@ export class TreeViewPage extends BasePage {
});
// Tab render is slow for playwright
await this.dashboard.waitForTabRender({ title });
await this.dashboard.waitForTabRender({ title, mode });
}
async verifyTable({ title, index, exists = true }: { title: string; index?: number; exists?: boolean }) {

29
tests/playwright/pages/Dashboard/common/Cell/DateTimeCell.ts

@ -23,7 +23,7 @@ export class DateTimeCellPageObject extends BasePage {
}
async save() {
await this.rootPage.locator('button:has-text("Ok")').click();
await this.rootPage.locator('button:has-text("Ok"):visible').click();
}
async selectDate({
@ -36,15 +36,15 @@ export class DateTimeCellPageObject extends BasePage {
const [year, month, day] = date.split('-');
// configure year
await this.rootPage.locator('.ant-picker-year-btn').click();
await this.rootPage.locator('.ant-picker-year-btn:visible').click();
await this.rootPage.locator(`td[title="${year}"]`).click();
// configure month
await this.rootPage.locator('.ant-picker-month-btn').click();
await this.rootPage.locator('.ant-picker-month-btn:visible').click();
await this.rootPage.locator(`td[title="${year}-${month}"]`).click();
// configure day
await this.rootPage.locator(`td[title="${year}-${month}-${day}"]`).click();
await this.rootPage.locator(`td[title="${year}-${month}-${day}"]:visible`).click();
}
async selectTime({
@ -60,14 +60,20 @@ export class DateTimeCellPageObject extends BasePage {
second?: number | null;
}) {
await this.rootPage
.locator(`.ant-picker-time-panel-column:nth-child(1) > .ant-picker-time-panel-cell:nth-child(${hour + 1})`)
.locator(
`.ant-picker-time-panel-column:nth-child(1) > .ant-picker-time-panel-cell:nth-child(${hour + 1}):visible`
)
.click();
await this.rootPage
.locator(`.ant-picker-time-panel-column:nth-child(2) > .ant-picker-time-panel-cell:nth-child(${minute + 1})`)
.locator(
`.ant-picker-time-panel-column:nth-child(2) > .ant-picker-time-panel-cell:nth-child(${minute + 1}):visible`
)
.click();
if (second != null) {
await this.rootPage
.locator(`.ant-picker-time-panel-column:nth-child(3) > .ant-picker-time-panel-cell:nth-child(${second + 1})`)
.locator(
`.ant-picker-time-panel-column:nth-child(3) > .ant-picker-time-panel-cell:nth-child(${second + 1}):visible`
)
.click();
}
}
@ -75,4 +81,13 @@ export class DateTimeCellPageObject extends BasePage {
async close() {
await this.rootPage.keyboard.press('Escape');
}
async setDateTime({ index, columnHeader, dateTime }: { index: number; columnHeader: string; dateTime: string }) {
const [date, time] = dateTime.split(' ');
const [hour, minute, second] = time.split(':');
await this.open({ index, columnHeader });
await this.selectDate({ date });
await this.selectTime({ hour: +hour, minute: +minute });
await this.save();
}
}

13
tests/playwright/pages/Dashboard/common/Cell/index.ts

@ -114,6 +114,11 @@ export class CellPageObject extends BasePage {
// if text is found, return
// if text is not found, throw error
let count = 0;
if (!(this.parent instanceof SharedFormPage)) {
await this.rootPage.locator(`td[data-testid="cell-${columnHeader}-${index}"]`).waitFor({ state: 'visible' });
}
await this.get({
index,
columnHeader,
@ -360,4 +365,12 @@ export class CellPageObject extends BasePage {
await this.get({ index, columnHeader }).press((await this.isMacOs()) ? 'Meta+C' : 'Control+C');
await this.verifyToast({ message: 'Copied to clipboard' });
}
async pasteFromClipboard({ index, columnHeader }: CellProps, ...clickOptions: Parameters<Locator['click']>) {
await this.get({ index, columnHeader }).scrollIntoViewIfNeeded();
await this.get({ index, columnHeader }).click(...clickOptions);
await (await this.get({ index, columnHeader }).elementHandle()).waitForElementState('stable');
await this.get({ index, columnHeader }).press((await this.isMacOs()) ? 'Meta+V' : 'Control+V');
}
}

2
tests/playwright/pages/Dashboard/common/Toolbar/Filter.ts

@ -298,4 +298,4 @@ export class ToolbarFilterPage extends BasePage {
return opListText;
}
}
}

1132
tests/playwright/tests/db/timezone.spec.ts

File diff suppressed because it is too large Load Diff

2
tests/playwright/tests/db/viewGridShare.spec.ts

@ -83,7 +83,7 @@ test.describe('Shared view', () => {
}
const expectedRecordsByDb = isSqlite(context) || isPg(context) ? sqliteExpectedRecords : expectedRecords;
await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000));
// verify order of records (original sort & filter)
for (const record of expectedRecordsByDb) {
await sharedPage.grid.cell.verify(record);

48
tests/playwright/tests/utils/config.ts

@ -0,0 +1,48 @@
const knexConfig = {
pg: {
client: 'pg',
connection: {
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'password',
database: 'postgres',
multipleStatements: true,
},
searchPath: ['public', 'information_schema'],
pool: { min: 0, max: 5 },
},
mysql: {
client: 'mysql2',
connection: {
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
database: 'sakila',
multipleStatements: true,
},
pool: { min: 0, max: 5 },
},
sqlite: {
client: 'sqlite3',
connection: {
filename: './mydb.sqlite3',
},
useNullAsDefault: true,
pool: { min: 0, max: 5 },
},
};
function getKnexConfig({ dbName, dbType }: { dbName: string; dbType: string }) {
const config = knexConfig[dbType];
if (dbType === 'sqlite') {
config.connection.filename = `./${dbName}.sqlite3`;
return config;
}
config.connection.database = dbName;
return config;
}
export { getKnexConfig };

12
tests/playwright/tests/utils/general.ts

@ -50,4 +50,14 @@ function getDefaultPwd() {
return 'Password123.';
}
export { getTextExcludeIconText, isSubset, getIconText, getDefaultPwd };
function getBrowserTimezoneOffset() {
// get timezone offset
const timezoneOffset = new Date().getTimezoneOffset();
const hours = Math.floor(Math.abs(timezoneOffset) / 60);
const minutes = Math.abs(timezoneOffset % 60);
const sign = timezoneOffset <= 0 ? '+' : '-';
const formattedOffset = `${sign}${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
return formattedOffset;
}
export { getTextExcludeIconText, isSubset, getIconText, getDefaultPwd, getBrowserTimezoneOffset };

Loading…
Cancel
Save