execute method

Future<PipelineSnapshot> execute(
  1. Pipeline pipeline, {
  2. PipelineIndexMode? indexMode,
  3. PipelineExplainOptions? explainOptions,
  4. Map<String, Object?>? rawOptions,
})

Executes pipeline on the server inside this transaction. Only in a read-only transaction (runTransaction(..., readOnly: true)): the pipeline reads the snapshot of the database that every read of the transaction sees.

In a read-write transaction the returned Future fails with a FirestoreException with code invalidArgument, as for Transaction.getQuery: such a transaction verifies at commit that the documents it read did not change, which is not possible for the results of a pipeline. A pipeline with a write stage (delete, update, insert, upsert) fails the same way: a read-only transaction cannot write. (The Enterprise emulator v1.22.0 would apply such a pipeline inside a read-only transaction, so the SDK checks it.)

indexMode, explainOptions and rawOptions are as for Pipeline.execute.

PORTING NOTE: the JS and Android client SDKs have no pipelines in transactions; the Node.js server SDK has Transaction.execute(pipeline).

Implementation

Future<PipelineSnapshot> execute(
  Pipeline pipeline, {
  PipelineIndexMode? indexMode,
  PipelineExplainOptions? explainOptions,
  Map<String, Object?>? rawOptions,
}) async {
  if (pipeline.firestore != firestore) {
    throw ArgumentError(
      'Provided pipeline is from a different Cloud Firestore instance.',
    );
  }
  pipeline.checkExecutable();
  for (final Stage stage in pipeline.stages) {
    if (_writeStages.contains(stage.name)) {
      throw FirestoreException(
        message:
            'A pipeline with a ${stage.name} stage cannot run in a transaction: '
            'read-only transactions cannot write.',
        errorCode: FirestoreErrorCode.invalidArgument,
      );
    }
  }
  final PipelineEncoder encoder = PipelineEncoder(firestore.modelDatabaseId);
  final proto.ExecutePipelineRequest request = pipeline.toExecuteRequest(
    encoder,
    indexMode: indexMode,
    explainOptions: explainOptions,
    rawOptions: rawOptions,
  );
  final List<proto.ExecutePipelineResponse> responses = await coreTransaction
      .executePipeline(request);
  return decodePipelineResponses(
    pipeline,
    firestore,
    encoder.serializer,
    responses,
  );
}