Skip to content

Commit

Permalink
[firestore] feat: support firestore aggregate query
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jan 30, 2024
1 parent 8227c16 commit 2efebfc
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
5 changes: 5 additions & 0 deletions firestore_flutter/_pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependency_overrides:
tekartik_firebase_flutter:
path: ../firebase_flutter
tekartik_firebase_firestore:
path: ../../firebase_firestore.dart/firestore
91 changes: 91 additions & 0 deletions firestore_flutter/lib/src/aggregate_query_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'package:tekartik_firebase_firestore_flutter/src/firestore_flutter.dart';

import 'import_firestore.dart';
import 'import_native.dart' as native;

class AggregateQueryFlutter implements AggregateQuery {
final QueryFlutter queryFlutter;
late final native.AggregateQuery nativeInstance;

native.AggregateField _convertField(AggregateField field) {
if (field is AggregateFieldCount) {
return native.count();
} else if (field is AggregateFieldAverage) {
return native.average(field.field);
} else if (field is AggregateFieldSum) {
return native.sum(field.field);
} else {
throw ArgumentError(field);
}
}

native.AggregateField? _convertFieldAt(
List<AggregateField> fields, int index) {
if (index < fields.length) {
return _convertField(fields[index]);
}
return null;
}

AggregateQueryFlutter(this.queryFlutter, List<AggregateField> fields) {
var i = 0;
nativeInstance = queryFlutter.nativeInstance.aggregate(
_convertFieldAt(fields, i++)!,
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
_convertFieldAt(fields, i++),
);
}

@override
Future<AggregateQuerySnapshot> get() async {
var nativeSnapshot = await nativeInstance.get();
return AggregateQuerySnapshotFlutter(this, nativeSnapshot);
}
}

class AggregateQuerySnapshotFlutter implements AggregateQuerySnapshot {
final AggregateQueryFlutter aggregateQueryFlutter;
late final native.AggregateQuerySnapshot nativeInstance;

AggregateQuerySnapshotFlutter(
this.aggregateQueryFlutter, this.nativeInstance);

@override
int? get count => nativeInstance.count;

@override
double? getAverage(String field) => nativeInstance.getAverage(field);

@override
double? getSum(String field) => nativeInstance.getSum(field);

@override
Query get query => aggregateQueryFlutter.queryFlutter;
}
9 changes: 9 additions & 0 deletions firestore_flutter/lib/src/firestore_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:meta/meta.dart';
import 'package:path/path.dart';
import 'package:tekartik_firebase_flutter/src/firebase_flutter.dart'; // ignore: implementation_imports

import 'aggregate_query_flutter.dart';
import 'document_snapshot_flutter.dart';
import 'import.dart';
import 'import_firestore.dart';
Expand Down Expand Up @@ -62,6 +63,9 @@ class FirestoreServiceFlutter

@override
bool get supportsTrackChanges => true;

@override
bool get supportsAggregateQueries => true;
}

class FirestoreFlutter with FirestoreDefaultMixin implements Firestore {
Expand Down Expand Up @@ -394,6 +398,11 @@ class QueryFlutter
whereIn: toNativeValues(whereIn),
isNull: isNull));
}

@override
AggregateQuery aggregate(List<AggregateField> fields) {
return AggregateQueryFlutter(this, fields);
}
}

mixin PathReferenceFlutterMixin {
Expand Down
4 changes: 4 additions & 0 deletions firestore_flutter/lib/src/import_native.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'package:cloud_firestore/cloud_firestore.dart';
// ignore: depend_on_referenced_packages
export 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart'
show AggregateField;
2 changes: 1 addition & 1 deletion firestore_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
url: https://github.com/tekartik/firebase_firestore.dart
path: firestore
ref: dart3a
version: '>=1.1.1'
version: '>=1.1.6'
tekartik_firebase_flutter:
git:
url: https://github.com/tekartik/firebase_flutter
Expand Down

0 comments on commit 2efebfc

Please sign in to comment.