Is this a good way to fetch all of the users inside this relational table?

Hi guys, Is this the best way to have an endpoint that gets all of my "Workspaces"? (I want to get all of the Workspaces a user is in, based on the UserWorkspace table I have) I don't like how I had to pass in that long array type to userWorkspace constant. Is there a better way to do this?
10 Replies
Gabriel
Gabriel2y ago
Here´s my prisma model btw:
Brendonovich
Brendonovich2y ago
I think your UserWorkspace model is redundant, Prisma can create those join tables for you if you configure your relations properly. Doing that would make the query a lot simpler. As for the query you have, I don't see why you'd need to put a custom type there? TS Should be able to infer the return type of the query.
Gabriel
Gabriel2y ago
Oh, you're right. I removed it and now it is working It was not working while I was writing it I think Lmao I thought that this was the way of doing a many-to-many relationship How do I build the two models then? (User and Workspace), to have a many-to-many relation
Gabriel
Gabriel2y ago
This is from the prisma docs:
Gabriel
Gabriel2y ago
I think I found it?
Brendonovich
Brendonovich2y ago
Yep, that's it. Custom join tables are definitely useful in the case that you have extra data you want to associate to the relation, but for the case you shared implicit m2m relations should be good
Gabriel
Gabriel2y ago
Thank you so much!!
Gabriel
Gabriel2y ago
Just for documentation of this question: here´s how I adapted it for the implicit version: Prisma schema:
Gabriel
Gabriel2y ago
Procedure:
Brendonovich
Brendonovich2y ago
gorgeous!
Want results from more Discord servers?
Add your server