Skip to content

Commit

Permalink
Mila/count add api interface (#6499)
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL authored Aug 4, 2022
1 parent 598e948 commit 7f42490
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
30 changes: 30 additions & 0 deletions common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ export type AddPrefixToKeys<Prefix extends string, T extends Record<string, unkn
[K in keyof T & string as `${Prefix}.${K}`]+?: T[K];
};

// @public (undocumented)
export class AggregateQuery<T = DocumentData> {
// (undocumented)
readonly query: Query<T>;
// (undocumented)
readonly type = "AggregateQuery";
}

// @public (undocumented)
export function aggregateQueryEqual(left: AggregateQuery, right: AggregateQuery): boolean;

// @public (undocumented)
export class AggregateQuerySnapshot {
// (undocumented)
getCount(): number | null;
// (undocumented)
readonly query: AggregateQuery;
// (undocumented)
readonly type = "AggregateQuerySnapshot";
}

// @public (undocumented)
export function aggregateQuerySnapshotEqual(left: AggregateQuerySnapshot, right: AggregateQuerySnapshot): boolean;

// @public
export function arrayRemove(...elements: unknown[]): FieldValue;

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

// @public (undocumented)
export function countQuery(query: Query): AggregateQuery;

// @public
export function deleteDoc(reference: DocumentReference<unknown>): Promise<void>;

Expand Down Expand Up @@ -209,6 +236,9 @@ export class GeoPoint {
};
}

// @public (undocumented)
export function getAggregateFromServerDirect(query: AggregateQuery): Promise<AggregateQuerySnapshot>;

// @public
export function getDoc<T>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;

Expand Down
9 changes: 9 additions & 0 deletions packages/firestore/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
* limitations under the License.
*/

export {
AggregateQuery,
AggregateQuerySnapshot,
aggregateQueryEqual,
aggregateQuerySnapshotEqual,
countQuery,
getAggregateFromServerDirect
} from './api/aggregate';

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

export {
Expand Down
25 changes: 25 additions & 0 deletions packages/firestore/src/api/aggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export {
AggregateQuery,
AggregateQuerySnapshot,
aggregateQueryEqual,
aggregateQuerySnapshotEqual,
countQuery,
getAggregateFromServerDirect
} from '../lite-api/aggregate';
69 changes: 69 additions & 0 deletions packages/firestore/src/lite-api/aggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DocumentData, Query, queryEqual } from './reference';

export class AggregateQuery<T = DocumentData> {
readonly type = 'AggregateQuery';
readonly query: Query<T>;

/** @hideconstructor */
constructor(query: Query<T>) {
this.query = query;
}
}

export class AggregateQuerySnapshot {
readonly type = 'AggregateQuerySnapshot';
readonly query: AggregateQuery;

/** @hideconstructor */
constructor(query: AggregateQuery, readonly _count: number) {
this.query = query;
}

getCount(): number | null {
return this._count;
}
}

export function countQuery(query: Query): AggregateQuery {
return new AggregateQuery(query);
}

export function getAggregateFromServerDirect(
query: AggregateQuery
): Promise<AggregateQuerySnapshot> {
return Promise.resolve(new AggregateQuerySnapshot(query, 42));
}

export function aggregateQueryEqual(
left: AggregateQuery,
right: AggregateQuery
): boolean {
return queryEqual(left.query, right.query);
}

export function aggregateQuerySnapshotEqual(
left: AggregateQuerySnapshot,
right: AggregateQuerySnapshot
): boolean {
return (
aggregateQueryEqual(left.query, right.query) &&
left.getCount() === right.getCount()
);
}

0 comments on commit 7f42490

Please sign in to comment.