Browse Source

Revert "Revert "refactor: timezone""

pull/5684/head
աɨռɢӄաօռɢ 1 year ago committed by GitHub
parent
commit
56a6466db3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10374
      package-lock.json
  2. 50
      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. 4
      packages/nc-gui/components/virtual-cell/components/ListItems.vue
  9. 3
      packages/nc-gui/composables/useMultiSelect/convertCellData.ts
  10. 48
      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. 26
      packages/nc-gui/utils/cell.ts
  15. 8
      packages/nc-gui/utils/dateTimeUtils.ts
  16. 1
      packages/nc-gui/utils/index.ts
  17. 2
      packages/nocodb/package.json
  18. 214
      packages/nocodb/src/db/BaseModelSqlv2.ts
  19. 14
      packages/nocodb/src/db/CustomKnex.ts
  20. 30
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  21. 34
      packages/nocodb/src/meta/meta.service.ts
  22. 63
      packages/nocodb/src/models/Model.ts
  23. 51
      packages/nocodb/tests/unit/model/tests/baseModelSql.test.ts
  24. 205
      packages/nocodb/tests/unit/rest/tests/tableRow.test.ts
  25. 4498
      tests/playwright/package-lock.json
  26. 3
      tests/playwright/package.json
  27. 9
      tests/playwright/pages/Dashboard/ExpandedForm/index.ts
  28. 24
      tests/playwright/pages/Dashboard/TreeView.ts
  29. 29
      tests/playwright/pages/Dashboard/common/Cell/DateTimeCell.ts
  30. 8
      tests/playwright/pages/Dashboard/common/Cell/index.ts
  31. 950
      tests/playwright/tests/db/timezone.spec.ts
  32. 2
      tests/playwright/tests/db/viewGridShare.spec.ts
  33. 48
      tests/playwright/tests/utils/config.ts

10374
package-lock.json generated

File diff suppressed because it is too large Load Diff

50
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, isMysql, 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
}
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'))
const formattedValue = dayjs(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 = formattedValue
emit('update:modelValue', formattedValue)
}
},
})

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>

4
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 }>()
@ -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.format('YYYY-MM-DD HH:mm:ssZ')
}
case UITypes.Time: {
let parsedTime = dayjs(value)

48
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,12 @@ export function useMultiSelect(
activeCell.col = col
}
function constructDateTimeFormat(column: ColumnType) {
const dateFormat = parseProp(column?.meta)?.date_format ?? dateFormats[0]
const timeFormat = parseProp(column?.meta)?.time_format ?? timeFormats[0]
return `${dateFormat} ${timeFormat}`
}
async function copyValue(ctx?: Cell) {
try {
if (selectedRange.start !== null && selectedRange.end !== null && !selectedRange.isSingleCell()) {
@ -106,6 +115,41 @@ 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) {
// 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(constructDateTimeFormat(columnObj))
if (!dayjs(textToCopy).isValid()) {
throw new Error('Invalid Date')
}
}
await copy(textToCopy)
message.success(t('msg.info.copiedToClipboard'))
}
@ -305,6 +349,7 @@ export function useMultiSelect(
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
isXcdbBase(meta.value?.base_id),
)
e.preventDefault()
@ -339,6 +384,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,

26
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,28 @@ 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
}
// cater MYSQL
result = result.replace('.000000', '')
// 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/, (d: string) => {
return dayjs(d).format('YYYY-MM-DD HH:mm:ssZ')
})
// 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(/\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(!result.includes('+')).local().format('YYYY-MM-DD HH:mm')
})
}

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'

2
packages/nocodb/package.json

@ -32,6 +32,8 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"watch:run": "cross-env NC_DISABLE_TELE=true EE=true nodemon -e ts,js -w ./src -x \"ts-node src/run/docker --log-error --project tsconfig.json\"",
"watch:run:mysql": "cross-env NC_DISABLE_TELE=true EE=true nodemon -e ts,js -w ./src -x \"ts-node src/run/dockerRunMysql --log-error --project tsconfig.json\"",
"watch:run:pg": "cross-env NC_DISABLE_TELE=true EE=true nodemon -e ts,js -w ./src -x \"ts-node src/run/dockerRunPG --log-error --project tsconfig.json\"",
"watch:run:playwright": "rm -f ./test_noco.db; cross-env DATABASE_URL=sqlite:./test_noco.db PLAYWRIGHT_TEST=true NC_DISABLE_TELE=true EE=true nodemon -e ts,js -w ./src -x \"ts-node src/run/testDocker --log-error --project tsconfig.json\"",
"watch:run:playwright:quick": "rm -f ./test_noco.db; cp ../../tests/playwright/fixtures/noco_0_91_7.db ./test_noco.db; cross-env DATABASE_URL=sqlite:./test_noco.db NC_DISABLE_TELE=true EE=true nodemon -e ts,js -w ./src -x \"ts-node src/run/docker --log-error --project tsconfig.json\"",
"watch:run:playwright:pg:cyquick": "rm -f ./test_noco.db; cp ../../tests/playwright/fixtures/noco_0_91_7.db ./test_noco.db; cross-env NC_DISABLE_TELE=true EE=true nodemon -e ts,js -w ./src -x \"ts-node src/run/dockerRunPG_CyQuick.ts --log-error --project tsconfig.json\"",

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

