ขยายการเชื่อมต่อข้อมูลด้วยฟังก์ชันคลาวด์

Cloud Functions for Firebase ช่วยให้คุณจัดการกิจกรรมใน Firebase Data Connect ได้ Cloud Functions ช่วยให้คุณเรียกใช้โค้ดฝั่งเซิร์ฟเวอร์ได้ เพื่อตอบสนองต่อเห�����ก��ร��์��่��งๆ เช่น การดำเนินการของการเปลี่ยนแปลงในบริการ Data Connect ซึ่งจะช่วยให้คุณเพิ่มตรรกะที่กำหนดเองได้โดยไม่ต้อง ติดตั้งใช้งานเซิร์ฟเวอร์ของคุณเอง

กรณีการใช้งานทั่วไป

  • การซิงค์ข้อมูล: ทำซ้ำหรือซิงค์ข้อมูลกับระบบอื่นๆ (เช่น Cloud Firestore, BigQuery หรือ API ภายนอก) หลังจากเกิดการเปลี่ยนแปลง

  • เวิร์กโฟลว์แบบอะซิงโครนัส: เริ่มกระบวนการที่ใช้เวลานาน เช่น การประมวลผลรูปภาพหรือการรวบรวมข้อมูล หลังจากมีการเปลี่ยนแปลงฐานข้อมูล

  • การมีส่วนร่วมของผู้ใช้: ส่งอีเมลหรือCloud Messagingการแจ้งเตือนถึงผู้ใช้หลังจากเหตุการณ์การเปลี่ยนแปลงที่เฉพาะเจาะจงในแอปพลิเคชัน เช่น การสร้างบัญชี

ทริกเกอร์ฟังก์ชันในการกลายพันธุ์ Data Connect

คุณสามารถเรียกใช้ฟังก์ชันเมื่อใดก็ตามที่มีการดำเนินการData Connectการเปลี่ยนแปลง โดยใช้ตัวแฮนเดิลเหตุการณ์onMutationExecuted ทริกเกอร์นี้จะเกิดขึ้นเมื่อมีการดำเนินการการเปลี่ยนแปลง

ฟังก์ชัน Mutation Event พื้นฐาน

ตัวอย่างพื้นฐานต่อไปนี้เป็นฟังก์ชันที่บันทึกรายละเอียดของ การเปลี่ยนแปลงใดๆ ที่ดำเนินการในบริการ Data Connect ของคุณ

Node.js

import { onMutationExecuted } from "firebase-functions/dataconnect";
import { logger } from "firebase-functions";

export const logMutation = onMutationExecuted(
  {
    /* Trigger on all mutations, spanning all services and connectors
       in us-central1 */
  },
  (event) => {
    logger.info("A mutation was executed!", {
      data: event.data,
    });
  }
);

Python

from firebase_functions import dataconnect_fn, logger

@dataconnect_fn.on_mutation_executed()
def log_mutation(event: dataconnect_fn.Event):
  logger.info("A mutation was executed!", event.data)

เมื่อทริกเกอร์จากการเปลี่ยนแปลงทั้งหมดในโปรเจ็กต์ คุณต้องไม่ทำการเปลี่ยนแปลงใดๆ ในตัวแฮนเดิลทริกเกอร์ มิเช่นนั้นจะทำให้เกิดลูปที่ไม่มีที่สิ้นสุด หากต้องการ ทำการเปลี่ยนแปลงในทริกเกอร์เหตุการณ์ ให้ใช้ตัวเลือกการกรองที่อธิบายไว้ ด้านล่าง และระวังอย่าให้การเปลี่ยนแปลงทริกเกอร์ตัวเอง

ตั้งค่าตำแหน่งฟังก์ชัน

สถานที่ตั้งของฟังก์ชันต้องตรงกับสถานที่ตั้งของบริการData Connect สำหรับกิจกรรมที่จะทริกเกอร์ฟังก์ชัน โดยค่าเริ่มต้น ภูมิภาคของฟังก์ชันคือ us-central1

