PrismaPostgresIntermediate
Dashboard stats — aggregate + groupBy
admin dashboard-এর numbers — count, sum, group।
4টি অংশবাংলা ভাষায়
await prisma.order.aggregate({
where: {
status: "paid",
createdAt: { gte: thirtyDaysAgo },
},
_count: { _all: true },
_sum: { total: true },
});const popular = await prisma.orderItem.groupBy({
by: ["productId"],
_sum: { qty: true },
orderBy: { _sum: { qty: "desc" } },
take: 5,
});
// productId → name join সহজ
const names = await prisma.product.findMany({
where: { id: { in: popular.map((p) => p.productId) } },
});| Stat | API |
|---|---|
| মোট order/revenue | count + _sum aggregate |
| জনপ্রিয় পণ্য | groupBy _sum qty |
| category-count | groupBy categoryId |
| monthly trend | raw-sql date_trunc |
rewrite SQL
অধিকাংশ stat-এ groupBy/aggregate-ই যথেষ্ট; date-series/জটিল report-এ $queryRaw (json_agg) আরও পরিষ্কার।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।