Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[internal] Query/AggregateQuery to proto representation (#8177)
* WIP: [internal] Query/AggregateQuery to proto representation. * using api classes. * move to a separate file. * Only return the QueryTarget object. * declare interface to avoid minification. * lint fix. * attempt to avoid minification of ToRunAggregationQueryRequestReturnType. * allow skipping aliasing.
- Loading branch information
Showing
3 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 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 { ensureFirestoreConfigured, Firestore } from '../api/database'; | ||
import { AggregateImpl } from '../core/aggregate'; | ||
import { queryToAggregateTarget, queryToTarget } from '../core/query'; | ||
import { AggregateSpec } from '../lite-api/aggregate_types'; | ||
import { Query } from '../lite-api/reference'; | ||
import { cast } from '../util/input_validation'; | ||
import { mapToArray } from '../util/obj'; | ||
|
||
import { toQueryTarget, toRunAggregationQueryRequest } from './serializer'; | ||
|
||
/** | ||
* @internal | ||
* @private | ||
* | ||
* This function is for internal use only. | ||
* | ||
* Returns the `QueryTarget` representation of the given query. Returns `null` | ||
* if the Firestore client associated with the given query has not been | ||
* initialized or has been terminated. | ||
* | ||
* @param query - The Query to convert to proto representation. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function _internalQueryToProtoQueryTarget(query: Query): any { | ||
const firestore = cast(query.firestore, Firestore); | ||
const client = ensureFirestoreConfigured(firestore); | ||
const serializer = client._onlineComponents?.datastore.serializer; | ||
if (serializer === undefined) { | ||
return null; | ||
} | ||
return toQueryTarget(serializer!, queryToTarget(query._query)).queryTarget; | ||
} | ||
|
||
/** | ||
* @internal | ||
* @private | ||
* | ||
* This function is for internal use only. | ||
* | ||
* Returns `RunAggregationQueryRequest` which contains the proto representation | ||
* of the given aggregation query request. Returns null if the Firestore client | ||
* associated with the given query has not been initialized or has been | ||
* terminated. | ||
* | ||
* @param query - The Query to convert to proto representation. | ||
* @param aggregateSpec - The set of aggregations and their aliases. | ||
*/ | ||
export function _internalAggregationQueryToProtoRunAggregationQueryRequest< | ||
AggregateSpecType extends AggregateSpec | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
>(query: Query, aggregateSpec: AggregateSpecType): any { | ||
const aggregates = mapToArray(aggregateSpec, (aggregate, alias) => { | ||
return new AggregateImpl( | ||
alias, | ||
aggregate.aggregateType, | ||
aggregate._internalFieldPath | ||
); | ||
}); | ||
const firestore = cast(query.firestore, Firestore); | ||
const client = ensureFirestoreConfigured(firestore); | ||
const serializer = client._onlineComponents?.datastore.serializer; | ||
if (serializer === undefined) { | ||
return null; | ||
} | ||
|
||
return toRunAggregationQueryRequest( | ||
serializer!, | ||
queryToAggregateTarget(query._query), | ||
aggregates, | ||
/* skipAliasing= */ true | ||
).request; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters