Firestore トリガー(第 2 世代)

Firestore データベース内のイベントによってトリガーされるように Cloud Functions を構成できます。トリガーされると、関数は Firestore API とクライアント ライブラリを介して、これらのイベントに応答して Firestore データベースを読み取り、更新できます。

一般的なライフサイクルの場合、Firestore の関数は次のように動作します。

  1. 特定のドキュメントに変更が加えられるのを待ちます。

  2. イベントが発生するとトリガーされ、そのタスクを実行します。

  3. 影響を受けるドキュメントのスナップショットを含むデータ オブジェクトを受信します。write または update イベントの場合、トリガー イベントの前後のドキュメントの状態を表すスナップショットがデータ オブジェクトに含まれます。

イベントタイプ

Firestore は、createupdatedeletewrite イベントをサポートしています。write イベントには、ドキュメントに対するすべての変更が含まれます。

イベントタイプ トリガー
google.cloud.firestore.document.v1.created(デフォルト) ドキュメントが最初に書き込まれたときにトリガーされます。
google.cloud.firestore.document.v1.updated すでに存在するドキュメントの値が変更されたときにトリガーされます。
google.cloud.firestore.document.v1.deleted データを含むドキュメントが削除されたときにトリガーされます。
google.cloud.firestore.document.v1.written ドキュメントが作成、更新、削除されたときにトリガーされます。

ワイルドカードは、次のように中括弧で記述します。 "projects/YOUR_PROJECT_ID/databases/(default)/documents/collection/{document_wildcard}"

ドキュメントのパスを指定する

関数をトリガーするには、リッスンするドキュメント パスを指定します。ドキュメント パスは、関数と同じ Google Cloud プロジェクトに存在する必要があります。

有効なドキュメント パスの例を次に示します。

  • users/marie: 有効なトリガー。1 つのドキュメント(/users/marie)をモニタリングします。

  • users/{username}: 有効なトリガー。すべてのユーザー ドキュメントをモニタリングします。コレクション内のすべてのドキュメントをモニタリングする場合は、ワイルドカードを使用します。

  • users/{username}/addresses: 無効なトリガー。ドキュメントではなく、サブコレクション addresses を参照します。

  • users/{username}/addresses/home: 有効なトリガー。すべてのユーザーの自宅住所のドキュメントをモニタリングします。

  • users/{username}/addresses/{addressId}: 有効なトリガー。すべての住所ドキュメントをモニタリングします。

  • users/{user=**}: 有効なトリガー。すべてのユーザー ドキュメントと、各ユーザー ドキュメントのサブコレクション(/users/userID/address/home/users/userID/phone/work など)内のドキュメントをモニタリングします。

ワイルドカードとパラメータ

モニタリングするドキュメントが不明な場合は、ドキュメント ID の代わりに {wildcard} を使用します。

  • users/{username} は、すべてのユーザー ドキュメントに対する変更をリッスンします。

この例では、users にあるドキュメントの任意のフィールドが変更されると、{username} というワイルドカードと照合されます。

users に含まれるドキュメントにサブコレクションがある場合、サブコレクションのいずれかに含まれるドキュメントのフィールドが変更されても、{username} ワイルドカードはトリガーされません。サブコレクション内のイベントにも応答することが目標なら、マルチセグメント ワイルドカード {username=**} を使用します。

ワイルドカードに一致した部分が、ドキュメント パスから抽出されます。明示的なコレクションまたはドキュメント ID に置き換えるワイルドカードは、必要な数だけ定義できます。マルチセグメント ワイルドカード({username=**} など)は 1 つまで使用できます。

イベントの構造

このトリガーは、次のようなイベントで関数を呼び出します。

{
    "oldValue": { // Update and Delete operations only
        A Document object containing a pre-operation document snapshot
    },
    "updateMask": { // Update operations only
        A DocumentMask object that lists changed fields.
    },
    "value": {
        // A Document object containing a post-operation document snapshot
    }
}

それぞれの Document オブジェクトには、1 つ以上の Value オブジェクトが含まれます。型の詳細については、Value ドキュメントをご覧ください。これは、Go などの型言語を使用して関数を記述する場合に便利です。

Firestore データベースを設定する

