diegomvh / angular-odata

Client side OData typescript library for Angular
https://www.npmjs.com/package/angular-odata
MIT License
46 stars 14 forks source link

How to perform deep filtering #84

Closed CorneLosVisma closed 8 months ago

CorneLosVisma commented 9 months ago

I just started using your library and I like it a lot. Hope you can help me with this.

I am trying to do queries like this:

https://services.odata.org/v4/TripPinService/People?$filter=Friends/any(a:a/FirstName%20eq%20%27Keith%27)&$expand=Friends

So: "get all People that have a Friend with FirstName=='Keith'"

Is this possible with your library?

CorneLosVisma commented 9 months ago

Ah wait, I think it is something like this:

people.query((q) => {
        q.filter({ Friends: { any: { Firstname: 'Keith' }}});
        q.expand('Friends');
    }
    .fetch()
    .subscribe();

Not sure it is the correct way but it seems to work. However I do not like this syntax very much.

Something like this would be nice :-)

people
  .whereAny('Friends', friend => friend.equals('Firstname', 'Keith'))
  .expand('Friends')
  .fetch()
  .subscribe();
diegomvh commented 9 months ago

Hi @CorneLosVisma The library support expression builder

people.query((q) => {
        q.filter(({e, t}) => e().any(t.Friends, ({e, t}) => e().eq(t.Firstname, 'Keith')));
        q.expand(({e, t}) => e().field(t.Friends));
    }
    .fetch()
    .subscribe();

Where in filter the optional parameters are:

e is the expression builder t is the type o are operations f are functions