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

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

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

การเปลี่ยนแปลงที่เริ่มโดย ประเภทการตรวจสอบสิทธิ์ การรับรองความถูกต้อง
ผู้ใช้ปลายทางที่ได้รับการตรวจสอบสิทธิ์ app_user UID ของโทเค็นการตรวจสอบสิทธิ์ Firebase
ผู้ใช้ปลายทางที่ไม่ได้รับการตรวจสอบสิทธิ์ unauthenticated ว่าง
SDK ผู้ดูแลระบบปลอมตัวเป็นผู้ใช้ปลายทาง app_user UID โทเค็นการรับรองความถูกต้องของ Firebase ของผู้ใช้ที่ถูกแอบอ้าง
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

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

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