Skip to content
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

Count issue with related models using though model #1795 #1801

Closed
wants to merge 1 commit into from

Conversation

regevbr
Copy link

@regevbr regevbr commented Nov 30, 2019

Fixes #1795 - fixed wrong behaviour of count, findOne, updateAll, destroyAll on related models that use through models.

Fixes #1796 - Optimized the bloated query being used on through models when performing find, making it more efficient

Added optimization when querying related models using the id in the filter, to make the call to the through model more efficient.

Applied the fix from strongloop/loopback/issues/1076 to the explicit call of find on a related model

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Commit messages are following our guidelines

@slnode
Copy link

slnode commented Nov 30, 2019

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@regevbr
Copy link
Author

regevbr commented Nov 30, 2019

@bajtos can you please review?
I added 4 tests that failed before the fix (TDD style) and fixed all problematic functions - count,findOne,updateAll and destroyAll

@regevbr
Copy link
Author

regevbr commented Nov 30, 2019

@bajtos I combined here the fix of #1796 as well and they are closely related and they use the same logic.

@dhmlau
Copy link
Member

dhmlau commented Dec 2, 2019

@slnode test please

@dhmlau dhmlau added the community-contribution Patches contributed by community label Dec 2, 2019
@bajtos
Copy link
Member

bajtos commented Dec 5, 2019

Thank you @regevbr for the pull request. This is a large change, I need to set aside some time to review it in detail. I'll try to get it done by the end of next week, before I leave for vacation.

@raymondfeng can you please take a look too?

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great that you have added tests too 👏

We are in trying to slowly move our test suite from callback/promise style to async/await. Please use that new style in new tests you are writing, see my comments below.

test/relations.test.js Outdated Show resolved Hide resolved
test/relations.test.js Outdated Show resolved Hide resolved
@regevbr
Copy link
Author

regevbr commented Dec 5, 2019

@bajtos sure I will try to get on it soon, I have a lot on my plate :-)

@regevbr
Copy link
Author

regevbr commented Dec 7, 2019

@bajtos I fixed the tests (which actually got me to find bugs I introduced so I fixed them as well)

@bajtos
Copy link
Member

bajtos commented Dec 13, 2019

Thank you @regevbr for the updates, the tests look much better now!

I am afraid I got stuck in other tasks and did not find enough time to review your changes. If nobody from @strongloop/loopback-3x-maintainers and/or @strongloop/loopback-maintainers steps up to review this pull request, then I am afraid you will have to wait until January.

Sorry for the delay 😢

@bajtos
Copy link
Member

bajtos commented Dec 13, 2019

@slnode test please

@bajtos
Copy link
Member

bajtos commented Jan 17, 2020

Sorry @regevbr for the long silence on our side. The pull request is quite involved, I find it difficult to find enough time (and energy) to dive into the changes. Is there any chance to split this patch into smaller pull requests, where each pull request would be a meaningful change on its own? The smaller the patch, the easier it is to review for us.

Copy link
Contributor

@agnes512 agnes512 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution 💪 LB3 is a bit different from LB4 so it took me a while to review the PR. I left some comments.

And if we can improve the PR description with the high-level idea of the changes, it would help us lot with reviewing it. Thanks!

patients.should.have.lengthOf(0);
});

it('should find scoped record with promises based on related model id', async function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some redundant tests. For example, this test, find , and find explicit look the same to me.

If just to keep one test, I think should find scoped record with promises based on related model id is the best title cause the change is related to scope.

Same for the rest tests.

}

return definition;
}

function smartMergeRelatedModelQuery(findData, queryRelated, keyFrom, IdKey) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add API documentation for this function.

Comment on lines +424 to +446
let scopeOnRelatedModel = false;
let queryRelated, keyFrom, relatedModel, IdKey, fieldsRelated;
if (this._scope && this._scope.collect &&
where !== null && typeof where === 'object') {
queryRelated = {
relation: this._scope.collect,
scope: {
where: where,
},
};
scopeOnRelatedModel = true;
relatedModel = targetModel.relations[queryRelated.relation].modelTo;
IdKey = idName(relatedModel);
keyFrom = targetModel.relations[queryRelated.relation].keyFrom || IdKey;
fieldsRelated = [keyFrom];
if (where[IdKey]) {
where = {
[keyFrom]: where[IdKey],
};
} else {
where = {};
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found repeated code in these CRUD methods. Maybe we can refactor them to have a helper function.

@stale
Copy link

stale bot commented Mar 17, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 17, 2020
@stale
Copy link

stale bot commented Mar 31, 2020

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Patches contributed by community stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Querying related models make unnecessary requests Count issue with related models using though model
6 participants