devnixs / ODataAngularResources

Fluent OData queries from Angular's Resources
MIT License
142 stars 38 forks source link

How to query related collections? #103

Open akaSybe opened 7 years ago

akaSybe commented 7 years ago

How to query related collections?

For example: in OData service I have endpoint: /odata/Users({key})/Orders

Is there a way to retrieve user orders by user ID using $odataresource?

patrickdizon commented 7 years ago

@akaSybe are you using Entity Framework? Im assuming you are... if you have a relationship with Orders in the User model you can use the $expand and $select parameters.

$expand=Orders,[OtherRelatedModels]&$select=Orders,[OtherRelatedModelsOrColumns]
Abenezer commented 7 years ago

yes you can use $expand but it would still nice to have this feature b/c their might be a custom logic on server side. i've tried custom actions $odataresource(config.apiUrl + 'Parents', { pid: '@ID' }, { GetStudents: { method: 'GET', url: config.apiUrl + 'Parents(:pid)/Students' }, ... but i cant get odata object to $filter and $select after custom get query

patrickdizon commented 7 years ago

My approach would be to filter using the related parent like so. Filtering the Orders Model in EF.

?$filter=Users/Id eq '123'&$expand=Users

Abenezer commented 7 years ago

off course its possible like this Parents.odata() .filter("ID", 8) .expandPredicate("Students").select("FName") .finish().single(); but wouldn't it be nice if we could just Parents.GetStudents({pid:8}).select('FName');

btw with custom action am able to do Parents.GetStudents({pid:8}) but not the odata part

patrickdizon commented 7 years ago

@Abenezer is your example still in reference to the OP? I dont see how your example would produce orders?

Abenezer commented 7 years ago

just the concept here is how i would do it for the mentioned issue

Users.odata() .filter("ID", 8) .expandPredicate("Orders").select({somepropoerty}) .finish().query(); but my point is if i have an end point /odata/Users({key})/Orders its better if i have the option (witch is possible on ngresrouce) to have a custom action and use it like Users.GetOrders({uid:8})
the only problem is i cant use odata actions like filter and select after that

btw i am requesting a feature -> to append odata actions on custom get resource actions if possible or what would be a perfect soln if we could use odata filters after get() like Users.get(8).Orders().select({someproperty}).exec() but i think this is hard to implement b/c ngresource is not implemented like this