Skip to content

Commit f1fbe90

Browse files
committed
Code cleanup
1 parent 89c05fa commit f1fbe90

File tree

5 files changed

+28
-59
lines changed

5 files changed

+28
-59
lines changed

packages/firestore/src/api/aggregate.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import { AggregateImpl } from '../core/aggregate';
2020
import { firestoreClientRunAggregateQuery } from '../core/firestore_client';
2121
import { count } from '../lite-api/aggregate';
2222
import {
23-
AggregateField,
24-
AggregateQuerySnapshot
23+
AggregateQuerySnapshot,
24+
CountAggregateSpec
2525
} from '../lite-api/aggregate_types';
2626
import { ObjectValue } from '../model/object_value';
2727
import { cast } from '../util/input_validation';
@@ -60,34 +60,12 @@ export {
6060
*/
6161
export function getCountFromServer(
6262
query: Query<unknown>
63-
): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> {
64-
const firestore = cast(query.firestore, Firestore);
65-
const client = ensureFirestoreConfigured(firestore);
66-
67-
const countQuerySpec: { count: AggregateField<number> } = {
63+
): Promise<AggregateQuerySnapshot<CountAggregateSpec>> {
64+
const countQuerySpec: CountAggregateSpec = {
6865
count: count()
6966
};
7067

71-
const internalAggregates = mapToArray(countQuerySpec, (aggregate, alias) => {
72-
return new AggregateImpl(
73-
alias,
74-
aggregate.aggregateType,
75-
aggregate._internalFieldPath
76-
);
77-
});
78-
79-
return firestoreClientRunAggregateQuery(
80-
client,
81-
query._query,
82-
internalAggregates
83-
).then(aggregateResult =>
84-
convertToAggregateQuerySnapshot(
85-
firestore,
86-
query,
87-
countQuerySpec,
88-
aggregateResult
89-
)
90-
);
68+
return getAggregateFromServer(query, countQuerySpec);
9169
}
9270

9371
/**

packages/firestore/src/core/aggregate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import { FieldPath } from '../model/path';
1919

20-
type AggregateType = 'count' | 'avg' | 'sum';
20+
/**
21+
* Union type representing the aggregate type to be performed.
22+
*/
23+
export type AggregateType = 'count' | 'avg' | 'sum';
2124

2225
/**
2326
* TODO

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import { mapToArray } from '../util/obj';
2626
import {
2727
AggregateField,
2828
AggregateQuerySnapshot,
29-
AggregateSpec
29+
AggregateSpec,
30+
CountAggregateSpec
3031
} from './aggregate_types';
3132
import { getDatastore } from './components';
3233
import { Firestore } from './database';
@@ -50,34 +51,12 @@ import { LiteUserDataWriter } from './reference_impl';
5051
*/
5152
export function getCount(
5253
query: Query<unknown>
53-
): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> {
54-
const firestore = cast(query.firestore, Firestore);
55-
const datastore = getDatastore(firestore);
56-
57-
const countQuerySpec: { count: AggregateField<number> } = {
54+
): Promise<AggregateQuerySnapshot<CountAggregateSpec>> {
55+
const countQuerySpec: CountAggregateSpec = {
5856
count: count()
5957
};
6058

61-
const internalAggregates = mapToArray(countQuerySpec, (aggregate, alias) => {
62-
return new AggregateImpl(
63-
alias,
64-
aggregate.aggregateType,
65-
aggregate._internalFieldPath
66-
);
67-
});
68-
69-
return invokeRunAggregationQueryRpc(
70-
datastore,
71-
query._query,
72-
internalAggregates
73-
).then(aggregateResult =>
74-
convertToAggregateQuerySnapshot(
75-
firestore,
76-
query,
77-
countQuerySpec,
78-
aggregateResult
79-
)
80-
);
59+
return getAggregate(query, countQuerySpec);
8160
}
8261

8362
/**

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { AggregateType } from '../core/aggregate';
1819
import { ObjectValue } from '../model/object_value';
1920
import { FieldPath as InternalFieldPath } from '../model/path';
2021

@@ -28,7 +29,7 @@ import { AbstractUserDataWriter } from './user_data_writer';
2829
/**
2930
* Union type representing the aggregate type to be performed.
3031
*/
31-
export type AggregateType = 'avg' | 'count' | 'sum';
32+
export { AggregateType };
3233

3334
/**
3435
* Represents an aggregation that can be performed by Firestore.
@@ -51,7 +52,7 @@ export class AggregateField<R> {
5152
*/
5253
constructor(
5354
public readonly aggregateType: AggregateType = 'count',
54-
methodName: string,
55+
methodName: string = 'new AggregateField',
5556
field?: string | FieldPath
5657
) {
5758
if (field !== undefined) {
@@ -75,6 +76,12 @@ export interface AggregateSpec {
7576
[field: string]: AggregateFieldType;
7677
}
7778

79+
/**
80+
* Helper AggregateSpec for count.
81+
* @internal
82+
*/
83+
export interface CountAggregateSpec { count: AggregateField<number> }
84+
7885
/**
7986
* A type whose keys are taken from an `AggregateSpec`, and whose values are the
8087
* result of the aggregation performed by the corresponding `AggregateField`

packages/firestore/test/lite/integration.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,9 +2701,11 @@ describe('Aggregate quries', () => {
27012701
where('key1', '==', 42),
27022702
where('key2', '<', 42)
27032703
);
2704-
await expect(getAggregate(query_, {
2705-
myCount: count()
2706-
})).to.be.eventually.rejectedWith(
2704+
await expect(
2705+
getAggregate(query_, {
2706+
myCount: count()
2707+
})
2708+
).to.be.eventually.rejectedWith(
27072709
/index.*https:\/\/console\.firebase\.google\.com/
27082710
);
27092711
});

0 commit comments

Comments
 (0)