Browse Source

fix: add missing await and type corrections

pull/7268/head
Pranav C 1 year ago
parent
commit
20075d19e7
  1. 13
      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

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

@ -11,15 +11,6 @@ import {
validateFormulaAndExtractTreeWithType, validateFormulaAndExtractTreeWithType,
} from 'nocodb-sdk' } from 'nocodb-sdk'
import type { ColumnType, FormulaType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk' import type { ColumnType, FormulaType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk'
import {
UITypes,
isLinksOrLTAR,
isNumericCol,
isSystemColumn,
jsepCurlyHook,
substituteColumnIdWithAliasInFormula,
validateFormulaAndExtractTreeWithType
} from 'nocodb-sdk'
import { import {
MetaInj, MetaInj,
NcAutocompleteTree, NcAutocompleteTree,
@ -88,11 +79,11 @@ const validators = {
formula_raw: [ formula_raw: [
{ {
validator: (_: any, formula: any) => { validator: (_: any, formula: any) => {
return new Promise<void>((resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
if (!formula?.trim()) return reject(new Error('Required')) if (!formula?.trim()) return reject(new Error('Required'))
try { try {
validateFormulaAndExtractTreeWithType({ formula, columns: supportedColumns.value, clientOrSqlUi: sqlUi.value }) await validateFormulaAndExtractTreeWithType({ formula, columns: supportedColumns.value, clientOrSqlUi: sqlUi.value })
} catch (e: any) { } catch (e: any) {
if (e instanceof FormulaError && e.extra?.key) { if (e instanceof FormulaError && e.extra?.key) {
return reject(new Error(t(e.extra.key, e.extra))) 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', () => { describe('Formula parsing and type validation', () => {
it('Simple formula', async () => { it('Simple formula', async () => {
const result = validateFormulaAndExtractTreeWithType({ const result = await validateFormulaAndExtractTreeWithType({
formula: '1 + 2', formula: '1 + 2',
columns: [], columns: [],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
@ -16,7 +16,7 @@ describe('Formula parsing and type validation', () => {
}); });
it('Formula with IF condition', async () => { it('Formula with IF condition', async () => {
const result = validateFormulaAndExtractTreeWithType({ const result = await validateFormulaAndExtractTreeWithType({
formula: 'IF({column}, "Found", BLANK())', formula: 'IF({column}, "Found", BLANK())',
columns: [ columns: [
{ {
@ -31,7 +31,7 @@ describe('Formula parsing and type validation', () => {
expect(result.dataType).toEqual(FormulaDataTypes.STRING); expect(result.dataType).toEqual(FormulaDataTypes.STRING);
}); });
it('Complex formula', async () => { it('Complex formula', async () => {
const result = validateFormulaAndExtractTreeWithType({ const result = await validateFormulaAndExtractTreeWithType({
formula: formula:
'SWITCH({column2},"value1",IF({column1}, "Found", BLANK()),"value2", 2)', 'SWITCH({column2},"value1",IF({column1}, "Found", BLANK()),"value2", 2)',
columns: [ columns: [
@ -51,7 +51,7 @@ describe('Formula parsing and type validation', () => {
expect(result.dataType).toEqual(FormulaDataTypes.STRING); expect(result.dataType).toEqual(FormulaDataTypes.STRING);
const result1 = validateFormulaAndExtractTreeWithType({ const result1 = await validateFormulaAndExtractTreeWithType({
formula: 'SWITCH({column2},"value1",IF({column1}, 1, 2),"value2", 2)', formula: 'SWITCH({column2},"value1",IF({column1}, 1, 2),"value2", 2)',
columns: [ columns: [
{ {

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

@ -6,12 +6,10 @@ import dayjs from 'dayjs';
import { import {
MssqlUi, MssqlUi,
MysqlUi, MysqlUi,
OracleUi,
PgUi, PgUi,
SnowflakeUi, SnowflakeUi,
SqlUiFactory, SqlUiFactory,
} from './sqlUi'; } from './sqlUi';
import { RollupColumn } from '../../../nocodb/src/models';
// todo: move to date utils and export, remove duplicate from gui // todo: move to date utils and export, remove duplicate from gui
@ -1260,11 +1258,10 @@ async function extractColumnIdentifierType({
| 'mariadb' | 'mariadb'
| 'sqlite' | 'sqlite'
| 'snowflake' | 'snowflake'
| MysqlUi | typeof MysqlUi
| MssqlUi | typeof MssqlUi
| SnowflakeUi | typeof SnowflakeUi
| PgUi | typeof PgUi
| OracleUi;
}) { }) {
const res: { const res: {
dataType?: FormulaDataTypes; dataType?: FormulaDataTypes;
@ -1422,8 +1419,7 @@ export async function validateFormulaAndExtractTreeWithType({
| typeof MysqlUi | typeof MysqlUi
| typeof MssqlUi | typeof MssqlUi
| typeof SnowflakeUi | typeof SnowflakeUi
| typeof PgUi | typeof PgUi;
| typeof OracleUi;
getMeta?: (tableId: string) => Promise<any>; getMeta?: (tableId: string) => Promise<any>;
}) { }) {
const colAliasToColMap = {}; 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 // formula may include double curly brackets in previous version
// convert to single curly bracket here for compatibility // convert to single curly bracket here for compatibility
// const _tree1 = jsep(_tree.replaceAll('{{', '{').replaceAll('}}', '}')); // const _tree1 = jsep(_tree.replaceAll('{{', '{').replaceAll('}}', '}'));
tree = validateFormulaAndExtractTreeWithType({ tree = await validateFormulaAndExtractTreeWithType({
formula: _tree.replaceAll('{{', '{').replaceAll('}}', '}'), formula: _tree.replaceAll('{{', '{').replaceAll('}}', '}'),
columns, columns,
clientOrSqlUi: baseModelSqlv2.clientType as clientOrSqlUi: baseModelSqlv2.clientType as

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

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

Loading…
Cancel
Save