importDatabase method

Future<void> importDatabase(
  1. String filePath
)

Implementation

Future<void> importDatabase(String filePath) async {
  final file = await _harnFile.getFile(filePath);
  final filename = file.split('/').last;
  final ext = filename.split('.').last;
  if (ext != 'harndb') {
    throw Exception(
        'Invalid file extension: $ext. Only .harn files can be imported.');
  }

  if (!await _harnFile.exists(file)) {
    throw Exception('File does not exist: $filePath');
  }

  final jsonString = await _harnFile.read(file);
  final Map<String, dynamic> harnData =
      jsonDecode(jsonString) as Map<String, dynamic>;

  for (final entry in harnData.entries) {
    final collectionName = entry.key;
    final collectionData = entry.value;
    final collect = collection(collectionName);
    for (final document in collectionData) {
      await collect.insert(document);
    }
  }
}