-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[firestore] feat: support firestore aggregate query
- Loading branch information
1 parent
8227c16
commit 2efebfc
Showing
5 changed files
with
110 additions
and
1 deletion.
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
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 |
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,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; | ||
} |
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
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; |
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