Node.js

import { onMutationExecuted } from "firebase-functions/dataconnect";

export const onMutationRegionOption = onMutationExecuted(
  {
    region: "europe-west1"  // Set if Data Connect service location is not us-central1
  },
  (event) => { /* ... */ }
);

Python

@dataconnect_fn.on_mutation_executed(
  region="europe-west1"  # Set if Data Connect service location is not us-central1
)
def mutation_executed_handler_region_option(event: dataconnect_fn.Event):
  pass

กรองเหตุการณ์

คุณสามารถกําหนดค่าแฮนเดิล onMutationExecuted ด้วยตัวเลือกเพื่อกรองเหตุการณ์ ตามแอตทริบิวต์ที่เฉพาะเจาะจงได้ ซึ่งจะมีประโยชน์เมื่อคุณต้องการทริกเกอร์ฟังก์ชันสำหรับ Mutation บางอย่างเท่านั้น

คุณกรองตาม service, connector และ operation ได้ดังนี้

Node.js

import { onMutationExecuted } from "firebase-functions/dataconnect";
import { logger } from "firebase-functions";

// Trigger this function only for the CreateUser mutation
// in the users connector of the myAppService service.
export const onUserCreate = onMutationExecuted(
  {
    service: "myAppService",
    connector: "users",
    operation: "CreateUser",
  },
  (event) => {
    logger.info("A new user was created!", event.data);
    // Add logic here: for example, sending a welcome email.
  }
);

Python

from firebase_functions import dataconnect_fn, logger

@dataconnect_fn.on_mutation_executed(
  service="myAppService",
  connector="users",
  operation="CreateUser"
):
def on_user_create(event: dataconnect_fn.Event):
  logger.info("A new user was created!", event.data)

ไวลด์การ์ดและกลุ่มการจับภาพ

คุณสามารถใช้ไวลด์การ์ดและกลุ่มการจับภาพเพื่อกรองทริกเกอร์ในค่าหลายค่าได้ กลุ่มที่บันทึกไว้จะพร้อมใช้งาน ใน event.params ดูข้อมูลเพิ่มเติมได้ที่ทำความเข้าใจรูปแบบเส้นทาง

ตัวอย่าง

Node.js

import { onMutationExecuted } from "firebase-functions/dataconnect";

// Trigger on all operations that match the pattern `User*`, on any service and
// connector.
export const onMutationWildcards = onMutationExecuted(
  {
    operation: "User*",
  },
  (event) => {}
);

// Trigger on all operations that match the pattern `User*`, on any service and
// connector. Capture the operation name in the variable `op`.
export const onMutationCaptureWildcards = onMutationExecuted(
  {
    operation: "{op=User*}",
  },
  (event) => {
    // `event.params.op` contains the operation name.
  }
);

// Trigger on all operations on the service `myAppService`. Capture the
// operation name in the variable `operation`.
export const onMutationCaptures = onMutationExecuted(
  {
    service: "myAppService",
    operation: "{operation}",
  },
  (event) => {
    // `event.params.operation` contains the operation name.
  }
);

Python

from firebase_functions import dataconnect_fn

# Trigger on all operations that match the pattern `User*`, on any service and
# connector.
@dataconnect_fn.on_mutation_executed(
  operation="User*"
)
def on_mutation_wildcards(event: dataconnect_fn.Event):
  pass

# Trigger on all operations that match the pattern `User*`, on any service and
# connector. Capture the operation name in the variable `op`.
@dataconnect_fn.on_mutation_executed(
  operation="{op=User*}"
)
def on_mutation_capture_wildcards(event: dataconnect_fn.Event):
  # `event.params["op"]` contains the operation name.
  pass

# Trigger on all operations on the service `myAppService`. Capture the
# operation name in the variable `operation`.
@dataconnect_fn.on_mutation_executed(
  service="myAppService",
  operation="{operation}"
)
def on_mutation_captures(event: dataconnect_fn.Event):
  # `event.params["operation"]` contains the operation name.
  pass

