mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Fix filter column aliasing (#14423)
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -243,8 +243,8 @@ export function applySort(
|
||||
knex,
|
||||
});
|
||||
|
||||
const colPath = getColumnPath({ path: column, collection, aliasMap, relations }) || '';
|
||||
const [alias, field] = colPath.split('.');
|
||||
const { columnPath } = getColumnPath({ path: column, collection, aliasMap, relations });
|
||||
const [alias, field] = columnPath.split('.');
|
||||
|
||||
return {
|
||||
order,
|
||||
@@ -353,17 +353,11 @@ export function applyFilter(
|
||||
|
||||
if (relationType === 'm2o' || relationType === 'a2o' || relationType === null) {
|
||||
if (filterPath.length > 1) {
|
||||
const columnName = getColumnPath({ path: filterPath, collection, relations, aliasMap });
|
||||
const { columnPath, targetCollection } = getColumnPath({ path: filterPath, collection, relations, aliasMap });
|
||||
|
||||
if (!columnName) continue;
|
||||
if (!columnPath) continue;
|
||||
|
||||
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);
|
||||
}
|
||||
applyFilterToQuery(columnPath, filterOperator, filterValue, logical, targetCollection);
|
||||
} else {
|
||||
applyFilterToQuery(`${collection}.${filterPath[0]}`, filterOperator, filterValue, logical);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ export type ColPathProps = {
|
||||
/**
|
||||
* Converts a Directus field list path to the correct SQL names based on the constructed alias map.
|
||||
* For example: ['author', 'role', 'name'] -> 'ljnsv.name'
|
||||
* Also returns the target collection of the column: 'directus_roles'
|
||||
*/
|
||||
export function getColumnPath({ path, collection, aliasMap, relations }: ColPathProps) {
|
||||
return followRelation(path);
|
||||
@@ -23,7 +24,7 @@ export function getColumnPath({ path, collection, aliasMap, relations }: ColPath
|
||||
pathParts: string[],
|
||||
parentCollection: string = collection,
|
||||
parentAlias?: string
|
||||
): string | void {
|
||||
): { columnPath: string; targetCollection: string } {
|
||||
/**
|
||||
* For A2M fields, the path can contain an optional collection scope <field>:<scope>
|
||||
*/
|
||||
@@ -54,11 +55,13 @@ export function getColumnPath({ path, collection, aliasMap, relations }: ColPath
|
||||
}
|
||||
|
||||
if (remainingParts.length === 1) {
|
||||
return `${alias || parent}.${remainingParts[0]}`;
|
||||
return { columnPath: `${alias || parent}.${remainingParts[0]}`, targetCollection: parent };
|
||||
}
|
||||
|
||||
if (remainingParts.length) {
|
||||
return followRelation(remainingParts, parent, alias);
|
||||
}
|
||||
|
||||
return { columnPath: '', targetCollection: '' };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,8 @@ test('Extracts path scope and returns correct alias for a2o', () => {
|
||||
};
|
||||
|
||||
const result = getColumnPath(input as ColPathProps);
|
||||
expect(result).toBe('abcdef.text');
|
||||
expect(result.columnPath).toBe('abcdef.text');
|
||||
expect(result.targetCollection).toBe('headings');
|
||||
});
|
||||
|
||||
test('Returns correct alias for m2o', () => {
|
||||
@@ -110,7 +111,8 @@ test('Returns correct alias for m2o', () => {
|
||||
};
|
||||
|
||||
const result = getColumnPath(input as ColPathProps);
|
||||
expect(result).toBe('grenv.name');
|
||||
expect(result.columnPath).toBe('grenv.name');
|
||||
expect(result.targetCollection).toBe('directus_roles');
|
||||
});
|
||||
|
||||
test('Returns correct alias for o2m (& uses the table name if no alias exists)', () => {
|
||||
@@ -139,5 +141,6 @@ test('Returns correct alias for o2m (& uses the table name if no alias exists)',
|
||||
};
|
||||
|
||||
const result = getColumnPath(input as ColPathProps);
|
||||
expect(result).toBe('categories.name');
|
||||
expect(result.columnPath).toBe('categories.name');
|
||||
expect(result.targetCollection).toBe('categories');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user