@@ -24,28 +24,30 @@ import { cast } from '../util/input_validation';
24
24
25
25
import { getDatastore } from './components' ;
26
26
import { Firestore } from './database' ;
27
- import { DocumentFieldValue , Query , queryEqual } from './reference' ;
27
+ import { Query , queryEqual } from './reference' ;
28
28
import { LiteUserDataWriter } from './reference_impl' ;
29
29
30
30
/**
31
- * An `AggregateField` computes some aggregation statistics from the result set of
32
- * an aggregation query.
31
+ * An `AggregateField`that captures input type T.
33
32
*/
34
33
export class AggregateField < T > {
35
34
type = 'AggregateField' ;
36
- _datum ?: T ;
35
+ }
37
36
38
- constructor ( readonly subType : string ) { }
37
+ /**
38
+ * Creates and returns an aggregation field that counts the documents in the result set.
39
+ *
40
+ * @returns An `AggregateField` object with number input type.
41
+ */
42
+ export function count ( ) : AggregateField < number > {
43
+ return new AggregateField < number > ( ) ;
39
44
}
40
45
41
46
/**
42
47
* The union of all `AggregateField` types that are returned from the factory
43
48
* functions.
44
49
*/
45
- export type AggregateFieldType =
46
- | AggregateField < number >
47
- | AggregateField < DocumentFieldValue | undefined >
48
- | AggregateField < number | undefined > ;
50
+ type AggregateFieldType = ReturnType < typeof count > ;
49
51
50
52
/**
51
53
* A type whose values are all `AggregateField` objects.
@@ -60,46 +62,28 @@ export type AggregateSpec = { [field: string]: AggregateFieldType };
60
62
* `AggregateField` from the input `AggregateSpec`.
61
63
*/
62
64
export type AggregateSpecData < T extends AggregateSpec > = {
63
- [ Property in keyof T ] -? : T [ Property ] [ '_datum' ] ;
65
+ [ P in keyof T ] : T [ P ] extends AggregateField < infer U > ? U : never ;
64
66
} ;
65
67
66
- /**
67
- * Creates and returns an aggregation field that counts the documents in the result set.
68
- * @returns An `AggregateField` object that includes number of documents.
69
- */
70
- export function count ( ) : AggregateField < number > {
71
- return new AggregateField < number > ( 'count' ) ;
72
- }
73
-
74
- /**
75
- * Compares two `AggregateField` instances for equality.
76
- * The two `AggregateField` instances are considered "equal" if and only if
77
- * they were created by the same factory function (e.g. `count()`, `min()`, and
78
- * `sum()`) with "equal" arguments.
79
- */
80
- export function aggregateFieldEqual (
81
- left : AggregateField < unknown > ,
82
- right : AggregateField < unknown >
83
- ) : boolean {
84
- return typeof left === typeof right && left . subType === right . subType ;
85
- }
86
-
87
68
/**
88
69
* An `AggregateQuerySnapshot` contains the results of running an aggregate query.
89
70
*/
90
71
export class AggregateQuerySnapshot < T extends AggregateSpec > {
91
72
readonly type = 'AggregateQuerySnapshot' ;
92
73
74
+ /** @hideconstructor */
93
75
constructor (
94
76
readonly query : Query < unknown > ,
95
- protected readonly _data : AggregateSpecData < T >
77
+ private readonly _data : AggregateSpecData < T >
96
78
) { }
97
79
98
80
/**
99
81
* The results of the requested aggregations. The keys of the returned object
100
82
* will be the same as those of the `AggregateSpec` object specified to the
101
83
* aggregation method, and the values will be the corresponding aggregation
102
84
* result.
85
+ *
86
+ * @returns The aggregation statistics result of running a query.
103
87
*/
104
88
data ( ) : AggregateSpecData < T > {
105
89
return this . _data ;
@@ -112,10 +96,11 @@ export class AggregateQuerySnapshot<T extends AggregateSpec> {
112
96
* whatever the server returns. If the server cannot be reached then the
113
97
* returned promise will be rejected.
114
98
*
115
- * This is a convenience shorthand for:
116
- * getAggregateFromServer(query, { count: count() }).
99
+ * @param query - The `Query` to execute.
100
+ *
101
+ * @returns An `AggregateQuerySnapshot` that contains the number of documents.
117
102
*/
118
- export function getCountFromServer (
103
+ export function getCount (
119
104
query : Query < unknown >
120
105
) : Promise < AggregateQuerySnapshot < { count : AggregateField < number > } > > {
121
106
const firestore = cast ( query . firestore , Firestore ) ;
@@ -150,6 +135,11 @@ export function getCountFromServer(
150
135
* Compares two `AggregateQuerySnapshot` instances for equality.
151
136
* Two `AggregateQuerySnapshot` instances are considered "equal" if they have
152
137
* the same underlying query, the same metadata, and the same data.
138
+ *
139
+ * @param left - The `AggregateQuerySnapshot` to compare.
140
+ * @param right - The `AggregateQuerySnapshot` to compare.
141
+ *
142
+ * @returns true if the AggregateQuerySnapshos are equal.
153
143
*/
154
144
export function aggregateSnapshotEqual < T extends AggregateSpec > (
155
145
left : AggregateQuerySnapshot < T > ,
0 commit comments