I want to request records from a specific model via REST-Api from a LoopBack application. I also want to enable related objects through an include filter. This works fine, but returns ALL related objects. Is it possible to limit them, as well as organize them by the field of related objects?
Model:
- DEPARTMENT Fields: - id - name - ... Relations_ -> hasMany: Messages Relations_ -> hasMany: Members - MESSAGE Fields: - id - senderId - body - ... - MEMBER Fields: - id - email - ...
Requests:
What I want to achieve is to request all departments with all their members, but only the last message ordered by a specific field (created timestamp).
The first approach could be a simple version of the query string of a GET request:
http:
This will return all departments with all messages and all participants. However, I would like to limit the number of messages returned to the last (or last 5 or any other, sorted by a specific field of the MESSAGE model.
I also tried the jsonfied query syntax:
http://loopback-server:3000/api/departments?filter={"include":{"relation": "messages","limit":1}}
Unfortunately, the βmarginalβ parameter is not used here to communicate messages.
The next option will return only the first section, which means that limit-param is applied to the department model, and not to the relationship model.
http://loopback-server:3000/api/departments?filter={"include":{"relation": "messages"},"limit":1}
Then I discovered an area- parameter and tried this:
http://loopback-server:3000/api/departments?filter={"include":{"relation": "messages","scope":{"limit":1, "skip":0}}}
This gives a really strange result. This means that all messages related to departments, instead of one specific record returning one message (it has more than 10), I would expect. Removing the scope parameter shows that departments do have many messages each.
(I know that URL parameters with all these special characters, such as {",:"}, must be encoded at the URL. I leave it clean for better readability)
My question is:
How to execute this request with a single request?