|
| 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 { PipelineSnapshot } from './snapshot'; |
| 16 | +import { FirestoreError } from '../util/error'; |
| 17 | +import { Unsubscribe } from './reference_impl'; |
| 18 | +import { Sort } from '../lite-api/stage'; |
| 19 | +import { Field } from '../lite-api/expressions'; |
| 20 | +import { ensureFirestoreConfigured, Firestore } from './database'; |
| 21 | +import { ViewSnapshot } from '../core/view_snapshot'; |
| 22 | +import { |
| 23 | + firestoreClientExecutePipeline, |
| 24 | + firestoreClientListen |
| 25 | +} from '../core/firestore_client'; |
| 26 | +import { Pipeline } from '../lite-api/pipeline'; |
| 27 | +import { PipelineResult } from '../lite-api/pipeline-result'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Executes this pipeline and returns a Promise to represent the asynchronous operation. |
| 31 | + * |
| 32 | + * <p>The returned Promise can be used to track the progress of the pipeline execution |
| 33 | + * and retrieve the results (or handle any errors) asynchronously. |
| 34 | + * |
| 35 | + * <p>The pipeline results are returned as a list of {@link PipelineResult} objects. Each {@link |
| 36 | + * PipelineResult} typically represents a single key/value map that has passed through all the |
| 37 | + * stages of the pipeline, however this might differ depending on the stages involved in the |
| 38 | + * pipeline. For example: |
| 39 | + * |
| 40 | + * <ul> |
| 41 | + * <li>If there are no stages or only transformation stages, each {@link PipelineResult} |
| 42 | + * represents a single document.</li> |
| 43 | + * <li>If there is an aggregation, only a single {@link PipelineResult} is returned, |
| 44 | + * representing the aggregated results over the entire dataset .</li> |
| 45 | + * <li>If there is an aggregation stage with grouping, each {@link PipelineResult} represents a |
| 46 | + * distinct group and its associated aggregated values.</li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * <p>Example: |
| 50 | + * |
| 51 | + * ```typescript |
| 52 | + * const futureResults = await firestore.pipeline().collection("books") |
| 53 | + * .where(gt(Field.of("rating"), 4.5)) |
| 54 | + * .select("title", "author", "rating") |
| 55 | + * .execute(); |
| 56 | + * ``` |
| 57 | + * |
| 58 | + * @return A Promise representing the asynchronous pipeline execution. |
| 59 | + */ |
| 60 | +export function execute<AppModelType>( |
| 61 | + pipeline: Pipeline<AppModelType> |
| 62 | +): Promise<Array<PipelineResult<AppModelType>>> { |
| 63 | + const client = ensureFirestoreConfigured(pipeline.liteDb as Firestore); |
| 64 | + return firestoreClientExecutePipeline(client, pipeline as Pipeline).then( |
| 65 | + result => { |
| 66 | + const docs = result.map( |
| 67 | + element => |
| 68 | + new PipelineResult<AppModelType>( |
| 69 | + pipeline.userDataWriter, |
| 70 | + element.key?.path |
| 71 | + ? pipeline.documentReferenceFactory(element.key) |
| 72 | + : undefined, |
| 73 | + element.fields, |
| 74 | + element.executionTime?.toTimestamp(), |
| 75 | + element.createTime?.toTimestamp(), |
| 76 | + element.updateTime?.toTimestamp() |
| 77 | + //this.converter |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + return docs; |
| 82 | + } |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * @internal |
| 88 | + * @private |
| 89 | + */ |
| 90 | +export function _onSnapshot( |
| 91 | + pipeline: Pipeline, |
| 92 | + next: (snapshot: PipelineSnapshot) => void, |
| 93 | + error?: (error: FirestoreError) => void, |
| 94 | + complete?: () => void |
| 95 | +): Unsubscribe { |
| 96 | + // this.stages.push( |
| 97 | + // new AddFields( |
| 98 | + // this.selectablesToMap([ |
| 99 | + // '__name__', |
| 100 | + // '__create_time__', |
| 101 | + // '__update_time__' |
| 102 | + // ]) |
| 103 | + // ) |
| 104 | + // ); |
| 105 | + |
| 106 | + pipeline.stages.push(new Sort([Field.of('__name__').ascending()])); |
| 107 | + |
| 108 | + const client = ensureFirestoreConfigured(pipeline.liteDb as Firestore); |
| 109 | + const observer = { |
| 110 | + next: (snapshot: ViewSnapshot) => { |
| 111 | + new PipelineSnapshot(pipeline, snapshot); |
| 112 | + }, |
| 113 | + error: error, |
| 114 | + complete: complete |
| 115 | + }; |
| 116 | + // TODO(pipeline) hook up options |
| 117 | + firestoreClientListen(client, pipeline, {}, observer); |
| 118 | + |
| 119 | + return () => {}; |
| 120 | +} |
0 commit comments