|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 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 { ensureFirestoreConfigured, Firestore } from '../api/database'; |
| 19 | +import { AggregateImpl } from '../core/aggregate'; |
| 20 | +import { queryToAggregateTarget, queryToTarget } from '../core/query'; |
| 21 | +import { AggregateSpec } from '../lite-api/aggregate_types'; |
| 22 | +import { Query } from '../lite-api/reference'; |
| 23 | +import { cast } from '../util/input_validation'; |
| 24 | +import { mapToArray } from '../util/obj'; |
| 25 | + |
| 26 | +import { toQueryTarget, toRunAggregationQueryRequest } from './serializer'; |
| 27 | + |
| 28 | +/** |
| 29 | + * @internal |
| 30 | + * @private |
| 31 | + * |
| 32 | + * This function is for internal use only. |
| 33 | + * |
| 34 | + * Returns the `QueryTarget` representation of the given query. Returns `null` |
| 35 | + * if the Firestore client associated with the given query has not been |
| 36 | + * initialized or has been terminated. |
| 37 | + * |
| 38 | + * @param query - The Query to convert to proto representation. |
| 39 | + */ |
| 40 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 41 | +export function _internalQueryToProtoQueryTarget(query: Query): any { |
| 42 | + const firestore = cast(query.firestore, Firestore); |
| 43 | + const client = ensureFirestoreConfigured(firestore); |
| 44 | + const serializer = client._onlineComponents?.datastore.serializer; |
| 45 | + if (serializer === undefined) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + return toQueryTarget(serializer!, queryToTarget(query._query)).queryTarget; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * @internal |
| 53 | + * @private |
| 54 | + * |
| 55 | + * This function is for internal use only. |
| 56 | + * |
| 57 | + * Returns `RunAggregationQueryRequest` which contains the proto representation |
| 58 | + * of the given aggregation query request. Returns null if the Firestore client |
| 59 | + * associated with the given query has not been initialized or has been |
| 60 | + * terminated. |
| 61 | + * |
| 62 | + * @param query - The Query to convert to proto representation. |
| 63 | + * @param aggregateSpec - The set of aggregations and their aliases. |
| 64 | + */ |
| 65 | +export function _internalAggregationQueryToProtoRunAggregationQueryRequest< |
| 66 | + AggregateSpecType extends AggregateSpec |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 68 | +>(query: Query, aggregateSpec: AggregateSpecType): any { |
| 69 | + const aggregates = mapToArray(aggregateSpec, (aggregate, alias) => { |
| 70 | + return new AggregateImpl( |
| 71 | + alias, |
| 72 | + aggregate.aggregateType, |
| 73 | + aggregate._internalFieldPath |
| 74 | + ); |
| 75 | + }); |
| 76 | + const firestore = cast(query.firestore, Firestore); |
| 77 | + const client = ensureFirestoreConfigured(firestore); |
| 78 | + const serializer = client._onlineComponents?.datastore.serializer; |
| 79 | + if (serializer === undefined) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + return toRunAggregationQueryRequest( |
| 84 | + serializer!, |
| 85 | + queryToAggregateTarget(query._query), |
| 86 | + aggregates, |
| 87 | + /* skipAliasing= */ true |
| 88 | + ).request; |
| 89 | +} |
0 commit comments