const Realm = require('realm');
const fs = require('fs');

// Function to export entire Realm database to JSON
async function exportRealmToJSON() {
  // Specify the file path for your Realm database
  const realmFile = './backup.zth';

  // Configure Realm with the specified file path
  const realm = await Realm.open({
    path: realmFile,
    readOnly: true,
  });

  // Get all objects from all available object types
  const allObjects = {};
  realm.schema.forEach((objectType) => {
    allObjects[objectType.name] = realm.objects(objectType.name);
  });

  // Convert to JSON
  const jsonData = JSON.stringify(allObjects, null, 2);

  // Print or save the JSON string as needed
  console.log(jsonData);

  // If you want to save to a file
  const exportFilePath = 'exported_data.json';
  fs.writeFileSync(exportFilePath, jsonData, 'utf-8');

  // Close the Realm instance
  realm.close();
}

// Call the function to export
exportRealmToJSON();