Skip to content

Commit 332096a

Browse files
committed
Comments and cleanup
1 parent ee14b2d commit 332096a

File tree

4 files changed

+78
-61
lines changed

4 files changed

+78
-61
lines changed

packages/firestore/src/api/aggregate.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,26 @@ export function getCountFromServer(
6666
}
6767

6868
/**
69-
* TODO
70-
* @param query
71-
* @param aggregateSpec
69+
* Calculates the specified aggregations over the documents in the result
70+
* set of the given query, without actually downloading the documents.
71+
*
72+
* Using this function to perform aggregations is efficient because only the
73+
* final aggregation values, not the documents' data, is downloaded. This
74+
* function can even perform aggregations if the documents if the result set
75+
* would be prohibitively large to download entirely (e.g. thousands of documents).
76+
*
77+
* The result received from the server is presented, unaltered, without
78+
* considering any local state. That is, documents in the local cache are not
79+
* taken into consideration, neither are local modifications not yet
80+
* synchronized with the server. Previously-downloaded results, if any, are not
81+
* used: every request using this source necessarily involves a round trip to
82+
* the server.
83+
*
84+
* @param query The query whose result set to aggregate over.
85+
* @param aggregateSpec An `AggregateSpec` object that specifies the aggregates
86+
* to perform over the result set. The AggregateSpec specifies aliases for each
87+
* aggregate, which can be used to retrieve the aggregate result.
88+
* @internal TODO (sum/avg) remove when public
7289
*/
7390
export function getAggregateFromServer<T extends AggregateSpec>(
7491
query: Query<unknown>,
@@ -78,37 +95,41 @@ export function getAggregateFromServer<T extends AggregateSpec>(
7895
const client = ensureFirestoreConfigured(firestore);
7996

8097
const internalAggregates = mapToArray(aggregateSpec, (aggregate, alias) => {
98+
// TODO (sum/avg) should alias validation be performed or should that be
99+
// delegated to the backend?
100+
81101
return new AggregateImpl(
82102
alias,
83103
aggregate.aggregateType,
84104
aggregate._internalFieldPath
85105
);
86106
});
87107

108+
// Run the aggregation and convert the results
88109
return firestoreClientRunAggregateQuery(
89110
client,
90111
query._query,
91112
internalAggregates
92113
).then(aggregateResult =>
93-
convertToAggregateQuerySnapshot(
94-
firestore,
95-
query,
96-
aggregateSpec,
97-
aggregateResult
98-
)
114+
convertToAggregateQuerySnapshot(firestore, query, aggregateResult)
99115
);
100116
}
101117

118+
/**
119+
* Converts the core aggregration result to an `AggregateQuerySnapshot`
120+
* that can be returned to the consumer.
121+
* @param query
122+
* @param aggregateResult Core aggregation result
123+
* @internal
124+
*/
102125
function convertToAggregateQuerySnapshot<T extends AggregateSpec>(
103126
firestore: Firestore,
104127
query: Query<unknown>,
105-
ref: T,
106128
aggregateResult: ObjectValue
107129
): AggregateQuerySnapshot<T> {
108130
const userDataWriter = new ExpUserDataWriter(firestore);
109131
const querySnapshot = new AggregateQuerySnapshot<T>(
110132
query,
111-
firestore,
112133
userDataWriter,
113134
aggregateResult
114135
);

packages/firestore/src/core/aggregate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { FieldPath } from '../model/path';
1919

2020
/**
2121
* Union type representing the aggregate type to be performed.
22+
* @internal
2223
*/
2324
export type AggregateType = 'count' | 'avg' | 'sum';
2425

packages/firestore/src/lite-api/aggregate.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { Firestore } from './database';
3333
import { FieldPath } from './field_path';
3434
import { Query, queryEqual } from './reference';
3535
import { LiteUserDataWriter } from './reference_impl';
36+
import { fieldPathFromArgument } from './user_data_reader';
3637

3738
/**
3839
* Calculates the number of documents in the result set of the given query,
@@ -59,9 +60,19 @@ export function getCount(
5960
}
6061

6162
/**
62-
* TODO
63-
* @param query
64-
* @param aggregates
63+
* Calculates the specified aggregations over the documents in the result
64+
* set of the given query, without actually downloading the documents.
65+
*
66+
* Using this function to perform aggregations is efficient because only the
67+
* final aggregation values, not the documents' data, is downloaded. This
68+
* function can even perform aggregations if the documents if the result set
69+
* would be prohibitively large to download entirely (e.g. thousands of documents).
70+
*
71+
* @param query The query whose result set to aggregate over.
72+
* @param aggregateSpec An `AggregateSpec` object that specifies the aggregates
73+
* to perform over the result set. The AggregateSpec specifies aliases for each
74+
* aggregate, which can be used to retrieve the aggregate result.
75+
* @internal TODO (sum/avg) remove when public
6576
*/
6677
export function getAggregate<T extends AggregateSpec>(
6778
query: Query<unknown>,
@@ -71,73 +82,76 @@ export function getAggregate<T extends AggregateSpec>(
7182
const datastore = getDatastore(firestore);
7283

7384
const internalAggregates = mapToArray(aggregateSpec, (aggregate, alias) => {
85+
// TODO (sum/avg) should alias validation be performed or should that be
86+
// delegated to the backend?
87+
7488
return new AggregateImpl(
7589
alias,
7690
aggregate.aggregateType,
7791
aggregate._internalFieldPath
7892
);
7993
});
8094

95+
// Run the aggregation and convert the results
8196
return invokeRunAggregationQueryRpc(
8297
datastore,
8398
query._query,
8499
internalAggregates
85100
).then(aggregateResult =>
86-
convertToAggregateQuerySnapshot(
87-
firestore,
88-
query,
89-
aggregateSpec,
90-
aggregateResult
91-
)
101+
convertToAggregateQuerySnapshot(firestore, query, aggregateResult)
92102
);
93103
}
94104

95105
function convertToAggregateQuerySnapshot<T extends AggregateSpec>(
96106
firestore: Firestore,
97107
query: Query<unknown>,
98-
ref: T,
99108
aggregateResult: ObjectValue
100109
): AggregateQuerySnapshot<T> {
101110
const userDataWriter = new LiteUserDataWriter(firestore);
102111
const querySnapshot = new AggregateQuerySnapshot<T>(
103112
query,
104-
firestore,
105113
userDataWriter,
106114
aggregateResult
107115
);
108116
return querySnapshot;
109117
}
110118

111119
/**
112-
* TODO
113-
* @param field
120+
* Create an AggregateField object that can be used to compute the sum of
121+
* a specified field over a range of documents in the result set of a query.
122+
* @param field Specifies the field to sum across the result set.
123+
* @internal TODO (sum/avg) remove when public
114124
*/
115125
export function sum(field: string | FieldPath): AggregateField<number> {
116-
return new AggregateField('sum', 'sum', field);
126+
return new AggregateField('sum', fieldPathFromArgument('sum', field));
117127
}
118128

119129
/**
120-
* TODO
121-
* @param field
130+
* Create an AggregateField object that can be used to compute the average of
131+
* a specified field over a range of documents in the result set of a query.
132+
* @param field Specifies the field to average across the result set.
133+
* @internal TODO (sum/avg) remove when public
122134
*/
123135
export function average(
124136
field: string | FieldPath
125137
): AggregateField<number | null> {
126-
return new AggregateField('avg', 'average', field);
138+
return new AggregateField('avg', fieldPathFromArgument('average', field));
127139
}
128140

129141
/**
130-
* TODO
142+
* Create an AggregateField object that can be used to compute the count of
143+
* documents in the result set of a query.
144+
* @internal TODO (sum/avg) remove when public
131145
*/
132146
export function count(): AggregateField<number> {
133-
return new AggregateField('count', 'count');
147+
return new AggregateField('count');
134148
}
135149

136150
/**
137151
* Compares two 'AggregateField` instances for equality.
138152
*
139-
* @param left
140-
* @param right
153+
* @param left Compare this AggregateField to the `right`.
154+
* @param right Compare this AggregateField to the `left`.
141155
*/
142156
export function aggregateFieldEqual(
143157
left: AggregateField<unknown>,

packages/firestore/src/lite-api/aggregate_types.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@ import { AggregateType } from '../core/aggregate';
1919
import { ObjectValue } from '../model/object_value';
2020
import { FieldPath as InternalFieldPath } from '../model/path';
2121

22-
import { average, count, sum } from './aggregate';
23-
import { Firestore } from './database';
24-
import { FieldPath } from './field_path';
2522
import { Query } from './reference';
26-
import { fieldPathFromArgument } from './user_data_reader';
2723
import { AbstractUserDataWriter } from './user_data_writer';
2824

29-
/**
30-
* Union type representing the aggregate type to be performed.
31-
*/
3225
export { AggregateType };
3326

3427
/**
@@ -39,38 +32,28 @@ export class AggregateField<R> {
3932
/** A type string to uniquely identify instances of this class. */
4033
readonly type = 'AggregateField';
4134

42-
/**
43-
* @internal
44-
*/
45-
readonly _internalFieldPath: InternalFieldPath | undefined;
46-
4735
/**
4836
* Create a new AggregateField<R>
49-
* @param aggregateType
50-
* @param field Optional
51-
* @param methodName
37+
* @param aggregateType Specifies the type of aggregation operation to perform.
38+
* @param _internalFieldPath Optionally specifies the field that is aggregated.
39+
* @internal
5240
*/
5341
constructor(
54-
public readonly aggregateType: AggregateType = 'count',
55-
methodName: string = 'new AggregateField',
56-
field?: string | FieldPath
57-
) {
58-
if (field !== undefined) {
59-
this._internalFieldPath = fieldPathFromArgument(methodName, field);
60-
}
61-
}
42+
// TODO (sum/avg) make aggregateType public when the feature is supported
43+
private readonly aggregateType: AggregateType = 'count',
44+
private readonly _internalFieldPath?: InternalFieldPath
45+
) {}
6246
}
6347

6448
/**
6549
* The union of all `AggregateField` types that are supported by Firestore.
6650
*/
6751
export type AggregateFieldType =
68-
| ReturnType<typeof count>
69-
| ReturnType<typeof sum>
70-
| ReturnType<typeof average>;
52+
| AggregateField<number>
53+
| AggregateField<number | null>;
7154

7255
/**
73-
* A type whose property values are all `AggregateField` objects.
56+
* Specifies a set of aggregations and their aliases.
7457
*/
7558
export interface AggregateSpec {
7659
[field: string]: AggregateFieldType;
@@ -92,7 +75,6 @@ export class AggregateQuerySnapshot<T extends AggregateSpec> {
9275
/** A type string to uniquely identify instances of this class. */
9376
readonly type = 'AggregateQuerySnapshot';
9477

95-
// TODO(sum/avg) can this be removed?
9678
/**
9779
* The underlying query over which the aggregations recorded in this
9880
* `AggregateQuerySnapshot` were performed.
@@ -102,7 +84,6 @@ export class AggregateQuerySnapshot<T extends AggregateSpec> {
10284
/** @hideconstructor */
10385
constructor(
10486
query: Query<unknown>,
105-
private readonly _firestore: Firestore,
10687
private readonly _userDataWriter: AbstractUserDataWriter,
10788
private readonly _data: ObjectValue
10889
) {

0 commit comments

Comments
 (0)