เข้าถึงข้อมูลการตรวจสอบสิทธิ์ผู้ใช้

คุณสามารถเข้าถึงข้อมูลการตรวจสอบสิทธิ์ผู้ใช้เกี่ยวกับผู้ใช้ที่ ทําให้เกิดเหตุการณ์ได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อมูลที่มีอยู่ในบริบทการตรวจสอบสิทธิ์ได้ที่บริบทการตรวจสอบสิทธิ์

ตัวอย่างต่อไปนี้แสดงวิธีเรียกข้อมูลการตรวจสอบสิทธิ์

Node.js

import { onMutationExecuted } from "firebase-functions/dataconnect";

export const onMutation = onMutationExecuted(
  { operation: "MyMutation" },
  (event) => {
    // mutationExecuted event provides authType and authId:
    // event.authType
    // event.authId
  }
);

Python

from firebase_functions import dataconnect_fn

@dataconnect_fn.on_mutation_executed(operation="MyMutation")
def mutation_executed_handler(event: dataconnect_fn.Event):
  # mutationExecuted event provides auth_type and auth_id, which are accessed as follows
  # event.auth_type
  # event.auth_id
  pass

ระบบจะป้อนข้อมูลประเภทการตรวจสอบสิทธิ์และรหัสการตรวจสอบสิทธิ์ดังนี้

การเปลี่ยนแปลงที่เริ่มโดย authtype authid
ผู้ใช้ปลายทางที่ได้รับการตรวจสอบสิทธิ์ app_user UID ของโทเค็นการตรวจสอบสิทธิ์ Firebase
ผู้ใช้ปลายทางที่ไม่ได้รับการตรวจสอบสิทธิ์ unauthenticated ว่าง
Admin SDK ที่แอบอ้างเป็นผู้ใช้ปลายทาง app_user UID โทเค็นการตรวจสอบสิทธิ์ Firebase ของผู้ใช้ที่ถูกแอบอ้าง
Admin SDK ปลอมแปลงคำขอที่ไม่ได้ตรวจสอบสิทธิ์ unauthenticated ว่าง
Admin SDK ที่มีสิทธิ์แบบเต็ม admin ว่าง

เข้าถึงข้อมูลเหตุการณ์

ออบเจ็กต์ CloudEvent ที่ส่งไปยังฟังก์ชันของคุณมีข้อมูลเกี่ยวกับ เหตุการณ์ที่ทริกเกอร์ฟังก์ชัน

แอตทริบิวต์เหตุการณ์

