|
| 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 |
| 35 | + * ``` |
| 36 | + * { |
| 37 | + * queryTarget: QueryTarget; |
| 38 | + * parent: ResourcePath |
| 39 | + * } |
| 40 | + * ``` |
| 41 | + * which contains the proto representation of the given query. Returns `null` if |
| 42 | + * the Firestore client associated with the given query has not been initialized |
| 43 | + * or has been terminated. |
| 44 | + * |
| 45 | + * @param query - The Query to convert to proto representation. |
| 46 | + */ |
| 47 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 48 | +export function _internalQueryToProtoQueryTarget(query: Query): any { |
| 49 | + const firestore = cast(query.firestore, Firestore); |
| 50 | + const client = ensureFirestoreConfigured(firestore); |
| 51 | + const serializer = client._onlineComponents?.datastore.serializer; |
| 52 | + if (serializer === undefined) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return toQueryTarget(serializer!, queryToTarget(query._query)); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @internal |
| 60 | + * @private |
| 61 | + * |
| 62 | + * This function is for internal use only. |
| 63 | + * |
| 64 | + * Returns |
| 65 | + * { |
| 66 | + * request: RunAggregationQueryRequest; |
| 67 | + * aliasMap: Record<string, string>; |
| 68 | + * parent: ResourcePath; |
| 69 | + * } |
| 70 | + * which contains the proto representation of the given aggregation query. |
| 71 | + * Returns null if the Firestore client associated with the given query has not |
| 72 | + * been initialized or has been terminated. |
| 73 | + * |
| 74 | + * @param query - The Query to convert to proto representation. |
| 75 | + * @param aggregateSpec - The set of aggregations and their aliases. |
| 76 | + */ |
| 77 | +export function _internalAggregationQueryToProtoRunAggregationQueryRequest< |
| 78 | + AggregateSpecType extends AggregateSpec |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 80 | +>(query: Query, aggregateSpec: AggregateSpecType): any { |
| 81 | + const aggregates = mapToArray(aggregateSpec, (aggregate, alias) => { |
| 82 | + return new AggregateImpl( |
| 83 | + alias, |
| 84 | + aggregate.aggregateType, |
| 85 | + aggregate._internalFieldPath |
| 86 | + ); |
| 87 | + }); |
| 88 | + const firestore = cast(query.firestore, Firestore); |
| 89 | + const client = ensureFirestoreConfigured(firestore); |
| 90 | + const serializer = client._onlineComponents?.datastore.serializer; |
| 91 | + if (serializer === undefined) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + return toRunAggregationQueryRequest( |
| 96 | + serializer!, |
| 97 | + queryToAggregateTarget(query._query), |
| 98 | + aggregates |
| 99 | + ); |
| 100 | +} |
0 commit comments