I want to implement many to many relation in loopback4 but I couldn't see the article or any documentation regarding many to many relation
Currently it is apparently not supported (only 1-to-many [hasMany] and the reciprocal relation [belongsTo] (see https://loopback.io/doc/en/lb4/Relations.html) but I have a similar need and I'd like to know (@raymondfeng) either for when it is planned on the roadmap and/or if it is possible to contribute to speed things up (and, if so, how to contribute).
Description / Steps to reproduce / Feature proposal
I want to implement many to many relation in loopback4 but I couldn't see the article or any documentation regarding many to many relation
+1
Thanks for the feedback! It seems like there are more users who are looking for the other model relation types than what we have so far. As you might've known already, support of hasOne https://github.com/strongloop/loopback-next/issues/1422 is in progress. Let me discuss with @raymondfeng and team and see if there's some document to get others started on adding more relation types.
cc @RaphaelDrai @elv1s
Hi all, thanks for your interest in contributing to more relation types! Would like to see who has a particular interest or need for a certain relation type. Not sure what's the best way to do a poll here, I'm going to add one comment for each relation type below, and if you would like to contribute or need it for your project, please upvote. Thanks!
Here are the outstanding relation types:
HasManyThrough
HasAndBelongsToMany
Polymorphic
EmbedsOne
EmbedsMany
ReferencesMany
cc @strongloop/loopback-next @strongloop/loopback-maintainers
HasManyThrough first but interested in other ones too
Le mer. 21 nov. 2018 à 15:51, Diana Lau notifications@github.com a écrit :
cc @strongloop/loopback-next
https://github.com/orgs/strongloop/teams/loopback-next
@strongloop/loopback-maintainers
https://github.com/orgs/strongloop/teams/loopback-maintainers—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/strongloop/loopback-next/issues/2043#issuecomment-440690172,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACqwzcLwquCODmr0Jyh0bDIsRG_u0JTxks5uxWhsgaJpZM4YmxSz
.
HasManyThrough, HasAndBelongsToMany.
Flick-the-switch type of Auto Update would be very nice. One that adds foreign keys automatically (which LB3 never could do), with cascade delete/restrict.
How can I contribute to the manyToMany ??? I really need it.
@b-admike @bajtos , what do you think is the best way to let others join forces? some content around how to get started or how the code are being organized? Thanks.
I am proposing to keep this issue focused on "many to many" relation, and discuss other relation types in _[EPIC] More Relations (post-GA) #1450_.
From what I remember from LB3, "many to many" relation is called HasAndBelongsToMany and leverages HasManyThrough under the hood.
So I think the first step is to figure out how to implement a "hasManyThrough" relation.
In my experience so far, adding a new relation type has always been a challenge. It took multiple iterations until we were able to find a good design and a robust implementation. I expect that hasManyThrough will be similar.
In general, each relation type needs the following artifacts:
A repository interface & implementation providing access to related model instances, constraining the target models to enforce the relation. See e.g. has-many.repository.ts
A decorator (typically for model properties) to specify relation metadata. See e.g. has-many.decorator.ts.
A repository factory that accepts the value of the foreign key and returns back the relation repository. See e.g. HasManyRepositoryFactory. https://github.com/strongloop/loopback-next/blob/f015845f60649bddfa22bdd364154ebcf029a37b/packages/repository/src/relations/has-many/has-many-repository.factory.ts#L21-L23
A function to create repository factory from relation metadata. See e.g. createHasManyRepositoryFactory. https://github.com/strongloop/loopback-next/blob/f015845f60649bddfa22bdd364154ebcf029a37b/packages/repository/src/relations/has-many/has-many-repository.factory.ts#L38-L57
A protected helper method in DefaultCrudRepository making it easier to setup repository factory for a named relation. See e.g. _createHasManyRepositoryFactoryFor. https://github.com/strongloop/loopback-next/blob/f015845f60649bddfa22bdd364154ebcf029a37b/packages/repository/src/repositories/legacy-juggler-bridge.ts#L172-L185
I think the steps 2-5 should be relatively straightforward because they will mostly copy the design we already have in place for "hasMany" relation. I am expecting the first step to be most challenging, as we need to find out how to implement data access operations leveraging a "through" table.
Process wise, I am proposing to start with a spike on the first step. Start designing a Repository interface and implementation that will provide the following APIs:
Use integration tests to test the implementation. See relation.factory.integration.ts for inspiration, but please start a new file instead.
Open a pull request as soon as you have the first method ("create"?) implemented, so that we can discuss the design & implementation early on, while there is little code written and changes are easy to make.
Thanks @bajtos, I'll see what I can do.
Hello @bajtos,
I am happy to contribute with the attached design document related to the implementation of hasAndBelongsToMany relation in LB4.
I am looking forward for your kind review, comments of the design document and the directives how to make progress of this document before starting the implementation contribution.
I'll be mostly away from my computer until the end of the year, I'll take a look in January. Merry Christmas! 🎄
Merry Christmas!
Hello @bajtos,
It is important for us to have in LB4 the hasAndBelongsToMany (the direct many-to-many relation) that we are using in our LB3 application. We are actually in a transition process to move our LB3 application to LB4 and hasAndBelongsToMany is considered as an high priority.
May I suggest to have a new and separated case for the hasManyThrough in order to avoid confusion with this current case?
Hi @bajtos,
Is it possible to you to get your comment/advice on the design document that I sent one month ago?
Thanks,
Raphael
@dhmlau, @raymondfeng , @gczobel-f5, @bajtos
Raymond Feng [6:56 PM]
gczobel [7:21 PM]
bajtos [11:11 AM]
I think LB4 should support both options:
Raphael Drai [10:58 AM]
Ok, I will start first with with option 2 "Advanced". I will make change in the design document that I sent then I will re-send it for review before starting the development. Thanks a lot.
For hasAndBelongsToMany relation type, please continue the discussion at #2308.
I hope this is an appropriate place to post this question.... Is there a workaround for implementing many-to-many relationships in Loopback 4.? I think there must be since just about any significant database model will contain some number of many-to-many relationships. Without official support or a good workaround, I doubt that Loopback 4 would have been deemed ready to release otherwise. Thanks
The HasManyThrough pull request is currently functional. There's still more work to do on it to make it more efficient with SQL, but it does work.
https://github.com/strongloop/loopback-next/pull/2359
The code for the pull request can be found at the following link.
https://github.com/codejamninja/loopback-next/tree/codejamninja/has-many-through-using-has-many
You can install it by running the following . . .
npm install --save https://github.com/codejamninja/loopback-next/releases/download/%40loopback%2Frepository-has-many-through-using-has-many/loopback-repository-has-many-through-using-has-many-1.2.1.tgz
. . . or by adding the following to the dependencies section in your package.json
"@loopback/repository": "https://github.com/codejamninja/loopback-next/releases/download/%40loopback%2Frepository-has-many-through-using-has-many/loopback-repository-has-many-through-using-has-many-1.2.1.tgz"
You can see an example of how to set it up and use it in the fixtures test.
For hasManyThrough, you have to explicitly define your own join (junction) table. It's very similar to the way it works in loopback 3. You can read more about it at the following link.
https://loopback.io/doc/en/lb3/HasManyThrough-relations.html
In the example I provided, the junction table is defined by the Order model. (Notice the two @belongsTo decorators.)
Also, notice the way Customers and Sellers are linked together through an Order with the @hasMany decorator.
Since this feature has not been accepted into master yet, please let me know if you have any questions or bugs.
Like existing relationships in Loopback 4, you will need to wire it together in the repository.
For the hasManyThrough relationship, you use the HasManyThroughRepositoryFactory. You can see how sellers are linked to customers in the customer repository at the links below.
@codejamninja great work!
Is it possible to create a repository with a simple example of 3 models implementing a many-to-many relationship?
Thank!
Yes, absolutely. Look at the example below.
https://github.com/codejamninja/medical-practice-api
Currently, the physician repository isn't complete, but to complete it, it would simply be implemented in an identical fashion to the patient repository.
Feel free to submit a pull request to finish the patient repository.
Yes, absolutely. Look at the example below.
https://github.com/codejamninja/medical-practice-api
Currently, the physician repository isn't complete, but to complete it, it would simply be implemented in an identical fashion to the patient repository.
Feel free to submit a pull request to finish the patient repository.
Hi @codejamninja , I'm using your example with relational databases and I notice that when I generate the database schema, the many-to-many relationship is not generated with foreignkey, there is some way in which this relationship is defined when migrating the schema ?
Any dates on when will this be merged?
@dakt I have to go through the comments and make some changes before the team can merge it. I will hopefully get around to it sometime this month. I have a feeling there will probably be around 3 pull requests before hasAndBelongsToMany is working the way it is intended to work.
is loopback 4 not ready for production use? it still seems to be missing this
@AzeemMichael if your application needs a many to many relationship, then yes it's probably not production ready. However I have built several loopback 4 apps that are in production that do not use the many to many relationship.
I find loopback 4 is one the greatest frameworks, however the many to many relation (HasManyThrough) is very essential to most of the work i intend to use loopback for, hope this be released soon.
Hello guys !
Do you know when this feature is going to be available in the framework ? thanks in advance.
This issue has been marked stale because it has not seen activity within six months. 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. This issue will be closed within 30 days of being stale.
Most helpful comment