Skip to content

Commit a30f9ae

Browse files
committed
Fix circular deps
1 parent 3b49905 commit a30f9ae

File tree

13 files changed

+271
-97
lines changed

13 files changed

+271
-97
lines changed

common/api-review/firestore.api.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ export class Firestore {
10761076
// Warning: (ae-incompatible-release-tags) The symbol "pipeline" is marked as @public, but its signature references "PipelineSource" which is marked as @beta
10771077
//
10781078
// (undocumented)
1079-
pipeline: () => PipelineSource | undefined;
1079+
pipeline: () => PipelineSource;
10801080
toJSON(): object;
10811081
type: 'firestore-lite' | 'firestore';
10821082
}
@@ -1815,7 +1815,6 @@ export type PersistentTabManager = PersistentSingleTabManager | PersistentMultip
18151815

18161816
// @beta
18171817
export class Pipeline<AppModelType = DocumentData> {
1818-
constructor(db: Firestore, stages: Stage[], converter?: unknown);
18191818
addFields(...fields: Selectable[]): Pipeline<AppModelType>;
18201819
aggregate(...accumulators: AccumulatorTarget[]): Pipeline<AppModelType>;
18211820
aggregate(options: {
@@ -1854,7 +1853,6 @@ export class PipelineResult<AppModelType = DocumentData> {
18541853

18551854
// @beta
18561855
export class PipelineSource {
1857-
constructor(db: Firestore);
18581856
// (undocumented)
18591857
collection(collectionPath: string): Pipeline;
18601858
// (undocumented)
@@ -1863,7 +1861,7 @@ export class PipelineSource {
18631861
database(): Pipeline;
18641862
// (undocumented)
18651863
documents(docs: DocumentReference[]): Pipeline;
1866-
}
1864+
}
18671865

18681866
// @public
18691867
export type Primitive = string | number | boolean | undefined | null;

packages/firestore/src/api/aggregate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { cast } from '../util/input_validation';
2525
import { mapToArray } from '../util/obj';
2626

2727
import { ensureFirestoreConfigured, Firestore } from './database';
28-
import { ExpUserDataWriter } from './reference_impl';
28+
import { ExpUserDataWriter } from './user_data_writer';
2929

3030
export {
3131
aggregateQuerySnapshotEqual,

packages/firestore/src/api/database.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ import {
4646
connectFirestoreEmulator,
4747
Firestore as LiteFirestore
4848
} from '../lite-api/database';
49-
import { Query } from '../lite-api/reference';
49+
import { DocumentReference, Query } from '../lite-api/reference';
50+
import { newUserDataReader } from '../lite-api/user_data_reader';
5051
import {
5152
indexedDbClearPersistence,
5253
indexedDbStoragePrefix
5354
} from '../local/indexeddb_persistence';
5455
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector';
5556
import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../local/lru_garbage_collector_impl';
57+
import { DocumentKey } from '../model/document_key';
58+
import { PipelineSource } from '../pipelines/api/pipeline-source';
5659
import { debugAssert } from '../util/assert';
5760
import { AsyncQueue } from '../util/async_queue';
5861
import { AsyncQueueImpl } from '../util/async_queue_impl';
@@ -64,7 +67,7 @@ import { Deferred } from '../util/promise';
6467
import { LoadBundleTask } from './bundle';
6568
import { CredentialsProvider } from './credentials';
6669
import { FirestoreSettings, PersistenceSettings } from './settings';
67-
import type {PipelineSource} from "../pipelines/api/pipeline-source";
70+
import { ExpUserDataWriter } from './user_data_writer';
6871

6972
export {
7073
connectFirestoreEmulator,
@@ -105,9 +108,18 @@ export class Firestore extends LiteFirestore {
105108
_online: OnlineComponentProviderFactory;
106109
};
107110

108-
pipeline = function(): PipelineSource {
109-
return new PipelineSource(this);
110-
}
111+
pipeline = (): PipelineSource => {
112+
const client = ensureFirestoreConfigured(this);
113+
const firestore = this;
114+
return new PipelineSource(
115+
client,
116+
newUserDataReader(firestore),
117+
new ExpUserDataWriter(firestore),
118+
(key: DocumentKey) => {
119+
return new DocumentReference(firestore, null, key);
120+
}
121+
);
122+
};
111123

112124
/** @hideconstructor */
113125
constructor(

packages/firestore/src/api/reference_impl.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from '../core/firestore_client';
3737
import { newQueryForPath, Query as InternalQuery } from '../core/query';
3838
import { ViewSnapshot } from '../core/view_snapshot';
39-
import { Bytes } from '../lite-api/bytes';
4039
import { FieldPath } from '../lite-api/field_path';
4140
import { validateHasExplicitOrderByForLimitToLast } from '../lite-api/query';
4241
import {
@@ -58,15 +57,14 @@ import {
5857
parseUpdateData,
5958
parseUpdateVarargs
6059
} from '../lite-api/user_data_reader';
61-
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
6260
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
6361
import { debugAssert } from '../util/assert';
64-
import { ByteString } from '../util/byte_string';
6562
import { FirestoreError } from '../util/error';
6663
import { cast } from '../util/input_validation';
6764

6865
import { ensureFirestoreConfigured, Firestore } from './database';
6966
import { DocumentSnapshot, QuerySnapshot, SnapshotMetadata } from './snapshot';
67+
import { ExpUserDataWriter } from './user_data_writer';
7068

7169
/**
7270
* An options object that can be passed to {@link (onSnapshot:1)} and {@link
@@ -123,21 +121,6 @@ export function getDoc<AppModelType, DbModelType extends DocumentData>(
123121
).then(snapshot => convertToDocSnapshot(firestore, reference, snapshot));
124122
}
125123

126-
export class ExpUserDataWriter extends AbstractUserDataWriter {
127-
constructor(protected firestore: Firestore) {
128-
super();
129-
}
130-
131-
protected convertBytes(bytes: ByteString): Bytes {
132-
return new Bytes(bytes);
133-
}
134-
135-
protected convertReference(name: string): DocumentReference {
136-
const key = this.convertDocumentKey(name, this.firestore._databaseId);
137-
return new DocumentReference(this.firestore, /* converter= */ null, key);
138-
}
139-
}
140-
141124
/**
142125
* Reads the document referred to by this `DocumentReference` from cache.
143126
* Returns an error if the document is not currently cached.

packages/firestore/src/api/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import { validateReference } from '../lite-api/write_batch';
2828
import { cast } from '../util/input_validation';
2929

3030
import { ensureFirestoreConfigured, Firestore } from './database';
31-
import { ExpUserDataWriter } from './reference_impl';
3231
import { DocumentSnapshot, SnapshotMetadata } from './snapshot';
3332
import { TransactionOptions } from './transaction_options';
33+
import { ExpUserDataWriter } from './user_data_writer';
3434

3535
/**
3636
* A reference to a transaction.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Bytes } from '../lite-api/bytes';
2+
import { DocumentReference } from '../lite-api/reference';
3+
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
4+
import { ByteString } from '../util/byte_string';
5+
6+
import { Firestore } from './database';
7+
8+
export class ExpUserDataWriter extends AbstractUserDataWriter {
9+
constructor(protected firestore: Firestore) {
10+
super();
11+
}
12+
13+
protected convertBytes(bytes: ByteString): Bytes {
14+
return new Bytes(bytes);
15+
}
16+
17+
protected convertReference(name: string): DocumentReference {
18+
const key = this.convertDocumentKey(name, this.firestore._databaseId);
19+
return new DocumentReference(this.firestore, /* converter= */ null, key);
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright 2024 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 {
19+
MapValue as ProtoMapValue,
20+
Value as ProtoValue
21+
} from '../protos/firestore_proto_api';
22+
23+
import { valueEquals } from './values';
24+
25+
/**
26+
* An AggregateResultValue represents a MapValue in the Firestore Proto.
27+
*/
28+
export class AggregateResultValue {
29+
constructor(readonly value: { mapValue: ProtoMapValue }) {}
30+
31+
static empty(): AggregateResultValue {
32+
return new AggregateResultValue({ mapValue: {} });
33+
}
34+
35+
aggregate(alias: string): ProtoValue | null {
36+
return this.value.mapValue.fields?.[alias] ?? null;
37+
}
38+
39+
isEqual(other: AggregateResultValue): boolean {
40+
return valueEquals(this.value, other.value);
41+
}
42+
}

packages/firestore/src/pipelines/api/expressions.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,10 @@ export class ArrayReverse extends FirestoreFunction {
24122412
/**
24132413
* @beta
24142414
*/
2415-
export class ArrayContains extends FirestoreFunction implements FilterCondition {
2415+
export class ArrayContains
2416+
extends FirestoreFunction
2417+
implements FilterCondition
2418+
{
24162419
constructor(private array: Expr, private element: Expr) {
24172420
super('array_contains', [array, element]);
24182421
}
@@ -2422,7 +2425,10 @@ export class ArrayContains extends FirestoreFunction implements FilterCondition
24222425
/**
24232426
* @beta
24242427
*/
2425-
export class ArrayContainsAll extends FirestoreFunction implements FilterCondition {
2428+
export class ArrayContainsAll
2429+
extends FirestoreFunction
2430+
implements FilterCondition
2431+
{
24262432
constructor(private array: Expr, private values: Expr[]) {
24272433
super('array_contains_all', [array, new ListOfExprs(values)]);
24282434
}
@@ -2432,7 +2438,10 @@ export class ArrayContainsAll extends FirestoreFunction implements FilterConditi
24322438
/**
24332439
* @beta
24342440
*/
2435-
export class ArrayContainsAny extends FirestoreFunction implements FilterCondition {
2441+
export class ArrayContainsAny
2442+
extends FirestoreFunction
2443+
implements FilterCondition
2444+
{
24362445
constructor(private array: Expr, private values: Expr[]) {
24372446
super('array_contains_any', [array, new ListOfExprs(values)]);
24382447
}
@@ -2618,7 +2627,10 @@ export class Like extends FirestoreFunction implements FilterCondition {
26182627
/**
26192628
* @beta
26202629
*/
2621-
export class RegexContains extends FirestoreFunction implements FilterCondition {
2630+
export class RegexContains
2631+
extends FirestoreFunction
2632+
implements FilterCondition
2633+
{
26222634
constructor(private expr: Expr, private pattern: Expr) {
26232635
super('regex_contains', [expr, pattern]);
26242636
}
@@ -6642,7 +6654,10 @@ export function timestampSub(
66426654
* @param params The arguments to pass to the function.
66436655
* @return A new {@code Function} representing the function call.
66446656
*/
6645-
export function genericFunction(name: string, params: Expr[]): FirestoreFunction {
6657+
export function genericFunction(
6658+
name: string,
6659+
params: Expr[]
6660+
): FirestoreFunction {
66466661
return new FirestoreFunction(name, params);
66476662
}
66486663

packages/firestore/src/pipelines/api/pipeline-source.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import { FirestoreClient } from '../../core/firestore_client';
16+
import { DocumentReference } from '../../lite-api/reference';
17+
import { UserDataReader } from '../../lite-api/user_data_reader';
18+
import { AbstractUserDataWriter } from '../../lite-api/user_data_writer';
19+
import { DocumentKey } from '../../model/document_key';
20+
1521
import { Pipeline } from './pipeline';
1622
import {
1723
CollectionGroupSource,
@@ -20,29 +26,63 @@ import {
2026
DocumentsSource
2127
} from './stage';
2228

23-
import { Firestore } from '../../api/database';
24-
import { DocumentReference } from '../../lite-api/reference';
25-
2629
/**
2730
* Represents the source of a Firestore {@link Pipeline}.
2831
* @beta
2932
*/
3033
export class PipelineSource {
31-
constructor(private db: Firestore) {}
34+
/**
35+
* @internal
36+
* @private
37+
* @param db
38+
* @param userDataReader
39+
* @param userDataWriter
40+
* @param documentReferenceFactory
41+
*/
42+
constructor(
43+
private db: FirestoreClient,
44+
private userDataReader: UserDataReader,
45+
private userDataWriter: AbstractUserDataWriter,
46+
private documentReferenceFactory: (id: DocumentKey) => DocumentReference
47+
) {}
3248

3349
collection(collectionPath: string): Pipeline {
34-
return new Pipeline(this.db, [new CollectionSource(collectionPath)]);
50+
return new Pipeline(
51+
this.db,
52+
this.userDataReader,
53+
this.userDataWriter,
54+
this.documentReferenceFactory,
55+
[new CollectionSource(collectionPath)]
56+
);
3557
}
3658

3759
collectionGroup(collectionId: string): Pipeline {
38-
return new Pipeline(this.db, [new CollectionGroupSource(collectionId)]);
60+
return new Pipeline(
61+
this.db,
62+
this.userDataReader,
63+
this.userDataWriter,
64+
this.documentReferenceFactory,
65+
[new CollectionGroupSource(collectionId)]
66+
);
3967
}
4068

4169
database(): Pipeline {
42-
return new Pipeline(this.db, [new DatabaseSource()]);
70+
return new Pipeline(
71+
this.db,
72+
this.userDataReader,
73+
this.userDataWriter,
74+
this.documentReferenceFactory,
75+
[new DatabaseSource()]
76+
);
4377
}
4478

4579
documents(docs: DocumentReference[]): Pipeline {
46-
return new Pipeline(this.db, [DocumentsSource.of(docs)]);
80+
return new Pipeline(
81+
this.db,
82+
this.userDataReader,
83+
this.userDataWriter,
84+
this.documentReferenceFactory,
85+
[DocumentsSource.of(docs)]
86+
);
4787
}
4888
}

packages/firestore/src/pipelines/api/pipeline-util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
1615
import {
1716
CompositeFilter as CompositeFilterInternal,
1817
CompositeOperator,

0 commit comments

Comments
 (0)