このドキュメントのサンプルをテストするには、Firestore データベースが必要です。関数をデプロイする前に用意しておく必要があります。Firestore データベースがまだない場合は、次のように作成します。

  1. Firestore の [データ] ページに移動します

  2. [ネイティブ モードを選択] をクリックします。

  3. データベースを配置するリージョン(ロケーション)を選択します。この選択は永続的に適用されます。

  4. [データベースを作成] をクリックします。

Firestore データモデルは、ドキュメントを含むコレクションで構成されます。ドキュメントには、一連の Key-Value ペアが含まれています。

このチュートリアルで作成する関数は、指定したコレクション内のドキュメントに変更を加えたときにトリガーされます。

例 1: Hello Firestore 関数

次のサンプル Cloud Functions は、トリガーとなった Firestore イベントのフィールドを出力します。

Node.js

protobufjs を使用して、イベントデータをデコードします。ソースに google.events.cloud.firestore.v1 data.proto を含めます。

/**
 * Cloud Event Function triggered by a change to a Firestore document.
 */
const functions = require('@google-cloud/functions-framework');
const protobuf = require('protobufjs');

functions.cloudEvent('helloFirestore', async cloudEvent => {
  console.log(`Function triggered by event on: ${cloudEvent.source}`);
  console.log(`Event type: ${cloudEvent.type}`);

  console.log('Loading protos...');
  const root = await protobuf.load('data.proto');
  const DocumentEventData = root.lookupType(
    'google.events.cloud.firestore.v1.DocumentEventData'
  );

  console.log('Decoding data...');
  const firestoreReceived = DocumentEventData.decode(cloudEvent.data);

  console.log('\nOld value:');
  console.log(JSON.stringify(firestoreReceived.oldValue, null, 2));

  console.log('\nNew value:');
  console.log(JSON.stringify(firestoreReceived.value, null, 2));
});

Python

from cloudevents.http import CloudEvent
import functions_framework
from google.events.cloud import firestore

@functions_framework.cloud_event
def hello_firestore(cloud_event: CloudEvent) -> None:
    """Triggers by a change to a Firestore document.

    Args:
        cloud_event: cloud event with information on the firestore event trigger
    """
    firestore_payload = firestore.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    print(f"Function triggered by change to: {cloud_event['source']}")

    print("\nOld value:")
    print(firestore_payload.old_value)

    print("\nNew value:")
    print(firestore_payload.value)

Go


// Package hellofirestore contains a Cloud Event Function triggered by a Cloud Firestore event.
package hellofirestore

import (
	"context"
	"fmt"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/firestoredata"
	"google.golang.org/protobuf/proto"
)

func init() {
	functions.CloudEvent("helloFirestore", HelloFirestore)
}

// HelloFirestore is triggered by a change to a Firestore document.
func HelloFirestore(ctx context.Context, event event.Event) error {
	var data firestoredata.DocumentEventData
	if err := proto.Unmarshal(event.Data(), &data); err != nil {
		return fmt.Errorf("proto.Unmarshal: %w", err)
	}

	fmt.Printf("Function triggered by change to: %v\n", event.Source())
	fmt.Printf("Old value: %+v\n", data.GetOldValue())
	fmt.Printf("New value: %+v\n", data.GetValue())
	return nil
}

Java

import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.logging.Logger;

