Browse Source

feat: add formula validation for all read/list methods and invoke it only when there is atleast one formula column

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5379/head
Pranav C 2 years ago
parent
commit
05ff4a3009
  1. 43
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

43
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -114,14 +114,23 @@ class BaseModelSqlv2 {
autoBind(this); autoBind(this);
} }
public async readByPk(id?: any): Promise<any> { public async readByPk(id?: any, validateFormula = false): Promise<any> {
const qb = this.dbDriver(this.tnPath); const qb = this.dbDriver(this.tnPath);
await this.selectObject({ qb }); await this.selectObject({ qb, validateFormula });
qb.where(_wherePk(this.model.primaryKeys, id)); qb.where(_wherePk(this.model.primaryKeys, id));
const data = (await this.execAndParse(qb))?.[0]; let data;
try {
data = (await this.execAndParse(qb))?.[0];
} catch (e) {
if (validateFormula || !haveFormulaColumn(await this.model.getColumns()))
throw e;
console.log(e);
return this.readByPk(id, true);
}
if (data) { if (data) {
const proto = await this.getProto(); const proto = await this.getProto();
@ -146,11 +155,12 @@ class BaseModelSqlv2 {
where?: string; where?: string;
filterArr?: Filter[]; filterArr?: Filter[];
sort?: string | string[]; sort?: string | string[];
} = {} } = {},
validateFormula = false
): Promise<any> { ): Promise<any> {
const { where, ...rest } = this._getListArgs(args as any); const { where, ...rest } = this._getListArgs(args as any);
const qb = this.dbDriver(this.tnPath); const qb = this.dbDriver(this.tnPath);
await this.selectObject({ qb }); await this.selectObject({ qb, validateFormula });
const aliasColObjMap = await this.model.getAliasColObjMap(); const aliasColObjMap = await this.model.getAliasColObjMap();
const sorts = extractSortsObject(rest?.sort, aliasColObjMap); const sorts = extractSortsObject(rest?.sort, aliasColObjMap);
@ -179,7 +189,16 @@ class BaseModelSqlv2 {
qb.orderBy(this.model.primaryKey.column_name); qb.orderBy(this.model.primaryKey.column_name);
} }
const data = await qb.first(); let data;
try {
data = await qb.first();
} catch (e) {
if (validateFormula || !haveFormulaColumn(await this.model.getColumns()))
throw e;
console.log(e);
return this.findOne(args, true);
}
if (data) { if (data) {
const proto = await this.getProto(); const proto = await this.getProto();
@ -287,10 +306,10 @@ class BaseModelSqlv2 {
try { try {
data = await this.execAndParse(qb); data = await this.execAndParse(qb);
} catch (e) { } catch (e) {
if (!validateFormula) { if (validateFormula || !haveFormulaColumn(await this.model.getColumns()))
return this.list(args, ignoreViewFilterAndSort, true); throw e;
} console.log(e);
throw e; return this.list(args, ignoreViewFilterAndSort, true);
} }
return data?.map((d) => { return data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
@ -3198,4 +3217,8 @@ function getCompositePk(primaryKeys: Column[], row) {
return primaryKeys.map((c) => row[c.title]).join('___'); return primaryKeys.map((c) => row[c.title]).join('___');
} }
function haveFormulaColumn(columns: Column[]) {
return columns.some((c) => c.uidt === UITypes.Formula);
}
export { BaseModelSqlv2 }; export { BaseModelSqlv2 };

Loading…
Cancel
Save