-
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
feat(repository): adding hasManyThrough (through model) to hasMany and its helpers #5354
Conversation
packages/repository/src/relations/has-many/has-many-through.helper.ts
Outdated
Show resolved
Hide resolved
29a7602
to
1500021
Compare
dbf9cbd
to
03c2547
Compare
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.
@agnes512 Appreciate your effort for helping on move the PR forward! Nice start 👍
.../src/__tests__/unit/repositories/relations-helpers/resolve-has-many-through-metadata.unit.ts
Outdated
Show resolved
Hide resolved
packages/repository/src/relations/has-many/has-many-through.helpers.ts
Outdated
Show resolved
Hide resolved
>( | ||
relationMeta: HasManyThroughResolvedDefinition, | ||
fkValue: ForeignKeyType, | ||
targetInstance?: Target, |
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.
hmm, why the 3rd parameter is a target instance instead of id/ids?
IIUC, usually hasManyThrough works in this way:
- e.g. Find all products belong to a category by
category1.products()
- the through constraint will be
{categoryId: 1}
- all
categoryProducts
withcategoryId = 1
are found, generate a collection of target ids according to records'productId
- then
Products.find(id: {inq: [1,2,3]})
If the product instance is already known, why would people search for it?
If the constraint is to perform PATCH/PUT/DELETE, why it doesn't take in id/ids
but one(and only one) instance?
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 was following 2359 and 4438. And I agree that createThroughConstraint
should only take in Through models and resolved relationMatadata and only generate {categoryId: 1}
as the constraint. I will fix this first.
Besides, as @bajtos mentioned in comment #2359 (comment), we should improve the process by converting the constraint to a SQL JOIN query (quote from the comment):
SELECT target.*
FROM CategoryProductLink as through
LEFT JOIN Product as target ON through.productId = target.id
# this condition is built from "through" constraint
WHERE through.categoryId = 1
# this condition is built from "target" constraint
AND target.country = 'us';
I don't know if this is doable with current connectors, but hopefully it is achievable with function createThroughConstraint
in the future as it has all of the pieces to build such queries.
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 agree with the discussion in #2359 (comment) 👍
A good summary is here #2359 (comment)
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 quickly skimmed through the proposed changes, they look pretty good 👍
The code coverage has decreased. AFAICT, unit tests are not covering all error paths in has-many-through.helpers.ts
, see https://coveralls.io/builds/30686736/source?filename=packages/repository/src/relations/has-many/has-many-through.helpers.ts Could you PTAL?
I'll leave the detailed review and final approvals to CODEOWNERS of repository & relations.
packages/repository/src/relations/has-many/has-many-through.helpers.ts
Outdated
Show resolved
Hide resolved
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 start! LGTM
Extract hasManyThrough relation definition and its helpers from #4438
Adding unit tests for
hasManyThrough.help
Changes:
HasManyDefinition
and get rid ofhasManyThroughDefinition
.Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