Open hakimio opened 2 months ago
Hi,
Is this something that might help ?
Here you cam combine multiple filters in one filter, there are 2 overloads for Filter
.
OR
, AND
Example 2 : Combination of 2...n filters
import { MyEntity } from 'LOCATION_OF_YOUR_ENTITY_TYPE';
import { Filter, BaseRepository } from '@dxfrontier/cds-ts-repository';
class MyRepository extends BaseRepository<MyEntity> {
constructor() {
super(MyEntity); // a CDS Typer entity type
}
public async aMethod() {
const filterLike = new Filter<MyEntity>({
field: 'customer_name',
operator: 'LIKE',
value: 'abs',
});
const filterBetween = new Filter<MyEntity>({
field: 'stock',
operator: 'BETWEEN',
value1: 11,
value2: 333,
});
const filterIn = new Filter<MyEntity>({
field: 'ID',
operator: 'IN',
value: [201, 203, 207],
});
/*
combinedLikeAndBetweenFilters translates to :
customer_name like 'abs' or stock between 11 and 333
*/
const combinedLikeAndBetweenFilters = new Filter('OR', filterLike, filterBetween);
/*
filters translates to :
(customer_name LIKE 'abs' OR stock BETWEEN 11 and 333) AND ID IN ('203', '201', '207')
*/
const filters = new Filter('AND', combinedLikeAndBetweenFilters, filterIn);
// Execute the query using the builder find
const results = await this.builder().find(filters).getExpand('orders').execute();
// OR
// Execute the query using the .find
const results2 = await this.find(filters);
}
}
While your suggested solution would solve the issue, I still think my proposed solution has much better DX. To be honest, I find it hard to follow the logic in your code.
My solution was inspired by DevExtreme UI library: DevExtreme DataSource filter.
Your proposal looks quite good, In practice we will have a multi dimensional array which has some sort of tuples inside [filter1, 'or / and', filter2, 'or', filterN, ...] then logical 'or / and' and again N tuple and again N logical or ...
.find([
[filter1, 'or', filter2],
'and',
[filter3, 'or', filter4]
])
Let's keep it here and when I have time I can have a look.
No rush. Basically, to implement this you would have to convert proposed nested filter array to sap query-by-example nested object. Shouldn't be too complicated.
Description
Right now it seems it's only possible to supply a single instance of
Filter
tofind()
andfindOne()
method. It would be great if we could supply an array ofFilter
instances.Suggested solution
Following simple case assumes
and
relation for filters:or
operator can also be used:Nested arrays can be used to group filters:
Side note
You can find how this can be translated to query-by-example object on capire.