Skip to content

Commit

Permalink
feat: fix export
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Aug 22, 2023
1 parent fa7f0bd commit c948993
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
25 changes: 19 additions & 6 deletions firestore_sembast/lib/utils/export_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Object>> exportLines(
{required List<CollectionReference> collections}) async {
if (this is FirestoreSembast) {
if (service.supportsListCollections) {
{required List<CollectionReference>? collections,
List<DocumentReference>? 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);
}
Expand Down
28 changes: 25 additions & 3 deletions firestore_sembast/test/export_utils_test.dart
Original file line number Diff line number Diff line change
@@ -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'
// }
// ]
// ]
});
});
}

0 comments on commit c948993

Please sign in to comment.