Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firestore): count() feature for counting documents without retrieving documents. #9699

Merged
merged 30 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1f2ffc0
feat(firestore): count feature. Dart implementation
russellwheatley Oct 10, 2022
bb0a9f5
feat(firestore): count feature. Dart implementation.
russellwheatley Oct 10, 2022
0e23ec0
feat(firestore): reverted iOS firestore change
russellwheatley Oct 10, 2022
7735485
feat(firestore): count(). Wire up user facing code
russellwheatley Oct 10, 2022
4dc2547
feat(firestore): count(). Wire up web implementation
russellwheatley Oct 10, 2022
1d130f7
feat(firestore): count(). clean up method channel & PI
russellwheatley Oct 10, 2022
e58846e
feat(firestore): count(). e2e test.
russellwheatley Oct 10, 2022
926cf58
feat(firestore): remove static.
russellwheatley Oct 10, 2022
1f25a58
feat(firestore): correct init of data
russellwheatley Oct 10, 2022
371ed9d
feat(firestore): count() API. licenses and inline documentation
russellwheatley Oct 10, 2022
467ce69
feat(firestore): expose Query on AggregateQuery
russellwheatley Oct 10, 2022
d25bca7
feat(firestore): count(). add unit test for user code
russellwheatley Oct 10, 2022
0a4f218
feat(firestore): count(). universal code unit test
russellwheatley Oct 10, 2022
36a1c99
feat(firestore): count(). unit tests
russellwheatley Oct 10, 2022
4c71036
feat(firestore): update code comment
russellwheatley Oct 10, 2022
93f612f
feat(firestore): count() API. iOS implementation
russellwheatley Oct 11, 2022
e5a860e
feat(firestore): count() API. format.
russellwheatley Oct 11, 2022
68c2137
feat(firestore): count() API e2e skip android
russellwheatley Oct 11, 2022
3fe511d
feat(firestore): count() API. expose query on snapshot
russellwheatley Oct 11, 2022
585c82a
feat(firestore): count() API. format
russellwheatley Oct 11, 2022
2893c80
feat(firestore): count() API. wording
russellwheatley Oct 11, 2022
bdc6889
feat(firestore): count() API. remove Firestore from test project
russellwheatley Oct 11, 2022
3c87ec5
feat(ios): update swiftformat and add back curly brace
russellwheatley Oct 11, 2022
b312ec8
fix(firestore): add nanopb pod to example
russellwheatley Oct 12, 2022
0a90db2
feat(firestore): implement android
russellwheatley Oct 13, 2022
6343092
feat(firestore): update iOS count()
russellwheatley Oct 13, 2022
4723bc7
test(firestore): update unit test
russellwheatley Oct 13, 2022
958db10
Merge branch 'master' into @russell/firestore-count
russellwheatley Oct 18, 2022
dd93f0f
chore: rm nanopb from example app
russellwheatley Oct 18, 2022
8f36e58
chore: rm foundation import
russellwheatley Oct 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:math';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

void runQueryTests() {
Expand Down Expand Up @@ -1807,6 +1808,22 @@ void runQueryTests() {
},
timeout: const Timeout.factor(3),
);

test('count()', () async {
final collection = await initializeTest('count');

await Future.wait([collection.add({'foo': 'bar'}), collection.add({'bar': 'baz'})]) ;

AggregateQuery query = collection.count();

AggregateQuerySnapshot snapshot = await query.get();

expect(
snapshot.count,
2,
);
// TODO(russellwheatley): remove when native code implemented
}, skip: !kIsWeb);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ Pod::Spec.new do |s|
s.license = { :file => '../LICENSE' }
s.authors = 'The Chromium Authors'
s.source = { :path => '.' }

s.source_files = 'Classes/**/*.{h,m}'
s.public_header_files = 'Classes/Public/*.h'
s.private_header_files = 'Classes/Private/*.h'

s.ios.deployment_target = '10.0'
s.ios.deployment_target = '11.0'
russellwheatley marked this conversation as resolved.
Show resolved Hide resolved
s.dependency 'Flutter'

s.dependency 'firebase_core'
s.dependency 'Firebase/Firestore', firebase_sdk_version

