Skip to content

Commit 9bd9a23

Browse files
committed
pipeline() factory is modular, for bundle size testing
1 parent fd20eff commit 9bd9a23

File tree

12 files changed

+289
-141
lines changed

12 files changed

+289
-141
lines changed

common/api-review/firestore-lite.api.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,14 @@ export class Pipeline<AppModelType = DocumentData> {
15891589
where(condition: FilterCondition & Constant): Pipeline<AppModelType>;
15901590
}
15911591

1592+
// @public (undocumented)
1593+
export function pipeline(query: Query): Pipeline;
1594+
1595+
// Warning: (ae-incompatible-release-tags) The symbol "pipeline" is marked as @public, but its signature references "PipelineSource" which is marked as @beta
1596+
//
1597+
// @public (undocumented)
1598+
export function pipeline(firestore: Firestore): PipelineSource;
1599+
15921600
// @beta
15931601
export class PipelineResult<AppModelType = DocumentData> {
15941602
/* Excluded from this release type: _ref */
@@ -2176,8 +2184,8 @@ export function xor(left: FilterExpr, ...right: FilterExpr[]): Xor;
21762184

21772185
// Warnings were encountered during analysis:
21782186
//
2179-
// /home/runner/work/firebase-js-sdk/firebase-js-sdk/packages/firestore/dist/lite/index.d.ts:9258:9 - (ae-incompatible-release-tags) The symbol "accumulators" is marked as @public, but its signature references "AccumulatorTarget" which is marked as @beta
2180-
// /home/runner/work/firebase-js-sdk/firebase-js-sdk/packages/firestore/dist/lite/index.d.ts:9259:9 - (ae-incompatible-release-tags) The symbol "groups" is marked as @public, but its signature references "Selectable" which is marked as @beta
2181-
// /home/runner/work/firebase-js-sdk/firebase-js-sdk/packages/firestore/dist/lite/index.d.ts:9288:9 - (ae-incompatible-release-tags) The symbol "orderings" is marked as @public, but its signature references "Ordering" which is marked as @beta
2187+
// /Users/markduckworth/projects/firebase-js-sdk/packages/firestore/dist/lite/index.d.ts:9257:9 - (ae-incompatible-release-tags) The symbol "accumulators" is marked as @public, but its signature references "AccumulatorTarget" which is marked as @beta
2188+
// /Users/markduckworth/projects/firebase-js-sdk/packages/firestore/dist/lite/index.d.ts:9258:9 - (ae-incompatible-release-tags) The symbol "groups" is marked as @public, but its signature references "Selectable" which is marked as @beta
2189+
// /Users/markduckworth/projects/firebase-js-sdk/packages/firestore/dist/lite/index.d.ts:9287:9 - (ae-incompatible-release-tags) The symbol "orderings" is marked as @public, but its signature references "Ordering" which is marked as @beta
21822190

21832191
```

common/api-review/firestore.api.md

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,10 +1073,6 @@ export interface FindNearestOptions {
10731073
// @public
10741074
export class Firestore {
10751075
get app(): FirebaseApp;
1076-
// Warning: (ae-incompatible-release-tags) The symbol "pipeline" is marked as @public, but its signature references "PipelineSource" which is marked as @beta
1077-
//
1078-
// (undocumented)
1079-
pipeline: () => PipelineSource;
10801076
toJSON(): object;
10811077
type: 'firestore-lite' | 'firestore';
10821078
}
@@ -1813,40 +1809,72 @@ export interface PersistentSingleTabManagerSettings {
18131809
// @public
18141810
export type PersistentTabManager = PersistentSingleTabManager | PersistentMultipleTabManager;
18151811

1816-
// @public
1812+
// @public (undocumented)
18171813
export class Pipeline<AppModelType = DocumentData> {
18181814
// Warning: (ae-incompatible-release-tags) The symbol "addFields" is marked as @public, but its signature references "Selectable" which is marked as @beta
18191815
addFields(...fields: Selectable[]): Pipeline<AppModelType>;
1816+
/* Excluded from this release type: __constructor */
18201817
// Warning: (ae-incompatible-release-tags) The symbol "aggregate" is marked as @public, but its signature references "AccumulatorTarget" which is marked as @beta
18211818
aggregate(...accumulators: AccumulatorTarget[]): Pipeline<AppModelType>;
1822-
aggregate(options: {
1823-
accumulators: AccumulatorTarget[];
1824-
groups?: Array<string | Selectable>;
1825-
}): Pipeline<AppModelType>;
1819+
/* Excluded from this release type: __constructor */
1820+
aggregate(options: { accumulators: AccumulatorTarget[]; groups?: Array<string | Selectable>; }): Pipeline<AppModelType>;
1821+
/* Excluded from this release type: __constructor */
1822+
// (undocumented)
1823+
converter: any;
1824+
/* Excluded from this release type: __constructor */
18261825
// Warning: (ae-incompatible-release-tags) The symbol "distinct" is marked as @public, but its signature references "Selectable" which is marked as @beta
18271826
distinct(...groups: Array<string | Selectable>): Pipeline<AppModelType>;
1827+
/* Excluded from this release type: __constructor */
18281828
// Warning: (ae-incompatible-release-tags) The symbol "execute" is marked as @public, but its signature references "PipelineResult" which is marked as @beta
18291829
execute(): Promise<Array<PipelineResult<AppModelType>>>;
1830+
/* Excluded from this release type: __constructor */
18301831
// Warning: (ae-incompatible-release-tags) The symbol "findNearest" is marked as @public, but its signature references "FindNearestOptions" which is marked as @beta
18311832
//
18321833
// (undocumented)
18331834
findNearest(options: FindNearestOptions): Pipeline<AppModelType>;
1835+
/* Excluded from this release type: __constructor */
18341836
genericStage(name: string, params: any[]): Pipeline<AppModelType>;
1837+
/* Excluded from this release type: __constructor */
18351838
limit(limit: number): Pipeline<AppModelType>;
1839+
/* Excluded from this release type: __constructor */
1840+
// (undocumented)
1841+
liteDb: any;
1842+
/* Excluded from this release type: __constructor */
18361843
offset(offset: number): Pipeline<AppModelType>;
1844+
/* Excluded from this release type: __constructor */
1845+
readUserData: any;
1846+
/* Excluded from this release type: __constructor */
18371847
// Warning: (ae-incompatible-release-tags) The symbol "select" is marked as @public, but its signature references "Selectable" which is marked as @beta
18381848
select(...selections: Array<Selectable | string>): Pipeline<AppModelType>;
1849+
/* Excluded from this release type: __constructor */
1850+
// (undocumented)
1851+
selectablesToMap: any;
1852+
/* Excluded from this release type: __constructor */
18391853
// Warning: (ae-incompatible-release-tags) The symbol "sort" is marked as @public, but its signature references "Ordering" which is marked as @beta
18401854
sort(...orderings: Ordering[]): Pipeline<AppModelType>;
1855+
/* Excluded from this release type: __constructor */
1856+
// (undocumented)
1857+
sort(options: { orderings: Ordering[]; }): Pipeline<AppModelType>;
1858+
/* Excluded from this release type: __constructor */
1859+
// (undocumented)
1860+
stages: any;
1861+
/* Excluded from this release type: __constructor */
18411862
// (undocumented)
1842-
sort(options: {
1843-
orderings: Ordering[];
1844-
}): Pipeline<AppModelType>;
1863+
userDataReader: any;
1864+
/* Excluded from this release type: __constructor */
18451865
// Warning: (ae-incompatible-release-tags) The symbol "where" is marked as @public, but its signature references "FilterCondition" which is marked as @beta
18461866
// Warning: (ae-incompatible-release-tags) The symbol "where" is marked as @public, but its signature references "Constant" which is marked as @beta
18471867
where(condition: FilterCondition & Constant): Pipeline<AppModelType>;
18481868
}
18491869

1870+
// Warning: (ae-incompatible-release-tags) The symbol "pipeline" is marked as @public, but its signature references "PipelineSource" which is marked as @beta
1871+
//
1872+
// @public (undocumented)
1873+
export function pipeline(firestore: Firestore): PipelineSource;
1874+
1875+
// @public (undocumented)
1876+
export function pipeline(query: Query): Pipeline;
1877+
18501878
// @beta
18511879
export class PipelineResult<AppModelType = DocumentData> {
18521880
/* Excluded from this release type: _ref */
@@ -2464,8 +2492,8 @@ export function xor(left: FilterExpr, ...right: FilterExpr[]): Xor;
24642492

24652493
// Warnings were encountered during analysis:
24662494
//
2467-
// /home/runner/work/firebase-js-sdk/firebase-js-sdk/packages/firestore/dist/index.d.ts:10108:9 - (ae-incompatible-release-tags) The symbol "accumulators" is marked as @public, but its signature references "AccumulatorTarget" which is marked as @beta
2468-
// /home/runner/work/firebase-js-sdk/firebase-js-sdk/packages/firestore/dist/index.d.ts:10109:9 - (ae-incompatible-release-tags) The symbol "groups" is marked as @public, but its signature references "Selectable" which is marked as @beta
2469-
// /home/runner/work/firebase-js-sdk/firebase-js-sdk/packages/firestore/dist/index.d.ts:10138:9 - (ae-incompatible-release-tags) The symbol "orderings" is marked as @public, but its signature references "Ordering" which is marked as @beta
2495+
// /Users/markduckworth/projects/firebase-js-sdk/packages/firestore/dist/index.d.ts:10093:26 - (ae-incompatible-release-tags) The symbol "accumulators" is marked as @public, but its signature references "AccumulatorTarget" which is marked as @beta
2496+
// /Users/markduckworth/projects/firebase-js-sdk/packages/firestore/dist/index.d.ts:10093:61 - (ae-incompatible-release-tags) The symbol "groups" is marked as @public, but its signature references "Selectable" which is marked as @beta
2497+
// /Users/markduckworth/projects/firebase-js-sdk/packages/firestore/dist/index.d.ts:10120:21 - (ae-incompatible-release-tags) The symbol "orderings" is marked as @public, but its signature references "Ordering" which is marked as @beta
24702498

24712499
```

packages/firestore/lite/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ export {
271271
updateDoc,
272272
setDoc,
273273
getDoc,
274-
getDocs
274+
getDocs,
275+
pipeline
275276
} from '../src/lite-api/reference_impl';
276277

277278
export {

packages/firestore/src/api.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
export { PipelineSource } from './lite-api/pipeline-source';
18+
export { PipelineSource } from './api/pipeline-source';
1919

2020
export { PipelineResult } from './lite-api/pipeline-result';
2121

22-
export { Pipeline } from './lite-api/pipeline';
22+
export { Pipeline } from './api/pipeline';
2323

2424
export {
2525
Stage,
@@ -308,7 +308,8 @@ export {
308308
export {
309309
ListenSource,
310310
SnapshotListenOptions,
311-
Unsubscribe
311+
Unsubscribe,
312+
pipeline
312313
} from './api/reference_impl';
313314

314315
export { TransactionOptions } from './api/transaction_options';

packages/firestore/src/api/database.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ import {
4646
connectFirestoreEmulator,
4747
Firestore as LiteFirestore
4848
} from '../lite-api/database';
49-
import { PipelineSource } from '../lite-api/pipeline-source';
50-
import { DocumentReference, Query } from '../lite-api/reference';
51-
import { newUserDataReader } from '../lite-api/user_data_reader';
49+
import { Query } from '../lite-api/reference';
5250
import {
5351
indexedDbClearPersistence,
5452
indexedDbStoragePrefix
5553
} from '../local/indexeddb_persistence';
5654
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector';
5755
import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../local/lru_garbage_collector_impl';
58-
import { DocumentKey } from '../model/document_key';
5956
import { debugAssert } from '../util/assert';
6057
import { AsyncQueue } from '../util/async_queue';
6158
import { AsyncQueueImpl } from '../util/async_queue_impl';
@@ -67,7 +64,6 @@ import { Deferred } from '../util/promise';
6764
import { LoadBundleTask } from './bundle';
6865
import { CredentialsProvider } from './credentials';
6966
import { FirestoreSettings, PersistenceSettings } from './settings';
70-
import { ExpUserDataWriter } from './user_data_writer';
7167

7268
export {
7369
connectFirestoreEmulator,
@@ -108,18 +104,6 @@ export class Firestore extends LiteFirestore {
108104
_online: OnlineComponentProviderFactory;
109105
};
110106

111-
pipeline = (): PipelineSource => {
112-
const firestore = this;
113-
return new PipelineSource(
114-
this,
115-
newUserDataReader(firestore),
116-
new ExpUserDataWriter(firestore),
117-
(key: DocumentKey) => {
118-
return new DocumentReference(firestore, null, key);
119-
}
120-
);
121-
};
122-
123107
/** @hideconstructor */
124108
constructor(
125109
authCredentialsProvider: CredentialsProvider<User>,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {
16+
CollectionGroupSource,
17+
CollectionSource,
18+
DatabaseSource,
19+
DocumentsSource
20+
} from '../lite-api/stage';
21+
import { UserDataReader } from '../lite-api/user_data_reader';
22+
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
23+
import { DocumentKey } from '../model/document_key';
24+
25+
import { Firestore } from './database';
26+
import { Pipeline } from './pipeline';
27+
import { DocumentReference } from './reference';
28+
29+
/**
30+
* Represents the source of a Firestore {@link Pipeline}.
31+
* @beta
32+
*/
33+
export class PipelineSource {
34+
/**
35+
* @internal
36+
* @private
37+
* @param db
38+
* @param userDataReader
39+
* @param userDataWriter
40+
* @param documentReferenceFactory
41+
*/
42+
constructor(
43+
private db: Firestore,
44+
private userDataReader: UserDataReader,
45+
private userDataWriter: AbstractUserDataWriter,
46+
private documentReferenceFactory: (id: DocumentKey) => DocumentReference
47+
) {}
48+
49+
collection(collectionPath: string): Pipeline {
50+
return new Pipeline(
51+
this.db,
52+
this.userDataReader,
53+
this.userDataWriter,
54+
this.documentReferenceFactory,
55+
[new CollectionSource(collectionPath)]
56+
);
57+
}
58+
59+
collectionGroup(collectionId: string): Pipeline {
60+
return new Pipeline(
61+
this.db,
62+
this.userDataReader,
63+
this.userDataWriter,
64+
this.documentReferenceFactory,
65+
[new CollectionGroupSource(collectionId)]
66+
);
67+
}
68+
69+
database(): Pipeline {
70+
return new Pipeline(
71+
this.db,
72+
this.userDataReader,
73+
this.userDataWriter,
74+
this.documentReferenceFactory,
75+
[new DatabaseSource()]
76+
);
77+
}
78+
79+
documents(docs: DocumentReference[]): Pipeline {
80+
return new Pipeline(
81+
this.db,
82+
this.userDataReader,
83+
this.userDataWriter,
84+
this.documentReferenceFactory,
85+
[DocumentsSource.of(docs)]
86+
);
87+
}
88+
}

packages/firestore/src/api/reference_impl.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
NextFn,
2525
PartialObserver
2626
} from '../api/observer';
27+
import { Pipeline } from '../api/pipeline';
28+
import { PipelineSource } from '../api/pipeline-source';
2729
import { ListenerDataSource } from '../core/event_manager';
2830
import {
2931
firestoreClientAddSnapshotsInSyncListener,
@@ -57,9 +59,10 @@ import {
5759
parseUpdateData,
5860
parseUpdateVarargs
5961
} from '../lite-api/user_data_reader';
62+
import { DocumentKey } from '../model/document_key';
6063
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
6164
import { debugAssert } from '../util/assert';
62-
import { FirestoreError } from '../util/error';
65+
import { Code, FirestoreError } from '../util/error';
6366
import { cast } from '../util/input_validation';
6467

6568
import { ensureFirestoreConfigured, Firestore } from './database';
@@ -842,3 +845,54 @@ function convertToDocSnapshot<AppModelType, DbModelType extends DocumentData>(
842845
ref.converter
843846
);
844847
}
848+
849+
/**
850+
* @private
851+
* @internal
852+
*/
853+
function _pipeline(query: Query): Pipeline {
854+
let pipeline: Pipeline;
855+
const firestore = cast(query.firestore, Firestore);
856+
if (query._query.collectionGroup) {
857+
pipeline = _pipelineSource(firestore).collectionGroup(
858+
query._query.collectionGroup
859+
);
860+
} else {
861+
pipeline = _pipelineSource(firestore).collection(
862+
query._query.path.canonicalString()
863+
);
864+
}
865+
866+
// TODO(pipeline) convert existing query filters, limits, etc into
867+
// pipeline stages
868+
869+
return pipeline;
870+
}
871+
872+
function _pipelineSource(firestore: Firestore): PipelineSource {
873+
return new PipelineSource(
874+
firestore,
875+
newUserDataReader(firestore),
876+
new ExpUserDataWriter(firestore),
877+
(key: DocumentKey) => {
878+
return new DocumentReference(firestore, null, key);
879+
}
880+
);
881+
}
882+
883+
export function pipeline(firestore: Firestore): PipelineSource;
884+
export function pipeline(query: Query): Pipeline;
885+
export function pipeline(
886+
queryOrFirestore: Query | Firestore
887+
): Pipeline | PipelineSource {
888+
if (queryOrFirestore instanceof Query) {
889+
return _pipeline(queryOrFirestore);
890+
} else if (queryOrFirestore instanceof Firestore) {
891+
return _pipelineSource(queryOrFirestore);
892+
}
893+
894+
throw new FirestoreError(
895+
Code.INVALID_ARGUMENT,
896+
'pipeline() requires argument of type Firestore or Query'
897+
);
898+
}

0 commit comments

Comments
 (0)