-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(firestore):
count()
feature for counting documents without ret…
…rieving documents. (#9699)
- Loading branch information
1 parent
1829ee7
commit ac0bf73
Showing
23 changed files
with
492 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/cloud_firestore/cloud_firestore/lib/src/aggregate_query.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of cloud_firestore; | ||
|
||
/// [AggregateQuery] represents the data at a particular location for retrieving metadata | ||
/// without retrieving the actual documents. | ||
class AggregateQuery { | ||
AggregateQuery._(this._delegate, this.query) { | ||
AggregateQueryPlatform.verifyExtends(_delegate); | ||
} | ||
|
||
/// [Query] represents the query over the data at a particular location used by the [AggregateQuery] to | ||
/// retrieve the metadata. | ||
final Query query; | ||
|
||
final AggregateQueryPlatform _delegate; | ||
|
||
/// Returns an [AggregateQuerySnapshot] with the count of the documents that match the query. | ||
Future<AggregateQuerySnapshot> get({ | ||
AggregateSource source = AggregateSource.server, | ||
}) async { | ||
return AggregateQuerySnapshot._(await _delegate.get(source: source), query); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/cloud_firestore/cloud_firestore/lib/src/aggregate_query_snapshot.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of cloud_firestore; | ||
|
||
/// [AggregateQuerySnapshot] represents a response to an [AggregateQuery] request. | ||
class AggregateQuerySnapshot { | ||
AggregateQuerySnapshot._(this._delegate, this.query) { | ||
AggregateQuerySnapshotPlatform.verifyExtends(_delegate); | ||
} | ||
final AggregateQuerySnapshotPlatform _delegate; | ||
|
||
/// [Query] represents the query over the data at a particular location used by the [AggregateQuery] to | ||
/// retrieve the metadata. | ||
final Query query; | ||
|
||
/// Returns the count of the documents that match the query. | ||
int get count => _delegate.count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/cloud_firestore/cloud_firestore/test/aggregate_query_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_firestore.dart'; | ||
import 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart'; | ||
import 'package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart'; | ||
import 'package:firebase_core/firebase_core.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
import './mock.dart'; | ||
|
||
int kCount = 4; | ||
|
||
void main() { | ||
setupCloudFirestoreMocks(); | ||
MethodChannelFirebaseFirestore.channel = const MethodChannel( | ||
'plugins.flutter.io/firebase_firestore', | ||
StandardMethodCodec(AggregateQueryMessageCodec()), | ||
); | ||
|
||
MethodChannelFirebaseFirestore.channel.setMockMethodCallHandler((call) async { | ||
if (call.method == 'AggregateQuery#count') { | ||
return { | ||
'count': kCount, | ||
}; | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
FirebaseFirestore? firestore; | ||
|
||
group('$AggregateQuery', () { | ||
setUpAll(() async { | ||
await Firebase.initializeApp(); | ||
firestore = FirebaseFirestore.instance; | ||
}); | ||
|
||
test('returns the correct `AggregateQuerySnapshot` with correct `count`', | ||
() async { | ||
Query query = firestore!.collection('flutter-tests'); | ||
AggregateQuery aggregateQuery = query.count(); | ||
|
||
expect(query, aggregateQuery.query); | ||
AggregateQuerySnapshot snapshot = await aggregateQuery.get(); | ||
|
||
expect(snapshot.count, equals(kCount)); | ||
}); | ||
}); | ||
} | ||
|
||
class AggregateQueryMessageCodec extends FirestoreMessageCodec { | ||
/// Constructor. | ||
const AggregateQueryMessageCodec(); | ||
static const int _kFirestoreInstance = 144; | ||
static const int _kFirestoreQuery = 145; | ||
static const int _kFirestoreSettings = 146; | ||
|
||
@override | ||
Object? readValueOfType(int type, ReadBuffer buffer) { | ||
switch (type) { | ||
// The following cases are only used by unit tests, and not by actual application | ||
// code paths. | ||
case _kFirestoreInstance: | ||
String appName = readValue(buffer)! as String; | ||
readValue(buffer); | ||
final FirebaseApp app = Firebase.app(appName); | ||
return MethodChannelFirebaseFirestore(app: app); | ||
case _kFirestoreQuery: | ||
Map<dynamic, dynamic> values = | ||
readValue(buffer)! as Map<dynamic, dynamic>; | ||
final FirebaseApp app = Firebase.app(); | ||
return MethodChannelQuery( | ||
MethodChannelFirebaseFirestore(app: app), | ||
values['path'], | ||
); | ||
case _kFirestoreSettings: | ||
readValue(buffer); | ||
return const Settings(); | ||
default: | ||
return super.readValueOfType(type, buffer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/aggregate_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// [AggregateSource] represents the source of data for an [AggregateQuery]. | ||
enum AggregateSource { | ||
/// Indicates that the data should be retrieved from the server. | ||
server, | ||
} |
33 changes: 33 additions & 0 deletions
33
...d_firestore_platform_interface/lib/src/method_channel/method_channel_aggregate_query.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:cloud_firestore_platform_interface/src/method_channel/utils/source.dart'; | ||
|
||
import 'method_channel_firestore.dart'; | ||
import '../../cloud_firestore_platform_interface.dart'; | ||
|
||
/// An implementation of [AggregateQueryPlatform] for the [MethodChannel] | ||
class MethodChannelAggregateQuery extends AggregateQueryPlatform { | ||
MethodChannelAggregateQuery(QueryPlatform query) : super(query); | ||
|
||
@override | ||
Future<AggregateQuerySnapshotPlatform> get({ | ||
required AggregateSource source, | ||
}) async { | ||
final Map<String, dynamic>? data = await MethodChannelFirebaseFirestore | ||
.channel | ||
.invokeMapMethod<String, dynamic>( | ||
'AggregateQuery#count', | ||
<String, dynamic>{ | ||
'query': query, | ||
'firestore': query.firestore, | ||
'source': getAggregateSourceString(source), | ||
}, | ||
); | ||
|
||
return AggregateQuerySnapshotPlatform( | ||
count: data!['count'] as int, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.