Browse Source

fix: pass getMeta methods and add missing await

pull/7268/head
Pranav C 8 months ago
parent
commit
a00379c85f
  1. 4
      packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts
  2. 10
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  3. 7
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  4. 20
      packages/nocodb/src/services/columns.service.ts

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

@ -10,6 +10,7 @@ describe('Formula parsing and type validation', () => {
formula: '1 + 2', formula: '1 + 2',
columns: [], columns: [],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({})
}); });
expect(result.dataType).toEqual(FormulaDataTypes.NUMERIC); expect(result.dataType).toEqual(FormulaDataTypes.NUMERIC);
@ -26,6 +27,7 @@ describe('Formula parsing and type validation', () => {
}, },
], ],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({})
}); });
expect(result.dataType).toEqual(FormulaDataTypes.STRING); expect(result.dataType).toEqual(FormulaDataTypes.STRING);
@ -47,6 +49,7 @@ describe('Formula parsing and type validation', () => {
}, },
], ],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({})
}); });
expect(result.dataType).toEqual(FormulaDataTypes.STRING); expect(result.dataType).toEqual(FormulaDataTypes.STRING);
@ -66,6 +69,7 @@ describe('Formula parsing and type validation', () => {
}, },
], ],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({})
}); });
expect(result1.dataType).toEqual(FormulaDataTypes.NUMERIC); expect(result1.dataType).toEqual(FormulaDataTypes.NUMERIC);

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

@ -1420,7 +1420,7 @@ export async function validateFormulaAndExtractTreeWithType({
| typeof MssqlUi | typeof MssqlUi
| typeof SnowflakeUi | typeof SnowflakeUi
| typeof PgUi; | typeof PgUi;
getMeta?: (tableId: string) => Promise<any>; getMeta: (tableId: string) => Promise<any>;
}) { }) {
const colAliasToColMap = {}; const colAliasToColMap = {};
const colIdToColMap = {}; const colIdToColMap = {};
@ -1494,11 +1494,11 @@ export async function validateFormulaAndExtractTreeWithType({
} }
} }
// get args type and validate // get args type and validate
const validateResult = (res.arguments = parsedTree.arguments.map( const validateResult = (res.arguments = await Promise.all(parsedTree.arguments.map(
(arg) => { (arg) => {
return validateAndExtract(arg); return validateAndExtract(arg);
} }
)); )));
const argTypes = validateResult.map((v: any) => v.dataType); const argTypes = validateResult.map((v: any) => v.dataType);
@ -1631,8 +1631,8 @@ export async function validateFormulaAndExtractTreeWithType({
'Unary expression is not supported' 'Unary expression is not supported'
); );
} else if (parsedTree.type === JSEPNode.BINARY_EXP) { } else if (parsedTree.type === JSEPNode.BINARY_EXP) {
res.left = validateAndExtract(parsedTree.left); res.left = await validateAndExtract(parsedTree.left);
res.right = validateAndExtract(parsedTree.right); res.right = await validateAndExtract(parsedTree.right);
if (['==', '<', '>', '<=', '>=', '!='].includes(parsedTree.operator)) { if (['==', '<', '>', '<=', '>=', '!='].includes(parsedTree.operator)) {
res.dataType = FormulaDataTypes.COND_EXP; res.dataType = FormulaDataTypes.COND_EXP;

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

@ -9,12 +9,12 @@ import {
import { Logger } from '@nestjs/common'; import { Logger } from '@nestjs/common';
import mapFunctionName from '../mapFunctionName'; import mapFunctionName from '../mapFunctionName';
import genRollupSelectv2 from '../genRollupSelectv2'; import genRollupSelectv2 from '../genRollupSelectv2';
import type Model from '~/models/Model';
import type RollupColumn from '~/models/RollupColumn'; import type RollupColumn from '~/models/RollupColumn';
import type LinkToAnotherRecordColumn from '~/models/LinkToAnotherRecordColumn'; import type LinkToAnotherRecordColumn from '~/models/LinkToAnotherRecordColumn';
import type LookupColumn from '~/models/LookupColumn'; import type LookupColumn from '~/models/LookupColumn';
import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2'; import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2';
import type Column from '~/models/Column'; import type Column from '~/models/Column';
import Model from '~/models/Model';
import NocoCache from '~/cache/NocoCache'; import NocoCache from '~/cache/NocoCache';
import { CacheGetType, CacheScope } from '~/utils/globals'; import { CacheGetType, CacheScope } from '~/utils/globals';
import { import {
@ -90,6 +90,11 @@ async function _formulaQueryBuilder(
| 'mariadb' | 'mariadb'
| 'sqlite' | 'sqlite'
| 'snowflake', | 'snowflake',
getMeta: async (modelId) => {
const model = await Model.get(modelId);
await model.getColumns();
return model;
},
}); });
// populate and save parsedTree to column if not exist // populate and save parsedTree to column if not exist

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

@ -214,6 +214,11 @@ export class ColumnsService {
formula: colBody.formula_raw || colBody.formula, formula: colBody.formula_raw || colBody.formula,
columns: table.columns, columns: table.columns,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => {
const model = await Model.get(modelId);
await model.getColumns();
return model;
},
}); });
try { try {
@ -943,6 +948,11 @@ export class ColumnsService {
formula: new_formula_raw, formula: new_formula_raw,
columns: table.columns, columns: table.columns,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => {
const model = await Model.get(modelId);
await model.getColumns();
return model;
},
}), }),
}); });
} }
@ -1009,6 +1019,11 @@ export class ColumnsService {
formula: new_formula_raw, formula: new_formula_raw,
columns: table.columns, columns: table.columns,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => {
const model = await Model.get(modelId);
await model.getColumns();
return model;
},
}), }),
}); });
} }
@ -1223,6 +1238,11 @@ export class ColumnsService {
colBody.formula?.replaceAll('{{', '{').replaceAll('}}', '}'), colBody.formula?.replaceAll('{{', '{').replaceAll('}}', '}'),
columns: table.columns, columns: table.columns,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => {
const model = await Model.get(modelId);
await model.getColumns();
return model;
},
}); });
try { try {

Loading…
Cancel
Save