@@ -21,12 +21,23 @@ import { Pipeline as LitePipeline } from '../lite-api/pipeline';
2121import { PipelineResult , PipelineSnapshot } from '../lite-api/pipeline-result' ;
2222import { PipelineSource } from '../lite-api/pipeline-source' ;
2323import { Stage } from '../lite-api/stage' ;
24- import { newUserDataReader } from '../lite-api/user_data_reader' ;
24+ import {
25+ newUserDataReader ,
26+ parseData ,
27+ UserDataReader ,
28+ UserDataSource
29+ } from '../lite-api/user_data_reader' ;
2530import { cast } from '../util/input_validation' ;
2631
2732import { ensureFirestoreConfigured , Firestore } from './database' ;
2833import { DocumentReference } from './reference' ;
2934import { ExpUserDataWriter } from './user_data_writer' ;
35+ import { PipelineOptions } from '../lite-api/pipeline_settings' ;
36+ import {
37+ StructuredPipeline ,
38+ StructuredPipelineOptions
39+ } from '../core/structured_pipeline' ;
40+ import { ApiClientObjectMap , Value } from '../protos/firestore_proto_api' ;
3041
3142declare module './database' {
3243 interface Firestore {
@@ -68,35 +79,68 @@ declare module './database' {
6879 * @param pipeline The pipeline to execute.
6980 * @return A Promise representing the asynchronous pipeline execution.
7081 */
71- export function execute ( pipeline : LitePipeline ) : Promise < PipelineSnapshot > {
82+ export function execute ( pipeline : LitePipeline ) : Promise < PipelineSnapshot > ;
83+ export function execute ( options : PipelineOptions ) : Promise < PipelineSnapshot > ;
84+ export function execute (
85+ pipelineOrOptions : LitePipeline | PipelineOptions
86+ ) : Promise < PipelineSnapshot > {
87+ let pipeline : LitePipeline =
88+ pipelineOrOptions instanceof LitePipeline
89+ ? pipelineOrOptions
90+ : pipelineOrOptions . pipeline ;
91+ let options : StructuredPipelineOptions = ! (
92+ pipelineOrOptions instanceof LitePipeline
93+ )
94+ ? pipelineOrOptions
95+ : { } ;
96+ let genericOptions : { [ name : string ] : unknown } =
97+ ( pipelineOrOptions as PipelineOptions ) . genericOptions ?? { } ;
98+
7299 const firestore = cast ( pipeline . _db , Firestore ) ;
73100 const client = ensureFirestoreConfigured ( firestore ) ;
74- return firestoreClientExecutePipeline ( client , pipeline ) . then ( result => {
75- // Get the execution time from the first result.
76- // firestoreClientExecutePipeline returns at least one PipelineStreamElement
77- // even if the returned document set is empty.
78- const executionTime =
79- result . length > 0 ? result [ 0 ] . executionTime ?. toTimestamp ( ) : undefined ;
80101
81- const docs = result
82- // Currently ignore any response from ExecutePipeline that does
83- // not contain any document data in the `fields` property.
84- . filter ( element => ! ! element . fields )
85- . map (
86- element =>
87- new PipelineResult (
88- pipeline . _userDataWriter ,
89- element . key ?. path
90- ? new DocumentReference ( firestore , null , element . key )
91- : undefined ,
92- element . fields ,
93- element . createTime ?. toTimestamp ( ) ,
94- element . updateTime ?. toTimestamp ( )
95- )
96- ) ;
102+ const udr = new UserDataReader (
103+ firestore . _databaseId ,
104+ /* ignoreUndefinedProperties */ true
105+ ) ;
106+ const context = udr . createContext ( UserDataSource . Argument , 'execute' ) ;
107+ const optionsOverride : ApiClientObjectMap < Value > =
108+ parseData ( genericOptions , context ) ?. mapValue ?. fields ?? { } ;
97109
98- return new PipelineSnapshot ( pipeline , docs , executionTime ) ;
99- } ) ;
110+ let structuredPipeline : StructuredPipeline = new StructuredPipeline (
111+ pipeline ,
112+ options ,
113+ optionsOverride
114+ ) ;
115+
116+ return firestoreClientExecutePipeline ( client , structuredPipeline ) . then (
117+ result => {
118+ // Get the execution time from the first result.
119+ // firestoreClientExecutePipeline returns at least one PipelineStreamElement
120+ // even if the returned document set is empty.
121+ const executionTime =
122+ result . length > 0 ? result [ 0 ] . executionTime ?. toTimestamp ( ) : undefined ;
123+
124+ const docs = result
125+ // Currently ignore any response from ExecutePipeline that does
126+ // not contain any document data in the `fields` property.
127+ . filter ( element => ! ! element . fields )
128+ . map (
129+ element =>
130+ new PipelineResult (
131+ pipeline . _userDataWriter ,
132+ element . key ?. path
133+ ? new DocumentReference ( firestore , null , element . key )
134+ : undefined ,
135+ element . fields ,
136+ element . createTime ?. toTimestamp ( ) ,
137+ element . updateTime ?. toTimestamp ( )
138+ )
139+ ) ;
140+
141+ return new PipelineSnapshot ( pipeline , docs , executionTime ) ;
142+ }
143+ ) ;
100144}
101145
102146// Augment the Firestore class with the pipeline() factory method
0 commit comments