Course

Aggregates - Count

In this lesson, we'll discuss the different ways to count records in Prisma. We'll cover the count method and how to count relationships.

Count method

In the previous project, we created a Review model. This model is attached to our Product model. Let's say we want to know how many reviews we have for a specific product. We can use the count method to calculate this.

//
const reviewsCount = await prisma.review.count({
  where: {
    productId: 1,
  },
});

Here we've called the count method on the review model. We've passed in a where clause that filters the reviews by the productId. This will return the number of reviews for the product with an id of 1.

This is a relatively simple implementation- and it's fast. The count method is a direct SQL query that returns the count of records that match the where clause.

Count relationships

We can also count relationships. For example, let's say we wanted to know the number of reviews on a series of products.

Let's think back to our pagination section. We used the take and skip methods to paginate through a set of records. We can use the count method in conjunction with take and skip to calculate the number of reviews for a set of products on each page:

//
const products = await prisma.product.findMany({
  // grabbing the first 10 products
  take: 10,
  skip: 0,
  // include the reviews related to each record
  include: {
    reviews: true,
  },
});

Previously we spoke about how can be used to include related records in a query. But it can also be used it add fields for our aggregate functions. In this case, we're including the reviews field. This will return the reviews for each product. And we'll add a special field to the product object that will contain the count of reviews for that product.

//
const test = await prisma.product.findMany({
  take: 5,
  skip: 0,
  include: {
    reviews: true,
    _count: {
      // add a _count field to the product object
      select: { reviews: true }, // count the reviews
    },
  },
});
//
const products = await prisma.product.findMany({
  // grabbing the first 10 products
  take: 10,
  skip: 0,
  // include the reviews related to each record
  include: {
    reviews: true,
  },
});
//
[
  {
    id: 20,
    name: "Test",
    category: "home",
    price: 22.99,
    description: "Wow what an awesome test product!",
    reviews: [[Object], [Object]],
  },
  {
    id: 21,
    name: "Another Product",
    category: "sports",
    price: 22,
    description: "asdasd",
    reviews: [],
  },
];
//
const test = await prisma.product.findMany({
  take: 5,
  skip: 0,
  include: {
    reviews: true,
    _count: {
      // add a _count field to the product object
      select: { reviews: true }, // count the reviews
    },
  },
});
//
[
  {
    id: 20,
    name: 'Lamp',
    category: 'home',
    price: 22.99,
    description: 'Super bright lamp',
    reviews: [ [Object], [Object] ],
    _count: { reviews: 2 } // count of reviews
  },
  {
    id: 21,
    name: 'Baseball bat',
    category: 'sports',
    price: 25,
    description: 'A bat!',
    reviews: [],
    _count: { reviews: 0 } // count of reviews
  },
]

Exercise

Can you get a count of all the reviews that a specific product category has? You'll want to use .

monkey
//
    const totalElectronicReviews = await prisma.review.count({
      where: {
        product: {
          category: "electronics",
        },
      },
    });

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Aggregates - Average