Skip to content

Commit ecd931d

Browse files
committed
New bundle analysis to evaluate useFirestorePipelines vs execute
1 parent 0750f35 commit ecd931d

File tree

13 files changed

+1410
-884
lines changed

13 files changed

+1410
-884
lines changed

packages/firestore/src/api.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export { PipelineSource } from './lite-api/pipeline-source';
1919

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

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

24-
export { useFirestorePipelines } from './api/database_augmentation';
24+
export { useFirestorePipelines, pipeline } from './api/pipeline_impl';
2525

2626
export { execute } from './lite-api/pipeline_impl';
2727

@@ -61,13 +61,13 @@ export {
6161
arrayContainsAny,
6262
arrayContainsAll,
6363
arrayLength,
64-
inAny,
65-
notInAny,
64+
eqAny,
65+
notEqAny,
6666
xor,
67-
ifFunction,
67+
cond,
6868
not,
69-
logicalMax,
70-
logicalMin,
69+
logicalMaximum,
70+
logicalMinimum,
7171
exists,
7272
isNan,
7373
reverse,
@@ -92,8 +92,8 @@ export {
9292
avgFunction,
9393
andFunction,
9494
orFunction,
95-
min,
96-
max,
95+
minimum,
96+
maximum,
9797
cosineDistance,
9898
dotProduct,
9999
euclideanDistance,
@@ -132,16 +132,17 @@ export {
132132
ArrayContainsAny,
133133
ArrayLength,
134134
ArrayElement,
135-
In,
135+
EqAny,
136+
NotEqAny,
136137
IsNan,
137138
Exists,
138139
Not,
139140
And,
140141
Or,
141142
Xor,
142-
If,
143-
LogicalMax,
144-
LogicalMin,
143+
Cond,
144+
LogicalMaximum,
145+
LogicalMinimum,
145146
Reverse,
146147
ReplaceFirst,
147148
ReplaceAll,
@@ -161,8 +162,8 @@ export {
161162
Count,
162163
Sum,
163164
Avg,
164-
Min,
165-
Max,
165+
Minimum,
166+
Maximum,
166167
CosineDistance,
167168
DotProduct,
168169
EuclideanDistance,
Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +0,0 @@
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 { Pipeline } from '../lite-api/pipeline';
19-
import { PipelineSource } from '../lite-api/pipeline-source';
20-
import { newUserDataReader } from '../lite-api/user_data_reader';
21-
import { DocumentKey } from '../model/document_key';
22-
23-
import { Firestore } from './database';
24-
import { DocumentReference, Query } from './reference';
25-
import { ExpUserDataWriter } from './user_data_writer';
26-
27-
export function useFirestorePipelines(): void {
28-
Firestore.prototype.pipeline = function (): PipelineSource {
29-
const firestore = this;
30-
return new PipelineSource(
31-
this,
32-
newUserDataReader(firestore),
33-
new ExpUserDataWriter(firestore),
34-
(key: DocumentKey) => {
35-
return new DocumentReference(firestore, null, key);
36-
}
37-
);
38-
};
39-
40-
Query.prototype.pipeline = function (): Pipeline {
41-
let pipeline;
42-
if (this._query.collectionGroup) {
43-
pipeline = this.firestore
44-
.pipeline()
45-
.collectionGroup(this._query.collectionGroup);
46-
} else {
47-
pipeline = this.firestore
48-
.pipeline()
49-
.collection(this._query.path.canonicalString());
50-
}
51-
52-
// TODO(pipeline) convert existing query filters, limits, etc into
53-
// pipeline stages
54-
55-
return pipeline;
56-
};
57-
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 { PipelineSource as LitePipelineSoure } from '../lite-api/pipeline-source';
19+
import {
20+
CollectionGroupSource,
21+
CollectionSource,
22+
DatabaseSource,
23+
DocumentsSource
24+
} from '../lite-api/stage';
25+
import { UserDataReader } from '../lite-api/user_data_reader';
26+
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
27+
import { DocumentKey } from '../model/document_key';
28+
import { cast } from '../util/input_validation';
29+
30+
import { Firestore } from './database';
31+
import { Pipeline } from './pipeline';
32+
import { DocumentReference } from './reference';
33+
34+
/**
35+
* Represents the source of a Firestore {@link Pipeline}.
36+
* @beta
37+
*/
38+
export class PipelineSource extends LitePipelineSoure {
39+
/**
40+
* @internal
41+
* @private
42+
* @param db
43+
* @param userDataReader
44+
* @param userDataWriter
45+
* @param documentReferenceFactory
46+
*/
47+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
48+
constructor(
49+
db: Firestore,
50+
userDataReader: UserDataReader,
51+
userDataWriter: AbstractUserDataWriter,
52+
documentReferenceFactory: (id: DocumentKey) => DocumentReference
53+
) {
54+
super(db, userDataReader, userDataWriter, documentReferenceFactory);
55+
}
56+
57+
collection(collectionPath: string): Pipeline {
58+
const db = cast<Firestore>(this.db, Firestore);
59+
return new Pipeline(
60+
db,
61+
this.userDataReader,
62+
this.userDataWriter,
63+
this.documentReferenceFactory,
64+
[new CollectionSource(collectionPath)]
65+
);
66+
}
67+
68+
collectionGroup(collectionId: string): Pipeline {
69+
const db = cast<Firestore>(this.db, Firestore);
70+
return new Pipeline(
71+
db,
72+
this.userDataReader,
73+
this.userDataWriter,
74+
this.documentReferenceFactory,
75+
[new CollectionGroupSource(collectionId)]
76+
);
77+
}
78+
79+
database(): Pipeline {
80+
const db = cast<Firestore>(this.db, Firestore);
81+
return new Pipeline(
82+
db,
83+
this.userDataReader,
84+
this.userDataWriter,
85+
this.documentReferenceFactory,
86+
[new DatabaseSource()]
87+
);
88+
}
89+
90+
documents(docs: DocumentReference[]): Pipeline {
91+
const db = cast<Firestore>(this.db, Firestore);
92+
return new Pipeline(
93+
db,
94+
this.userDataReader,
95+
this.userDataWriter,
96+
this.documentReferenceFactory,
97+
[DocumentsSource.of(docs)]
98+
);
99+
}
100+
}

packages/firestore/src/api/pipeline.ts

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

18-
import { firestoreClientExecutePipeline } from '../core/firestore_client';
1918
import { Pipeline as LitePipeline } from '../lite-api/pipeline';
2019
import { PipelineResult } from '../lite-api/pipeline-result';
21-
import { PipelineSource } from '../lite-api/pipeline-source';
22-
import { DocumentData, DocumentReference, Query } from '../lite-api/reference';
20+
import { DocumentData, DocumentReference } from '../lite-api/reference';
2321
import { Stage } from '../lite-api/stage';
2422
import { UserDataReader } from '../lite-api/user_data_reader';
2523
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
2624
import { DocumentKey } from '../model/document_key';
27-
import { cast } from '../util/input_validation';
2825

29-
import { ensureFirestoreConfigured, Firestore } from './database';
26+
import { Firestore } from './database';
3027

3128
export class Pipeline<
3229
AppModelType = DocumentData
@@ -61,6 +58,24 @@ export class Pipeline<
6158
);
6259
}
6360

61+
protected newPipeline(
62+
db: Firestore,
63+
userDataReader: UserDataReader,
64+
userDataWriter: AbstractUserDataWriter,
65+
documentReferenceFactory: (id: DocumentKey) => DocumentReference,
66+
stages: Stage[],
67+
converter: unknown = {}
68+
): Pipeline<AppModelType> {
69+
return new Pipeline<AppModelType>(
70+
db,
71+
userDataReader,
72+
userDataWriter,
73+
documentReferenceFactory,
74+
stages,
75+
converter
76+
);
77+
}
78+
6479
/**
6580
* Executes this pipeline and returns a Promise to represent the asynchronous operation.
6681
*
@@ -93,43 +108,8 @@ export class Pipeline<
93108
* @return A Promise representing the asynchronous pipeline execution.
94109
*/
95110
execute(): Promise<Array<PipelineResult<AppModelType>>> {
96-
const firestore = cast(this._db, Firestore);
97-
const client = ensureFirestoreConfigured(firestore);
98-
return firestoreClientExecutePipeline(client, this).then(result => {
99-
const docs = result.map(
100-
element =>
101-
new PipelineResult<AppModelType>(
102-
this.userDataWriter,
103-
element.key?.path
104-
? this.documentReferenceFactory(element.key)
105-
: undefined,
106-
element.fields,
107-
element.executionTime?.toTimestamp(),
108-
element.createTime?.toTimestamp(),
109-
element.updateTime?.toTimestamp()
110-
//this.converter
111-
)
112-
);
113-
114-
return docs;
115-
});
111+
throw new Error(
112+
'Pipelines not initialized. Your application must call `useFirestorePipelines()` before using Firestore Pipeline features.'
113+
);
116114
}
117115
}
118-
119-
/**
120-
* Experimental Modular API for console testing.
121-
* @param firestore
122-
*/
123-
export function pipeline(firestore: Firestore): PipelineSource;
124-
125-
/**
126-
* Experimental Modular API for console testing.
127-
* @param query
128-
*/
129-
export function pipeline(query: Query): Pipeline;
130-
131-
export function pipeline(
132-
firestoreOrQuery: Firestore | Query
133-
): PipelineSource | Pipeline {
134-
return firestoreOrQuery.pipeline();
135-
}

0 commit comments

Comments
 (0)