Browse Source

refactor(nocodb): move the common datetime functions to sdk

pull/6870/head
աɨռɢӄաօռɢ 1 year ago
parent
commit
206703d32f
  1. 11
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  2. 2
      packages/nocodb/src/db/functionMappings/sqlite.ts
  3. 30
      packages/nocodb/src/helpers/formulaFnHelper.ts
  4. 75
      packages/nocodb/src/utils/dateTimeUtils.ts

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

@ -1,5 +1,9 @@
import jsep from 'jsep'; import jsep from 'jsep';
import { jsepCurlyHook, UITypes } from 'nocodb-sdk'; import {
jsepCurlyHook,
UITypes,
validateDateWithUnknownFormat,
} from 'nocodb-sdk';
import mapFunctionName from '../mapFunctionName'; import mapFunctionName from '../mapFunctionName';
import genRollupSelectv2 from '../genRollupSelectv2'; import genRollupSelectv2 from '../genRollupSelectv2';
import type Column from '~/models/Column'; import type Column from '~/models/Column';
@ -10,10 +14,7 @@ import type LookupColumn from '~/models/LookupColumn';
import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2'; import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2';
import NocoCache from '~/cache/NocoCache'; import NocoCache from '~/cache/NocoCache';
import { CacheGetType, CacheScope } from '~/utils/globals'; import { CacheGetType, CacheScope } from '~/utils/globals';
import { import { convertDateFormatForConcat } from '~/helpers/formulaFnHelper';
convertDateFormatForConcat,
validateDateWithUnknownFormat,
} from '~/helpers/formulaFnHelper';
import FormulaColumn from '~/models/FormulaColumn'; import FormulaColumn from '~/models/FormulaColumn';
// todo: switch function based on database // todo: switch function based on database

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

@ -1,9 +1,9 @@
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { convertToTargetFormat, getDateFormat } from 'nocodb-sdk';
import commonFns from './commonFns'; import commonFns from './commonFns';
import type { MapFnArgs } from '../mapFunctionName'; import type { MapFnArgs } from '../mapFunctionName';
import { convertUnits } from '~/helpers/convertUnits'; import { convertUnits } from '~/helpers/convertUnits';
import { getWeekdayByText } from '~/helpers/formulaFnHelper'; import { getWeekdayByText } from '~/helpers/formulaFnHelper';
import { convertToTargetFormat, getDateFormat } from '~/utils/dateTimeUtils';
const sqlite3 = { const sqlite3 = {
...commonFns, ...commonFns,

30
packages/nocodb/src/helpers/formulaFnHelper.ts

@ -1,12 +1,7 @@
import dayjs from 'dayjs';
import { UITypes } from 'nocodb-sdk'; import { UITypes } from 'nocodb-sdk';
import { convertDateFormat } from './convertDateFormat'; import { convertDateFormat } from './convertDateFormat';
import Column from '~/models/Column'; import Column from '~/models/Column';
// todo: tobe fixed
// import customParseFormat from 'dayjs/plugin/customParseFormat';
// extend(customParseFormat);
export function getWeekdayByText(v: string) { export function getWeekdayByText(v: string) {
return { return {
monday: 0, monday: 0,
@ -31,31 +26,6 @@ export function getWeekdayByIndex(idx: number): string {
}[idx || 0]; }[idx || 0];
} }
export function validateDateWithUnknownFormat(v: string) {
const dateFormats = [
'DD-MM-YYYY',
'MM-DD-YYYY',
'YYYY-MM-DD',
'DD/MM/YYYY',
'MM/DD/YYYY',
'YYYY/MM/DD',
'DD MM YYYY',
'MM DD YYYY',
'YYYY MM DD',
];
for (const format of dateFormats) {
if (dayjs(v, format, true).isValid() as any) {
return true;
}
for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) {
if (dayjs(v, `${format} ${timeFormat}`, true).isValid() as any) {
return true;
}
}
}
return false;
}
export async function convertDateFormatForConcat( export async function convertDateFormatForConcat(
o, o,
columnIdToUidt, columnIdToUidt,

75
packages/nocodb/src/utils/dateTimeUtils.ts

@ -1,75 +0,0 @@
import dayjs from 'dayjs';
export const dateFormats = [
'DD-MM-YYYY',
'MM-DD-YYYY',
'YYYY-MM-DD',
'DD/MM/YYYY',
'MM/DD/YYYY',
'YYYY/MM/DD',
'DD MM YYYY',
'MM DD YYYY',
'YYYY MM DD',
];
export function validateDateFormat(v: string) {
return dateFormats.includes(v);
}
export function validateDateWithUnknownFormat(v: string) {
for (const format of dateFormats) {
if (dayjs(v, format, true).isValid() as any) {
return true;
}
for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) {
if (dayjs(v, `${format} ${timeFormat}`, true).isValid() as any) {
return true;
}
}
}
return false;
}
export function getDateFormat(v: string) {
for (const format of dateFormats) {
if (dayjs(v, format, true).isValid()) {
return format;
}
}
return 'YYYY/MM/DD';
}
export function getDateTimeFormat(v: string) {
for (const format of dateFormats) {
for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) {
const dateTimeFormat = `${format} ${timeFormat}`;
if (dayjs(v, dateTimeFormat, true).isValid() as any) {
return dateTimeFormat;
}
}
}
return 'YYYY/MM/DD';
}
export function parseStringDate(v: string, dateFormat: string) {
const dayjsObj = dayjs(v);
if (dayjsObj.isValid()) {
v = dayjsObj.format('YYYY-MM-DD');
} else {
v = dayjs(v, dateFormat).format('YYYY-MM-DD');
}
return v;
}
export function convertToTargetFormat(
v: string,
oldDataFormat,
newDateFormat: string,
) {
if (
!dateFormats.includes(oldDataFormat) ||
!dateFormats.includes(newDateFormat)
)
return v;
return dayjs(v, oldDataFormat).format(newDateFormat);
}
Loading…
Cancel
Save