-
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: adding polymorphic relations support for models #8026
Conversation
Pull Request Test Coverage Report for Build 1670301030Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Thanks for your contributions, @OnTheThirdDay! The maintainers will try to find time to thoroughly review it; In the mean time, feel reach out here on the |
Ahh sorry I think I should check out a new branch before fetching from origin repo to my forked repo... Should we cancel this another workflow job or just let it run? |
No worries; When incorporating changes from master, we'd use So in this case, could you drop the merge commit, and rebase against master?
I've went ahead and executed the workflow jobs; After your first PR is merged into this repo, future PRs won't require manual approval. Generally, we encourage users to push as often as they need to test their changes as needed. For testing individual packages locally, we can run: npx lerna run --scope='@loopback/repository' test This can be helpful if you want to quickly test changes without running the entire test suite. The different ways lerna can be used to scope packages and dependencies can be found here: https://www.npmjs.com/package/@lerna/filter-options |
Thanks for the information. I have rebased it. |
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.
Really sorry that I couldn't review this earlier. I've checked the PR and there doesn't seem to be any issue with the code and tests. I've pinged the other maintainers to get their input.
Cool. If you find that the current implementation is ok, I will update the doc/cli/example, and add the feature to support polymorphic types search in inclusion filter. |
Previously(the initial commit to support polymorphic types), if an included relation points to a polymorphic type, then the repository only sees it as the base type, so that deeper inclusion is not supported. This commit tries to support a finer-grained inclusion filter by specifying
|
I am half way updating the cli, but I am recently too busy to finish it soon. How about I update the docs first after you finish reviewing the code, and later add the support for cli? The change will not break the current usage. |
Sorry for the delay on getting back on this (Most maintainers are catching up with their own December backlog); I've pinged them and there wasn't any objection to this PR so we should be good to merge. |
Cool. I will update the doc later. btw, should it be a single section for polymorphic types or mention this feature in individual relations? |
It should be it's own page, similar to the other relations. |
@OnTheThirdDay Hoping to find some time over the next week or so to review this whole feature and the PR and see how it has come together. :) Also... could you and I get a little chat going via email? rtaylor.info (at) gmail Some cool things to chat about as well. |
can this be merged now? |
@lygstate Code-wise I think so, but haven't finish the documentation... Edit: Just added the documentation. |
I have added the documentation. I feel this feature is very straightforward. @achrinza Please have a check. |
The docs LGTM 👍 Assuming there's no test failures, the last step is to squash it into a single commit. If you'd like to continue the work in adding CLI scaffolding support, we can start a new PR after this is merged. |
@achrinza Done :) |
Addressing the feature parity of polymorphic relations from lb3, and the syntax is similar to lb3. The changes are backward compatible with previous apis. hasOne, belongsTo, and hasManyThrough relations are supported, while hasMany relation can only facilitate this feature with an additional one-on-one relation. Related to #2487 Signed-off-by: David Zhang <[email protected]>
Thanks for your contributions, @OnTheThirdDay! Your PR has been merged 🎉 |
Hi everyone. We have a difference betwen belongsTo and hasOne/hasMany in the semantic. BelongsTo adds the foreignKey on the source where hasOne/hasMany will look it on the target. Thats why we can resolve relations in both direction. Relations design came from LB3 is a great reference. But as I undestood (only for the polymorphic version of belongTo) now we have a hasOne decorator that adds foreignKey and foreignType to the source instead of looking into the target. Is it right? |
Hi @mikeevstropov, the semantics of the Some examples can be found in the docs: https://loopback.io/doc/en/lb4/Polymorphic-relation.html Edit: #8667 indicates this may not be the case; Will confirm this in that issue. |
@achrinza Hi! Thank you for reply. As you said, example shows hasOne decorator in the same model with discriminator, and it looks weird. Just to be clear, previously, hasOne/hasMany may be applied to the model B only if the model A has foreignKey and discriminator defined by BelongsTo, because hasOne/hasMany is used to resolve relation in the opposite direction only. |
Hi, in LB4 the polymorphism is extended to the owners. In LB3, A belongsTo B, B hasOne A, the B is a polymorphic type(imageable either Product or Employee); while in LB4, either A or B can be polymorphic. |
@OnTheThirdDay Hi! If so, do we have the diffirence between belongsTo and hasOne? If no, how to resolve relation in the opposite direction (get Delivery from Letter or Parcel)? In lb3 you are using hasOne/hasMany for that. |
Hi, the doc is currently not showing it(because I wanted to show the difference between a normal relation and polymorphic relation, but might've caused confusing) -- Letter or Parcel have a deliveryId field. Here is an example https://github.com/OnTheThirdDay/lb4-polymorphic-example
|
Okay, got it. New feature of hasOne adds discriminator to the source and fk to the target and this approach is not matched to relations design of LB2/3. Based on this, the relation between hasOne/hasMany <-> belongsTo is violated. Also we can't migrate to LB4 with polymorphic feature without change the data, because previously the discriminator and fk was at the same model. Guys, do the LB4 trying to save backward compatible with the previous version? |
Hi, I am not sure if I am getting it right -- do you mean in LB2/3 A belongsTo B(superclass of C and D), discriminator is on A? I think this is identical to the LB4 implementation. For fk, I think we still have keyTo on A. |
BelongsTo adds a foreignKey to the source. Polymorphic belongsTo adds a foreignKey and discriminator to the source. HasOne/hasMany is used on the “other side” of a belongsTo relation. Polymorphic belongsTo adds
Picture with related Employee. {
"id": "1-picture",
"imageableId": "1-employee",
"imageableType": "Employee"
"imageable": {
"id": "1-employee",
"name": "Miroslav Bajtoš"
}
} Polymorphic hasOne/hasMany looks at the
Employee with related Picture. {
"id": "1-employee",
"name": "Miroslav Bajtoš"
"picture": {
"id": "1-picture",
"imageableId": "1-employee",
"imageableType": "Employee"
}
}, HasOne relation is a degenerate case of a hasMany. Does the new inplementation of polymorphic hasOne resolves the target by foreignKey and discriminator stored in target? Do we have polymorphic belongsTo to store a foreignKey and discriminator at source? |
I think we are not adding new fields for hasOne and hasMany when they are only served as the opposite direction of a polymorphic belongsTo. |
@OnTheThirdDay Okay, does the new inplementation of polymorphic hasOne resolves the target by foreignKey and discriminator stored in target? |
I think currently the discriminator is not checked, which may cause some problem. I will write a fix. |
Polymorphic hasOne/hasMany were designed only to resolve opposite direction, i guess |
Sorry that I am too busy recently, so still not being able to find time to make a fix to this issue. Hopefully I can find some time in the coming few months. Also happy to assist if anyone's willing to pick it up. |
…ions The `options` object will often have a `transaction`, which contains database connection resources that must not be cloned. Fixes #10194, introduced by #8026 Signed-off-by: Matthew Gabeler-Lee <[email protected]>
Addressing the feature parity of polymorphic relations from lb3, and the syntax is similar to lb3. The changes are backward compatible with the previous APIs. hasOne, belongsTo, and hasManyThrough relations are supported, while hasMany relation cannot facilitate this feature without a through model.
packages repository and repository-tests are updated.
related to #2487
Signed-off-by: David Zhang [email protected]
Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈
API changes:
defining polymorphic models
define the polymorphic models
define the polymorphic property
or a customized discriminator
setting up repository
Query with inclusions
Creation with polymorphic relations