@ -1,6 +1,8 @@
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 { nocoExecute } from 'nc-help';
import {
AuditOperationSubTypes,
@ -16,8 +18,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 +56,9 @@ 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);
const GROUP_COL = '__nc_group_id';
@ -1600,6 +1612,61 @@ 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 it 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 !== 'datetime2') {
const col = `${sanitize(alias || this.model.table_name)}.${
column.column_name
}`;
res[sanitize(column.title || column.column_name)] =
this.dbDriver.raw(
`SWITCHOFFSET(??, DATEPART(TZOFFSET, ??)) AT TIME ZONE 'UTC'`,
[col, col],
);
}
}
res[sanitize(column.title || column.column_name)] = sanitize(
`${alias || this.model.table_name}.${column.column_name}`,
);
break;
case 'LinkToAnotherRecord':
case 'Lookup':
break;
@ -1727,7 +1794,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);
@ -1865,7 +1936,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);
@ -1913,6 +1988,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';
}
@ -1941,7 +2026,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 = [];
@ -2098,7 +2187,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,
);
}),
);
@ -2161,7 +2254,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 = [];
@ -2213,7 +2310,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) {
@ -2259,7 +2360,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 = [];
@ -3145,16 +3248,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(
@ -3192,6 +3302,82 @@ class BaseModelSqlv2 {
}
return data;
}
private _convertDateFormat(
dateTimeColumns: Record<string, any>[],
d: Record<string, any>,
) {
if (!d) return d;
for (const col of dateTimeColumns) {
if (!d[col.title]) 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);
if (dateTimeColumns.length) {
if (Array.isArray(data)) {
data = data.map((d) => this._convertDateFormat(dateTimeColumns, d));
} else {
this._convertDateFormat(dateTimeColumns, data);
}
}
}
return data;
}
}
function extractSortsObject(

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: '=',

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

@ -537,6 +537,36 @@ async function _formulaQueryBuilder(
};
};
break;
case UITypes.DateTime:
if (knex.clientType().startsWith('mysql')) {
aliasToColumn[col.id] = async (): Promise<any> => {
return {
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 {
builder: knex
.raw(
`?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`,
[col.column_name],
)
.wrap('(', ')'),
};
};
} else {
aliasToColumn[col.id] = () =>
Promise.resolve({ builder: col.column_name });
}
break;
default:
aliasToColumn[col.id] = () =>
Promise.resolve({ builder: col.column_name });

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,

63
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';
@ -443,8 +445,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 =
@ -455,6 +467,55 @@ 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) {
// 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) {
// 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(? AT TIME ZONE 'UTC', 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;

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

@ -1,8 +1,9 @@
import 'mocha';
import request from 'supertest';
import { UITypes } from 'nocodb-sdk';
import { expect } from 'chai';
import init from '../../init';
import { createProject, createSakilaProject } from '../../factory/project';
import request from 'supertest';
import { ColumnType, UITypes } from 'nocodb-sdk';
import {
createColumn,
createLookupColumn,
@ -11,18 +12,18 @@ import {
} from '../../factory/column';
import { createTable, getTable } from '../../factory/table';
import {
createBulkRows,
createChildRow,
createRow,
generateDefaultRowAttributes,
getOneRow,
getRow,
listRow,
createBulkRows,
} from '../../factory/row';
import { isMysql, isPg, isSqlite } from '../../init/db';
import Model from '../../../../src/models/Model';
import Project from '../../../../src/models/Project';
import { expect } from 'chai';
import type { ColumnType } from 'nocodb-sdk';
import type Model from '../../../../src/models/Model';
import type Project from '../../../../src/models/Project';
const isColumnsCorrectInResponse = (row, columns: ColumnType[]) => {
const responseColumnsListStr = Object.keys(row).sort().join(',');
@ -160,7 +161,7 @@ function tableTest() {
it('Get desc sorted table data list with required columns', async function () {
const firstNameColumn = customerColumns.find(
(col) => col.title === 'FirstName'
(col) => col.title === 'FirstName',
);
const visibleColumns = [firstNameColumn];
const sortInfo = [{ fk_column_id: firstNameColumn.id, direction: 'desc' }];
@ -214,7 +215,7 @@ function tableTest() {
it('Get asc sorted table data list with required columns', async function () {
const firstNameColumn = customerColumns.find(
(col) => col.title === 'FirstName'
(col) => col.title === 'FirstName',
);
const visibleColumns = [firstNameColumn];
const sortInfo = [{ fk_column_id: firstNameColumn.id, direction: 'asc' }];
@ -427,7 +428,7 @@ function tableTest() {
});
const paymentListColumn = (await rentalTable.getColumns()).find(
(c) => c.title === 'Payment List'
(c) => c.title === 'Payment List',
);
const nestedFilter = {
@ -513,11 +514,11 @@ function tableTest() {
});
const paymentListColumn = (await rentalTable.getColumns()).find(
(c) => c.title === 'Payment List'
(c) => c.title === 'Payment List',
);
const returnDateColumn = (await rentalTable.getColumns()).find(
(c) => c.title === 'ReturnDate'
(c) => c.title === 'ReturnDate',
);
const nestedFilter = {
@ -630,15 +631,15 @@ function tableTest() {
});
const paymentListColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Payment List'
(c) => c.title === 'Payment List',
);
const activeColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Active'
(c) => c.title === 'Active',
);
const addressColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Address'
(c) => c.title === 'Address',
);
const nestedFilter = [
@ -786,11 +787,11 @@ function tableTest() {
});
const paymentListColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Payment List'
(c) => c.title === 'Payment List',
);
const activeColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Active'
(c) => c.title === 'Active',
);
const nestedFields = {
@ -866,7 +867,7 @@ function tableTest() {
}
const nestedRentalResponse = Object.keys(
ascResponse.body.list[0]['Rental List']
ascResponse.body.list[0]['Rental List'],
);
if (
nestedRentalResponse.includes('ReturnDate') &&
@ -877,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,
@ -914,7 +917,7 @@ function tableTest() {
if (
(response.body.list as Array<any>).every(
(row) =>
parseInt(row['Formula']) !== parseInt(row[rollupColumnTitle]) + 10
parseInt(row['Formula']) !== parseInt(row[rollupColumnTitle]) + 10,
)
) {
throw new Error('Wrong formula');
@ -998,13 +1001,13 @@ function tableTest() {
it('Find one sorted table data list with required columns', async function () {
const firstNameColumn = customerColumns.find(
(col) => col.title === 'FirstName'
(col) => col.title === 'FirstName',
);
const visibleColumns = [firstNameColumn];
let response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`,
)
.set('xc-auth', context.token)
.query({
@ -1025,7 +1028,7 @@ function tableTest() {
response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`,
)
.set('xc-auth', context.token)
.query({
@ -1047,7 +1050,7 @@ function tableTest() {
it('Find one desc sorted and with rollup table data list with required columns', async function () {
const firstNameColumn = customerColumns.find(
(col) => col.title === 'FirstName'
(col) => col.title === 'FirstName',
);
const rollupColumn = await createRollupColumn(context, {
@ -1064,7 +1067,7 @@ function tableTest() {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`,
)
.set('xc-auth', context.token)
.query({
@ -1095,11 +1098,11 @@ function tableTest() {
});
const paymentListColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Payment List'
(c) => c.title === 'Payment List',
);
const activeColumn = (await customerTable.getColumns()).find(
(c) => c.title === 'Active'
(c) => c.title === 'Active',
);
const nestedFields = {
@ -1152,7 +1155,7 @@ function tableTest() {
const ascResponse = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/find-one`,
)
.set('xc-auth', context.token)
.query({
@ -1179,7 +1182,7 @@ function tableTest() {
it('Groupby desc sorted and with rollup table data list with required columns', async function () {
const firstNameColumn = customerColumns.find(
(col) => col.title === 'FirstName'
(col) => col.title === 'FirstName',
);
const rollupColumn = await createRollupColumn(context, {
@ -1196,7 +1199,7 @@ function tableTest() {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/groupby`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/groupby`,
)
.set('xc-auth', context.token)
.query({
@ -1215,7 +1218,7 @@ function tableTest() {
it('Groupby desc sorted and with rollup table data list with required columns', async function () {
const firstNameColumn = customerColumns.find(
(col) => col.title === 'FirstName'
(col) => col.title === 'FirstName',
);
const rollupColumn = await createRollupColumn(context, {
@ -1232,7 +1235,7 @@ function tableTest() {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/groupby`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/groupby`,
)
.set('xc-auth', context.token)
.query({
@ -1260,7 +1263,7 @@ function tableTest() {
const readResponse = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${row['CustomerId']}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${row['CustomerId']}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1396,7 +1399,7 @@ function tableTest() {
if (
!(response.body.message[0] as string).includes(
'is a LinkToAnotherRecord of'
'is a LinkToAnotherRecord of',
)
) {
throw new Error('Should give ltar foreign key error');
@ -1411,7 +1414,7 @@ function tableTest() {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${row['CustomerId']}/exist`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${row['CustomerId']}/exist`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1424,7 +1427,7 @@ function tableTest() {
it('Exist should be false table row when it does not exists', async function () {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/998546/exist`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/998546/exist`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1531,7 +1534,7 @@ function tableTest() {
.patch(`/api/v1/db/data/bulk/noco/${project.id}/${table.id}`)
.set('xc-auth', context.token)
.send(
rows.map((row) => ({ title: `new-${row['Title']}`, id: row['Id'] }))
rows.map((row) => ({ title: `new-${row['Title']}`, id: row['Id'] })),
)
.expect(200);
const updatedRows: Array<any> = await listRow({ project, table });
@ -1612,7 +1615,7 @@ function tableTest() {
it('Export csv', async () => {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.title}/export/csv`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.title}/export/csv`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1631,14 +1634,14 @@ function tableTest() {
it('Export excel', async () => {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.title}/export/excel`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.title}/export/excel`,
)
.set('xc-auth', context.token)
.expect(200);
if (
!response['header']['content-disposition'].includes(
'Customer-export.xlsx'
'Customer-export.xlsx',
)
) {
throw new Error('Wrong file name');
@ -1653,11 +1656,11 @@ function tableTest() {
it('Nested row list hm', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1672,11 +1675,11 @@ function tableTest() {
it('Nested row list hm with limit and offset', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`,
)
.set('xc-auth', context.token)
.query({
@ -1698,11 +1701,11 @@ function tableTest() {
it('Row list hm with invalid table id', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/wrong-id/${rowId}/hm/${rentalListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/wrong-id/${rowId}/hm/${rentalListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1750,11 +1753,11 @@ function tableTest() {
});
const filmTable = await getTable({ project: sakilaProject, name: 'film' });
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1774,11 +1777,11 @@ function tableTest() {
});
const filmTable = await getTable({ project: sakilaProject, name: 'film' });
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.query({
@ -1805,11 +1808,11 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1823,12 +1826,12 @@ function tableTest() {
it('Create hm relation with invalid table id', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const refId = 1;
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/hm/${rentalListColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/hm/${rentalListColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1841,12 +1844,12 @@ function tableTest() {
it('Create hm relation with non ltar column', async () => {
const rowId = 1;
const firstNameColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'FirstName'
(column) => column.title === 'FirstName',
)!;
const refId = 1;
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${firstNameColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${firstNameColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1863,7 +1866,7 @@ function tableTest() {
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/invalid-column/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/invalid-column/${refId}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1898,20 +1901,20 @@ function tableTest() {
it('Create list hm', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const refId = 1;
const lisResponseBeforeUpdate = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1919,7 +1922,7 @@ function tableTest() {
const lisResponseAfterUpdate = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -1942,7 +1945,7 @@ function tableTest() {
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/invalid-column/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/invalid-column/${refId}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1962,12 +1965,12 @@ function tableTest() {
name: 'actor',
});
const firstNameColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'FirstName'
(column) => column.title === 'FirstName',
)!;
const refId = 1;
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${firstNameColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${firstNameColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(404);
@ -1985,13 +1988,13 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const refId = 1;
await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(400);
@ -2007,20 +2010,20 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const refId = 2;
const lisResponseBeforeUpdate = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2028,7 +2031,7 @@ function tableTest() {
const lisResponseAfterUpdate = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2044,12 +2047,12 @@ function tableTest() {
it('List hm with non ltar column', async () => {
const rowId = 1;
const firstNameColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'FirstName'
(column) => column.title === 'FirstName',
)!;
await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${firstNameColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${firstNameColumn.id}`,
)
.set('xc-auth', context.token)
.expect(400);
@ -2058,12 +2061,12 @@ function tableTest() {
it('List mm with non ltar column', async () => {
const rowId = 1;
const firstNameColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'FirstName'
(column) => column.title === 'FirstName',
)!;
await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/mm/${firstNameColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/mm/${firstNameColumn.id}`,
)
.set('xc-auth', context.token)
.expect(400);
@ -2076,20 +2079,20 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const refId = 1;
const lisResponseBeforeDelete = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
await request(context.app)
.delete(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2097,7 +2100,7 @@ function tableTest() {
const lisResponseAfterDelete = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2116,13 +2119,13 @@ function tableTest() {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const refId = 76;
const response = await request(context.app)
.delete(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`,
)
.set('xc-auth', context.token)
.expect(400);
@ -2130,7 +2133,7 @@ function tableTest() {
// todo: only keep generic error message once updated in noco catchError middleware
if (
!response.body.message?.includes(
"The column 'customer_id' cannot be null"
"The column 'customer_id' cannot be null",
) &&
!response.body.message?.includes("Column 'customer_id' cannot be null") &&
!response.body.message?.includes('Cannot add or update a child row') &&
@ -2139,7 +2142,7 @@ function tableTest() {
) {
console.log(
'Delete list hm with existing ref row id with non nullable clause',
response.body
response.body,
);
throw new Error('Wrong error message');
}
@ -2169,7 +2172,7 @@ function tableTest() {
const response = await request(context.app)
.delete(
`/api/v1/db/data/noco/${project.id}/${table.id}/${row['Id']}/hm/${ltarColumn.id}/${childRow['Id']}`
`/api/v1/db/data/noco/${project.id}/${table.id}/${row['Id']}/hm/${ltarColumn.id}/${childRow['Id']}`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2180,7 +2183,9 @@ function tableTest() {
throw new Error('Was not deleted');
}
if (response.body['msg'] !== 'The relation data has been deleted successfully') {
if (
response.body['msg'] !== 'The relation data has been deleted successfully'
) {
throw new Error('Response incorrect');
}
});
@ -2188,12 +2193,12 @@ function tableTest() {
it('Exclude list hm', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2207,12 +2212,12 @@ function tableTest() {
it('Exclude list hm with limit and offset', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.query({
@ -2239,12 +2244,12 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2262,12 +2267,12 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.query({
@ -2294,12 +2299,12 @@ function tableTest() {
name: 'address',
});
const cityColumn = (await addressTable.getColumns()).find(
(column) => column.title === 'City'
(column) => column.title === 'City',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${addressTable.id}/${rowId}/bt/${cityColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/${addressTable.id}/${rowId}/bt/${cityColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.expect(200);
@ -2315,12 +2320,12 @@ function tableTest() {
name: 'address',
});
const cityColumn = (await addressTable.getColumns()).find(
(column) => column.title === 'City'
(column) => column.title === 'City',
)!;
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/${addressTable.id}/${rowId}/bt/${cityColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/${addressTable.id}/${rowId}/bt/${cityColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.query({
@ -2336,12 +2341,12 @@ function tableTest() {
it('Create nested hm relation with invalid table id', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
(column) => column.title === 'Rental List',
)!;
const refId = 1;
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/hm/${rentalListColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/hm/${rentalListColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.expect(404);
@ -2359,11 +2364,11 @@ function tableTest() {
name: 'actor',
});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
(column) => column.title === 'Film List',
)!;
const response = await request(context.app)
.post(
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/mm/${filmListColumn.id}/exclude`
`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/mm/${filmListColumn.id}/exclude`,
)
.set('xc-auth', context.token)
.expect(404);
@ -2383,7 +2388,7 @@ function tableTest() {
const response = await request(context.app)
.get(
`/api/v1/db/data/noco/${sakilaProject.id}/Film/group/${ratingColumn.id}`
`/api/v1/db/data/noco/${sakilaProject.id}/Film/group/${ratingColumn.id}`,
)
.set('xc-auth', context.token)
.expect(200);

4498
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();
}
}

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

@ -360,4 +360,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');
}
}

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

@ -0,0 +1,950 @@
import { expect, test } from '@playwright/test';
import { DashboardPage } from '../../pages/Dashboard';
import setup from '../../setup';
import { knex } from 'knex';
import { Api, UITypes } from 'nocodb-sdk';
import { ProjectsPage } from '../../pages/ProjectsPage';
import { isMysql, isPg, isSqlite } from '../../setup/db';
import { getKnexConfig } from '../utils/config';
let api: Api<any>, records: any[];
const columns = [
{
column_name: 'Id',
title: 'Id',
uidt: UITypes.ID,
ai: 1,
pk: 1,
},
{
column_name: 'DateTime',
title: 'DateTime',
uidt: UITypes.DateTime,
},
];
const rowAttributes = [
{ Id: 1, DateTime: '2021-01-01 00:00:00' },
{ Id: 2, DateTime: '2021-01-01 04:00:00+04:00' },
{ Id: 3, DateTime: '2020-12-31 20:00:00-04:00' },
];
async function timezoneSuite(token?: string, skipTableCreate?: boolean) {
api = new Api({
baseURL: `http://localhost:8080/`,
headers: {
'xc-auth': token,
},
});
const projectList = await api.project.list();
for (const project of projectList.list) {
// delete project with title 'xcdb' if it exists
if (project.title === 'xcdb') {
await api.project.delete(project.id);
}
}
const project = await api.project.create({ title: 'xcdb' });
if (skipTableCreate) return { project };
const table = await api.base.tableCreate(project.id, project.bases?.[0].id, {
table_name: 'dateTimeTable',
title: 'dateTimeTable',
columns: columns,
});
return { project, table };
}
async function connectToExtDb(context: any) {
if (isPg(context)) {
await api.base.create(context.project.id, {
alias: 'datetimetable',
type: 'pg',
config: getKnexConfig({ dbName: 'datetimetable', dbType: 'pg' }),
inflection_column: 'camelize',
inflection_table: 'camelize',
});
} else if (isMysql(context)) {
await api.base.create(context.project.id, {
alias: 'datetimetable',
type: 'mysql2',
config: getKnexConfig({ dbName: 'datetimetable', dbType: 'mysql' }),
inflection_column: 'camelize',
inflection_table: 'camelize',
});
} else if (isSqlite(context)) {
await api.base.create(context.project.id, {
alias: 'datetimetable',
type: 'sqlite3',
config: {
client: 'sqlite3',
connection: {
client: 'sqlite3',
database: 'datetimetable',
connection: {
filename: '../../tests/playwright/mydb.sqlite3',
},
useNullAsDefault: true,
},
},
inflection_column: 'camelize',
inflection_table: 'camelize',
});
}
}
test.describe('Timezone : Japan/Tokyo', () => {
let dashboard: DashboardPage;
let context: any;
test.beforeEach(async ({ page }) => {
context = await setup({ page, isEmptyProject: true });
dashboard = new DashboardPage(page, context.project);
if (!isSqlite(context)) return;
try {
const { project, table } = await timezoneSuite(context.token);
await api.dbTableRow.bulkCreate('noco', project.id, table.id, rowAttributes);
records = await api.dbTableRow.list('noco', project.id, table.id, { limit: 10 });
} catch (e) {
console.error(e);
}
await page.reload();
});
// DST independent test
test.use({
locale: 'ja-JP', // Change to Japanese locale
timezoneId: 'Asia/Tokyo', // Set timezone to Tokyo timezone
});
/*
* This test is to verify the display value of DateTime column in the grid
* when the timezone is set to Asia/Tokyo
*
* The test inserts 3 rows using API
* 1. DateTime inserted without timezone
* 2. DateTime inserted with timezone (UTC+4)
* 3. DateTime inserted with timezone (UTC-4)
*
* Expected display values:
* Display value is converted to Asia/Tokyo
*/
test('API insert, verify display value', async () => {
if (!isSqlite(context)) return;
await dashboard.clickHome();
const projectsPage = new ProjectsPage(dashboard.rootPage);
await projectsPage.openProject({ title: 'xcdb', withoutPrefix: true });
await dashboard.treeView.openTable({ title: 'dateTimeTable' });
// DateTime inserted using API without timezone is converted to UTC
// Display value is converted to Asia/Tokyo
await dashboard.grid.cell.verifyDateCell({ index: 0, columnHeader: 'DateTime', value: '2021-01-01 09:00' });
// DateTime inserted using API with timezone is converted to UTC
// Display value is converted to Asia/Tokyo
await dashboard.grid.cell.verifyDateCell({ index: 1, columnHeader: 'DateTime', value: '2021-01-01 09:00' });
await dashboard.grid.cell.verifyDateCell({ index: 2, columnHeader: 'DateTime', value: '2021-01-01 09:00' });
});
/*
* This test is to verify the API read response of DateTime column
* when the timezone is set to Asia/Tokyo
*
* The test inserts 3 rows using API
* 1. DateTime inserted without timezone
* 2. DateTime inserted with timezone (UTC+4)
* 3. DateTime inserted with timezone (UTC-4)
*
* Expected API response:
* API response is in UTC
*/
test('API Insert, verify API read response', async () => {
if (!isSqlite(context)) return;
// UTC expected response
const dateUTC = ['2021-01-01 00:00:00+00:00', '2021-01-01 00:00:00+00:00', '2021-01-01 00:00:00+00:00'];
const readDate = records.list.map(record => record.DateTime);
// expect API response to be in UTC
expect(readDate).toEqual(dateUTC);
});
});
// Change browser timezone & locale to Asia/Hong-Kong
//
test.describe('Timezone : Asia/Hong-kong', () => {
let dashboard: DashboardPage;
let context: any;
test.beforeEach(async ({ page }) => {
context = await setup({ page, isEmptyProject: true });
dashboard = new DashboardPage(page, context.project);
try {
const { project, table } = await timezoneSuite(context.token);
await api.dbTableRow.bulkCreate('noco', project.id, table.id, rowAttributes);
} catch (e) {
console.error(e);
}
await page.reload();
});
test.use({
locale: 'zh-HK',
timezoneId: 'Asia/Hong_Kong',
});
/*
* This test is to verify the display value of DateTime column in the grid
* when the timezone is set to Asia/Hong-Kong
*
* The test inserts 3 rows using API
* 1. DateTime inserted without timezone
* 2. DateTime inserted with timezone (UTC+4)
* 3. DateTime inserted with timezone (UTC-4)
*
* Expected display values:
* Display value is converted to Asia/Hong-Kong
*/
test('API inserted, verify display value', async () => {
await dashboard.clickHome();
const projectsPage = new ProjectsPage(dashboard.rootPage);
await projectsPage.openProject({ title: 'xcdb', withoutPrefix: true });
await dashboard.treeView.openTable({ title: 'dateTimeTable' });
// DateTime inserted using API without timezone is converted to UTC
// Display value is converted to Asia/Hong_Kong
await dashboard.grid.cell.verifyDateCell({ index: 0, columnHeader: 'DateTime', value: '2021-01-01 08:00' });
// DateTime inserted using API with timezone is converted to UTC
// Display value is converted to Asia/Hong_Kong
await dashboard.grid.cell.verifyDateCell({ index: 1, columnHeader: 'DateTime', value: '2021-01-01 08:00' });
await dashboard.grid.cell.verifyDateCell({ index: 2, columnHeader: 'DateTime', value: '2021-01-01 08:00' });
});
});
test.describe('Timezone', () => {
let dashboard: DashboardPage;
let context: any;
test.use({
locale: 'zh-HK',
timezoneId: 'Asia/Hong_Kong',
});
test.beforeEach(async ({ page }) => {
context = await setup({ page, isEmptyProject: true });
dashboard = new DashboardPage(page, context.project);
if (!isSqlite(context)) return;
const { project } = await timezoneSuite(context.token, true);
context.project = project;
// Using API for test preparation was not working
// Hence switched over to UI based table creation
await dashboard.clickHome();
const projectsPage = new ProjectsPage(dashboard.rootPage);
await projectsPage.openProject({ title: 'xcdb', withoutPrefix: true });
await dashboard.treeView.createTable({ title: 'dateTimeTable', mode: 'Xcdb' });
await dashboard.grid.column.create({
title: 'DateTime',
type: 'DateTime',
dateFormat: 'YYYY-MM-DD',
timeFormat: 'HH:mm',
});
await dashboard.grid.cell.dateTime.setDateTime({
index: 0,
columnHeader: 'DateTime',
dateTime: '2021-01-01 08:00:00',
});
// await dashboard.rootPage.reload();
});
/*
* This test is to verify the display value & API response of DateTime column in the grid
* when the value inserted is from the UI
*
* Note: Timezone for this test is set as Asia/Hong-Kong
*
* 1. Create table with DateTime column
* 2. Insert DateTime value from UI '2021-01-01 08:00:00'
* 3. Verify display value : should be '2021-01-01 08:00:00'
* 4. Verify API response, expect UTC : should be '2021-01-01 00:00:00'
*
*/
test('Cell insert', async () => {
if (!isSqlite(context)) return;
// Verify stored value in database is UTC
records = await api.dbTableRow.list('noco', context.project.id, 'dateTimeTable', { limit: 10 });
const readDate = records.list[0].DateTime;
// skip seconds from readDate
// stored value expected to be in UTC
expect(readDate.slice(0, 16)).toEqual('2021-01-01 00:00');
// DateTime inserted from cell is converted to UTC & stored
// Display value is same as inserted value
await dashboard.grid.cell.verifyDateCell({ index: 0, columnHeader: 'DateTime', value: '2021-01-01 08:00' });
});
/*
* This test is to verify the display value & API response of DateTime column in the grid
* when the value inserted is from expanded record
*
* Note: Timezone for this test is set as Asia/Hong-Kong
*
* 1. Create table with DateTime column
* 2. Insert DateTime value from UI '2021-01-01 08:00:00'
* 3. Expand record & update DateTime value to '2021-02-02 12:30:00'
* 4. Verify display value : should be '2021-02-02 12:30:00'
* 5. Verify API response, expect UTC : should be '2021-02-02 04:30:00'
*
*/
test('Expanded record insert', async () => {
if (!isSqlite(context)) return;
await dashboard.grid.openExpandedRow({ index: 0 });
await dashboard.expandedForm.fillField({
columnTitle: 'DateTime',
value: '2021-02-02 12:30:00',
type: 'dateTime',
});
await dashboard.expandedForm.save();
records = await api.dbTableRow.list('noco', context.project.id, 'dateTimeTable', { limit: 10 });
const readDate = records.list[0].DateTime;
// skip seconds from readDate
// stored value expected to be in UTC
expect(readDate.slice(0, 16)).toEqual('2021-02-02 04:30');
// DateTime inserted from cell is converted to UTC & stored
// Display value is same as inserted value
await dashboard.grid.cell.verifyDateCell({ index: 0, columnHeader: 'DateTime', value: '2021-02-02 12:30' });
});
/*
* This test is to verify the display value & API response of DateTime column in the grid
* when the value inserted is from copy and paste
*
* Note: Timezone for this test is set as Asia/Hong-Kong
*
* 1. Create table with DateTime column
* 2. Insert DateTime value from UI '2021-01-01 08:00:00'
* 3. Add new row & copy and paste DateTime value to '2021-01-01 08:00:00'
* 4. Verify display value : should be '2021-01-01 08:00:00'
* 5. Verify API response, expect UTC : should be '2021-01-01 00:00:00'
*
*/
test('Copy paste', async () => {
if (!isSqlite(context)) return;
await dashboard.grid.addNewRow({ index: 1, columnHeader: 'Title', value: 'Copy paste test' });
await dashboard.rootPage.reload();
await dashboard.rootPage.waitForTimeout(1000);
await dashboard.grid.cell.copyToClipboard(
{
index: 0,
columnHeader: 'DateTime',
},
{ position: { x: 1, y: 1 } }
);
expect(await dashboard.grid.cell.getClipboardText()).toBe('2021-01-01 08:00');
await dashboard.grid.cell.pasteFromClipboard({ index: 1, columnHeader: 'DateTime' });
records = await api.dbTableRow.list('noco', context.project.id, 'dateTimeTable', { limit: 10 });
const readDate = records.list[1].DateTime;
// skip seconds from readDate
// stored value expected to be in UTC
expect(readDate.slice(0, 16)).toEqual('2021-01-01 00:00');
// DateTime inserted from cell is converted to UTC & stored
// Display value is same as inserted value
await dashboard.grid.cell.verifyDateCell({ index: 1, columnHeader: 'DateTime', value: '2021-01-01 08:00' });
});
});
async function createTableWithDateTimeColumn(database: string, setTz = false) {
if (database === 'pg') {
const config = getKnexConfig({ dbName: 'postgres', dbType: 'pg' });
const pgknex = knex(config);
await pgknex.raw(`DROP DATABASE IF EXISTS datetimetable`);
await pgknex.raw(`CREATE DATABASE datetimetable`);
await pgknex.destroy();
const config2 = getKnexConfig({ dbName: 'datetimetable', dbType: 'pg' });
const pgknex2 = knex(config2);
await pgknex2.raw(`
CREATE TABLE my_table (
title SERIAL PRIMARY KEY,
datetime_without_tz TIMESTAMP WITHOUT TIME ZONE,
datetime_with_tz TIMESTAMP WITH TIME ZONE
);
-- SET timezone = 'Asia/Hong_Kong';
-- SELECT pg_sleep(1);
INSERT INTO my_table (datetime_without_tz, datetime_with_tz)
VALUES
('2023-04-27 10:00:00', '2023-04-27 10:00:00'),
('2023-04-27 10:00:00+05:30', '2023-04-27 10:00:00+05:30');
`);
await pgknex2.destroy();
} else if (database === 'mysql') {
const config = getKnexConfig({ dbName: 'sakila', dbType: 'mysql' });
const mysqlknex = knex(config);
await mysqlknex.raw(`DROP DATABASE IF EXISTS datetimetable`);
await mysqlknex.raw(`CREATE DATABASE datetimetable`);
if (setTz) {
await mysqlknex.raw(`SET GLOBAL time_zone = '+08:00'`);
// wait for 1 second for the timezone to be set
await mysqlknex.raw(`SELECT SLEEP(1)`);
}
await mysqlknex.destroy();
const config2 = getKnexConfig({ dbName: 'datetimetable', dbType: 'mysql' });
const mysqlknex2 = knex(config2);
await mysqlknex2.raw(`
USE datetimetable;
CREATE TABLE my_table (
title INT AUTO_INCREMENT PRIMARY KEY,
datetime_without_tz DATETIME,
datetime_with_tz TIMESTAMP
);
INSERT INTO my_table (datetime_without_tz, datetime_with_tz)
VALUES
('2023-04-27 10:00:00', '2023-04-27 10:00:00'),
('2023-04-27 10:00:00+05:30', '2023-04-27 10:00:00+05:30');
`);
await mysqlknex2.destroy();
} else if (database === 'sqlite') {
const config = getKnexConfig({ dbName: 'mydb', dbType: 'sqlite' });
// SQLite supports just one type of datetime
// Timezone information, if specified is stored as is in the database
// https://www.sqlite.org/lang_datefunc.html
const sqliteknex = knex(config);
await sqliteknex.raw(`DROP TABLE IF EXISTS my_table`);
await sqliteknex.raw(`
CREATE TABLE my_table (
title INTEGER PRIMARY KEY AUTOINCREMENT,
datetime_without_tz DATETIME,
datetime_with_tz DATETIME )`);
const datetimeData = [
['2023-04-27 10:00:00', '2023-04-27 10:00:00'],
['2023-04-27 10:00:00+05:30', '2023-04-27 10:00:00+05:30'],
];
for (const data of datetimeData) {
await sqliteknex('my_table').insert({
datetime_without_tz: data[0],
datetime_with_tz: data[1],
});
}
await sqliteknex.destroy();
}
}
function getDateTimeInLocalTimeZone(dateString: string) {
// create a Date object with the input string
// assumes local system timezone
const date = new Date(dateString);
// get the timezone offset in minutes and convert to milliseconds
// subtract the offset from the provided time in milliseconds for IST
const offsetMs = date.getTimezoneOffset() * 60 * 1000;
// adjust the date by the offset
const adjustedDate = new Date(date.getTime() - offsetMs);
// format the adjusted date as a string in the desired format
const outputString = adjustedDate.toISOString().slice(0, 16).replace('T', ' ');
// output the result
return outputString;
}
function getDateTimeInUTCTimeZone(dateString: string) {
// create a Date object with the input string
const date = new Date(dateString);
// get the timezone offset in minutes and convert to milliseconds
// subtract the offset from the provided time in milliseconds for IST
// const offsetMs = date.getTimezoneOffset() * 60 * 1000;
// adjust the date by the offset
// const adjustedDate = new Date(date.getTime() + offsetMs);
const adjustedDate = new Date(date.getTime());
// format the adjusted date as a string in the desired format
const outputString = adjustedDate.toISOString().slice(0, 19).replace('T', ' ');
// output the result
return `${outputString}+00:00`;
}
test.describe.serial('External DB - DateTime column', async () => {
let dashboard: DashboardPage;
let context: any;
const expectedDisplayValues = {
pg: {
// PG ignores timezone information for datetime without timezone
DatetimeWithoutTz: [
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+00:00'),
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+00:00'),
],
// PG stores datetime with timezone information in UTC
DatetimeWithTz: [
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+00:00'),
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+05:30'),
],
},
sqlite: {
// without +HH:MM information, display value is same as inserted value
// with +HH:MM information, display value is converted to browser timezone
// SQLite doesn't have with & without timezone fields; both are same in this case
DatetimeWithoutTz: [
getDateTimeInLocalTimeZone(`2023-04-27 10:00:00`),
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+05:30'),
],
DatetimeWithTz: [
getDateTimeInLocalTimeZone(`2023-04-27 10:00:00`),
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+05:30'),
],
},
mysql: {
DatetimeWithoutTz: [
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+00:00'),
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+05:30'),
],
DatetimeWithTz: [
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+00:00'),
getDateTimeInLocalTimeZone('2023-04-27 10:00:00+05:30'),
],
},
};
test.beforeEach(async ({ page }) => {
context = await setup({ page, isEmptyProject: true });
dashboard = new DashboardPage(page, context.project);
api = new Api({
baseURL: `http://localhost:8080/`,
headers: {
'xc-auth': context.token,
},
});
await createTableWithDateTimeColumn(context.dbType);
});
test('Formula, verify display value', async () => {
await connectToExtDb(context);
await dashboard.rootPage.reload();
await dashboard.rootPage.waitForTimeout(2000);
// insert a record to work with formula experiments
//
await dashboard.treeView.openBase({ title: 'datetimetable' });
await dashboard.treeView.openTable({ title: 'MyTable' });
// Insert new row
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithoutTz',
dateTime: '2023-04-27 10:00:00',
});
await dashboard.rootPage.waitForTimeout(1000);
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithTz',
dateTime: '2023-04-27 10:00:00',
});
// Create formula column (dummy)
api = new Api({
baseURL: `http://localhost:8080/`,
headers: {
'xc-auth': context.token,
},
});
const table = await api.dbTable.list(context.project.id);
let table_data: any;
table_data = await api.dbTableColumn.create(table.list.find(x => x.title === 'MyTable').id, {
title: 'formula-1',
uidt: UITypes.Formula,
formula_raw: '0',
});
table_data = await api.dbTableColumn.create(table.list.find(x => x.title === 'MyTable').id, {
title: 'formula-2',
uidt: UITypes.Formula,
formula_raw: '0',
});
async function verifyFormula({
formula,
expectedDisplayValue,
}: {
formula: string[];
expectedDisplayValue: string[];
}) {
// Update formula column to compute "month" instead of "day"
await api.dbTableColumn.update(table_data.columns[3].id, {
title: 'formula-1',
column_name: 'formula-1',
uidt: UITypes.Formula,
formula_raw: formula[0],
});
await dashboard.rootPage.waitForTimeout(1000);
await api.dbTableColumn.update(table_data.columns[4].id, {
title: 'formula-2',
column_name: 'formula-2',
uidt: UITypes.Formula,
formula_raw: formula[1],
});
// reload page
await dashboard.rootPage.reload();
await dashboard.grid.cell.verify({
index: 2,
columnHeader: 'formula-1',
value: expectedDisplayValue[0],
});
await dashboard.grid.cell.verify({
index: 2,
columnHeader: 'formula-2',
value: expectedDisplayValue[1],
});
}
// verify display value for formula columns (formula-1, formula-2)
await verifyFormula({
formula: ['DATEADD(DatetimeWithoutTz, 1, "day")', 'DATEADD(DatetimeWithTz, 1, "day")'],
expectedDisplayValue: ['2023-04-28 10:00', '2023-04-28 10:00'],
});
await verifyFormula({
formula: ['DATEADD(DatetimeWithoutTz, 1, "month")', 'DATEADD(DatetimeWithTz, 1, "month")'],
expectedDisplayValue: ['2023-05-27 10:00', '2023-05-27 10:00'],
});
await verifyFormula({
formula: ['DATEADD(DatetimeWithoutTz, 1, "year")', 'DATEADD(DatetimeWithTz, 1, "year")'],
expectedDisplayValue: ['2024-04-27 10:00', '2024-04-27 10:00'],
});
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithTz',
dateTime: '2024-04-27 10:00:00',
});
if (!isSqlite(context)) {
// SQLite : output is in decimal format; MySQL & Postgres : output is in integer format
await verifyFormula({
formula: [
'DATETIME_DIFF({DatetimeWithoutTz}, {DatetimeWithTz}, "days")',
'DATETIME_DIFF({DatetimeWithTz}, {DatetimeWithoutTz}, "days")',
],
expectedDisplayValue: ['-366', '366'],
});
await verifyFormula({
formula: [
'DATETIME_DIFF({DatetimeWithoutTz}, {DatetimeWithTz}, "months")',
'DATETIME_DIFF({DatetimeWithTz}, {DatetimeWithoutTz}, "months")',
],
expectedDisplayValue: ['-12', '12'],
});
}
});
test('Verify display value, UI insert, API response', async () => {
await connectToExtDb(context);
await dashboard.rootPage.reload();
await dashboard.rootPage.waitForTimeout(2000);
// 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')}`;
await dashboard.treeView.openBase({ title: 'datetimetable' });
await dashboard.treeView.openTable({ title: 'MyTable' });
if (isSqlite(context)) {
expectedDisplayValues['sqlite'].DatetimeWithoutTz[0] = getDateTimeInLocalTimeZone(
`2023-04-27 10:00:00${formattedOffset}`
);
expectedDisplayValues['sqlite'].DatetimeWithTz[0] = getDateTimeInLocalTimeZone(
`2023-04-27 10:00:00${formattedOffset}`
);
}
// display value for datetime column without tz should be same as stored value
// display value for datetime column with tz should be converted to browser timezone (HK in this case)
await dashboard.grid.cell.verifyDateCell({
index: 0,
columnHeader: 'DatetimeWithoutTz',
value: expectedDisplayValues[context.dbType].DatetimeWithoutTz[0],
});
await dashboard.grid.cell.verifyDateCell({
index: 1,
columnHeader: 'DatetimeWithoutTz',
value: expectedDisplayValues[context.dbType].DatetimeWithoutTz[1],
});
await dashboard.grid.cell.verifyDateCell({
index: 0,
columnHeader: 'DatetimeWithTz',
value: expectedDisplayValues[context.dbType].DatetimeWithTz[0],
});
await dashboard.grid.cell.verifyDateCell({
index: 1,
columnHeader: 'DatetimeWithTz',
value: expectedDisplayValues[context.dbType].DatetimeWithTz[1],
});
// Insert new row
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithoutTz',
dateTime: '2023-04-27 10:00:00',
});
await dashboard.rootPage.waitForTimeout(1000);
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithTz',
dateTime: '2023-04-27 10:00:00',
});
// reload page & verify if inserted values are shown correctly
await dashboard.rootPage.reload();
await dashboard.grid.cell.verifyDateCell({
index: 2,
columnHeader: 'DatetimeWithoutTz',
value: '2023-04-27 10:00',
});
await dashboard.grid.cell.verifyDateCell({
index: 2,
columnHeader: 'DatetimeWithTz',
value: '2023-04-27 10:00',
});
// verify API response
// Note that, for UI inserted records - second part of datetime may be non-zero (though not shown in UI)
// Hence, we skip seconds from API response
//
const records = await api.dbTableRow.list('noco', context.project.id, 'MyTable', { limit: 10 });
let dateTimeWithoutTz = records.list.map(record => record.DatetimeWithoutTz);
let dateTimeWithTz = records.list.map(record => record.DatetimeWithTz);
let expectedDateTimeWithoutTz = [];
let expectedDateTimeWithTz = [];
if (isSqlite(context)) {
expectedDateTimeWithoutTz = [
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
getDateTimeInUTCTimeZone('2023-04-27 10:00:00+05:30'),
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
expectedDateTimeWithTz = [
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
getDateTimeInUTCTimeZone('2023-04-27 10:00:00+05:30'),
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
} else if (isPg(context)) {
expectedDateTimeWithoutTz = [
'2023-04-27 10:00:00+00:00',
'2023-04-27 10:00:00+00:00',
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
expectedDateTimeWithTz = [
'2023-04-27 10:00:00+00:00',
'2023-04-27 04:30:00+00:00',
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
} else if (isMysql(context)) {
expectedDateTimeWithoutTz = [
'2023-04-27 10:00:00+00:00',
'2023-04-27 04:30:00+00:00',
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
expectedDateTimeWithTz = [
'2023-04-27 10:00:00+00:00',
'2023-04-27 04:30:00+00:00',
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
}
// reset seconds to 00 using string functions in dateTimeWithoutTz
dateTimeWithoutTz = dateTimeWithoutTz.map(
dateTimeString => dateTimeString.substring(0, 17) + '00' + dateTimeString.substring(19)
);
dateTimeWithTz = dateTimeWithTz.map(
dateTimeString => dateTimeString.substring(0, 17) + '00' + dateTimeString.substring(19)
);
console.log('dateTimeWithoutTz', dateTimeWithoutTz);
console.log('dateTimeWithTz', dateTimeWithTz);
expect(dateTimeWithoutTz).toEqual(expectedDateTimeWithoutTz);
expect(dateTimeWithTz).toEqual(expectedDateTimeWithTz);
});
});
test.describe('Ext DB MySQL : DB Timezone configured as HKT', () => {
let dashboard: DashboardPage;
let context: any;
test.beforeEach(async ({ page }) => {
context = await setup({ page, isEmptyProject: true });
dashboard = new DashboardPage(page, context.project);
if (!isMysql(context)) {
return;
}
api = new Api({
baseURL: `http://localhost:8080/`,
headers: {
'xc-auth': context.token,
},
});
await createTableWithDateTimeColumn(context.dbType, true);
});
test.afterEach(async () => {
if (isMysql(context)) {
// Reset DB Timezone
const config = getKnexConfig({ dbName: 'sakila', dbType: 'mysql' });
const mysqlknex = knex(config);
await mysqlknex.raw(`SET GLOBAL time_zone = '+00:00'`);
await mysqlknex.destroy();
}
});
test('Ext DB MySQL : DB Timezone configured as HKT', async () => {
if (!isMysql(context)) {
return;
}
// 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')}`;
// connect after timezone is set
await connectToExtDb(context);
await dashboard.rootPage.reload();
await dashboard.rootPage.waitForTimeout(2000);
await dashboard.treeView.openBase({ title: 'datetimetable' });
await dashboard.treeView.openTable({ title: 'MyTable' });
// display value for datetime column without tz should be same as stored value
// display value for datetime column with tz should be converted to browser timezone (HK in this case)
await dashboard.grid.cell.verifyDateCell({
index: 0,
columnHeader: 'DatetimeWithoutTz',
value: getDateTimeInLocalTimeZone('2023-04-27 10:00+08:00'),
});
await dashboard.grid.cell.verifyDateCell({
index: 1,
columnHeader: 'DatetimeWithoutTz',
value: getDateTimeInLocalTimeZone('2023-04-27 10:00+05:30'),
});
await dashboard.grid.cell.verifyDateCell({
index: 0,
columnHeader: 'DatetimeWithTz',
value: getDateTimeInLocalTimeZone('2023-04-27 10:00+08:00'),
});
await dashboard.grid.cell.verifyDateCell({
index: 1,
columnHeader: 'DatetimeWithTz',
value: getDateTimeInLocalTimeZone('2023-04-27 10:00+05:30'),
});
// Insert new row
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithoutTz',
dateTime: '2023-04-27 10:00:00',
});
await dashboard.grid.cell.dateTime.setDateTime({
index: 2,
columnHeader: 'DatetimeWithTz',
dateTime: '2023-04-27 10:00:00',
});
// reload page & verify if inserted values are shown correctly
await dashboard.rootPage.reload();
await dashboard.grid.cell.verifyDateCell({
index: 2,
columnHeader: 'DatetimeWithoutTz',
value: '2023-04-27 10:00',
});
await dashboard.grid.cell.verifyDateCell({
index: 2,
columnHeader: 'DatetimeWithTz',
value: '2023-04-27 10:00',
});
// verify API response
// Note that, for UI inserted records - second part of datetime may be non-zero (though not shown in UI)
// Hence, we skip seconds from API response
//
const records = await api.dbTableRow.list('sakila', context.project.id, 'MyTable', { limit: 10 });
let dateTimeWithoutTz = records.list.map(record => record.DatetimeWithoutTz);
let dateTimeWithTz = records.list.map(record => record.DatetimeWithTz);
const expectedDateTimeWithoutTz = [
'2023-04-27 02:00:00+00:00',
'2023-04-27 04:30:00+00:00',
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
const expectedDateTimeWithTz = [
'2023-04-27 02:00:00+00:00',
'2023-04-27 04:30:00+00:00',
getDateTimeInUTCTimeZone(`2023-04-27 10:00:00${formattedOffset}`),
];
// reset seconds to 00 using string functions in dateTimeWithoutTz
dateTimeWithoutTz = dateTimeWithoutTz.map(
dateTimeString => dateTimeString.substring(0, 17) + '00' + dateTimeString.substring(19)
);
dateTimeWithTz = dateTimeWithTz.map(
dateTimeString => dateTimeString.substring(0, 17) + '00' + dateTimeString.substring(19)
);
console.log('dateTimeWithoutTz', dateTimeWithoutTz);
console.log('dateTimeWithTz', dateTimeWithTz);
expect(dateTimeWithoutTz).toEqual(expectedDateTimeWithoutTz);
expect(dateTimeWithTz).toEqual(expectedDateTimeWithTz);
});
});

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 };
Loading…
Cancel
Save