Browse Source

fix: add option to validate and throw error in filter

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/6770/head
Pranav C 1 year ago
parent
commit
ee0f42c52a
  1. 6
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 64
      packages/nocodb/src/db/conditionV2.ts

6
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -3180,7 +3180,7 @@ class BaseModelSqlv2 {
const { where } = this._getListArgs(args); const { where } = this._getListArgs(args);
const qb = this.dbDriver(this.tnPath); const qb = this.dbDriver(this.tnPath);
const aliasColObjMap = await this.model.getAliasColObjMap(); const aliasColObjMap = await this.model.getAliasColObjMap();
const filterObj = extractFilterFromXwhere(where, aliasColObjMap); const filterObj = extractFilterFromXwhere(where, aliasColObjMap, true);
await conditionV2( await conditionV2(
this, this,
@ -3197,6 +3197,8 @@ class BaseModelSqlv2 {
}), }),
], ],
qb, qb,
undefined,
true
); );
const execQueries: ((trx: Knex.Transaction, qb: any) => Promise<any>)[] = const execQueries: ((trx: Knex.Transaction, qb: any) => Promise<any>)[] =
[]; [];
@ -5004,7 +5006,7 @@ export function extractCondition(
validateFilterComparison(aliasColObjMap[alias].uidt, op, sub_op); validateFilterComparison(aliasColObjMap[alias].uidt, op, sub_op);
} else if (throwErrorIfInvalid) { } else if (throwErrorIfInvalid) {
NcError.badRequest(`Column ${alias} not found.`); NcError.badRequest(`Column '${alias}' not found.`);
} }
return new Filter({ return new Filter({

64
packages/nocodb/src/db/conditionV2.ts

@ -21,13 +21,21 @@ export default async function conditionV2(
conditionObj: Filter | Filter[], conditionObj: Filter | Filter[],
qb: Knex.QueryBuilder, qb: Knex.QueryBuilder,
alias?: string, alias?: string,
throwErrorIfInvalid = false,
) { ) {
if (!conditionObj || typeof conditionObj !== 'object') { if (!conditionObj || typeof conditionObj !== 'object') {
return; return;
} }
(await parseConditionV2(baseModelSqlv2, conditionObj, { count: 0 }, alias))( (
qb, await parseConditionV2(
); baseModelSqlv2,
conditionObj,
{ count: 0 },
alias,
undefined,
throwErrorIfInvalid,
)
)(qb);
} }
function getLogicalOpMethod(filter: Filter) { function getLogicalOpMethod(filter: Filter) {
@ -49,6 +57,7 @@ const parseConditionV2 = async (
aliasCount = { count: 0 }, aliasCount = { count: 0 },
alias?, alias?,
customWhereClause?, customWhereClause?,
throwErrorIfInvalid = false,
) => { ) => {
const knex = baseModelSqlv2.dbDriver; const knex = baseModelSqlv2.dbDriver;
@ -60,7 +69,14 @@ const parseConditionV2 = async (
if (Array.isArray(_filter)) { if (Array.isArray(_filter)) {
const qbs = await Promise.all( const qbs = await Promise.all(
_filter.map((child) => _filter.map((child) =>
parseConditionV2(baseModelSqlv2, child, aliasCount, alias), parseConditionV2(
baseModelSqlv2,
child,
aliasCount,
alias,
undefined,
throwErrorIfInvalid,
),
), ),
); );
@ -76,7 +92,14 @@ const parseConditionV2 = async (
const qbs = await Promise.all( const qbs = await Promise.all(
(children || []).map((child) => (children || []).map((child) =>
parseConditionV2(baseModelSqlv2, child, aliasCount), parseConditionV2(
baseModelSqlv2,
child,
aliasCount,
undefined,
undefined,
throwErrorIfInvalid,
),
), ),
); );
@ -89,7 +112,12 @@ const parseConditionV2 = async (
}; };
} else { } else {
const column = await filter.getColumn(); const column = await filter.getColumn();
if (!column) return () => {}; if (!column) {
if (throwErrorIfInvalid) {
throw new Error(`Invalid column id '${filter.fk_column_id}' in filter`);
}
return;
}
if (column.uidt === UITypes.LinkToAnotherRecord) { if (column.uidt === UITypes.LinkToAnotherRecord) {
const colOptions = const colOptions =
(await column.getColOptions()) as LinkToAnotherRecordColumn; (await column.getColOptions()) as LinkToAnotherRecordColumn;
@ -153,6 +181,9 @@ const parseConditionV2 = async (
fk_column_id: childModel?.displayValue?.id, fk_column_id: childModel?.displayValue?.id,
}), }),
aliasCount, aliasCount,
undefined,
undefined,
throwErrorIfInvalid,
) )
)(selectQb); )(selectQb);
@ -216,6 +247,9 @@ const parseConditionV2 = async (
fk_column_id: parentModel?.displayValue?.id, fk_column_id: parentModel?.displayValue?.id,
}), }),
aliasCount, aliasCount,
undefined,
undefined,
throwErrorIfInvalid,
) )
)(selectQb); )(selectQb);
@ -292,6 +326,9 @@ const parseConditionV2 = async (
fk_column_id: parentModel?.displayValue?.id, fk_column_id: parentModel?.displayValue?.id,
}), }),
aliasCount, aliasCount,
undefined,
undefined,
throwErrorIfInvalid,
) )
)(selectQb); )(selectQb);
@ -310,6 +347,7 @@ const parseConditionV2 = async (
filter, filter,
knex, knex,
aliasCount, aliasCount,
throwErrorIfInvalid,
); );
} else if ( } else if (
[UITypes.Rollup, UITypes.Links].includes(column.uidt) && [UITypes.Rollup, UITypes.Links].includes(column.uidt) &&
@ -843,6 +881,7 @@ async function generateLookupCondition(
filter: Filter, filter: Filter,
knex, knex,
aliasCount = { count: 0 }, aliasCount = { count: 0 },
throwErrorIfInvalid = false,
): Promise<any> { ): Promise<any> {
const colOptions = await col.getColOptions<LookupColumn>(); const colOptions = await col.getColOptions<LookupColumn>();
const relationColumn = await colOptions.getRelationColumn(); const relationColumn = await colOptions.getRelationColumn();
@ -880,6 +919,7 @@ async function generateLookupCondition(
knex, knex,
alias, alias,
aliasCount, aliasCount,
throwErrorIfInvalid,
); );
return (qbP: Knex.QueryBuilder) => { return (qbP: Knex.QueryBuilder) => {
@ -906,6 +946,7 @@ async function generateLookupCondition(
knex, knex,
alias, alias,
aliasCount, aliasCount,
throwErrorIfInvalid,
); );
return (qbP: Knex.QueryBuilder) => { return (qbP: Knex.QueryBuilder) => {
@ -943,6 +984,7 @@ async function generateLookupCondition(
knex, knex,
childAlias, childAlias,
aliasCount, aliasCount,
throwErrorIfInvalid,
); );
return (qbP: Knex.QueryBuilder) => { return (qbP: Knex.QueryBuilder) => {
@ -962,6 +1004,7 @@ async function nestedConditionJoin(
knex, knex,
alias: string, alias: string,
aliasCount: { count: number }, aliasCount: { count: number },
throwErrorIfInvalid = false,
) { ) {
if ( if (
lookupColumn.uidt === UITypes.Lookup || lookupColumn.uidt === UITypes.Lookup ||
@ -1044,6 +1087,7 @@ async function nestedConditionJoin(
knex, knex,
relAlias, relAlias,
aliasCount, aliasCount,
throwErrorIfInvalid,
); );
} else { } else {
switch (relationColOptions.type) { switch (relationColOptions.type) {
@ -1059,6 +1103,8 @@ async function nestedConditionJoin(
}), }),
aliasCount, aliasCount,
relAlias, relAlias,
undefined,
throwErrorIfInvalid,
) )
)(qb); )(qb);
} }
@ -1075,6 +1121,8 @@ async function nestedConditionJoin(
}), }),
aliasCount, aliasCount,
relAlias, relAlias,
undefined,
throwErrorIfInvalid,
) )
)(qb); )(qb);
} }
@ -1091,6 +1139,8 @@ async function nestedConditionJoin(
}), }),
aliasCount, aliasCount,
relAlias, relAlias,
undefined,
throwErrorIfInvalid,
) )
)(qb); )(qb);
} }
@ -1108,6 +1158,8 @@ async function nestedConditionJoin(
}), }),
aliasCount, aliasCount,
alias, alias,
undefined,
throwErrorIfInvalid,
) )
)(qb); )(qb);
} }

Loading…
Cancel
Save