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

fix: Apply filters from relations to query #207

Merged
merged 6 commits into from
Dec 6, 2023

Conversation

cbjjensen
Copy link
Contributor

No description provided.

@cbjjensen
Copy link
Contributor Author

cbjjensen commented Dec 4, 2023

This change allows us to pass filters to the relatinoships of the query.

Sample usage:

        query.relations[usersAssociationTable].query = {
          relations: [{
            name: 'user', query: {
              filter: {
                uuid: { eq: 'e405f151-bd78-4f61-b7e9-23ca4b89b6c2' }
              },
            }
          }]

        } as any

Output SQL

SELECT "Table"."id", "Table"."some_identifier"
FROM myTable "Table"
LEFT JOIN "public"."user" "user" ON "user"."id"="users_association"."user_id"   
LEFT JOIN "public"."users_association" "users_association" ON "users_association"."some_identifier"="Table"."some_identifier"   
WHERE ("user"."uuid" = $1);

 -- PARAMETERS: ["e405f151-bd78-4f61-b7e9-23ca4b89b6c2"]

Previously, the relations' filters would not be passed down, and would result in the below query instead:

SELECT "Table"."id", "Table"."some_identifier"
FROM myTable "Table"
LEFT JOIN "public"."user" "user" ON "user"."id"="users_association"."user_id"   
LEFT JOIN "public"."users_association" "users_association" ON "users_association"."some_identifier"="Table"."some_identifier";

It appears that Doug left a comment in the code indicating that this was a missing feature, so i've removed it, and addressed it by calling applyFilters from the applyRelationJoinsRecursive logic.

@cbjjensen cbjjensen marked this pull request as ready for review December 4, 2023 21:34
@TriPSs
Copy link
Owner

TriPSs commented Dec 5, 2023

Shit, this is a good one. Thanks!

It's true that the filters where not applied, mainly also when implementing the lookahead this is something that could never be passed down by the query itself so there was no need yet to implement that.

@TriPSs
Copy link
Owner

TriPSs commented Dec 5, 2023

Could you check the linting and please use conventional commits for the changelog.

There is also no need to bump the package.json :)

name: 'oneTestRelation',
query: {
filter: {
numberType: { eq: 123 }
Copy link
Owner

Choose a reason for hiding this comment

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

Is it me or don't I see this one back inside the snapshot?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it looks like you're right. I'll try to fix. Although it looks like some of the other where filters are not being built in the snapshots for even some of the other things unrelated to this change 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

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

comments have been addressed: 0ec70e2

In this commit, I address the issue where relations filters were not being applied. Also adding tests.
@cbjjensen cbjjensen changed the title address filtering relations comment fix: Apply filters from relations to query Dec 5, 2023
@cbjjensen
Copy link
Contributor Author

Could you check the linting and please use conventional commits for the changelog.

There is also no need to bump the package.json :)

I've no idea what i'm doing, in regards to commit history, haha. let me know if it needs to be cleaned up. Usually i'm the type of person that does test, test, oops, test, test, fixed a thing, so this styling is new to me!

@cbjjensen cbjjensen requested a review from TriPSs December 5, 2023 22:17
@@ -121,6 +130,22 @@ describe('RelationQueryBuilder', (): void => {
const query: Query<TestRelation> = { filter: { relationName: { eq: 'foo' } } }
expectSQLSnapshot(TestEntity, testEntity, 'testRelations', query)
})
it('should apply filtering from relations query filter', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I move this to filter-query tests?

Copy link
Owner

Choose a reason for hiding this comment

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

Yea let's do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

55fa165 addressed!

@TriPSs
Copy link
Owner

TriPSs commented Dec 6, 2023

Thanks for your work on this already 🔥 !

@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (3ceb52d) 79.15% compared to head (55fa165) 86.28%.
Report is 1 commits behind head on master.

Files Patch % Lines
...es/query-typeorm/src/query/filter-query.builder.ts 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #207      +/-   ##
==========================================
+ Coverage   79.15%   86.28%   +7.13%     
==========================================
  Files         688      688              
  Lines        9748     9750       +2     
  Branches      857      858       +1     
==========================================
+ Hits         7716     8413     +697     
+ Misses       1288      619     -669     
+ Partials      744      718      -26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cbjjensen
Copy link
Contributor Author

@TriPSs this is ready for your approval. As a side question - once this is merged, will it auto increment the version in package json ? If not, when/how do i pull down the new version with the changes

@TriPSs
Copy link
Owner

TriPSs commented Dec 6, 2023

@TriPSs this is ready for your approval. As a side question - once this is merged, will it auto increment the version in package json ? If not, when/how do i pull down the new version with the changes

The CI will bump versions and release it.

@TriPSs TriPSs merged commit ba9456c into TriPSs:master Dec 6, 2023
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants