|
| 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 { deepEqual } from '@firebase/util'; |
| 19 | + |
| 20 | +import { Value } from '../protos/firestore_proto_api'; |
| 21 | +import { invokeRunAggregationQueryRpc } from '../remote/datastore'; |
| 22 | +import { hardAssert } from '../util/assert'; |
| 23 | +import { cast } from '../util/input_validation'; |
| 24 | + |
| 25 | +import { getDatastore } from './components'; |
| 26 | +import { Firestore } from './database'; |
| 27 | +import { Query, queryEqual } from './reference'; |
| 28 | +import { LiteUserDataWriter } from './reference_impl'; |
| 29 | + |
| 30 | +/** |
| 31 | + * An `AggregateField`that captures input type T. |
| 32 | + */ |
| 33 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 34 | +export class AggregateField<T> { |
| 35 | + type = 'AggregateField'; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Creates and returns an aggregation field that counts the documents in the result set. |
| 40 | + * @returns An `AggregateField` object with number input type. |
| 41 | + */ |
| 42 | +export function count(): AggregateField<number> { |
| 43 | + return new AggregateField<number>(); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * The union of all `AggregateField` types that are returned from the factory |
| 48 | + * functions. |
| 49 | + */ |
| 50 | +export type AggregateFieldType = ReturnType<typeof count>; |
| 51 | + |
| 52 | +/** |
| 53 | + * A type whose values are all `AggregateField` objects. |
| 54 | + * This is used as an argument to the "getter" functions, and the snapshot will |
| 55 | + * map the same names to the corresponding values. |
| 56 | + */ |
| 57 | +export interface AggregateSpec { |
| 58 | + [field: string]: AggregateFieldType; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * A type whose keys are taken from an `AggregateSpec` type, and whose values |
| 63 | + * are the result of the aggregation performed by the corresponding |
| 64 | + * `AggregateField` from the input `AggregateSpec`. |
| 65 | + */ |
| 66 | +export type AggregateSpecData<T extends AggregateSpec> = { |
| 67 | + [P in keyof T]: T[P] extends AggregateField<infer U> ? U : never; |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * An `AggregateQuerySnapshot` contains the results of running an aggregate query. |
| 72 | + */ |
| 73 | +export class AggregateQuerySnapshot<T extends AggregateSpec> { |
| 74 | + readonly type = 'AggregateQuerySnapshot'; |
| 75 | + |
| 76 | + /** @hideconstructor */ |
| 77 | + constructor( |
| 78 | + readonly query: Query<unknown>, |
| 79 | + private readonly _data: AggregateSpecData<T> |
| 80 | + ) {} |
| 81 | + |
| 82 | + /** |
| 83 | + * The results of the requested aggregations. The keys of the returned object |
| 84 | + * will be the same as those of the `AggregateSpec` object specified to the |
| 85 | + * aggregation method, and the values will be the corresponding aggregation |
| 86 | + * result. |
| 87 | + * |
| 88 | + * @returns The aggregation statistics result of running a query. |
| 89 | + */ |
| 90 | + data(): AggregateSpecData<T> { |
| 91 | + return this._data; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Counts the number of documents in the result set of the given query, ignoring |
| 97 | + * any locally-cached data and any locally-pending writes and simply surfacing |
| 98 | + * whatever the server returns. If the server cannot be reached then the |
| 99 | + * returned promise will be rejected. |
| 100 | + * |
| 101 | + * @param query - The `Query` to execute. |
| 102 | + * |
| 103 | + * @returns An `AggregateQuerySnapshot` that contains the number of documents. |
| 104 | + */ |
| 105 | +export function getCount( |
| 106 | + query: Query<unknown> |
| 107 | +): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> { |
| 108 | + const firestore = cast(query.firestore, Firestore); |
| 109 | + const datastore = getDatastore(firestore); |
| 110 | + const userDataWriter = new LiteUserDataWriter(firestore); |
| 111 | + return invokeRunAggregationQueryRpc(datastore, query._query).then(result => { |
| 112 | + hardAssert( |
| 113 | + result[0] !== undefined, |
| 114 | + 'Aggregation fields are missing from result.' |
| 115 | + ); |
| 116 | + |
| 117 | + const counts = Object.entries(result[0]) |
| 118 | + .filter(([key, value]) => key === 'count_alias') |
| 119 | + .map(([key, value]) => userDataWriter.convertValue(value as Value)); |
| 120 | + |
| 121 | + const countValue = counts[0]; |
| 122 | + |
| 123 | + hardAssert( |
| 124 | + typeof countValue === 'number', |
| 125 | + 'Count aggregate field value is not a number: ' + countValue |
| 126 | + ); |
| 127 | + |
| 128 | + return Promise.resolve( |
| 129 | + new AggregateQuerySnapshot<{ count: AggregateField<number> }>(query, { |
| 130 | + count: countValue |
| 131 | + }) |
| 132 | + ); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Compares two `AggregateQuerySnapshot` instances for equality. |
| 138 | + * Two `AggregateQuerySnapshot` instances are considered "equal" if they have |
| 139 | + * the same underlying query, and the same data. |
| 140 | + * |
| 141 | + * @param left - The `AggregateQuerySnapshot` to compare. |
| 142 | + * @param right - The `AggregateQuerySnapshot` to compare. |
| 143 | + * |
| 144 | + * @returns true if the AggregateQuerySnapshots are equal. |
| 145 | + */ |
| 146 | +export function aggregateQuerySnapshotEqual<T extends AggregateSpec>( |
| 147 | + left: AggregateQuerySnapshot<T>, |
| 148 | + right: AggregateQuerySnapshot<T> |
| 149 | +): boolean { |
| 150 | + return ( |
| 151 | + queryEqual(left.query, right.query) && deepEqual(left.data(), right.data()) |
| 152 | + ); |
| 153 | +} |
0 commit comments