Browse Source

fix: add missing await and type corrections

pull/7268/head
Pranav C 1 year ago
parent
commit
20075d19e7
  1. 15
      packages/nc-gui/components/smartsheet/column/FormulaOptions.vue
  2. 8
      packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts
  3. 14
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  4. 2
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  5. 8
      packages/nocodb/src/services/columns.service.ts

15
packages/nc-gui/components/smartsheet/column/FormulaOptions.vue

@ -11,15 +11,6 @@ import {
validateFormulaAndExtractTreeWithType,
} from 'nocodb-sdk'
import type { ColumnType, FormulaType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk'
import {
UITypes,
isLinksOrLTAR,
isNumericCol,
isSystemColumn,
jsepCurlyHook,
substituteColumnIdWithAliasInFormula,
validateFormulaAndExtractTreeWithType
} from 'nocodb-sdk'
import {
MetaInj,
NcAutocompleteTree,
@ -87,12 +78,12 @@ const refTables = computed(() => {
const validators = {
formula_raw: [
{
validator: (_: any, formula: any) => {
return new Promise<void>((resolve, reject) => {
validator: (_: any, formula: any) => {
return new Promise<void>(async (resolve, reject) => {
if (!formula?.trim()) return reject(new Error('Required'))
try {
validateFormulaAndExtractTreeWithType({ formula, columns: supportedColumns.value, clientOrSqlUi: sqlUi.value })
await validateFormulaAndExtractTreeWithType({ formula, columns: supportedColumns.value, clientOrSqlUi: sqlUi.value })
} catch (e: any) {
if (e instanceof FormulaError && e.extra?.key) {
return reject(new Error(t(e.extra.key, e.extra)))

8
packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts

@ -6,7 +6,7 @@ import UITypes from './UITypes';
describe('Formula parsing and type validation', () => {
it('Simple formula', async () => {
const result = validateFormulaAndExtractTreeWithType({
const result = await validateFormulaAndExtractTreeWithType({
formula: '1 + 2',
columns: [],
clientOrSqlUi: 'mysql2',
@ -16,7 +16,7 @@ describe('Formula parsing and type validation', () => {
});
it('Formula with IF condition', async () => {
const result = validateFormulaAndExtractTreeWithType({
const result = await validateFormulaAndExtractTreeWithType({
formula: 'IF({column}, "Found", BLANK())',
columns: [
{
@ -31,7 +31,7 @@ describe('Formula parsing and type validation', () => {
expect(result.dataType).toEqual(FormulaDataTypes.STRING);
});
it('Complex formula', async () => {
const result = validateFormulaAndExtractTreeWithType({
const result = await validateFormulaAndExtractTreeWithType({
formula:
'SWITCH({column2},"value1",IF({column1}, "Found", BLANK()),"value2", 2)',
columns: [
@ -51,7 +51,7 @@ describe('Formula parsing and type validation', () => {
expect(result.dataType).toEqual(FormulaDataTypes.STRING);
const result1 = validateFormulaAndExtractTreeWithType({
const result1 = await validateFormulaAndExtractTreeWithType({
formula: 'SWITCH({column2},"value1",IF({column1}, 1, 2),"value2", 2)',
columns: [
{

14
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -6,12 +6,10 @@ import dayjs from 'dayjs';
import {
MssqlUi,
MysqlUi,
OracleUi,
PgUi,
SnowflakeUi,
SqlUiFactory,
} from './sqlUi';
import { RollupColumn } from '../../../nocodb/src/models';
// todo: move to date utils and export, remove duplicate from gui
@ -1260,11 +1258,10 @@ async function extractColumnIdentifierType({
| 'mariadb'
| 'sqlite'
| 'snowflake'
| MysqlUi
| MssqlUi
| SnowflakeUi
| PgUi
| OracleUi;
| typeof MysqlUi
| typeof MssqlUi
| typeof SnowflakeUi
| typeof PgUi
}) {
const res: {
dataType?: FormulaDataTypes;
@ -1422,8 +1419,7 @@ export async function validateFormulaAndExtractTreeWithType({
| typeof MysqlUi
| typeof MssqlUi
| typeof SnowflakeUi
| typeof PgUi
| typeof OracleUi;
| typeof PgUi;
getMeta?: (tableId: string) => Promise<any>;
}) {
const colAliasToColMap = {};

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

@ -77,7 +77,7 @@ async function _formulaQueryBuilder(
// formula may include double curly brackets in previous version
// convert to single curly bracket here for compatibility
// const _tree1 = jsep(_tree.replaceAll('{{', '{').replaceAll('}}', '}'));
tree = validateFormulaAndExtractTreeWithType({
tree = await validateFormulaAndExtractTreeWithType({
formula: _tree.replaceAll('{{', '{').replaceAll('}}', '}'),
columns,
clientOrSqlUi: baseModelSqlv2.clientType as

8
packages/nocodb/src/services/columns.service.ts

@ -210,7 +210,7 @@ export class ColumnsService {
colBody.formula_raw || colBody.formula,
table.columns,
);
colBody.parsed_tree = validateFormulaAndExtractTreeWithType({
colBody.parsed_tree = await validateFormulaAndExtractTreeWithType({
formula: colBody.formula_raw || colBody.formula,
columns: table.columns,
clientOrSqlUi: source.type,
@ -939,7 +939,7 @@ export class ColumnsService {
]);
await FormulaColumn.update(c.id, {
formula_raw: new_formula_raw,
parsed_tree: validateFormulaAndExtractTreeWithType({
parsed_tree: await validateFormulaAndExtractTreeWithType({
formula: new_formula_raw,
columns: table.columns,
clientOrSqlUi: source.type,
@ -1005,7 +1005,7 @@ export class ColumnsService {
]);
await FormulaColumn.update(c.id, {
formula_raw: new_formula_raw,
parsed_tree: validateFormulaAndExtractTreeWithType({
parsed_tree: await validateFormulaAndExtractTreeWithType({
formula: new_formula_raw,
columns: table.columns,
clientOrSqlUi: source.type,
@ -1215,7 +1215,7 @@ export class ColumnsService {
colBody.formula_raw || colBody.formula,
table.columns,
);
colBody.parsed_tree = validateFormulaAndExtractTreeWithType({
colBody.parsed_tree = await validateFormulaAndExtractTreeWithType({
// formula may include double curly brackets in previous version
// convert to single curly bracket here for compatibility
formula:

Loading…
Cancel
Save