s.static_framework = true
s.pod_target_xcconfig = {
'GCC_PREPROCESSOR_DEFINITIONS' => "LIBRARY_VERSION=\\@\\\"#{library_version}\\\" LIBRARY_NAME=\\@\\\"flutter-fire-fst\\\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:meta/meta.dart';

export 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart'
show
AggregateSource,
ListEquality,
FieldPath,
Blob,
Expand Down Expand Up @@ -46,3 +47,5 @@ part 'src/snapshot_metadata.dart';
part 'src/transaction.dart';
part 'src/utils/codec_utility.dart';
part 'src/write_batch.dart';
part 'src/aggregate_query.dart';
part 'src/aggregate_query_snapshot.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
part of cloud_firestore;

class AggregateQuery {
AggregateQuery._(this._delegate) {
AggregateQueryPlatform.verifyExtends(_delegate);
russellwheatley marked this conversation as resolved.
Show resolved Hide resolved
}
final AggregateQueryPlatform _delegate;

Future<AggregateQuerySnapshot> get({AggregateSource source = AggregateSource.server}) async {
return AggregateQuerySnapshot._(await _delegate.get(source: source));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
part of cloud_firestore;

class AggregateQuerySnapshot {
russellwheatley marked this conversation as resolved.
Show resolved Hide resolved
AggregateQuerySnapshot._(this._delegate) {
AggregateQuerySnapshotPlatform.verifyExtends(_delegate);
}
final AggregateQuerySnapshotPlatform _delegate;

int get count => _delegate.count;
}
12 changes: 12 additions & 0 deletions packages/cloud_firestore/cloud_firestore/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ abstract class Query<T extends Object?> {
required FromFirestore<R> fromFirestore,
required ToFirestore<R> toFirestore,
});

AggregateQuery count();
}

/// Represents a [Query] over the data at a particular location.
Expand Down Expand Up @@ -808,6 +810,11 @@ class _JsonQuery implements Query<Map<String, dynamic>> {

@override
int get hashCode => Object.hash(runtimeType, firestore, _delegate);

@override
AggregateQuery count() {
return AggregateQuery._(_delegate.count());
}
}

class _WithConverterQuery<T extends Object?> implements Query<T> {
Expand Down Expand Up @@ -970,4 +977,9 @@ class _WithConverterQuery<T extends Object?> implements Query<T> {
@override
int get hashCode =>
Object.hash(runtimeType, _fromFirestore, _toFirestore, _originalQuery);

@override
AggregateQuery count() {
return _originalQuery.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export 'src/platform_interface/platform_interface_transaction.dart';
export 'src/platform_interface/platform_interface_write_batch.dart';
export 'src/platform_interface/platform_interface_load_bundle_task.dart';
export 'src/platform_interface/platform_interface_load_bundle_task_snapshot.dart';
export 'src/platform_interface/platform_interface_aggregate_query.dart';
export 'src/platform_interface/platform_interface_aggregate_query_snapshot.dart';
export 'src/aggregate_source.dart';
export 'src/snapshot_metadata.dart';
export 'src/source.dart';
export 'src/load_bundle_task_state.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum AggregateSource {
russellwheatley marked this conversation as resolved.
Show resolved Hide resolved
server,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:cloud_firestore_platform_interface/src/method_channel/utils/source.dart';
russellwheatley marked this conversation as resolved.
Show resolved Hide resolved
import 'package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_aggregate_query.dart';

import 'method_channel_firestore.dart';
import '../../cloud_firestore_platform_interface.dart';
import '../aggregate_source.dart';
import '../platform_interface/platform_interface_aggregate_query_snapshot.dart';

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,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:cloud_firestore_platform_interface/src/internal/pointer.dart';
import 'package:collection/collection.dart';
import 'package:flutter/services.dart';

import 'method_channel_aggregate_query.dart';
import 'method_channel_firestore.dart';
import 'method_channel_query_snapshot.dart';
import 'utils/source.dart';
Expand Down Expand Up @@ -212,6 +213,13 @@ class MethodChannelQuery extends QueryPlatform {
});
}

@override
AggregateQueryPlatform count() {
return MethodChannelAggregateQuery(
this,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a styleguide for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't AFAIK, it just follows the same implementation style as the rest of the plugins.

);
}

@override
bool operator ==(Object other) {
return runtimeType == other.runtimeType &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// 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/aggregate_source.dart';
import 'package:cloud_firestore_platform_interface/src/source.dart';

/// Converts [Source] to [String]
Expand All @@ -16,3 +17,13 @@ String getSourceString(Source source) {
return 'default';
}
}


/// Converts [AggregateSource] to [String]
String getAggregateSourceString(AggregateSource source) {
switch (source) {
case AggregateSource.server:
default:
russellwheatley marked this conversation as resolved.
Show resolved Hide resolved
return 'server';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_aggregate_query_snapshot.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import '../../cloud_firestore_platform_interface.dart';
import '../aggregate_source.dart';

abstract class AggregateQueryPlatform extends PlatformInterface {
AggregateQueryPlatform(this.query) : super(token: _token);

static final Object _token = Object();

/// Throws an [AssertionError] if [instance] does not extend
/// [AggregateQueryPlatform].
///
/// This is used by the app-facing [AggregateQuery] to ensure that
/// the object in which it's going to delegate calls has been
/// constructed properly.
static void verifyExtends(AggregateQueryPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
}

final QueryPlatform query;

Future<AggregateQuerySnapshotPlatform> get({required AggregateSource source}) async {
throw UnimplementedError('get() is not implemented');
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

class AggregateQuerySnapshotPlatform extends PlatformInterface {
AggregateQuerySnapshotPlatform({required count}) : _count = count, super(token: _token);

static final Object _token = Object();

/// Throws an [AssertionError] if [instance] does not extend
/// [AggregateQuerySnapshotPlatform].
///
/// This is used by the app-facing [AggregateQuerySnapshot] to ensure that
/// the object in which it's going to delegate calls has been
/// constructed properly.
static void verifyExtends(AggregateQuerySnapshotPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
}

final int _count;

int get count => _count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import 'platform_interface_query.dart';
import 'platform_interface_query_snapshot.dart';
import 'platform_interface_transaction.dart';
import 'platform_interface_write_batch.dart';
import 'platform_interface_aggregate_query.dart';
import 'platform_interface_aggregate_query_snapshot.dart';

/// Defines an interface to work with Cloud Firestore on web and mobile
abstract class FirebaseFirestorePlatform extends PlatformInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,8 @@ abstract class QueryPlatform extends PlatformInterface {
QueryPlatform where(List<List<dynamic>> conditions) {
throw UnimplementedError('where() is not implemented');
}

AggregateQueryPlatform count(){
throw UnimplementedError('count() is not implemented');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ignore_for_file: require_trailing_commas
// Copyright 2017, 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/cloud_firestore_platform_interface.dart';

import 'interop/firestore.dart' as firestore_interop;

/// Web implementation for Firestore [AggregateQueryPlatform].
class AggregateQueryWeb extends AggregateQueryPlatform {
/// instance of Firestore from the web plugin
// final firestore_interop.Firestore firestoreWeb;

/// instance of DocumentReference from the web plugin
final firestore_interop.AggregateQuery _delegate;
final firestore_interop.Query _webQuery;

/// Creates an instance of [DocumentReferenceWeb] which represents path
/// at [pathComponents] and uses implementation of [firestoreWeb]
AggregateQueryWeb(
QueryPlatform query, this._webQuery): _delegate = firestore_interop.AggregateQuery(_webQuery), super(query);

@override
Future<AggregateQuerySnapshotPlatform> get({required AggregateSource source}) async {
// There isn't a source option on web
firestore_interop.AggregateQuerySnapshot snapshot = await _delegate.get();

return AggregateQuerySnapshotPlatform(count: snapshot.count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,29 @@ abstract class FieldValue {
static final FieldValue _serverTimestamp = _FieldValueServerTimestamp();
static final FieldValue _delete = _FieldValueDelete();
}

class AggregateQuery {
AggregateQuery(Query query) : _jsQuery = query.jsObject;
final firestore_interop.QueryJsImpl _jsQuery;
Future<AggregateQuerySnapshot> get() async {
return handleThenable<firestore_interop.AggregateQuerySnapshotJsImpl>(
firestore_interop.getCountFromServer(_jsQuery))
.then(AggregateQuerySnapshot.getInstance);
}
}

class AggregateQuerySnapshot
extends JsObjectWrapper<firestore_interop.AggregateQuerySnapshotJsImpl> {
static final _expando = Expando<AggregateQuerySnapshot>();
late final Map<String, Object> _data;
/// Creates a new Firestore from a [jsObject].
static AggregateQuerySnapshot getInstance(firestore_interop.AggregateQuerySnapshotJsImpl jsObject) {

return _expando[jsObject] ??= AggregateQuerySnapshot._fromJsObject(jsObject);
}

AggregateQuerySnapshot._fromJsObject(firestore_interop.AggregateQuerySnapshotJsImpl jsObject)
: _data = Map.from(dartify(jsObject.data())), super.fromJsObject(jsObject);

int get count => _data['count']! as int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,15 @@ external Object get arrayRemove;

@JS()
external Object get arrayUnion;


@JS()
external PromiseJsImpl<AggregateQuerySnapshotJsImpl> getCountFromServer(
QueryJsImpl query,
);


@JS('AggregateQuerySnapshot')
abstract class AggregateQuerySnapshotJsImpl {
external Map<String, Object> data();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_inte
import 'package:collection/collection.dart';
import 'package:cloud_firestore_web/src/utils/encode_utility.dart';

import 'aggregate_query_web.dart';
import 'internals.dart';
import 'interop/firestore.dart' as firestore_interop;
import 'utils/web_utils.dart';
Expand Down Expand Up @@ -232,4 +233,9 @@ class QueryWeb extends QueryPlatform {
'where': conditions,
});
}

@override
AggregateQueryPlatform count(){
return AggregateQueryWeb(this, _webQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
part of firebase_core_web;

/// The currently supported Firebase JS SDK version.
const String supportedFirebaseJsSdkVersion = '9.9.0';
const String supportedFirebaseJsSdkVersion = '9.11.0';