check-parentid.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function checkParentId() {
  4. const bookId = 2;
  5. // 查询前3个章
  6. const chapters = await prisma.bookChapter.findMany({
  7. where: { bookId, level: 1 },
  8. orderBy: { number: 'asc' },
  9. take: 2
  10. });
  11. console.log('=== Level 1 (章) ===');
  12. chapters.forEach(c => {
  13. console.log(`id=${c.id}, parentId=${c.parentId}, number=${c.number}, title=${c.title.substring(0, 20)}`);
  14. });
  15. if (chapters.length > 0) {
  16. const firstChapterId = chapters[0].id;
  17. // 查询这个章的节
  18. const sections = await prisma.bookChapter.findMany({
  19. where: { bookId, level: 2, parentId: firstChapterId },
  20. orderBy: { number: 'asc' },
  21. take: 3
  22. });
  23. console.log(`\n=== Level 2 (节, parentId=${firstChapterId}) ===`);
  24. sections.forEach(s => {
  25. console.log(`id=${s.id}, parentId=${s.parentId}, number=${s.number}, title=${s.title.substring(0, 20)}`);
  26. });
  27. if (sections.length > 0) {
  28. const firstSectionId = sections[0].id;
  29. // 查询这个节的小节
  30. const subsections = await prisma.bookChapter.findMany({
  31. where: { bookId, level: 3, parentId: firstSectionId },
  32. orderBy: { number: 'asc' },
  33. take: 3
  34. });
  35. console.log(`\n=== Level 3 (小节, parentId=${firstSectionId}) ===`);
  36. subsections.forEach(sub => {
  37. console.log(`id=${sub.id}, parentId=${sub.parentId}, number=${sub.number}, title=${sub.title.substring(0, 20)}`);
  38. });
  39. }
  40. }
  41. await prisma.$disconnect();
  42. }
  43. checkParentId().catch(console.error);