:wave: Hey folks. Is there anyway I can order the ...
# prisma-client
j
👋 Hey folks. Is there anyway I can order the overarching objects by the column in a deeply nested relation? For example, I have a class, which has a service, which has a booking group, which has bookings. I'd like to order the list of classes returned by a
prisma.class.findMany
, by the
booking.startTime
(see an example in the thread below👇):
👀 1
Copy code
const classes = await prisma.class.findMany({
      where: {
        facilityId: Number(facilityId),
      },
      include: {
        service: {
          include: {
            bookingGroups: {
              include: {
                bookings: {
                  include: {
                    participants: {
                      select: {
                        user: {
                          select: {
                            roles: true,
                          },
                        },
                      },
                    },
                  },
                  orderBy: {
                    startTime: "desc",
                  },
                },
                owner: true,
                repeat: true,
              },
            },
            memberships: {
              include: {
                membership: true,
              },
            },
          },
        },
        instructors: {
          include: {
            user: true,
          },
        },
        facility: {
          include: {
            owner: true,
          },
        },
      },
// This is where I'm trying to orderBy
      orderBy: [
        {
          service: {
            bookingGroups: {
               bookings: {
                  startTime: "asc",
               },
            },
          },
        },
      ],
    });
n
Hey Jeet 👋 What is the type of relation between booking group and bookings? From this comment, you could use orderBy in a relation to traverse to a to-one side, coming from a many or one side (m -> 1, 1 -> 1).