Source: lib/db/migrations.js

  1. import { ObjectID } from "bson";
  2. import { getInsights } from "../insights";
  3. import { getDreamsCollection } from "../mongodb";
  4. import { decryptDream } from "../transformations";
  5. import { hasCommentedOnDream } from "./reads";
  6. import { generateCompletion } from "./writes";
  7. /**
  8. * @dataCorrection
  9. */
  10. export async function addMissingWordFreqData() {
  11. const collection = await getDreamsCollection();
  12. const cursor = collection.find();
  13. const result = await cursor.toArray();
  14. if (result.lenght === 0) {
  15. return null;
  16. }
  17. const privateDeams = result.filter((data) => data.visibility === "private");
  18. const publicDreams = result.filter((data) => data.visibility !== "private");
  19. await Promise.all([
  20. ...privateDeams.map((data) => {
  21. const decrypted = decryptDream(data.dream);
  22. const insights = getInsights(decrypted.text);
  23. collection.updateOne(
  24. {
  25. _id: ObjectID(data._id),
  26. },
  27. {
  28. $set: insights,
  29. }
  30. );
  31. }),
  32. ...publicDreams.map((data) => {
  33. const insights = getInsights(data.dream.text);
  34. collection.updateOne(
  35. {
  36. _id: ObjectID(data._id),
  37. },
  38. {
  39. $set: insights,
  40. }
  41. );
  42. }),
  43. ]);
  44. await cursor.close();
  45. return result;
  46. }
  47. /**
  48. * @dataCorrection
  49. */
  50. export async function addMissingAICommentData() {
  51. const collection = await getDreamsCollection();
  52. const cursor = collection.find();
  53. const result = await cursor.toArray();
  54. if (result.lenght === 0) {
  55. return null;
  56. }
  57. const privateDeams = result.filter((data) => data.visibility === "private");
  58. const publicDreams = result.filter((data) => data.visibility !== "private");
  59. await Promise.all([
  60. ...privateDeams.map(async (data) => {
  61. ["Sonio", "Sonia"].map(async (aiName) => {
  62. const hasCommented = await hasCommentedOnDream(aiName, data._id);
  63. if (!hasCommented) {
  64. const decrypted = decryptDream(data.dream);
  65. return generateCompletion(
  66. data.dream._id,
  67. decrypted.text,
  68. undefined,
  69. data.dream.userId
  70. );
  71. }
  72. });
  73. }),
  74. ...publicDreams.map(async (data) => {
  75. ["Sonio", "Sonia"].map(async (aiName) => {
  76. const hasCommented = await hasCommentedOnDream(aiName, data._id);
  77. if (!hasCommented) {
  78. return generateCompletion(
  79. data._id,
  80. data.dream.text,
  81. undefined,
  82. data.userId
  83. );
  84. }
  85. });
  86. }),
  87. ]);
  88. await cursor.close();
  89. return result;
  90. }