@@ -22,18 +22,20 @@ import { Query, queryToTarget } from '../core/query';
2222import { Document } from '../model/document' ;
2323import { DocumentKey } from '../model/document_key' ;
2424import { Mutation } from '../model/mutation' ;
25- import { ObjectValue } from '../model/object_value' ;
2625import {
26+ ApiClientObjectMap ,
2727 BatchGetDocumentsRequest as ProtoBatchGetDocumentsRequest ,
2828 BatchGetDocumentsResponse as ProtoBatchGetDocumentsResponse ,
2929 RunAggregationQueryRequest as ProtoRunAggregationQueryRequest ,
3030 RunAggregationQueryResponse as ProtoRunAggregationQueryResponse ,
3131 RunQueryRequest as ProtoRunQueryRequest ,
32- RunQueryResponse as ProtoRunQueryResponse
32+ RunQueryResponse as ProtoRunQueryResponse ,
33+ Value
3334} from '../protos/firestore_proto_api' ;
3435import { debugAssert , debugCast , hardAssert } from '../util/assert' ;
3536import { AsyncQueue } from '../util/async_queue' ;
3637import { Code , FirestoreError } from '../util/error' ;
38+ import { isNullOrUndefined } from '../util/types' ;
3739
3840import { Connection } from './connection' ;
3941import {
@@ -50,8 +52,7 @@ import {
5052 toMutation ,
5153 toName ,
5254 toQueryTarget ,
53- toRunAggregationQueryRequest ,
54- fromAggregationResult
55+ toRunAggregationQueryRequest
5556} from './serializer' ;
5657
5758/**
@@ -243,9 +244,9 @@ export async function invokeRunAggregationQueryRpc(
243244 datastore : Datastore ,
244245 query : Query ,
245246 aggregates : Aggregate [ ]
246- ) : Promise < ObjectValue > {
247+ ) : Promise < ApiClientObjectMap < Value > > {
247248 const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
248- const request = toRunAggregationQueryRequest (
249+ const { request, aliasMap } = toRunAggregationQueryRequest (
249250 datastoreImpl . serializer ,
250251 queryToTarget ( query ) ,
251252 aggregates
@@ -267,8 +268,31 @@ export async function invokeRunAggregationQueryRpc(
267268 filteredResult . length === 1 ,
268269 'Aggregation fields are missing from result.'
269270 ) ;
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+ } , { } ) ;
270294
271- return fromAggregationResult ( filteredResult [ 0 ] ) ;
295+ return remappedFields ;
272296}
273297
274298export function newPersistentWriteStream (
0 commit comments