Skip to content

Commit f25b9e5

Browse files
authored
[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.
1 parent 14f9da6 commit f25b9e5

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

packages/firestore/src/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ export {
219219
*/
220220
export { isBase64Available as _isBase64Available } from './platform/base64';
221221
export { DatabaseId as _DatabaseId } from './core/database_info';
222+
export {
223+
_internalQueryToProtoQueryTarget,
224+
_internalAggregationQueryToProtoRunAggregationQueryRequest
225+
} from './remote/internal_serializer';
222226
export {
223227
cast as _cast,
224228
validateIsNotUsedTogether as _validateIsNotUsedTogether
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

packages/firestore/src/remote/serializer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,8 @@ export function toQueryTarget(
903903
export function toRunAggregationQueryRequest(
904904
serializer: JsonProtoSerializer,
905905
target: Target,
906-
aggregates: Aggregate[]
906+
aggregates: Aggregate[],
907+
skipAliasing?: boolean
907908
): {
908909
request: ProtoRunAggregationQueryRequest;
909910
aliasMap: Record<string, string>;
@@ -919,7 +920,9 @@ export function toRunAggregationQueryRequest(
919920
// Map all client-side aliases to a unique short-form
920921
// alias. This avoids issues with client-side aliases that
921922
// exceed the 1500-byte string size limit.
922-
const serverAlias = `aggregate_${aggregationNum++}`;
923+
const serverAlias = skipAliasing
924+
? aggregate.alias
925+
: `aggregate_${aggregationNum++}`;
923926
aliasMap[serverAlias] = aggregate.alias;
924927

925928
if (aggregate.aggregateType === 'count') {

0 commit comments

Comments
 (0)