1. /**
  2. * Archives record content for exploratory data analysis.
  3. *
  4. * @param {Array<Object>} records An array of record objects.
  5. * @param {string} archivePath The path to the directory where the archives will be saved.
  6. */
  7. function archiveRecords(records, archivePath) {
  8. if (!records || !Array.isArray(records) || records.length === 0) {
  9. console.warn("No records provided. Exiting.");
  10. return;
  11. }
  12. if (!archivePath) {
  13. console.warn("No archive path provided. Exiting.");
  14. return;
  15. }
  16. // Ensure archive path exists
  17. if (!fs.existsSync(archivePath)) {
  18. fs.mkdirSync(archivePath, { recursive: true });
  19. }
  20. for (let i = 0; i < records.length; i++) {
  21. const record = records[i];
  22. if (typeof record !== 'object' || record === null) {
  23. console.warn(`Skipping invalid record at index ${i}`);
  24. continue;
  25. }
  26. const recordKey = `record_${i}`; // unique filename
  27. const archiveFilePath = `${archivePath}/${recordKey}.json`;
  28. try {
  29. fs.writeFileSync(archiveFilePath, JSON.stringify(record, null, 2)); // Save as JSON with indentation
  30. console.log(`Archived record ${i} to ${archiveFilePath}`);
  31. } catch (err) {
  32. console.error(`Error archiving record ${i} to ${archiveFilePath}:`, err);
  33. }
  34. }
  35. }
  36. // Require the fs module (Node.js specific)
  37. const fs = require('fs');

Add your comment