|
17 | 17 |
|
18 | 18 | import { Query } from '../api';
|
19 | 19 | import { firestoreClientRunCountQuery } from '../core/firestore_client';
|
20 |
| -import { AggregateField, AggregateQuerySnapshot } from '../lite-api/aggregate'; |
| 20 | +import { |
| 21 | + AggregateField, |
| 22 | + AggregateQuerySnapshot |
| 23 | +} from '../lite-api/aggregate_types'; |
21 | 24 | import { cast } from '../util/input_validation';
|
22 | 25 |
|
23 | 26 | import { ensureFirestoreConfigured, Firestore } from './database';
|
| 27 | +import { ExpUserDataWriter } from './reference_impl'; |
| 28 | + |
| 29 | +export { aggregateQuerySnapshotEqual } from '../lite-api/aggregate'; |
24 | 30 |
|
25 | 31 | /**
|
26 |
| - * Executes the query and returns the results as a `AggregateQuerySnapshot` from the |
27 |
| - * server. Returns an error if the network is not available. |
| 32 | + * Calculates the number of documents in the result set of the given query, |
| 33 | + * without actually downloading the documents. |
| 34 | + * |
| 35 | + * Using this function to count the documents is efficient because only the |
| 36 | + * final count, not the documents' data, is downloaded. This function can even |
| 37 | + * count the documents if the result set would be prohibitively large to |
| 38 | + * download entirely (e.g. thousands of documents). |
28 | 39 | *
|
29 |
| - * @param query - The `Query` to execute. |
| 40 | + * The result received from the server is presented, unaltered, without |
| 41 | + * considering any local state. That is, documents in the local cache are not |
| 42 | + * taken into consideration, neither are local modifications not yet |
| 43 | + * synchronized with the server. Previously-downloaded results, if any, are not |
| 44 | + * used: every request using this source necessarily involves a round trip to |
| 45 | + * the server. |
30 | 46 | *
|
31 |
| - * @returns A `Promise` that will be resolved with the results of the query. |
| 47 | + * @param query - The query whose result set size to calculate. |
| 48 | + * @returns A Promise that will be resolved with the count; the count can be |
| 49 | + * retrieved from `snapshot.data().count`, where `snapshot` is the |
| 50 | + * `AggregateQuerySnapshot` to which the returned Promise resolves. |
32 | 51 | */
|
33 | 52 | export function getCountFromServer(
|
34 | 53 | query: Query<unknown>
|
35 | 54 | ): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>> {
|
36 | 55 | const firestore = cast(query.firestore, Firestore);
|
37 | 56 | const client = ensureFirestoreConfigured(firestore);
|
38 |
| - return firestoreClientRunCountQuery(client, query); |
| 57 | + const userDataWriter = new ExpUserDataWriter(firestore); |
| 58 | + return firestoreClientRunCountQuery(client, query, userDataWriter); |
39 | 59 | }
|
0 commit comments