15
15
* limitations under the License.
16
16
*/
17
17
18
- import { CompositeFilterOpEnum } from '../protos/firestore_proto_api' ;
18
+ import { Value } from '../protos/firestore_proto_api' ;
19
19
import { invokeRunAggregationQueryRpc } from '../remote/datastore' ;
20
20
import { hardAssert } from '../util/assert' ;
21
21
import { cast } from '../util/input_validation' ;
22
+
22
23
import { getDatastore } from './components' ;
23
24
import { Firestore } from './database' ;
24
25
import { Query , queryEqual } from './reference' ;
25
26
import { LiteUserDataWriter } from './reference_impl' ;
26
27
27
28
/**
28
- * A `AggregateQuery` computes some aggregation statistics from the result set of
29
+ * An `AggregateQuery` computes some aggregation statistics from the result set of
29
30
* a base `Query`.
30
31
*/
31
32
export class AggregateQuery {
@@ -44,7 +45,7 @@ export class AggregateQuery {
44
45
}
45
46
46
47
/**
47
- * A `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
48
+ * An `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
48
49
*/
49
50
export class AggregateQuerySnapshot {
50
51
readonly type = 'AggregateQuerySnapshot' ;
@@ -56,7 +57,7 @@ export class AggregateQuerySnapshot {
56
57
}
57
58
58
59
/**
59
- * @return The result of a document count aggregation. Returns null if no count aggregation is
60
+ * @returns The result of a document count aggregation. Returns null if no count aggregation is
60
61
* available in the result.
61
62
*/
62
63
getCount ( ) : number | null {
@@ -67,43 +68,42 @@ export class AggregateQuerySnapshot {
67
68
/**
68
69
* Creates an `AggregateQuery` counting the number of documents matching this query.
69
70
*
70
- * @return An `AggregateQuery` object that can be used to count the number of documents in
71
+ * @returns An `AggregateQuery` object that can be used to count the number of documents in
71
72
* the result set of this query.
72
73
*/
73
74
export function countQuery ( query : Query < unknown > ) : AggregateQuery {
74
75
return new AggregateQuery ( query ) ;
75
76
}
76
77
77
78
export function getAggregateFromServerDirect (
78
- aggregateQuery : AggregateQuery
79
+ query : AggregateQuery
79
80
) : Promise < AggregateQuerySnapshot > {
80
- const firestore = cast ( aggregateQuery . query . firestore , Firestore ) ;
81
+ const firestore = cast ( query . query . firestore , Firestore ) ;
81
82
const datastore = getDatastore ( firestore ) ;
82
83
const userDataWriter = new LiteUserDataWriter ( firestore ) ;
83
84
84
- return invokeRunAggregationQueryRpc ( datastore , aggregateQuery ) . then (
85
- result => {
86
- hardAssert (
87
- result [ 0 ] !== undefined ,
88
- 'Aggregation fields are missing from result.'
89
- ) ;
85
+ return invokeRunAggregationQueryRpc ( datastore , query ) . then ( result => {
86
+ hardAssert (
87
+ result [ 0 ] !== undefined ,
88
+ 'Aggregation fields are missing from result.'
89
+ ) ;
90
90
91
- const countField = ( result [ 0 ] as any ) . count_alias ;
92
- hardAssert (
93
- countField !== undefined ,
94
- 'Count field is missing from result.'
95
- ) ;
91
+ const counts = Object . entries ( result [ 0 ] )
92
+ . filter ( ( [ key , value ] ) => key === 'count_alias' )
93
+ . map ( ( [ key , value ] ) => userDataWriter . convertValue ( value as Value ) ) ;
96
94
97
- const countAggregateResult = userDataWriter . convertValue ( countField ) ;
98
- hardAssert (
99
- typeof countAggregateResult === 'number' ,
100
- 'Count aggeragte field is not a number: ' + countAggregateResult
101
- ) ;
102
- return Promise . resolve (
103
- new AggregateQuerySnapshot ( aggregateQuery , countAggregateResult )
104
- ) ;
105
- }
106
- ) ;
95
+ const count = counts [ 0 ] ;
96
+ hardAssert (
97
+ count !== undefined ,
98
+ 'count_alias property is missing from result.'
99
+ ) ;
100
+ hardAssert (
101
+ typeof count === 'number' ,
102
+ 'Count aggeragte field is not a number: ' + count
103
+ ) ;
104
+
105
+ return Promise . resolve ( new AggregateQuerySnapshot ( query , count ) ) ;
106
+ } ) ;
107
107
}
108
108
109
109
export function aggregateQueryEqual (
0 commit comments