From f7df204e0b3ff81d86f126e46786d7ab650035e8 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 14 Apr 2023 17:27:10 -0400 Subject: [PATCH] perf: parallelize database calls to improve speed (#8275) --- .../trpc/server/routers/viewer/bookings.tsx | 216 +++++++++--------- 1 file changed, 108 insertions(+), 108 deletions(-) diff --git a/packages/trpc/server/routers/viewer/bookings.tsx b/packages/trpc/server/routers/viewer/bookings.tsx index 51fd4afa38..fd6c3f64e2 100644 --- a/packages/trpc/server/routers/viewer/bookings.tsx +++ b/packages/trpc/server/routers/viewer/bookings.tsx @@ -200,133 +200,133 @@ export const bookingsRouter = router({ const passedBookingsStatusFilter = bookingListingFilters[bookingListingByStatus]; const orderBy = bookingListingOrderby[bookingListingByStatus]; - const bookingsQuery = await prisma.booking.findMany({ - where: { - OR: [ - { - userId: user.id, - }, - { - attendees: { - some: { - email: user.email, + const [bookingsQuery, recurringInfoBasic, recurringInfoExtended] = await Promise.all([ + prisma.booking.findMany({ + where: { + OR: [ + { + userId: user.id, + }, + { + attendees: { + some: { + email: user.email, + }, }, }, - }, - { - eventType: { - team: { - members: { - some: { - userId: user.id, - role: { - in: ["ADMIN", "OWNER"], + { + eventType: { + team: { + members: { + some: { + userId: user.id, + role: { + in: ["ADMIN", "OWNER"], + }, }, }, }, }, }, - }, - { - seatsReferences: { - some: { - attendee: { - email: user.email, + { + seatsReferences: { + some: { + attendee: { + email: user.email, + }, + }, + }, + }, + ], + AND: [passedBookingsStatusFilter, ...(filtersCombined ?? [])], + }, + select: { + ...bookingMinimalSelect, + uid: true, + recurringEventId: true, + location: true, + eventType: { + select: { + slug: true, + id: true, + eventName: true, + price: true, + recurringEvent: true, + team: { + select: { + name: true, }, }, }, }, - ], - AND: [passedBookingsStatusFilter, ...(filtersCombined ?? [])], - }, - select: { - ...bookingMinimalSelect, - uid: true, - recurringEventId: true, - location: true, - eventType: { - select: { - slug: true, - id: true, - eventName: true, - price: true, - recurringEvent: true, - team: { - select: { - name: true, + status: true, + paid: true, + payment: { + select: { + paymentOption: true, + amount: true, + currency: true, + success: true, + }, + }, + user: { + select: { + id: true, + name: true, + email: true, + }, + }, + rescheduled: true, + references: true, + isRecorded: true, + seatsReferences: { + where: { + attendee: { + email: user.email, + }, + }, + select: { + referenceUid: true, + attendee: { + select: { + email: true, + }, }, }, }, }, - status: true, - paid: true, - payment: { - select: { - paymentOption: true, - amount: true, - currency: true, - success: true, + orderBy, + take: take + 1, + skip, + }), + prisma.booking.groupBy({ + by: ["recurringEventId"], + _min: { + startTime: true, + }, + _count: { + recurringEventId: true, + }, + where: { + recurringEventId: { + not: { equals: null }, }, + userId: user.id, }, - user: { - select: { - id: true, - name: true, - email: true, + }), + prisma.booking.groupBy({ + by: ["recurringEventId", "status", "startTime"], + _min: { + startTime: true, + }, + where: { + recurringEventId: { + not: { equals: null }, }, + userId: user.id, }, - rescheduled: true, - references: true, - isRecorded: true, - seatsReferences: { - where: { - attendee: { - email: user.email, - }, - }, - select: { - referenceUid: true, - attendee: { - select: { - email: true, - }, - }, - }, - }, - }, - orderBy, - take: take + 1, - skip, - }); - - const recurringInfoBasic = await prisma.booking.groupBy({ - by: ["recurringEventId"], - _min: { - startTime: true, - }, - _count: { - recurringEventId: true, - }, - where: { - recurringEventId: { - not: { equals: null }, - }, - userId: user.id, - }, - }); - - const recurringInfoExtended = await prisma.booking.groupBy({ - by: ["recurringEventId", "status", "startTime"], - _min: { - startTime: true, - }, - where: { - recurringEventId: { - not: { equals: null }, - }, - userId: user.id, - }, - }); + }), + ]); const recurringInfo = recurringInfoBasic.map( (