แอตทริบิวต์ ประเภท คำอธิบาย
id string ตัวระบุที่ไม่ซ้ำกันสำหรับเหตุการณ์
source string ทรัพยากรตัวเชื่อมต่อที่สร้างเหตุการณ์ (เช่น //firebasedataconnect.googleapis.com/projects/*/locations/*/services/*/connectors/*)
specversion string CloudEvents เวอร์ชันข้อกำหนด (เช่น "1.0")
type string ประเภทของเหตุการณ์: google.firebase.dataconnect.connector.v1.mutationExecuted
time string การประทับเวลา (รูปแบบ ISO 8601) ของเวลาที่สร้างกิจกรรม
subject string ไม่บังคับ ข้อมูลเพิ่มเติมเกี่ยวกับบริบทของเหตุการณ์ เช่น ชื่อการดำเนินการ
params object แผนที่รูปแบบเส้นทางที่บันทึกไว้
authType string Enum ที่แสดงถึงประเภทของหลักการที่ทริกเกอร์เหตุการณ์
authId string ตัวระบุที่ไม่ซ้ำกันของหลักการที่ทริกเกอร์เหตุการณ์
data MutationEventData เพย์โหลดของData Connectเหตุการณ์ ดูส่วนถัดไป

เพย์โหลดข้อมูล

ออบเจ็กต์ MutationEventData มีเพย์โหลดของเหตุการณ์ Data Connect ดังนี้

{
  // ...
  "authType": // ...
  "data": {
    "payload": {
      "variables": {
        "userId": "user123",
        "updateData": {
          "displayName": "New Name"
        }
      },
      "data": {
        "updateUser": {
          "id": "user123",
          "displayName": "New Name",
          "email": "user@example.com"
        }
      },
      "errors": []
    }
  }
}
  • payload.variables: ออบเจ็กต์ที่มีตัวแปรที่ส่งไปยังการเปลี่ยนแปลง
  • payload.data: ออบเจ็กต์ที่มีข้อมูลที่มิวเทชันแสดงผล
  • payload.errors: อาร์เรย์ของข้อผิดพลาดที่เกิดขึ้นระหว่างการดำเนินการของ การเปลี่ยนแปลง หากการเปลี่ยนแปลงสำเร็จ อาร์เรย์นี้จะว่างเปล่า

ตัวอย่าง

วิธีเข้��ถึงตัวแปรการเปลี่ยนแปลงและข้อมูลที่ส่งคืนมีดังนี้

Node.js

import { onMutationExecuted } from "firebase-functions/dataconnect";
import { logger } from "firebase-functions";

export const processNewUserData = onMutationExecuted(
  {
    "service": "myAppService",
    "connector": "users",
    "operation": "CreateUser",
  },
  (event) => {
    // The variables passed to the mutation
    const mutationVariables = event.data.payload.variables;

    // The data returned by the mutation
    const returnedData = event.data.payload.data;

    logger.info("Processing mutation with variables:", mutationVariables);
    logger.info("Mutation returned:", returnedData);

    // ... your custom logic here
  }
);

Python

from firebase_functions import dataconnect_fn, logger

@dataconnect_fn.on_mutation_executed(
  service="myAppService",
  connector="users",
  operation="CreateUser"
):
def process_new_user_data(event: dataconnect_fn.Event):
  # The variables passed to the mutation
  mutation_vars = event.data.payload.variables
  # The data returned by the mutation
  returned_data = event.data.payload.data

  logger.info("Processing mutation with variables:", mutationVariables)
  logger.info("Mutation returned", returnedData)

  # ... your custom logic here

โปรดทราบว่าเหตุการณ์ Data Connect ไม่ได้ให้ข้อมูลสแนปชอต "ก่อน" ของข้อมูล ซึ่งแตกต่างจากทริกเกอร์ฐานข้อมูลอื่นๆ บางรายการ เช่น Cloud Firestore หรือ Realtime Database เนื่องจาก Data Connect พร็อกซีคำขอไปยังฐานข้อมูลพื้นฐาน จึงไม่สามารถรับสแนปชอต "ก่อน" ของข้อมูลได้ในลักษณะธุรกรรม แต่คุณจะมีสิทธิ์เข้าถึงอาร์กิวเมนต์ที่ส่งไปยังการเปลี่ยนแปลง และข้อมูลที่การเปลี่ยนแปลงนั้นส่งคืน

ผลที่ตามมาอย่างหนึ่งคือคุณไม่สามารถใช้กลยุทธ์การเปรียบเทียบสแนปชอต "ก่อน" และ "หลัง" เพื่อหลีกเลี่ยงลูปที่ไม่มีที่สิ้นสุด ซึ่งเหตุการณ์ทริกเกอร์จะทริกเกอร์เหตุการณ์เดียวกัน หา����้องทำการเปลี่ยนแปลงจากฟังก์ชันที่ทริกเกอร์โดยเหตุการณ์การเปลี่ยนแปลง ให้ใช้ตัวกรองเหตุการณ์และระมัดระวังเพื่อให้แน่ใจว่าไม่มีการเปลี่ยนแปลงใดๆ ที่จะทริกเกอร์ตัวเองได้ แม้จะโดยอ้อมก็ตาม