Skip to content

Commit

Permalink
move to a separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannas committed Apr 17, 2024
1 parent bad3afc commit 123f953
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 53 deletions.
6 changes: 3 additions & 3 deletions packages/firestore/src/api.ts
Expand Up @@ -220,9 +220,9 @@ export {
export { isBase64Available as _isBase64Available } from './platform/base64';
export { DatabaseId as _DatabaseId } from './core/database_info';
export {
queryToProtoQueryTarget as _queryToQueryTargetProto,
aggregationQueryToProtoRunAggregationQueryRequest as _aggregationQueryToProtoRunAggregationQueryRequest
} from './remote/serializer';
_internalQueryToProtoQueryTarget,
_internalAggregationQueryToProtoRunAggregationQueryRequest
} from './remote/internal_serializer';
export {
cast as _cast,
validateIsNotUsedTogether as _validateIsNotUsedTogether
Expand Down
100 changes: 100 additions & 0 deletions packages/firestore/src/remote/internal_serializer.ts
@@ -0,0 +1,100 @@
/**
* @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
* ```
* {
* queryTarget: QueryTarget;
* parent: ResourcePath
* }
* ```
* which contains the proto 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));
}

/**
* @internal
* @private
*
* This function is for internal use only.
*
* Returns
* {
* request: RunAggregationQueryRequest;
* aliasMap: Record<string, string>;
* parent: ResourcePath;
* }
* which contains the proto representation of the given aggregation 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.
* @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
);
}
52 changes: 2 additions & 50 deletions packages/firestore/src/remote/serializer.ts
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { Aggregate, AggregateImpl } from '../core/aggregate';
import { Aggregate } from '../core/aggregate';
import { Bound } from '../core/bound';
import { DatabaseId } from '../core/database_info';
import {
Expand All @@ -32,7 +32,6 @@ import {
newQuery,
newQueryForPath,
Query,
queryToAggregateTarget,
queryToTarget
} from '../core/query';
import { SnapshotVersion } from '../core/snapshot_version';
Expand Down Expand Up @@ -94,7 +93,7 @@ import { debugAssert, fail, hardAssert } from '../util/assert';
import { ByteString } from '../util/byte_string';
import { Code, FirestoreError } from '../util/error';
import { isNullOrUndefined } from '../util/types';
import { Query as ApiQuery } from '../lite-api/reference';

import { ExistenceFilter } from './existence_filter';
import { Serializer } from './number_serializer';
import { mapCodeFromRpcCode } from './rpc_error';
Expand All @@ -105,10 +104,6 @@ import {
WatchTargetChange,
WatchTargetChangeState
} from './watch_change';
import { AggregateSpec } from '../lite-api/aggregate_types';
import { mapToArray } from '../util/obj';
import {ensureFirestoreConfigured, Firestore} from "../api/database";
import {cast} from "../util/input_validation";

const DIRECTIONS = (() => {
const dirs: { [dir: string]: ProtoOrderDirection } = {};
Expand Down Expand Up @@ -905,49 +900,6 @@ export function toQueryTarget(
return { queryTarget, parent };
}

export function queryToProtoQueryTarget(
query: ApiQuery
): { queryTarget: ProtoQueryTarget; parent: ResourcePath } | null {
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));
}

export function aggregationQueryToProtoRunAggregationQueryRequest<
AggregateSpecType extends AggregateSpec
>(
query: ApiQuery,
aggregateSpec: AggregateSpecType
): {
request: ProtoRunAggregationQueryRequest;
aliasMap: Record<string, string>;
parent: ResourcePath;
} | null {
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
);
}

export function toRunAggregationQueryRequest(
serializer: JsonProtoSerializer,
target: Target,
Expand Down

0 comments on commit 123f953

Please sign in to comment.