-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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): support for aggregate queries including sum() & average()
#8115
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
* | ||
*/ | ||
|
||
import static com.google.firebase.firestore.AggregateField.average; | ||
import static com.google.firebase.firestore.AggregateField.sum; | ||
import static io.invertase.firebase.firestore.ReactNativeFirebaseFirestoreCommon.rejectPromiseFirestoreException; | ||
import static io.invertase.firebase.firestore.ReactNativeFirebaseFirestoreSerialize.snapshotToWritableMap; | ||
import static io.invertase.firebase.firestore.UniversalFirebaseFirestoreCommon.getFirestoreForApp; | ||
|
@@ -26,6 +28,9 @@ | |
import com.facebook.react.bridge.*; | ||
import com.google.android.gms.tasks.Tasks; | ||
import com.google.firebase.firestore.*; | ||
|
||
import java.util.ArrayList; | ||
|
||
import io.invertase.firebase.common.ReactNativeFirebaseEventEmitter; | ||
import io.invertase.firebase.common.ReactNativeFirebaseModule; | ||
|
||
|
@@ -193,6 +198,99 @@ public void collectionCount( | |
}); | ||
} | ||
|
||
@ReactMethod | ||
public void aggregateQuery( | ||
String appName, | ||
String databaseId, | ||
String path, | ||
String type, | ||
ReadableArray filters, | ||
ReadableArray orders, | ||
ReadableMap options, | ||
ReadableArray aggregateQueries, | ||
Promise promise | ||
){ | ||
FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId); | ||
ReactNativeFirebaseFirestoreQuery firestoreQuery = | ||
new ReactNativeFirebaseFirestoreQuery( | ||
appName, | ||
databaseId, | ||
getQueryForFirestore(firebaseFirestore, path, type), | ||
filters, | ||
orders, | ||
options); | ||
|
||
ArrayList<AggregateField> aggregateFields = new ArrayList<>(); | ||
|
||
for (int i = 0; i < aggregateQueries.size(); i++) { | ||
ReadableMap aggregateQuery = aggregateQueries.getMap(i); | ||
|
||
String aggregateType = aggregateQuery.getString("aggregateType"); | ||
String fieldPath = aggregateQuery.getString("field"); | ||
|
||
assert aggregateType != null; | ||
switch (aggregateType) { | ||
case "count": | ||
aggregateFields.add(AggregateField.count()); | ||
break; | ||
case "sum": | ||
assert fieldPath != null; | ||
aggregateFields.add(AggregateField.sum(fieldPath)); | ||
break; | ||
case "average": | ||
assert fieldPath != null; | ||
aggregateFields.add(AggregateField.average(fieldPath)); | ||
break; | ||
default: | ||
throw new Error("Invalid AggregateType: " + aggregateType); | ||
} | ||
} | ||
AggregateQuery firestoreAggregateQuery = firestoreQuery.query.aggregate(aggregateFields.get(0), | ||
aggregateFields.subList(1, aggregateFields.size()).toArray(new AggregateField[0])); | ||
|
||
firestoreAggregateQuery | ||
.get(AggregateSource.SERVER) | ||
.addOnCompleteListener( | ||
task -> { | ||
if (task.isSuccessful()) { | ||
WritableMap result = Arguments.createMap(); | ||
AggregateQuerySnapshot snapshot = task.getResult(); | ||
|
||
for (int k = 0; k < aggregateQueries.size(); k++) { | ||
ReadableMap aggQuery = aggregateQueries.getMap(k); | ||
String aggType = aggQuery.getString("aggregateType"); | ||
String field = aggQuery.getString("field"); | ||
String key = aggQuery.getString("key"); | ||
assert key != null; | ||
assert aggType != null; | ||
switch (aggType) { | ||
case "count": | ||
result.putDouble(key, Long.valueOf(snapshot.getCount()).doubleValue()); | ||
break; | ||
case "sum": | ||
assert field != null; | ||
Number sum = (Number) snapshot.get(sum(field)); | ||
assert sum != null; | ||
result.putDouble(key, sum.doubleValue()); | ||
break; | ||
case "average": | ||
assert field != null; | ||
Number average = snapshot.get(average(field)); | ||
assert average != null; | ||
result.putDouble(key, average.doubleValue()); | ||
break; | ||
default: | ||
throw new Error("Invalid AggregateType: " + aggType); | ||
} | ||
} | ||
|
||
promise.resolve(result); | ||
} else { | ||
rejectPromiseFirestoreException(promise, task.getException()); | ||
} | ||
}); | ||
} | ||
|
||
@ReactMethod | ||
public void collectionGet( | ||
String appName, | ||
|
@@ -214,6 +312,8 @@ public void collectionGet( | |
orders, | ||
options); | ||
handleQueryGet(firestoreQuery, getSource(getOptions), promise); | ||
|
||
|
||
Comment on lines
+315
to
+316
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. low-value comment: that should fail |
||
} | ||
|
||
private void handleQueryOnSnapshot( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,83 @@ - (void)invalidate { | |
}]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(aggregateQuery | ||
: (FIRApp *)firebaseApp | ||
: (NSString *)databaseId | ||
: (NSString *)path | ||
: (NSString *)type | ||
: (NSArray *)filters | ||
: (NSArray *)orders | ||
: (NSDictionary *)options | ||
: (NSArray *)aggregateQueries | ||
: (RCTPromiseResolveBlock)resolve | ||
: (RCTPromiseRejectBlock)reject) { | ||
FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp | ||
databaseId:databaseId]; | ||
FIRQuery *query = [RNFBFirestoreCommon getQueryForFirestore:firestore path:path type:type]; | ||
|
||
NSMutableArray<FIRAggregateField *> *aggregateFields = | ||
[[NSMutableArray<FIRAggregateField *> alloc] init]; | ||
|
||
for (NSDictionary *aggregateQuery in aggregateQueries) { | ||
NSString *aggregateType = aggregateQuery[@"aggregateType"]; | ||
NSString *fieldPath = aggregateQuery[@"field"]; | ||
assert(aggregateType); | ||
assert(fieldPath); | ||
|
||
if([aggregateType isEqualToString:@"count"]){ | ||
[aggregateFields addObject:[FIRAggregateField aggregateFieldForCount]]; | ||
} else if([aggregateType isEqualToString:@"sum"]){ | ||
[aggregateFields | ||
addObject:[FIRAggregateField aggregateFieldForSumOfField:fieldPath]]; | ||
} else if([aggregateType isEqualToString:@"average"]){ | ||
[aggregateFields | ||
addObject:[FIRAggregateField aggregateFieldForAverageOfField:fieldPath]]; | ||
} else { | ||
NSString *reason = [@"Invalid Aggregate Type: " stringByAppendingString:aggregateType]; | ||
@throw [NSException exceptionWithName:@"RNFB Firestore: Invalid Aggregate Type" | ||
reason:reason | ||
userInfo:nil]; | ||
} | ||
} | ||
|
||
FIRAggregateQuery *aggregateQuery = [query aggregate:aggregateFields]; | ||
|
||
[aggregateQuery | ||
aggregationWithSource:FIRAggregateSourceServer | ||
completion:^(FIRAggregateQuerySnapshot *_Nullable snapshot, | ||
NSError *_Nullable error) { | ||
if (error) { | ||
[RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error]; | ||
} else { | ||
NSMutableDictionary *snapshotMap = [NSMutableDictionary dictionary]; | ||
|
||
for (NSDictionary *aggregateQuery in aggregateQueries) { | ||
NSString *aggregateType = aggregateQuery[@"aggregateType"]; | ||
NSString *fieldPath = aggregateQuery[@"field"]; | ||
NSString *key = aggregateQuery[@"key"]; | ||
assert(key); | ||
|
||
if([aggregateType isEqualToString:@"count"]){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also really low-value comment (because, lint...) but |
||
snapshotMap[key] = snapshot.count; | ||
} else if([aggregateType isEqualToString:@"sum"]){ | ||
NSNumber *sum = [snapshot | ||
valueForAggregateField:[FIRAggregateField | ||
aggregateFieldForSumOfField:fieldPath]]; | ||
snapshotMap[key] = sum; | ||
} else if([aggregateType isEqualToString:@"average"]){ | ||
NSNumber *average = [snapshot | ||
valueForAggregateField: | ||
[FIRAggregateField | ||
aggregateFieldForAverageOfField:fieldPath]]; | ||
snapshotMap[key] = (average == nil ? [NSNull null] : average); | ||
} | ||
} | ||
resolve(snapshotMap); | ||
} | ||
}]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(collectionGet | ||
: (FIRApp *)firebaseApp | ||
: (NSString *)databaseId | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are any of these asserts useful? It was my understanding that assert verifications are disabled by default so this would never really trigger anything, and if it did trigger the whole app dies (via an AssertionError) without notice of what did it since there is no exception handler set to catch Error (vs a try/catch with typical catch of Exception wrapping a block where you use Object.requireNonNull