1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Next.js】Prismaのaggregate/groupByで管理画面に売上集計を実装する

1
Posted at

はじめに

ECサイトの管理者ダッシュボードに売上集計(総売上・総注文数・ステータス別集計)を表示したいとき、Prismaの集計メソッドが便利です。この記事では aggregategroupBy の使い分けと実装例をまとめます。

この記事の対象読者・前提条件

対象読者

  • Prismaで基本的なCRUDはできるが、集計処理はまだ使ったことがない方
  • Next.jsで管理者ダッシュボードを作りたい方

前提条件

  • Next.js 14以降(App Router)を使用
  • Prismaセットアップ済み、Orderモデルに totalAmountstatus がある

実装

1. aggregate — 合計・件数・平均を計算する

aggregate_sum(合計)、_count(件数)、_avg(平均)などを一度に取得できます。

// app/admin/page.tsx
const salesData = await prisma.order.aggregate({
  _sum: { totalAmount: true },
  _count: true,
});

// salesData._sum.totalAmount → 総売上金額
// salesData._count → 総注文数

解説: SQLの SUM()COUNT() と同じですが、Prismaだと型安全に書けます。Server ComponentなのでPrismaを直接呼び出せます。

2. groupBy — ステータス別に集計する

groupBy で特定のフィールドごとにグループ化して集計できます。

const ordersByStatus = await prisma.order.groupBy({
  by: ["status"],
  _count: true,
});

// 結果例:
// [
//   { status: "pending", _count: 5 },
//   { status: "paid", _count: 12 },
//   { status: "shipped", _count: 3 },
// ]

解説: SQLの GROUP BY status と同じです。by にグループ化したいフィールドを配列で指定します。

3. ダッシュボードに表示する

// app/admin/page.tsx
export default async function AdminDashboard() {
  const salesData = await prisma.order.aggregate({
    _sum: { totalAmount: true },
    _count: true,
  });

  const ordersByStatus = await prisma.order.groupBy({
    by: ["status"],
    _count: true,
  });

  return (
    <>
      <h1>管理者ダッシュボード</h1>
      <p>総売上: ¥{salesData._sum.totalAmount?.toLocaleString()}</p>
      <p>総注文数: {salesData._count}</p>
      <h2>ステータス別</h2>
      {ordersByStatus.map((item) => (
        <p key={item.status}>{item.status}: {item._count}</p>
      ))}
    </>
  );
}

解説: admin/layout.tsx で認証・権限チェック済みなので、page.tsx では認証処理は不要です。

まとめ

  • aggregate は全体の合計・件数・平均を取得したいときに使う
  • groupBy は特定のフィールドごとに分けて集計したいときに使う
  • Server Component では Prisma を直接呼び出せるので、API Route を経由する必要がない

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?