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

Support direct relationships targeting intermediate resources #3

Open
joelalejandro opened this issue Nov 24, 2020 · 3 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@joelalejandro
Copy link
Member

No description provided.

@joelalejandro joelalejandro added the enhancement New feature or request label Nov 24, 2020
@joelalejandro joelalejandro self-assigned this Nov 24, 2020
@kristian240
Copy link

Hi @joelalejandro! Do you have an idea how this should be implemented? Do you have ETA when this could be finished? Maybe I can help you with this one?

Just to make sure I got this right...if we have movies and actors in many to many relationship we want to be able to make this request GET /actors?include=movies and GET /movies?include=actors?

@joelalejandro
Copy link
Member Author

joelalejandro commented Jul 27, 2021

Hey @kristian240! I haven't had the time to check this out yet. This addon creates dynamically-instantiated resource definitions for intermediate tables to support many-to-many relationships:

movies <=> movie_actors <=> actors

In this case, the MovieActors resource is created dynamically. The problem with this is that such resource is not available for reference in other relationships.

Let's assume your intermediate table has three fields: id, movie_id, actor_id. And, in another resource, you want to reduce FK repetition by reusing the movie_actors.id field, so you'd want to do something like:

{
  relationships: {
    movieAuthor: {
      foreignKeyName: 'movie_actor_id',
      belongsTo: true,
      type: () => MovieActor
    }
  }
}

If you feel up to the challenge, I can assign this issue to you!

@kristian240
Copy link

@joelalejandro not sure if I got a general idea, maybe I completely misunderstood this issue/feature from the begging. Because of this, I would like to ask you few more questions.

When is this used? Why isn't enough?

// Movie Resource
{
  relationships: {
      actors: {
        type: () => Actors,
        manyToMany: true,
      }
    }
}

I do not see why the MovieActor resource relationship is needed. What is the goal of that?

The problem with this is that such resource is not available for reference in other relationships.

Also, how is this different than creating a resource manually? If the resource is created automatically, then this example API does not look doable. Where is MovieActor coming from? Maybe the type function can get some params that can be used to extract needed type from the types array (where generated resources are appended).

type: (types) => return types.find(MoveActorPredicate),

In the end, does this have any effect to the final API? Should the following be possible after this implementation?

Just to make sure I got this right...if we have movies and actors in many to many relationship we want to be able to make this request GET /actors?include=movies and GET /movies?include=actors?

Or is something like /movies?include=movieActor.actor is the way to go. I might be missing something from the JSON:API spec so I'll check that too.

Thanks for your time 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants