Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Peck 690 posts 1896 karma points c-trib
    Mar 10, 2023 @ 16:26
    David Peck
    0

    Many to many look-up table support

    I'm looking at the best way to handle a many to many relationship between entities in two different collections. I want to model this in the DB with a junction table. Obviously I could create a collection specifically for the junction table but ... yuk.

    The best solutions that I can think of is with a custom repository or it might be possible simply by listening to save events?

    I find it hard to imagine that I'm the first to have this need any advice, anecdotal or otherwise, is appreciated.

  • sarafoster 1 post 40 karma points
    Mar 11, 2023 @ 11:47
    sarafoster
    100

    Creating a junction table to model a many-to-many relationship between entities is a common and effective approach. You can create the junction table in the database and map it to your domain model using an ORM (Object-Relational Mapping) tool.

    Regarding your question about the best way to handle this, it really depends on the specific requirements of your application and the technologies you are using.

    If you are using an ORM, you can create a custom repository or use an existing repository to handle the CRUD operations for the junction table. The repository can encapsulate the logic for adding and removing relationships between entities. You can also define methods in the repository to query for entities based on their relationships.

    If you are not using an ORM, you can listen to save events and perform the necessary operations to update the junction table manually. For example, if you are using a MongoDB database and the Mongoose library, you can listen to the 'save' event and update the junction collection using a pre-save hook.

    In general, the best approach is to choose the solution that is most maintainable and scalable for your application. If you have a lot of entities and relationships, a custom repository might be more appropriate. If your application is simpler, listening to save events might be a good option.

  • David Peck 690 posts 1896 karma points c-trib
    Mar 12, 2023 @ 07:33
    David Peck
    0

    Ta. I’m just using NPoco. EF Core would be a good fit, but I’m don’t see the need to add another library when NPoco comes with.

    I’ll use a custom repository. The more I think about it events would be too hacky.

Please Sign in or register to post replies

Write your reply to:

Draft