@@ -22,18 +22,20 @@ import { Query, queryToTarget } from '../core/query';
22
22
import { Document } from '../model/document' ;
23
23
import { DocumentKey } from '../model/document_key' ;
24
24
import { Mutation } from '../model/mutation' ;
25
- import { ObjectValue } from '../model/object_value' ;
26
25
import {
26
+ ApiClientObjectMap ,
27
27
BatchGetDocumentsRequest as ProtoBatchGetDocumentsRequest ,
28
28
BatchGetDocumentsResponse as ProtoBatchGetDocumentsResponse ,
29
29
RunAggregationQueryRequest as ProtoRunAggregationQueryRequest ,
30
30
RunAggregationQueryResponse as ProtoRunAggregationQueryResponse ,
31
31
RunQueryRequest as ProtoRunQueryRequest ,
32
- RunQueryResponse as ProtoRunQueryResponse
32
+ RunQueryResponse as ProtoRunQueryResponse ,
33
+ Value
33
34
} from '../protos/firestore_proto_api' ;
34
35
import { debugAssert , debugCast , hardAssert } from '../util/assert' ;
35
36
import { AsyncQueue } from '../util/async_queue' ;
36
37
import { Code , FirestoreError } from '../util/error' ;
38
+ import { isNullOrUndefined } from '../util/types' ;
37
39
38
40
import { Connection } from './connection' ;
39
41
import {
@@ -50,8 +52,7 @@ import {
50
52
toMutation ,
51
53
toName ,
52
54
toQueryTarget ,
53
- toRunAggregationQueryRequest ,
54
- fromAggregationResult
55
+ toRunAggregationQueryRequest
55
56
} from './serializer' ;
56
57
57
58
/**
@@ -243,9 +244,9 @@ export async function invokeRunAggregationQueryRpc(
243
244
datastore : Datastore ,
244
245
query : Query ,
245
246
aggregates : Aggregate [ ]
246
- ) : Promise < ObjectValue > {
247
+ ) : Promise < ApiClientObjectMap < Value > > {
247
248
const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
248
- const request = toRunAggregationQueryRequest (
249
+ const { request, aliasMap } = toRunAggregationQueryRequest (
249
250
datastoreImpl . serializer ,
250
251
queryToTarget ( query ) ,
251
252
aggregates
@@ -267,8 +268,31 @@ export async function invokeRunAggregationQueryRpc(
267
268
filteredResult . length === 1 ,
268
269
'Aggregation fields are missing from result.'
269
270
) ;
271
+ debugAssert (
272
+ ! isNullOrUndefined ( filteredResult [ 0 ] . result ) ,
273
+ 'aggregationQueryResponse.result'
274
+ ) ;
275
+ debugAssert (
276
+ ! isNullOrUndefined ( filteredResult [ 0 ] . result . aggregateFields ) ,
277
+ 'aggregationQueryResponse.result.aggregateFields'
278
+ ) ;
279
+
280
+ // Remap the short-form aliases that were sent to the server
281
+ // to the client-side aliases. Users will access the results
282
+ // using the client-side alias.
283
+ const unmappedAggregateFields = filteredResult [ 0 ] . result ?. aggregateFields ;
284
+ const remappedFields = Object . keys ( unmappedAggregateFields ) . reduce <
285
+ ApiClientObjectMap < Value >
286
+ > ( ( accumulator , key ) => {
287
+ debugAssert (
288
+ ! isNullOrUndefined ( aliasMap [ key ] ) ,
289
+ `'${ key } ' not present in aliasMap result`
290
+ ) ;
291
+ accumulator [ aliasMap [ key ] ] = unmappedAggregateFields [ key ] ! ;
292
+ return accumulator ;
293
+ } , { } ) ;
270
294
271
- return fromAggregationResult ( filteredResult [ 0 ] ) ;
295
+ return remappedFields ;
272
296
}
273
297
274
298
export function newPersistentWriteStream (
0 commit comments