Skip to content

Commit

Permalink
[cloud_firestore] Add metadata to QuerySnapshot (firebase#1915)
Browse files Browse the repository at this point in the history
* Add metadata to QuerySnapshot
  • Loading branch information
kroikie authored Jul 28, 2019
1 parent cc7353b commit 37636e1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.8+1

* Add `metadata` to `QuerySnapshot`.

## 0.12.8

* Updated how document ids are generated to more closely match native implementations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ private Map<String, Object> parseQuerySnapshot(QuerySnapshot querySnapshot) {
}
data.put("documentChanges", documentChanges);

Map<String, Object> metadata = new HashMap<>();
metadata.put("hasPendingWrites", querySnapshot.getMetadata().hasPendingWrites());
metadata.put("isFromCache", querySnapshot.getMetadata().isFromCache());
data.put("metadata", metadata);

return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void main() {
.where('message', isEqualTo: 'Hello world!')
.limit(1);
final QuerySnapshot querySnapshot = await query.getDocuments();
expect(querySnapshot.metadata, isNotNull);
expect(querySnapshot.documents.first['message'], 'Hello world!');
final DocumentReference firstDoc =
querySnapshot.documents.first.reference;
Expand All @@ -70,6 +71,7 @@ void main() {
.limit(1);
final QuerySnapshot querySnapshot = await query.getDocuments();
expect(querySnapshot.documents.first['stars'], 5);
expect(querySnapshot.metadata, isNotNull);
});

test('increment', () async {
Expand Down
4 changes: 4 additions & 0 deletions packages/cloud_firestore/ios/Classes/CloudFirestorePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ static FIRFirestoreSource getSource(NSDictionary *arguments) {
@"documentChanges" : documentChanges,
@"documents" : documents,
@"metadatas" : metadatas,
@"metadata" : @{
@"hasPendingWrites" : @(snapshot.metadata.hasPendingWrites),
@"isFromCache" : @(snapshot.metadata.isFromCache),
}
};
}

Expand Down
8 changes: 7 additions & 1 deletion packages/cloud_firestore/lib/src/query_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class QuerySnapshot {
data['documentChanges'][index],
_firestore,
);
});
}),
metadata = SnapshotMetadata._(
data['metadata']['hasPendingWrites'],
data['metadata']['isFromCache'],
);

/// Gets a list of all the documents included in this snapshot
final List<DocumentSnapshot> documents;
Expand All @@ -34,5 +38,7 @@ class QuerySnapshot {
/// is the first snapshot, all documents will be in the list as Added changes.
final List<DocumentChange> documentChanges;

final SnapshotMetadata metadata;

final Firestore _firestore;
}
2 changes: 1 addition & 1 deletion packages/cloud_firestore/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database
live synchronization and offline support on Android and iOS.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_firestore
version: 0.12.8
version: 0.12.8+1

flutter:
plugin:
Expand Down
10 changes: 10 additions & 0 deletions packages/cloud_firestore/test/cloud_firestore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void main() {
'paths': <String>["${methodCall.arguments['path']}/0"],
'documents': <dynamic>[kMockDocumentSnapshotData],
'metadatas': <Map<String, dynamic>>[kMockSnapshotMetadata],
'metadata': kMockSnapshotMetadata,
'documentChanges': <dynamic>[
<String, dynamic>{
'oldIndex': -1,
Expand Down Expand Up @@ -105,6 +106,7 @@ void main() {
'paths': <String>["${methodCall.arguments['path']}/0"],
'documents': <dynamic>[kMockDocumentSnapshotData],
'metadatas': <Map<String, dynamic>>[kMockSnapshotMetadata],
'metadata': kMockSnapshotMetadata,
'documentChanges': <dynamic>[
<String, dynamic>{
'oldIndex': -1,
Expand Down Expand Up @@ -614,6 +616,10 @@ void main() {
test('getDocumentsFromCollection', () async {
QuerySnapshot snapshot =
await collectionReference.getDocuments(source: Source.server);
expect(snapshot.metadata.hasPendingWrites,
equals(kMockSnapshotMetadata['hasPendingWrites']));
expect(snapshot.metadata.isFromCache,
equals(kMockSnapshotMetadata['isFromCache']));
DocumentSnapshot document = snapshot.documents.first;
expect(document.documentID, equals('0'));
expect(document.reference.path, equals('foo/0'));
Expand Down Expand Up @@ -781,6 +787,10 @@ void main() {
});
test('getDocumentsFromCollectionGroup', () async {
QuerySnapshot snapshot = await collectionGroupQuery.getDocuments();
expect(snapshot.metadata.hasPendingWrites,
equals(kMockSnapshotMetadata['hasPendingWrites']));
expect(snapshot.metadata.isFromCache,
equals(kMockSnapshotMetadata['isFromCache']));
DocumentSnapshot document = snapshot.documents.first;
expect(document.documentID, equals('0'));
expect(document.reference.path, equals('bar/0'));
Expand Down

0 comments on commit 37636e1

Please sign in to comment.