Hey all, I'm working on a schema and I'm not sure ...
# prisma-whats-new
m
Hey all, I'm working on a schema and I'm not sure the best practice for what I'm wanting to accomplish. I have a two Types involved in this: Team User The User is a part of a Team, and a User has many teams and a team has many Users. I'm wanting a boolean field called "isParticipating" that signifies if the User is participating in a particular team. They can be on a Team and not be participating. Putting in the field in User or Team wouldn't seem to work because of the many-to-many relationship. How would I create this field? Is there some kind of metadata that I could add to a User's Role in the team or would I create a new Type called 'Role' and link that to a User and a Team? Any guidance would help. Thanks!
r
@maxnachamkin I guess you could have either a type that represents the relationship User => UserTeam => Team, where you store attributes that belong to the relationship. Or you could keep two lists on the user: one for all teams, and one for teams they're currently participating in.
m
Roger that. Thank you.
n
exactly my thoughts as well @raul 🙂
r
@nilan Beat that for a newcomer to the Slack channel! Lol
💯 1
m
Ok I think I got it. I created a 'UserTeam' Type with one-many relationships on Users and Teams, so in UserTeam it creates a 'user' field and a 'team' field, and in Users creates 'teamsMeta' [UserTeam] and in Teams creates 'usersMeta' [UserTeam]. That'd allow me to store 'isParticipating' and other values tied to a User within a particular team. That makes sense, right?
n
Perfectly 👍
m
Solid. Appreciate the help. ^_^
a
Depending on how often you actually need that metadata, it might be a good idea to also create the direct many to many relationship between User and Team, because that would take only one extra call instead of three. In other words, you can opt to only store the exceptions in the extended UserTeam type+relations...
m
Hey @agartha, can you elaborate? I already have many-to-many relationships setup between User and Team.
a
If you use an intermediate Type, UserTeam, creating that relationship will involve three steps: createUserTeam, addToUsersMeta, addToTeamMeta. All this, to set a flag on isParticipating... Also quering gets a lot more complex. If you have a lot of places, where you only need to know if a user is on a team, and a limited amount of times where you actually use the additional metadata, you might decided to keep using the many-to-many relationship you already have, and maybe keep the UserTeam relationship structure updated in the background (using event handlers on Create/Update). This is, however, strictly a performance/query complexity benefit, but as you know 'premature optimization is the root of all evil' so feel free to ignore all this until it becomes a problem.
m
Gotcha. Thanks for the explanation.
s
User => Team => Squad It's all about using the right words. 🙂
❓ 1
Maybe I misunderstood the relationships. I just re-read and yeah I see what you're driving at. Disregard
👍🏻 1
a
I just read about nested mutations. This makes using a meta-type actually not that much harder than a direct relationship. So I actually don't know if my reasoning is still valid when using nested mutations...