Inversed relationship field #357
Replies: 7 comments 8 replies
-
Hey @donstephan — This is great! We have been looking forward to someone sharing code to do this. I think this would be great to offer as a plugin. It's possible that we may support bi-directional relationships internally in the future but with a setup like yours, and the fact that it's completely possible currently, I can't say that the likeliness is high. Sounds like you have a great handle on how plugins work already even down to setting the new fields to I did notice that through how you are specifying a field's type as Instead, the plugin's API could be adjusted to just accept options when the plugin is added to a user's config, and then append a field into the end of each relevant collection, using the specs that have been passed. There could be a few other TypeScript-friendly ways to do this too. I would also say that instead of injecting the full field config of the referenced relationship, you might also just inject a To take this a step further, for performance reasons, you might want to write a series of Great work! |
Beta Was this translation helpful? Give feedback.
-
I'd definitely need such a feature. I have a "User" and "Author" object. Posts on my website are created by multiple author. However, not every author needs to be an admin user which can log in to the admin panel. So, I made an Author object with Name, Avatar, bio etc. and a relationship to User (with auth=true). Now, I only have to fill in that relationship if the author is a user which can log-in. Only issue: the User field does not have access to the Name/author/bio fields of Author. Would be great if that bidirectional relationship/polymorphism could work great and be displayed nicely in the user interface and API. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hey +1 to enforncing bi-directional relationships. I think this is very common in building every type of application (at least CRUD type). I think using Mongo is problematic here since relations are not "real" - I think in this case framework (CMS) should abstract this away and simulate strong bi-directional relationship 👍 Till then, maybe its worth to document this as some tutorial/guide how to achieve it with hooks? I think people will try to handle this on frontend which is dangerous due to transactions |
Beta Was this translation helpful? Give feedback.
-
Hope this can be a first-class citizen of Payload soon - Relationships are only half as useful without it. |
Beta Was this translation helpful? Give feedback.
-
Any updates on whether this will come in 3.0? |
Beta Was this translation helpful? Give feedback.
-
We added the |
Beta Was this translation helpful? Give feedback.
-
Hey all,
I've been messing with Payload a few days here and just wanted to share a feature I implemented as a plugin and see if it can be a good use case for a payload field. The plugin creates a new field that is an inversed relationship field. It's a virtual field that returns relation data in the opposite manner. Here's an example of how it can be used and what it looks like:
Say you have two collections, a
users
collection and aroles
collection. In this context, a user can have many roles for many different organizations.Roles
have a relationship tousers
on theuserId
field. It would messy to have to maintain an array ofroleIds
on theuser
object (however that is very possible and easy to do with hooks). Instead you can choose to add an inversed relationship field in theusers
collection. This field looks like so:It describes the collection (by slug) that it is inversed by and which field the relation is on. My plugin currently then swaps that field type for an array type, adds in the field schema of the inversed collection and makes it so the admin UI is disabled for this array. This allows it to pass GraphQL schema generation and show the data in the admin UI. Lastly it sets up an
afterRead
hook on that field which performs the query to get the documents in question. So now when I perform a query like so:I get back:
Happy to chat about the use cases for this. Again it works fantastic in a plugin context and happy to leave it in that realm but figured I'd share and see if anyone is looking for a feature like this.
Beta Was this translation helpful? Give feedback.
All reactions