-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include related models with a custom scope #3453
Comments
Was trying to do a filter query include: [
{ relation: 'relatedModel', scope: { include:[{ relation: 'relatedRelatedModel' }] } },
] but got shut down by 'scope not supported' error. I see it's written in the type definitions though, is this just a bad package version on my part perhaps? Or is scope within a relation actually not hashed out completely yet? How should we be handling nested relationships? ie: I have a source base model w/ a hasmany to a mapping model, which has a hasmany mapping to another target model. Should I just custom spin a controller method to do it all manually for now? It would be great to just use the inclusion resolver like this. edit ---- right I see in the source it's not implemented, my bad. I'll spin up a custom method to link it all together for now. |
I have the same issue and need to query nested relations... |
i need inclusion in inclusion toooo. @luncht1me |
Me too ! :) |
Would really like to see this added. |
Is too hard use lb4 without nested include relations.. mostly if you came from lb3.. |
Do you can show to us what you did to get around this problem? |
@bajtos do we want to support all query clauses |
Including the where in this scope would greatly help us out. While we don't need it for our current implementation (admin users can see everything), we will need to start filtering our related model results in the coming few months and having the ability to scope/where would be huge. It would be a shame to have to drop to a direct SQL implementation. |
@agnes512 I believe this story only supports |
+1 We also need the same. When can we expect this ? I see its in December milestone. Is it correct ?
|
@samarpanB @mamiller93, we acknowledged the importance of having a custom scope. Thanks for your input! |
I see two sub-features here: (1) Filter which included models are returned, see the original issue description: filter.include = [{
relation: 'products',
scope: {
where: {active: true},
limit: 1
}
}] (2) Recursive include (include models related to related models), as described in #3453 (comment): filter.include = [{
relation: 'relatedModel',
scope: {
include: [{
relation: 'relatedRelatedModel'
}]
},
}] Ideally, we should support both flavors and also a combination of them. I think this should be actually easy to achieve, all we need to do is pass If my gut feeling is wrong, then we can pick first the option that is easier to implement and then decide what to do about the rest. |
closing as done |
Hello. Can you please tell us the syntax for nested relations for url. I have tried something like this but it is giving error 500.
I have three models. Users have has-many carts and carts belongs-to product. |
Try incrementing the include filter as follows:
|
Still not working. |
In that case you'll probably have to provide an example repo reproducing the problem so someone can take a look for you. |
I have posted a question on stack overflow https://stackoverflow.com/questions/59435371/loopback-4-include-nested-relations |
@bajtos @agnes512 thank you for the great work. Examples: await exampleModelRepository.find({ include: [{ relation: "user", scope: {fields: { username: false } } }] } ) This one doesn't work. Returns the included model's parent property as undefined. Obviously, the intended behavior is to only show the username: await exampleModelRepository.find({ include: [{ relation: "user", scope: {fields: { username: true } } }] } ) |
@pratikjaiswal15 hi, could you try to encode your filter with
then do Sorry that we haven't found a proper way to query the nested relations with square brackets url format. |
@shadyanwar I just tried it out and reproduced it. For example, for hasMany relation,
Similarly, for belongsTo relation, an
I think our |
@agnes512 No problem. Thank you.
No problem. Thank you |
Hi, I'm wondering if the 'limit' param works if added to the scope of nested relations, to limit the number of items of a hasMany relation. I have a strange behaviour in the response.
I want to get the businessPartners with only the first address for each one. My filter param looks like
The where clause in the scope of the include works well, but the limit: 1 param returns the address for only one businessPartner, not one for each businessPartner. If I increase the value of the limit, e.g. 10, the response returns max 10 address, differently distribuited on the businessPartners. Am I wrong in something? Is this the intended behaviour? I'm using a postgresql connector and this is my ecosystem (Nodejs v12.16.2 npm v6.14.4 - Postresql 12)
Thank you very much |
Besides specifying the relation name to include, it's also possible to specify additional
scope
constraints:order
,limit
,skip
andfields
scope.where
constraintFor example, the following filter will include only the first active product:
The initial version of Inclusion does not support custom scopes. In this story, we should improve
findByForeignKeys
function to support additional scope constraints.LB3 test suite: loopback-datasource-juggler/test/include.test.js#L247-L253)
See also #3387
Acceptance criteria
The text was updated successfully, but these errors were encountered: