Skip to content

Commit

Permalink
feat: sembast support for list collections
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Aug 21, 2023
1 parent cea9dcf commit b4beabd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
7 changes: 7 additions & 0 deletions firestore/lib/src/firestore_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ class DocumentReferenceLogger
}
}
}

@override
Future<List<CollectionReference>> listCollections() => ref.listCollections();
}

class TransactionLogger with TransactionMixin implements Transaction {
Expand Down Expand Up @@ -745,6 +748,10 @@ class FirestoreLogger

@override
FirestoreService get service => firestore.service;

@override
Future<List<CollectionReference>> listCollections() =>
firestore.listCollections();
}

class FirestoreServiceLogger
Expand Down
40 changes: 40 additions & 0 deletions firestore_sembast/lib/src/firestore_sembast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class FirestoreServiceSembast

@override
bool get supportsTrackChanges => true;

@override
bool get supportsListCollections => true;
}

FirestoreServiceSembast? _firestoreServiceSembastMemory;
Expand Down Expand Up @@ -300,6 +303,25 @@ class FirestoreSembast extends Object

@override
FirestoreService get service => firestoreService;

@override
Future<List<CollectionReference>> listCollections() async {
var db = await ready;

var ids = <String>{};
for (var record in await docStore.find(db)) {
final recordPath = record.key;

final parentPath = url.dirname(recordPath);
print(parentPath);
var collParentPath = url.dirname(parentPath);
print(collParentPath);
if (collParentPath == '.') {
ids.add(basename(parentPath));
}
}
return ids.map((e) => collection(e)).toList();
}
}

class WriteBatchSembast extends WriteBatchBase implements WriteBatch {
Expand Down Expand Up @@ -449,6 +471,24 @@ class DocumentReferenceSembast extends FirestoreReferenceBase
@override
Stream<DocumentSnapshot> onSnapshot({bool includeMetadataChanges = false}) =>
firestoreSembast.onSnapshot(this);

@override
Future<List<CollectionReference>> listCollections() async {
var db = await firestoreSembast.ready;

print('path: $path');
var ids = <String>{};
for (var record in await docStore.find(db)) {
final recordPath = record.key;
// print('recordPath: $recordPath');
final parentPath = url.dirname(recordPath);
var collParentPath = url.dirname(parentPath);
if (collParentPath == path) {
ids.add(basename(parentPath));
}
}
return ids.map((e) => collection(e)).toList();
}
}

class CollectionReferenceSembast extends QuerySembast
Expand Down
6 changes: 4 additions & 2 deletions firestore_test/lib/firestore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import 'package:tekartik_firebase_firestore_test/utils_query_test.dart';
import 'package:tekartik_firebase_firestore_test/utils_test.dart';
import 'package:test/test.dart';

import 'list_collections_test.dart';

var testsRefPath = 'tests/tekartik_firestore/tests';
bool skipConcurrentTransactionTests = false;

List<DocumentReference?> docsKeys(List<DocumentSnapshot> snashots) =>
Expand Down Expand Up @@ -114,8 +117,6 @@ void runApp(
}
});
group('firestore', () {
var testsRefPath = 'tests/tekartik_firestore/tests';

timestampGroup(service: firestoreService, firestore: firestore);
CollectionReference? getTestsRef() {
return firestore.collection(testsRefPath);
Expand Down Expand Up @@ -1808,6 +1809,7 @@ void runApp(
.select([]);
expect((await query.get()).docs, isNotEmpty);
}, skip: true);
runListCollectionsTest(firestore: firestore);
});
}

Expand Down
32 changes: 32 additions & 0 deletions firestore_test/lib/list_collections_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ignore_for_file: inference_failure_on_collection_literal

import 'package:path/path.dart';
import 'package:tekartik_firebase_firestore/firestore.dart';
// ignore: implementation_imports
import 'package:tekartik_firebase_firestore_test/utils_test.dart';
import 'package:test/test.dart';

void runListCollectionsTest({
required Firestore firestore,
}) {
test('list root collections', () async {
var collectionId = 'tekartik_test_root_collection';
var doc = firestore.doc('$collectionId/doc');
await doc.set({});
var collections = await firestore.listCollections();
var collection =
collections.firstWhere((element) => element.id == collectionId);
expect(collection.path, collectionId);
await doc.delete();
}, solo: true, skip: !firestore.service.supportsListCollections);

test('list doc collections', () async {
var parent = url.join(testsRefPath, 'tekartik_test_collection');
var collectionId = 'sub';
var doc = firestore.doc('$parent/$collectionId/doc');
await doc.set({});
var collections = await firestore.doc(parent).listCollections();
expect(collections.map((e) => e.id), contains(collectionId));
await doc.delete();
}, skip: !firestore.service.supportsListCollections);
}
1 change: 1 addition & 0 deletions firestore_test/lib/utils_collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void runUtilsCollectionTests(
Future<bool> findInCollection() async {
var querySnapshot = await ref.get();
for (var doc in querySnapshot.docs) {
devPrint('doc ${doc.ref.path}');
if (doc.ref.path == itemDoc.path) {
return true;
}
Expand Down

0 comments on commit b4beabd

Please sign in to comment.