|
17 | 17 |
|
18 | 18 | import { Pipeline } from '../api/pipeline'; |
19 | 19 | import { firestoreClientExecutePipeline } from '../core/firestore_client'; |
| 20 | +import { |
| 21 | + StructuredPipeline, |
| 22 | + StructuredPipelineOptions |
| 23 | +} from '../core/structured_pipeline'; |
20 | 24 | import { Pipeline as LitePipeline } from '../lite-api/pipeline'; |
21 | 25 | import { PipelineResult, PipelineSnapshot } from '../lite-api/pipeline-result'; |
22 | 26 | import { PipelineSource } from '../lite-api/pipeline-source'; |
| 27 | +import { PipelineOptions } from '../lite-api/pipeline_settings'; |
23 | 28 | import { Stage } from '../lite-api/stage'; |
24 | | -import { newUserDataReader } from '../lite-api/user_data_reader'; |
| 29 | +import { |
| 30 | + newUserDataReader, |
| 31 | + parseData, |
| 32 | + UserDataReader, |
| 33 | + UserDataSource |
| 34 | +} from '../lite-api/user_data_reader'; |
| 35 | +import { ApiClientObjectMap, Value } from '../protos/firestore_proto_api'; |
25 | 36 | import { cast } from '../util/input_validation'; |
26 | 37 |
|
27 | 38 | import { ensureFirestoreConfigured, Firestore } from './database'; |
@@ -68,35 +79,68 @@ declare module './database' { |
68 | 79 | * @param pipeline The pipeline to execute. |
69 | 80 | * @return A Promise representing the asynchronous pipeline execution. |
70 | 81 | */ |
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 | + const pipeline: LitePipeline = |
| 88 | + pipelineOrOptions instanceof LitePipeline |
| 89 | + ? pipelineOrOptions |
| 90 | + : pipelineOrOptions.pipeline; |
| 91 | + const options: StructuredPipelineOptions = !( |
| 92 | + pipelineOrOptions instanceof LitePipeline |
| 93 | + ) |
| 94 | + ? pipelineOrOptions |
| 95 | + : {}; |
| 96 | + const genericOptions: { [name: string]: unknown } = |
| 97 | + (pipelineOrOptions as PipelineOptions).genericOptions ?? {}; |
| 98 | + |
72 | 99 | const firestore = cast(pipeline._db, Firestore); |
73 | 100 | 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; |
80 | 101 |
|
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 ?? {}; |
97 | 109 |
|
98 | | - return new PipelineSnapshot(pipeline, docs, executionTime); |
99 | | - }); |
| 110 | + const 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 | + ); |
100 | 144 | } |
101 | 145 |
|
102 | 146 | // Augment the Firestore class with the pipeline() factory method |
|
0 commit comments