test-upsert.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function testUpsert() {
  4. console.log('测试 upsert 功能(parentId=0)...\n');
  5. const bookId = 1;
  6. const bookIdNum = parseInt(bookId);
  7. // 先清空旧数据
  8. console.log('1. 清空bookId=1的旧数据...');
  9. await prisma.bookChapter.deleteMany({
  10. where: { bookId: bookIdNum }
  11. });
  12. console.log('✅ 清空完成\n');
  13. // 测试 upsert 创建章(parentId=0)
  14. console.log('2. 测试 upsert 创建章(parentId=0)...');
  15. const chapter1 = await prisma.bookChapter.upsert({
  16. where: {
  17. bookId_parentId_level_number: {
  18. bookId: bookIdNum,
  19. parentId: 0,
  20. level: 1,
  21. number: 1,
  22. }
  23. },
  24. update: {
  25. title: '第一章:测试章节(更新)',
  26. },
  27. create: {
  28. bookId: bookIdNum,
  29. parentId: 0,
  30. level: 1,
  31. number: 1,
  32. title: '第一章:测试章节',
  33. summary: '这是一个测试章节',
  34. status: 'completed',
  35. }
  36. });
  37. console.log(`✅ 创建成功: id=${chapter1.id}, parentId=${chapter1.parentId}\n`);
  38. // 再次 upsert(应该更新而不是创建)
  39. console.log('3. 再次 upsert(应该更新)...');
  40. const chapter1Updated = await prisma.bookChapter.upsert({
  41. where: {
  42. bookId_parentId_level_number: {
  43. bookId: bookIdNum,
  44. parentId: 0,
  45. level: 1,
  46. number: 1,
  47. }
  48. },
  49. update: {
  50. title: '第一章:测试章节(第二次更新)',
  51. },
  52. create: {
  53. bookId: bookIdNum,
  54. parentId: 0,
  55. level: 1,
  56. number: 1,
  57. title: '第一章:测试章节',
  58. }
  59. });
  60. console.log(`✅ 更新成功: id=${chapter1Updated.id}, title=${chapter1Updated.title}\n`);
  61. // 测试创建节(parentId=章ID)
  62. console.log('4. 测试创建节(parentId=章ID)...');
  63. const section1 = await prisma.bookChapter.upsert({
  64. where: {
  65. bookId_parentId_level_number: {
  66. bookId: bookIdNum,
  67. parentId: chapter1.id,
  68. level: 2,
  69. number: 1,
  70. }
  71. },
  72. update: {
  73. title: '第一节:测试节(更新)',
  74. },
  75. create: {
  76. bookId: bookIdNum,
  77. parentId: chapter1.id,
  78. level: 2,
  79. number: 1,
  80. title: '第一节:测试节',
  81. summary: '这是一个测试节',
  82. status: 'completed',
  83. }
  84. });
  85. console.log(`✅ 创建成功: id=${section1.id}, parentId=${section1.parentId}\n`);
  86. // 验证数据
  87. console.log('5. 验证数据库中的数据...');
  88. const allChapters = await prisma.bookChapter.findMany({
  89. where: { bookId: bookIdNum },
  90. orderBy: [
  91. { level: 'asc' },
  92. { number: 'asc' }
  93. ]
  94. });
  95. console.log(`总计 ${allChapters.length} 条记录:`);
  96. allChapters.forEach(c => {
  97. console.log(` - id=${c.id}, level=${c.level}, parentId=${c.parentId}, number=${c.number}, title=${c.title}`);
  98. });
  99. // 验证没有重复
  100. const duplicates = await prisma.$queryRaw`
  101. SELECT bookId, parentId, level, number, COUNT(*) as cnt
  102. FROM BookChapter
  103. WHERE bookId = ${bookIdNum}
  104. GROUP BY bookId, parentId, level, number
  105. HAVING cnt > 1
  106. `;
  107. if (duplicates.length > 0) {
  108. console.log('\n❌ 发现重复数据:', duplicates);
  109. } else {
  110. console.log('\n✅ 没有重复数据!');
  111. }
  112. console.log('\n🎉 测试完成!');
  113. await prisma.$disconnect();
  114. }
  115. testUpsert().catch(err => {
  116. console.error('❌ 测试失败:', err);
  117. process.exit(1);
  118. });