-
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
Fix: hasManyThrough inclusion performance #8251
Fix: hasManyThrough inclusion performance #8251
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes LGTM; If possible, I'd like another maintainer's approval before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just some minor comments
@@ -185,19 +231,27 @@ export function hasManyThroughInclusionResolverAcceptance( | |||
expect(toJSON(result)).to.deepEqual(toJSON(expected)); | |||
}); | |||
|
|||
it('honours limit scope when returning a model', async () => { | |||
it('honours limit and order scope when returning a model', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question - how is this test showing it's honouring the order
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 3 customers of which only 2 are selected due to the limit
. The two that are expected to be returned are Zelda and Voldemort (in that order). Since that order does not correlate to either the creation sequence of the customers nor the linking sequence of the carts to the customers, it can only be explained by successful usage of the order
clause. (Although it could technically be caused by unexpected random order returned by the memory connnector ..)
Perhaps I should add a comment with this explanation to the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed my response was about a different section of the changeset.
In this test, I added an order clause which should put the second cart item (shield) as the first of the included cart items. The last expect
tests whether that is the case.
|
||
// get target ids from the through entities by foreign key | ||
const allIds = _.uniq( | ||
throughResult.map(x => x?.map(entity => entity[throughKeyTo])).flat(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: would it be possible to use descriptive variables instead of x
for here and below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change x
to throughEntity
.
const pickFieldList = Array.isArray(scope?.fields) | ||
? scope?.fields | ||
: undefined; | ||
const omitFieldList = scope?.fields |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍 I think it might be nice to include a comment explaining what this does, but not necessary.
- scope.fields is
undefined
-> [] - scope.fields is an array of properties e.g. ["id", "name"] -> []
- scope.fields is an object = {id: true, name: false} -> ['name']
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a comment or two.
Questions about updating the PR:
|
During the review process, it is easier for us if you do separate commits to address comments so we can see the difference. But since this is a small PR, you can just squash it right away to the relevant commit.
I would recommend rebasing, but the final commits will be rebased when it's merged even if you don't rebase locally. Here's some docs for reference https://loopback.io/doc/en/lb4/submitting_a_pr.html#5-pr-review-process |
6b050bd
to
d84bc11
Compare
Finally gotten around to update this PR. I did rebase and squash however, so I'll describe the changes: Next to adding several comments and renaming some variables based on the review so far, there is one bigger change: instead of manually building a |
@0x0aNL Can you please rebase this PR so that we can revive this PR and merge it then ? |
d84bc11
to
d47e056
Compare
I have rebased the PR! |
Signed-off-by: Roderik van Heijst <rvanheijst@0x0a.nl>
…ation inclusion Signed-off-by: Roderik van Heijst <rvanheijst@0x0a.nl>
d47e056
to
765157c
Compare
Rebased once again and fixed performance in cases where no |
Pull Request Test Coverage Report for Build 5750231698
💛 - Coveralls |
It seems like this PR has been approved for a while. Merging it now. @0x0aNL, thanks for your contribution. Your PR has been landed! 🎉 |
Fixes #8250
As described in my proposal in #8250, this PR changes the hasManyThrough inclusion resolver from performing at least one query per through model, to performing a single query per inclusion level.
Changes in this PR:
hasManyThrough inclusion resolver:
findByForeignKeys
callrepository-tests:
CartItem
toCustomer
(I wrote this test to test the scope magic. Additionally, the current hasManyThrough relation test uses
UserLink
withUser
<>User
, but I found sometimes that issues occur when includingA
<>B
<>A
instead ofA
<>A
- see Regression: secondary HasManyThrough inclusion fails with circular dependency #7599. )Possible issues:
limit
andfields
are emulated and as such may not work properly or keep working properlylimit
andfields
, even if properly emulated, are enough to satisfy allscope
cases, despite all tests passingNotes:
Item
in an inclusion of depth 3 with bi-directional hasManyThrough (e.g., when findingItem
with relationCategory
with relationItem
, allItems
will be queried twice)Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated