diff --git a/firestore_sembast/lib/utils/export_utils.dart b/firestore_sembast/lib/utils/export_utils.dart index ac74511..460f30d 100644 --- a/firestore_sembast/lib/utils/export_utils.dart +++ b/firestore_sembast/lib/utils/export_utils.dart @@ -14,16 +14,29 @@ export 'package:sembast/utils/sembast_import_export.dart' extension TekartikSembastUtils on Firestore { /// Export the database as a list of json encodable lines Future> exportLines( - {required List collections}) async { - if (this is FirestoreSembast) { - if (service.supportsListCollections) { + {required List? collections, + List? documents}) async { + if (this is! FirestoreSembast) { + if (!service.supportsListCollections) { throw UnsupportedError('Cannot list collections'); } var firestore = newFirestoreMemory(); - for (var collection in collections) { - await collection.recursiveCopyTo( - firestore, firestore.collection(collection.path)); + if (collections != null || documents == null) { + collections ??= await firestore.listCollections(); + for (var collection in collections) { + await collection.recursiveCopyTo( + firestore, firestore.collection(collection.path)); + } } + if (documents != null) { + for (var doc in documents) { + await doc.recursiveCopyTo(firestore, firestore.doc(doc.path)); + } + } + return firestore.exportLines( + collections: + collections?.map((e) => firestore.collection(e.path)).toList(), + documents: documents?.map((e) => firestore.doc(e.path)).toList()); } return exportDatabaseLines(await sembastDatabase); } diff --git a/firestore_sembast/test/export_utils_test.dart b/firestore_sembast/test/export_utils_test.dart index 910142f..6b47fe4 100644 --- a/firestore_sembast/test/export_utils_test.dart +++ b/firestore_sembast/test/export_utils_test.dart @@ -1,10 +1,32 @@ import 'package:dev_test/test.dart'; +import 'package:tekartik_firebase_firestore_sembast/firestore_sembast.dart'; +import 'package:tekartik_firebase_firestore_sembast/utils/export_utils.dart'; void main() { group('export_utils', () { - test('copy', () async { - // var srcFirestore = newFirestoreServiceMemory(); - // var dstFirestore = newFirestoreServiceMemory(); + test('export', () async { + var firestore = newFirestoreMemory(); + var export = await firestore.exportLines(collections: []); + expect(export, [ + {'sembast_export': 1, 'version': 1} + ]); + await firestore.doc('test/doc').set({'test': 1}); + export = await firestore + .exportLines(collections: [firestore.collection('test')]); + expect(export[1], {'store': 'doc'}); + expect((export[2] as List)[0], 'test/doc'); + // {'sembast_export': 1, 'version': 1}, + // {'store': 'doc'}, + // [ + // 'test/doc', + // { + // 'test': 1, + // '$rev': 1, + // '$createTime': '2023-08-22T17:45:34.489989Z', + // '$updateTime': '2023-08-22T17:45:34.489989Z' + // } + // ] + // ] }); }); }