/**
* Archives record content for exploratory data analysis.
*
* @param {Array<Object>} records An array of record objects.
* @param {string} archivePath The path to the directory where the archives will be saved.
*/
function archiveRecords(records, archivePath) {
if (!records || !Array.isArray(records) || records.length === 0) {
console.warn("No records provided. Exiting.");
return;
}
if (!archivePath) {
console.warn("No archive path provided. Exiting.");
return;
}
// Ensure archive path exists
if (!fs.existsSync(archivePath)) {
fs.mkdirSync(archivePath, { recursive: true });
}
for (let i = 0; i < records.length; i++) {
const record = records[i];
if (typeof record !== 'object' || record === null) {
console.warn(`Skipping invalid record at index ${i}`);
continue;
}
const recordKey = `record_${i}`; // unique filename
const archiveFilePath = `${archivePath}/${recordKey}.json`;
try {
fs.writeFileSync(archiveFilePath, JSON.stringify(record, null, 2)); // Save as JSON with indentation
console.log(`Archived record ${i} to ${archiveFilePath}`);
} catch (err) {
console.error(`Error archiving record ${i} to ${archiveFilePath}:`, err);
}
}
}
// Require the fs module (Node.js specific)
const fs = require('fs');
Add your comment