import { PrismaClient } from "@prisma/client"; import { PrismaMariaDb } from "@prisma/adapter-mariadb"; import "dotenv/config"; const base = process.env.DATABASE_URL.replace("mysql://", "mariadb://"); const cs = base + (base.includes("?") ? "&" : "?") + "connection_limit=5"; const prisma = new PrismaClient({ adapter: new PrismaMariaDb(cs) }); // 模拟API的查询 const allTopLevel = await prisma.forumCategory.findMany({ where: { parentId: null }, include: { children: { orderBy: [{ sortOrder: "asc" }], }, }, orderBy: [{ sortOrder: "asc" }, { createdAt: "desc" }], }); console.log("=== API查询结果 (parentId=null + children) ==="); for (const cat of allTopLevel) { console.log(`\n[${cat.slug}] ${cat.name} topicCount=${cat.topicCount}`); for (const child of cat.children) { console.log(` └─ [${child.slug}] ${child.name} topicCount=${child.topicCount} (子板块数: ?)`); } } // 看看community页面查询的industry console.log("\n\n=== industry板块 children话题统计 ==="); const industry = await prisma.forumCategory.findUnique({ where: { slug: "industry" }, include: { children: true } }); if (industry) { for (const c of industry.children) { const subCount = await prisma.forumCategory.count({ where: { parentId: c.id } }); const subWithTopic = await prisma.forumCategory.findMany({ where: { parentId: c.id }, select: { name: true, topicCount: true } }); console.log(` ${c.name} (${c.slug}) 子板块数=${subCount}:`); for (const s of subWithTopic) { console.log(` - ${s.name}: ${s.topicCount}`); } } } await prisma.$disconnect();