Skip to content

Commit 7b3eee2

Browse files
committed
add aggregateQuery and AggregateQuerySnapshot class
1 parent b92e492 commit 7b3eee2

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

common/api-review/firestore.api.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ export type AddPrefixToKeys<Prefix extends string, T extends Record<string, unkn
1717
[K in keyof T & string as `${Prefix}.${K}`]+?: T[K];
1818
};
1919

20+
// @public (undocumented)
21+
export class AggregateQuery<T = DocumentData> {
22+
// (undocumented)
23+
readonly query: Query<T>;
24+
// (undocumented)
25+
readonly type = "AggregateQuery";
26+
}
27+
28+
// @public (undocumented)
29+
export function aggregateQueryEqual(left: AggregateQuery, right: AggregateQuery): boolean;
30+
31+
// @public (undocumented)
32+
export class AggregateQuerySnapshot {
33+
// (undocumented)
34+
getCount(): number | null;
35+
// (undocumented)
36+
readonly query: AggregateQuery;
37+
// (undocumented)
38+
readonly type = "AggregateQuerySnapshot";
39+
}
40+
41+
// @public (undocumented)
42+
export function aggregateQuerySnapshotEqual(left: AggregateQuerySnapshot, right: AggregateQuerySnapshot): boolean;
43+
2044
// @public
2145
export function arrayRemove(...elements: unknown[]): FieldValue;
2246

@@ -69,6 +93,9 @@ export function connectFirestoreEmulator(firestore: Firestore, host: string, por
6993
mockUserToken?: EmulatorMockTokenOptions | string;
7094
}): void;
7195

96+
// @public (undocumented)
97+
export function countQuery(query: Query): AggregateQuery;
98+
7299
// @public
73100
export function deleteDoc(reference: DocumentReference<unknown>): Promise<void>;
74101

@@ -209,6 +236,9 @@ export class GeoPoint {
209236
};
210237
}
211238

239+
// @public (undocumented)
240+
export function getAggregateFromServerDirect(query: AggregateQuery): Promise<AggregateQuerySnapshot>;
241+
212242
// @public
213243
export function getDoc<T>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
214244

packages/firestore/src/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
export {
18+
AggregateQuery,
19+
AggregateQuerySnapshot,
20+
aggregateQueryEqual,
21+
aggregateQuerySnapshotEqual,
22+
countQuery,
23+
getAggregateFromServerDirect
24+
} from './api/aggregate';
1725

1826
export { FieldPath, documentId } from './api/field_path';
1927

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export {
19+
AggregateQuery,
20+
AggregateQuerySnapshot,
21+
aggregateQueryEqual,
22+
aggregateQuerySnapshotEqual,
23+
countQuery,
24+
getAggregateFromServerDirect
25+
} from '../lite-api/aggregate';
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {DocumentData, Query, queryEqual} from './reference';
19+
20+
21+
export class AggregateQuery<T = DocumentData>{
22+
readonly type = "AggregateQuery";
23+
readonly query: Query<T>;
24+
25+
/** @hideconstructor */
26+
constructor(query: Query<T>) {
27+
this.query = query;
28+
}
29+
}
30+
31+
export class AggregateQuerySnapshot {
32+
readonly type = "AggregateQuerySnapshot";
33+
readonly query: AggregateQuery;
34+
35+
/** @hideconstructor */
36+
constructor(query: AggregateQuery, readonly _count: number) {
37+
this.query = query;
38+
}
39+
40+
getCount(): number | null {
41+
return this._count;
42+
}
43+
}
44+
45+
export function countQuery(query: Query): AggregateQuery {
46+
return new AggregateQuery(query);
47+
}
48+
49+
export function getAggregateFromServerDirect(query: AggregateQuery): Promise<AggregateQuerySnapshot> {
50+
return Promise.resolve(new AggregateQuerySnapshot(query, 42));
51+
}
52+
53+
export function aggregateQueryEqual(left: AggregateQuery, right: AggregateQuery): boolean {
54+
return queryEqual(left.query, right.query);
55+
}
56+
57+
export function aggregateQuerySnapshotEqual(left: AggregateQuerySnapshot, right: AggregateQuerySnapshot): boolean {
58+
return aggregateQueryEqual(left.query, right.query) && left.getCount() === right.getCount();
59+
}

0 commit comments

Comments
 (0)