public class FirebaseFirestore implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());

  @Override
  public void accept(CloudEvent event) throws InvalidProtocolBufferException {
    DocumentEventData firestorEventData = DocumentEventData.parseFrom(event.getData().toBytes());

    logger.info("Function triggered by event on: " + event.getSource());
    logger.info("Event type: " + event.getType());

    logger.info("Old value:");
    logger.info(firestorEventData.getOldValue().toString());

    logger.info("New value:");
    logger.info(firestorEventData.getValue().toString());
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.Firestore.V1;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FirebaseFirestore;

public class Function : ICloudEventFunction<DocumentEventData>
{
    private readonly ILogger _logger;

    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public Task HandleAsync(CloudEvent cloudEvent, DocumentEventData data, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Function triggered by event on {subject}", cloudEvent.Subject);
        _logger.LogInformation("Event type: {type}", cloudEvent.Type);
        MaybeLogDocument("Old value", data.OldValue);
        MaybeLogDocument("New value", data.Value);

        // In this example, we don't need to perform any asynchronous operations, so the
        // method doesn't need to be declared async.
        return Task.CompletedTask;
    }

    /// <summary>
    /// Logs the names and values of the fields in a document in a very simplistic way.
    /// </summary>
    private void MaybeLogDocument(string message, Document document)
    {
        if (document is null)
        {
            return;
        }

        // ConvertFields converts the Firestore representation into a .NET-friendly
        // representation.
        IReadOnlyDictionary<string, object> fields = document.ConvertFields();
        var fieldNamesAndTypes = fields
            .OrderBy(pair => pair.Key)
            .Select(pair => $"{pair.Key}: {pair.Value}");
        _logger.LogInformation(message + ": {fields}", string.Join(", ", fieldNamesAndTypes));
    }
}

Hello Firestore 関数をデプロイする

  1. Firestore データベースをまだ設定していない場合は、設定します。

  2. Firestore トリガーを使用して Hello Firestore 関数をデプロイするには、サンプルコード(Java の場合は pom.xml ファイル)を含むディレクトリで次のコマンドを実行します。

    gcloud functions deploy FUNCTION_NAME \
    --gen2 \
    --runtime=RUNTIME \
    --region=REGION \
    --trigger-location=TRIGGER REGION \
    --source=. \
    --entry-point=ENTRY_POINT \
    --trigger-event-filters=type=google.cloud.firestore.document.v1.written \
    --trigger-event-filters=database='(default)' \
    --trigger-event-filters-path-pattern=document='users/{username}'
    

    次のように置き換えます。

    • FUNCTION_NAME: デプロイされる関数の名前。
    • RUNTIME: 関数で使用される言語ランタイム。
    • REGION: 関数をデプロイするリージョン。
    • TRIGGER_REGION: トリガーのロケーション。Firestore データベースのリージョンと同じにする必要があります。
    • ENTRY_POINT: ソースコード内の関数のエントリ ポイント。これは、関数の実行時に実行されるコードです。

    他のフィールドはそのまま使用します。

    • --trigger-event-filters=type=google.cloud.firestore.document.v1.written は、google.cloud.firestore.document.v1.written イベントタイプに従って、ドキュメントが作成、更新、削除されたとき関数がトリガーされるように指定します。
    • --trigger-event-filters=database='(default)' には Firebase データベースを指定します。デフォルトのデータベース名には (default) を使用します。
    • --trigger-event-filters-path-pattern=document='users/{username}' は、関連する変更をモニタリングするドキュメントのパスパターンを指定します。このパスパターンは、users コレクション内のすべてのドキュメントをモニタリングする必要があることを示しています。詳細については、パスパターンについてをご覧ください。

Hello Firestore 関数をテストする

Hello Firestore 関数をテストするには、Firestore データベースusers というコレクションを設定します。

  1. Firestore の [データ] ページで、[コレクションを開始] をクリックします。

  2. コレクション ID として users を指定します。

  3. コレクションの最初のドキュメントの追加を開始するには、[最初のドキュメントの追加] で、自動生成されたドキュメント ID を使用します。

  4. ドキュメントに少なくとも 1 つのフィールドを追加し、名前と値を指定します。この例では、名前は「username」、値は「rowan」です。

    Firestore コレクションの作成を示すスクリーンショット

  5. 完了したら [保存] をクリックします。

    この操作により新しいドキュメントが作成され、関数がトリガーされます。

  6. 関数がトリガーされたことを確認するには、Google Cloud コンソールの Cloud Functions の概要ページで関数のリンク名をクリックして、[関数の詳細] ページを開きます。

  7. [ログ] タブを開き、次の文字列を探します。

Function triggered by change to: //firestore.googleapis.com/projects/your-project-id/databases/(default)'

例 2: Convert to Uppercase 関数

次の例では、ユーザーが追加した値を取得し、その場所にある文字列を大文字に変換して、値を大文字の文字列に置き換えています。

Node.js

protobufjs を使用して、イベントデータをデコードします。ソースに google.events.cloud.firestore.v1 data.proto を含めます。

const functions = require('@google-cloud/functions-framework');
const Firestore = require('@google-cloud/firestore');
const protobuf = require('protobufjs');

const firestore = new Firestore({
  projectId: process.env.GOOGLE_CLOUD_PROJECT,
});

// Converts strings added to /messages/{pushId}/original to uppercase
functions.cloudEvent('makeUpperCase', async cloudEvent => {
  console.log('Loading protos...');
  const root = await protobuf.load('data.proto');
  const DocumentEventData = root.lookupType(
    'google.events.cloud.firestore.v1.DocumentEventData'
  );

  console.log('Decoding data...');
  const firestoreReceived = DocumentEventData.decode(cloudEvent.data);

  const resource = firestoreReceived.value.name;
  const affectedDoc = firestore.doc(resource.split('/documents/')[1]);

  const curValue = firestoreReceived.value.fields.original.stringValue;
  const newValue = curValue.toUpperCase();

  if (curValue === newValue) {
    // Value is already upper-case
    // Don't perform a(nother) write to avoid infinite loops
    console.log('Value is already upper-case.');
    return;
  }

  console.log(`Replacing value: ${curValue} --> ${newValue}`);
  affectedDoc.set({
    original: newValue,
  });
});

Python

from cloudevents.http import CloudEvent
import functions_framework
from google.cloud import firestore
from google.events.cloud import firestore as firestoredata

client = firestore.Client()

# Converts strings added to /messages/{pushId}/original to uppercase
@functions_framework.cloud_event
def make_upper_case(cloud_event: CloudEvent) -> None:
    firestore_payload = firestoredata.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    path_parts = firestore_payload.value.name.split("/")
    separator_idx = path_parts.index("documents")
    collection_path = path_parts[separator_idx + 1]
    document_path = "/".join(path_parts[(separator_idx + 2) :])

    print(f"Collection path: {collection_path}")
    print(f"Document path: {document_path}")

    affected_doc = client.collection(collection_path).document(document_path)

    cur_value = firestore_payload.value.fields["original"].string_value
    new_value = cur_value.upper()

    if cur_value != new_value:
        print(f"Replacing value: {cur_value} --> {new_value}")
        affected_doc.set({"original": new_value})
    else:
        # Value is already upper-case
        # Don't perform a second write (which can trigger an infinite loop)
        print("Value is already upper-case.")

Go


// Package upper contains a Firestore Cloud Function.
package upper

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"strings"

	"cloud.google.com/go/firestore"
	firebase "firebase.google.com/go/v4"
	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/firestoredata"
	"google.golang.org/protobuf/proto"
)

// set the GOOGLE_CLOUD_PROJECT environment variable when deploying.
var projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")

// client is a Firestore client, reused between function invocations.
var client *firestore.Client

func init() {
	// Use the application default credentials.
	conf := &firebase.Config{ProjectID: projectID}

	// Use context.Background() because the app/client should persist across
	// invocations.
	ctx := context.Background()

	app, err := firebase.NewApp(ctx, conf)
	if err != nil {
		log.Fatalf("firebase.NewApp: %v", err)
	}

	client, err = app.Firestore(ctx)
	if err != nil {
		log.Fatalf("app.Firestore: %v", err)
	}

	// Register cloud event function
	functions.CloudEvent("MakeUpperCase", MakeUpperCase)
}

// MakeUpperCase is triggered by a change to a Firestore document. It updates
// the `original` value of the document to upper case.
func MakeUpperCase(ctx context.Context, e event.Event) error {
	var data firestoredata.DocumentEventData
	if err := proto.Unmarshal(e.Data(), &data); err != nil {
		return fmt.Errorf("proto.Unmarshal: %w", err)
	}

	if data.GetValue() == nil {
		return errors.New("Invalid message: 'Value' not present")
	}

	fullPath := strings.Split(data.GetValue().GetName(), "/documents/")[1]
	pathParts := strings.Split(fullPath, "/")
	collection := pathParts[0]
	doc := strings.Join(pathParts[1:], "/")

	var originalStringValue string
	if v, ok := data.GetValue().GetFields()["original"]; ok {
		originalStringValue = v.GetStringValue()
	} else {
		return errors.New("Document did not contain field \"original\"")
	}

	newValue := strings.ToUpper(originalStringValue)
	if originalStringValue == newValue {
		log.Printf("%q is already upper case: skipping", originalStringValue)
		return nil
	}
	log.Printf("Replacing value: %q -> %q", originalStringValue, newValue)

	newDocumentEntry := map[string]string{"original": newValue}
	_, err := client.Collection(collection).Doc(doc).Set(ctx, newDocumentEntry)
	if err != nil {
		return fmt.Errorf("Set: %w", err)
	}
	return nil
}

Java

import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.SetOptions;
import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.events.cloud.firestore.v1.Value;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

