Skip to content

Conversation

@IndySeh
Copy link

@IndySeh IndySeh commented Jul 24, 2024

Description


  • Refactored the code to use the count() function for calculating totalAttendees directly in the query.
  • Removed the previous implementation that used the reduce function to calculate the total number of attendees.

Changes Made


  • Updated the query to use count(attendees.id) for counting total attendees.
  • Removed the reduce function logic and replaced it with the new method of extracting totalAttendees.
const counts = await db
      .select({
        totalAttendees: count(attendees.id),
      })
      .from(events)
      .leftJoin(rsvps, eq(rsvps.eventId, events.id))
      .leftJoin(attendees, eq(attendees.id, rsvps.attendeeId))
      .where(eq(events.createdById, userId))
      .execute()

    const [firstCount] = counts
    const totalAttendees = firstCount.totalAttendees | 0

    return totalAttendees

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant