Thomas Yankue
07/26/2022, 2:50 PMfriendship model that had an array of user IDs, or an array of users, but the problem was to fetch all of user A's friends, I could easily get all the IDs, but then I had to perform 1 query for each friend to fetch the whole user model. It worked but it seemed very inefficient - is there a better way or is this the only way it can work?Lloyd Richards
07/27/2022, 5:38 AMinclude field in the options. basically a JOIN in the query. These can be nested to retrieve most data in one operation:
const result = prisma.user.findFirst({
where: {
id: "1234",
},
include: {
friendships: {
include: {
user: true,
},
},
},
});
I think there is a section on this in the docs:
https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#include-deeply-nested-relations