public class FirebaseFirestoreReactive implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestoreReactive.class.getName());
  private final Firestore firestore;

  private static final String FIELD_KEY = "original";
  private static final String APPLICATION_PROTOBUF = "application/protobuf";

  public FirebaseFirestoreReactive() {
    this(FirestoreOptions.getDefaultInstance().getService());
  }

  public FirebaseFirestoreReactive(Firestore firestore) {
    this.firestore = firestore;
  }

  @Override
  public void accept(CloudEvent event)
      throws InvalidProtocolBufferException, InterruptedException, ExecutionException {
    if (event.getData() == null) {
      logger.warning("No data found in event!");
      return;
    }

    if (!event.getDataContentType().equals(APPLICATION_PROTOBUF)) {
      logger.warning(String.format("Found unexpected content type %s, expected %s",
          event.getDataContentType(),
          APPLICATION_PROTOBUF));
      return;
    }

    DocumentEventData firestoreEventData = DocumentEventData
        .parseFrom(event.getData().toBytes());

    // Get the fields from the post-operation document snapshot
    // https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents#Document
    Map<String, Value> fields = firestoreEventData.getValue().getFieldsMap();
    if (!fields.containsKey(FIELD_KEY)) {
      logger.warning("Document does not contain original field");
      return;
    }
    String currValue = fields.get(FIELD_KEY).getStringValue();
    String newValue = currValue.toUpperCase();

    if (currValue.equals(newValue)) {
      logger.info("Value is already upper-case");
      return;
    }

    // Retrieve the document name from the resource path:
    // projects/{project_id}/databases/{database_id}/documents/{document_path}
    String affectedDoc = firestoreEventData.getValue()
        .getName()
        .split("/documents/")[1]
        .replace("\"", "");

    logger.info(String.format("Replacing values: %s --> %s", currValue, newValue));

    // Wait for the async call to complete
    this.firestore
        .document(affectedDoc)
        .set(Map.of(FIELD_KEY, newValue), SetOptions.merge())
        .get();
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Firestore;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Google.Events.Protobuf.Cloud.Firestore.V1;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace FirestoreReactive;

public class Startup : FunctionsStartup
{
    public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
        services.AddSingleton(FirestoreDb.Create());
}

// Register the startup class to provide the Firestore dependency.
[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<DocumentEventData>
{
    private readonly ILogger _logger;
    private readonly FirestoreDb _firestoreDb;

    public Function(ILogger<Function> logger, FirestoreDb firestoreDb) =>
        (_logger, _firestoreDb) = (logger, firestoreDb);

    public async Task HandleAsync(CloudEvent cloudEvent, DocumentEventData data, CancellationToken cancellationToken)
    {
        // Get the recently-written value. This expression will result in a null value
        // if any of the following is true:
        // - The event doesn't contain a "new" document
        // - The value doesn't contain a field called "original"
        // - The "original" field isn't a string
        string currentValue = data.Value?.ConvertFields().GetValueOrDefault("original") as string;
        if (currentValue is null)
        {
            _logger.LogWarning($"Event did not contain a suitable document");
            return;
        }

        string newValue = currentValue.ToUpperInvariant();
        if (newValue == currentValue)
        {
            _logger.LogInformation("Value is already upper-cased; no replacement necessary");
            return;
        }

        // The CloudEvent subject is "documents/x/y/...".
        // The Firestore SDK FirestoreDb.Document method expects a reference relative to
        // "documents" (so just the "x/y/..." part). This may be simplified over time.
        if (cloudEvent.Subject is null || !cloudEvent.Subject.StartsWith("documents/"))
        {
            _logger.LogWarning("CloudEvent subject is not a document reference.");
            return;
        }
        string documentPath = cloudEvent.Subject.Substring("documents/".Length);

        _logger.LogInformation("Replacing '{current}' with '{new}' in '{path}'", currentValue, newValue, documentPath);
        await _firestoreDb.Document(documentPath).UpdateAsync("original", newValue);
    }
}

Convert to Uppercase 関数をデプロイする

  1. Firestore データベースをまだ設定していない場合は、設定します。

  2. ドキュメント companies/{CompanyId} の書き込みイベントによってトリガーされる関数をデプロイするには、次のコマンドを使用します。

    gcloud functions deploy FUNCTION_NAME \
    --gen2 \
    --runtime=RUNTIME \
    --trigger-location=TRIGGER REGION \
    --region=REGION \
    --source=. \
    --entry-point=ENTRY_POINT \
    --trigger-event-filters=type=google.cloud.firestore.document.v1.written \
    --trigger-event-filters=database='(default)' \
    --trigger-event-filters-path-pattern=document='messages/{pushId}'
    

    次のように置き換えます。

    • FUNCTION_NAME: デプロイされる関数の名前。
    • RUNTIME: 関数で使用される言語ランタイム。
    • REGION: 関数をデプロイするリージョン。
    • TRIGGER_REGION: トリガーのロケーション。Firestore データベースのリージョンと同じにする必要があります。
    • ENTRY_POINT: ソースコード内の関数のエントリ ポイント。これは、関数の実行時に実行されるコードです。

    他のフィールドはそのまま使用します。

    • --trigger-event-filters=type=google.cloud.firestore.document.v1.written は、google.cloud.firestore.document.v1.written イベントタイプに従って、ドキュメントが作成、更新、削除されたとき関数がトリガーされるように指定します。
    • --trigger-event-filters=database='(default)' には、Firestore データベースを指定します。デフォルトのデータベース名には (default) を使用します。
    • --trigger-event-filters-path-pattern=document='messages/{pushId}' は、関連する変更をモニタリングするドキュメントのパスパターンを指定します。このパスパターンは、messages コレクション内のすべてのドキュメントをモニタリングする必要があることを示しています。詳細については、パスパターンについてをご覧ください。

Convert to Uppercase 関数をテストする

デプロイした Convert to Uppercase 関数をテストするには、Firestore データベースmessages というコレクションを設定します。

  1. Firestore の [データ] ページに移動します。

  2. [コレクションを開始] をクリックします。

  3. コレクション ID として messages を指定します。

  4. コレクションの最初のドキュメントの追加を開始するには、[最初のドキュメントの追加] で、自動生成されたドキュメント ID を使用します。

  5. デプロイされた関数をトリガーするには、フィールド名が「original」で、フィールドの値が小文字からなる単語のドキュメントを追加します。次に例を示します。

    Firestore コレクションの作成を示すスクリーンショット

  6. ドキュメントを保存すると、値フィールドの小文字の単語が大文字に変換されます。

    その後、フィールド値を編集して小文字を含めると、関数が再度トリガーされ、すべての小文字が大文字に変換されます。

制限事項

Cloud Functions の Firestore トリガーには、次の制限事項があります。

  • 順序は保証されません。短時間に複数の変更を行うと、予期しない順序で関数の呼び出しがトリガーされることがあります。
  • イベントは必ず 1 回以上処理されますが、1 つのイベントで関数が複数回呼び出される場合があります。「正確に 1 回」のメカニズムに依存することは避け、べき等性がある関数を記述してください。
  • Datastore モードの Firestore には、Cloud Functions(第 2 世代)が必要です。Cloud Functions(第 1 世代)では、Datastore モードはサポートされていません。
  • Cloud Functions(第 1 世代)は「(デフォルト)」データベースでのみ動作し、Firestore の名前付きデータベースをサポートしていません。名前付きデータベースのイベントを構成するには、Cloud Functions(第 2 世代)を使用してください。
  • セッションは、単一のデータベースに関連付けられます。複数のデータベースに一致するトリガーは作成できません。
  • データベースを削除しても、そのデータベースのトリガーは自動的に削除されません。トリガーはイベントの配信を停止しますが、トリガーを削除するまで存在し続けます。
  • 一致したイベントが最大リクエスト サイズを超えると、Cloud Functions(第 1 世代)に配信されない可能性があります。
    • リクエスト サイズが原因で配信されなかったイベントは、プラットフォーム ログに記録され、プロジェクトのログ使用量にカウントされます。
    • これらのログは、ログ エクスプローラで「サイズが第 1 世代の上限を超えているため、イベントを Cloud Functions に配信できません...」という error 重大度メッセージとともに表示されます。関数名は functionName フィールドで確認できます。receiveTimestamp フィールドが現在から 1 時間以内であれば、タイムスタンプの前後のスナップショットで問題のドキュメントを読み取ることで、実際のイベントの内容を推測できます。
    • このようなケイデンスを回避するには、次のようにします。
      • Cloud Functions(第 2 世代)への移行とアップグレードを行う
      • ドキュメントのサイズを縮小する
      • 問題の Cloud Functions を削除する
    • 除外を使用してロギング自体を無効にすることもできますが、問題のあるイベントは配信されないことに注意してください。