Back to all reviewers

Select specific database fields

calcom/cal.com
Based on 6 comments
TypeScript

Always explicitly select only the database fields you actually need instead of using broad includes or fetching entire objects. This practice significantly improves query performance, reduces memory usage, and minimizes data exposure.

Database TypeScript

Reviewer Prompt

Always explicitly select only the database fields you actually need instead of using broad includes or fetching entire objects. This practice significantly improves query performance, reduces memory usage, and minimizes data exposure.

Why this matters:

  • Reduces database load and network transfer
  • Improves application performance
  • Prevents accidental exposure of sensitive data
  • Makes code more maintainable by clearly showing data dependencies

How to apply:

Instead of fetching everything:

// โŒ Avoid - fetches all fields
const booking = await prisma.booking.findUnique({
  where: { uid: bookingUid },
  include: {
    attendees: true,
    user: true,
    eventType: {
      include: {
        owner: true,
      }
    }
  }
});

Be specific about what you need:

// โœ… Better - select only required fields
const booking = await prisma.booking.findUnique({
  where: { uid: bookingUid },
  include: {
    attendees: {
      select: {
        id: true,
        email: true,
        name: true
      }
    },
    user: {
      select: {
        id: true,
        name: true
      }
    },
    eventType: {
      select: {
        id: true,
        title: true
      }
    }
  }
});

Special considerations:

  • For JSON columns, you cannot use select - use fields: true or omit entirely
  • Avoid redundant queries when data is already available from previous fetches
  • Consider conditional subqueries for performance-critical paths (e.g., only fetch attendees for seated events)
6
Comments Analyzed
TypeScript
Primary Language
Database
Category

Source Discussions