Fix aliased DB table names in filter query (#13575)

* Fix aliased DB table names in filter query

* Rename to originalCollectionName for clarity

* Update api/src/utils/apply-query.ts

Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
This commit is contained in:
ian
2022-06-14 21:14:45 +08:00
committed by GitHub
parent f17a1d8988
commit f8b73ce236

View File

@@ -357,7 +357,13 @@ export function applyFilter(
if (!columnName) continue;
applyFilterToQuery(columnName, filterOperator, filterValue, logical);
if (relation?.related_collection) {
applyFilterToQuery(columnName, filterOperator, filterValue, logical, relation.related_collection); // m2o
} else if (filterPath[0].includes(':')) {
applyFilterToQuery(columnName, filterOperator, filterValue, logical, filterPath[0].split(':')[1]); // a2o
} else {
applyFilterToQuery(columnName, filterOperator, filterValue, logical);
}
} else {
applyFilterToQuery(`${collection}.${filterPath[0]}`, filterOperator, filterValue, logical);
}
@@ -394,7 +400,13 @@ export function applyFilter(
}
}
}
function applyFilterToQuery(key: string, operator: string, compareValue: any, logical: 'and' | 'or' = 'and') {
function applyFilterToQuery(
key: string,
operator: string,
compareValue: any,
logical: 'and' | 'or' = 'and',
originalCollectionName?: string
) {
const [table, column] = key.split('.');
// Is processed through Knex.Raw, so should be safe to string-inject into these where queries
@@ -436,9 +448,10 @@ export function applyFilter(
// Cast filter value (compareValue) based on type of field being filtered against
const [collection, field] = key.split('.');
const mappedCollection = originalCollectionName || collection;
if (collection in schema.collections && field in schema.collections[collection].fields) {
const type = schema.collections[collection].fields[field].type;
if (mappedCollection in schema.collections && field in schema.collections[mappedCollection].fields) {
const type = schema.collections[mappedCollection].fields[field].type;
if (['date', 'dateTime', 'time', 'timestamp'].includes(type)) {
if (Array.isArray(compareValue)) {