update-all-content-to-hello.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function updateAllSubsections() {
  4. console.log('\n📝 开始更新所有小节内容为"你好"...\n');
  5. // 查找所有level=3的小节
  6. const subsections = await prisma.bookChapter.findMany({
  7. where: {
  8. level: 3
  9. },
  10. select: {
  11. id: true,
  12. title: true,
  13. bookId: true,
  14. }
  15. });
  16. console.log(`找到 ${subsections.length} 个小节\n`);
  17. let updatedCount = 0;
  18. let failedCount = 0;
  19. // 批量更新
  20. for (const sub of subsections) {
  21. try {
  22. await prisma.bookChapter.update({
  23. where: { id: sub.id },
  24. data: {
  25. content: '你好',
  26. contentStatus: 'completed',
  27. wordCount: 2,
  28. }
  29. });
  30. updatedCount++;
  31. if (updatedCount % 50 === 0) {
  32. console.log(`已更新 ${updatedCount}/${subsections.length} 个小节...`);
  33. }
  34. } catch (error) {
  35. failedCount++;
  36. console.error(`更新失败: 小节ID=${sub.id}, ${error.message}`);
  37. }
  38. }
  39. console.log(`\n✅ 更新完成!`);
  40. console.log(` 成功: ${updatedCount} 个`);
  41. console.log(` 失败: ${failedCount} 个`);
  42. console.log(` 总计: ${subsections.length} 个\n`);
  43. await prisma.$disconnect();
  44. }
  45. updateAllSubsections().catch(console.error);