How to restrict relationship records with filter enabled in loopback?

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://loopback-server:3000/api/departments?filter[include]=members&filter[include]=messages 

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?

+5
source share
2 answers

It is not possible to query relationships by their properties (yet). As for the limit, your last scope approach should be slightly modified:

 "scope":{{"include":{"relation": "messages","limit":1, "skip":0}}} 

Here you can read about relationship queries by their properties:

https://github.com/strongloop/loopback/issues/517

+2
source

I don’t know which version you are in, but for Loopback 3

can you do this.

 include: { { relation: 'Messages', // include the messages object scope: { // this is where you do a normal filter where: {<whatevercondition>}, order: "<fieldname> <ASC/DESC>", limit:1, include:{ //yes, you can include 3rd level relation as well. } } }, { relation: 'Members', // include the Members object scope: { // further filter the related model order: "<fieldname> <ASC/DESC>", limit: <whateverlimityoument> } } } 
+1
source

Source: https://habr.com/ru/post/1264558/


All Articles