ด้วย Cloud Functions คุณสามารถติดตั้งใช้งานโค้ด Node.js เพื่อจัดการเหตุการณ์ที่ทริกเกอร์ โดยการเปลี่ยนแปลงในฐานข้อมูล Cloud Firestore ได้ ซึ่งช่วยให้คุณเพิ่มฟังก์ชันการทำงานฝั่งเซิร์ฟเวอร์ลงในแอปได้อย่างง่ายดายโดยไม่ต้องเรียกใช้เซิร์ฟเวอร์ของคุณเอง
ดูตัวอย่างกรณีการใช้งานได้ที่ ฉันทำอะไรได้บ้างด้วย Cloud Functions? หรือที่เก็บ GitHub ของ ตัวอย่างฟังก์ชัน
ทริกเกอร์ฟังก์ชัน Cloud Firestore
SDK ของ Cloud Functions for Firebase จะส่งออกออบเจ็กต์ functions.firestore
ที่ช่วยให้คุณสร้างแฮนเดิลเลอร์ที่เชื่อมโยงกับเหตุการณ์ Cloud Firestore ที่เฉพาะเจาะจงได้
| ประเภทเหตุการณ์ | ทริกเกอร์ |
|---|---|
onCreate |
ทริกเกอร์เมื่อมีการเขียนเอกสารเป็นครั้งแรก |
onUpdate |
ทริกเกอร์เมื่อเอกสารมีอยู่แล้วและมีการเปลี่ยนแปลงค่า |
onDelete |
ทริกเกอร์เมื่อมีการลบเอกสารที่มีข้อมูล |
onWrite |
ทริกเกอร์เมื่อมีการทริกเกอร์ onCreate, onUpdate หรือ onDelete |
หากยังไม่ได้เปิดใช้โปรเจ็กต์สำหรับ Cloud Functions for Firebase โปรดอ่าน เริ่มต้นใช้งาน: เขียนและติดตั้งใช้งานฟังก์ชันแรก เพื่อกำหนดค่าและตั้งค่าโปรเจ็กต์ Cloud Functions for Firebase
การเขียนฟังก์ชันที่ทริกเกอร์โดย Cloud Firestore
กำหนดทริกเกอร์ฟังก์ชัน
หากต้องการกำหนดทริกเกอร์ Cloud Firestore ให้ระบุเส้นทางเอกสารและประเภทเหตุการณ์
Node.js
const functions = require('firebase-functions');
exports.myFunction = functions.firestore
.document('my-collection/{docId}')
.onWrite((change, context) => { /* ... */ });
เส้นทางเอกสารสามารถอ้างอิงได้ทั้ง เอกสารที่เฉพาะเจาะจง หรือ รูปแบบไวลด์การ์ด
ระบุเอกสารเดียว
หากต้องการทริกเกอร์เหตุการณ์สำหรับการเปลี่ยนแปลง ใดๆ ในเอกสารที่เฉพาะเจาะจง คุณสามารถใช้ฟังก์ชันต่อไปนี้
Node.js
// Listen for any change on document `marie` in collection `users` exports.myFunctionName = functions.firestore .document('users/marie').onWrite((change, context) => { // ... Your code here });
ระบุกลุ่มเอกสารโดยใช้ไวลด์การ์ด
หากต้องการแนบทริกเกอร์กับกลุ่มเอกสาร เช่น เอกสารใดๆ ในคอลเล็กชันหนึ่ง ให้ใช้ {wildcard} แทนรหัสเอกสาร
Node.js
// Listen for changes in all documents in the 'users' collection exports.useWildcard = functions.firestore .document('users/{userId}') .onWrite((change, context) => { // If we set `/users/marie` to {name: "Marie"} then // context.params.userId == "marie" // ... and ... // change.after.data() == {name: "Marie"} });
ในตัวอย่างนี้ เมื่อมีการเปลี่ยนแปลงฟิลด์ใดๆ ในเอกสารใดๆ ใน users ระบบจะจับคู่กับไวลด์การ์ดที่เรียกว่า userId
หากเอกสารใน users มีคอลเล็กชันย่อย และมีการเปลี่ยนแปลงฟิลด์ในเอกสารของคอลเล็กชันย่อยรายการใดรายการหนึ่ง ระบบจะ ไม่ ทริกเกอร์ไวลด์การ์ด userId
ระบบจะแยกการจับคู่ไวลด์การ์ดออกจากเส้นทางเอกสารและจัดเก็บไว้ใน context.params
คุณกำหนดไวลด์การ์ดได้มากเท่าที่ต้องการเพื่อแทนที่รหัสคอลเล็กชันหรือเอกสารที่ชัดเจน เช่น
Node.js
// Listen for changes in all documents in the 'users' collection and all subcollections exports.useMultipleWildcards = functions.firestore .document('users/{userId}/{messageCollectionId}/{messageId}') .onWrite((change, context) => { // If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then // context.params.userId == "marie"; // context.params.messageCollectionId == "incoming_messages"; // context.params.messageId == "134"; // ... and ... // change.after.data() == {body: "Hello"} });
ทริกเกอร์เหตุการณ์
ทริกเกอร์ฟังก์ชันเมื่อมีการสร้างเอกสารใหม่
คุณสามารถทริกเกอร์ฟังก์ชันให้เริ่มทำงานทุกครั้งที่สร้างเอกสารใหม่ในคอลเล็กชัน
ได้โดยใช้แฮนเดิลเลอร์ onCreate() ที่มี ไวลด์การ์ด
ฟังก์ชันตัวอย่างนี้จะเรียก createUser ทุกครั้งที่มีการเพิ่มโปรไฟล์ผู้ใช้ใหม่
Node.js
exports.createUser = functions.firestore .document('users/{userId}') .onCreate((snap, context) => { // Get an object representing the document // e.g. {'name': 'Marie', 'age': 66} const newValue = snap.data(); // access a particular field as you would any JS property const name = newValue.name; // perform desired operations ... });
ทริกเกอร์ฟังก์ชันเมื่อมีการอัปเดตเอกสาร
นอกจากนี้ คุณยังทริกเกอร์ฟังก์ชันให้เริ่มทำงานเมื่อมีการอัปเดตเอกสารได้โดยใช้ฟังก์ชัน
onUpdate() ที่มี ไวลด์การ์ด ฟังก์ชันตัวอย่างนี้จะเรียก updateUser หากผู้ใช้เปลี่ยนโปรไฟล์
Node.js
exports.updateUser = functions.firestore .document('users/{userId}') .onUpdate((change, context) => { // Get an object representing the document // e.g. {'name': 'Marie', 'age': 66} const newValue = change.after.data(); // ...or the previous value before this update const previousValue = change.before.data(); // access a particular field as you would any JS property const name = newValue.name; // perform desired operations ... });
ทริกเกอร์ฟังก์ชันเมื่อมีการลบเอกสาร
นอกจากนี้ คุณยังทริกเกอร์ฟังก์ชันเมื่อมีการลบเอกสารได้โดยใช้ฟังก์ชัน
onDelete() ที่มี ไวลด์การ์ด ฟังก์ชันตัวอย่างนี้จะเรียก deleteUser เมื่อผู้ใช้ลบโปรไฟล์ผู้ใช้
Node.js
exports.deleteUser = functions.firestore .document('users/{userID}') .onDelete((snap, context) => { // Get an object representing the document prior to deletion // e.g. {'name': 'Marie', 'age': 66} const deletedValue = snap.data(); // perform desired operations ... });
ทริกเกอร์ฟังก์ชันสำหรับการเปลี่ยนแปลงทั้งหมดในเอกสาร
หากไม่สนใจประเภทเหตุการณ์ที่เริ่มทำงาน คุณสามารถฟังการเปลี่ยนแปลงทั้งหมด
ในเอกสาร Cloud Firestore ได้โดยใช้ฟังก์ชัน onWrite()
ที่มี ไวลด์การ์ด ฟังก์ชันตัวอย่างนี้จะเรียก modifyUser หากมีการสร้าง อัปเดต หรือลบผู้ใช้
Node.js
exports.modifyUser = functions.firestore .document('users/{userID}') .onWrite((change, context) => { // Get an object with the current document value. // If the document does not exist, it has been deleted. const document = change.after.exists ? change.after.data() : null; // Get an object with the previous document value (for update or delete) const oldDocument = change.before.data(); // perform desired operations ... });
การอ่านและการเขียนข้อมูล
เมื่อมีการทริกเกอร์ฟังก์ชัน ฟังก์ชันจะให้สแนปชอตของข้อมูลที่เกี่ยวข้องกับเหตุการณ์ คุณสามารถใช้สแนปชอตนี้เพื่ออ่านหรือเขียนเอกสารที่ทริกเกอร์เหตุการณ์ หรือใช้ Firebase Admin SDK เพื่อเข้าถึงส่วนอื่นๆ ของฐานข้อมูล
ข้อมูลเหตุการณ์
การอ่านข้อมูล
เมื่อมีการทริกเกอร์ฟังก์ชัน คุณอาจต้องการรับข้อมูลจากเอกสารที่อัปเดต หรือรับข้อมูลก่อนการอัปเดต คุณรับข้อมูลก่อนหน้าได้โดยใช้ change.before.data() ซึ่งมีสแนปชอตเอกสารก่อนการอัปเดต
ในทำนองเดียวกัน change.after.data() จะมีสถานะสแนปชอตเอกสารหลังการอัปเดต
Node.js
exports.updateUser2 = functions.firestore .document('users/{userId}') .onUpdate((change, context) => { // Get an object representing the current document const newValue = change.after.data(); // ...or the previous value before this update const previousValue = change.before.data(); });
คุณเข้าถึงพร็อพเพอร์ตี้ได้เช่นเดียวกับในออบเจ็กต์อื่นๆ หรือจะใช้ฟังก์ชัน get เพื่อเข้าถึงฟิลด์ที่เฉพาะเจาะจงก็ได้
Node.js
// Fetch data using standard accessors const age = snap.data().age; const name = snap.data()['name']; // Fetch data using built in accessor const experience = snap.get('experience');
การเขียนข้อมูล
การเรียกใช้ฟังก์ชันแต่ละครั้งจะเชื่อมโยงกับเอกสารที่เฉพาะเจาะจงในฐานข้อมูล
Cloud Firestore คุณเข้าถึงเอกสารดังกล่าวเป็น DocumentReference ได้ในพร็อพเพอร์ตี้ ref ของสแนปชอตที่ส่งคืนไปยังฟังก์ชัน
DocumentReference นี้มาจาก
Cloud Firestore Node.js SDK
และมีเมธอดต่างๆ เช่น update(), set() และ remove() เพื่อให้คุณแก้ไขเอกสารที่ทริกเกอร์ฟังก์ชันได้อย่างง่ายดาย
Node.js
// Listen for updates to any `user` document. exports.countNameChanges = functions.firestore .document('users/{userId}') .onUpdate((change, context) => { // Retrieve the current and previous value const data = change.after.data(); const previousData = change.before.data(); // We'll only update if the name has changed. // This is crucial to prevent infinite loops. if (data.name == previousData.name) { return null; } // Retrieve the current count of name changes let count = data.name_change_count; if (!count) { count = 0; } // Then return a promise of a set operation to update the count return change.after.ref.set({ name_change_count: count + 1 }, {merge: true}); });
ข้อมูลนอกเหตุการณ์ทริกเกอร์
Cloud Functions จะทำงานในสภาพแวดล้อมที่เชื่อถือได้ ซึ่งหมายความว่าฟังก์ชันได้รับอนุญาตให้เป็นบัญชีบริการในโปรเจ็กต์ของคุณ คุณสามารถอ่านและเขียนได้ โดยใช้ Firebase Admin SDK:
Node.js
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.writeToFirestore = functions.firestore
.document('some/doc')
.onWrite((change, context) => {
db.doc('some/otherdoc').set({ ... });
});
ข้อจำกัด
โปรดทราบข้อจำกัดต่อไปนี้สำหรับ Cloud Firestore ทริกเกอร์สำหรับ Cloud Functions
- Cloud Functions (รุ่นที่ 1) ข้อกำหนดเบื้องต้นคือต้องมีฐานข้อมูล "(default)" ในโหมด Firestore แบบเนทีฟ โดยไม่รองรับฐานข้อมูลที่มีชื่อของ Cloud Firestore หรือโหมด Datastore โปรดใช้ Cloud Functions (รุ่นที่ 2) เพื่อกำหนดค่าเหตุการณ์ในกรณีดังกล่าว
- การตั้งค่าข้ามโปรเจ็กต์ด้วย Cloud Functions และทริกเกอร์ Cloud Firestore เป็นข้อจำกัด หากต้องการตั้งค่า Cloud Firestore ทริกเกอร์ Cloud Functions ต้องอยู่ในโปรเจ็กต์เดียวกัน
- ไม่มีการรับประกันลำดับ การเปลี่ยนแปลงอย่างรวดเร็วอาจทริกเกอร์การเรียกใช้ฟังก์ชันในลำดับที่ไม่คาดคิด
- ระบบจะส่งเหตุการณ์อย่างน้อย 1 ครั้ง แต่เหตุการณ์เดียวอาจส่งผลให้มีการเรียกใช้ฟังก์ชันหลายครั้ง หลีกเลี่ยงการพึ่ง กลไกการทำงานแบบส่งครั้งเดียวพอดี และเขียน ฟังก์ชันที่ทำงานซ้ำได้
- Cloud Firestore ในโหมด Datastore ต้องใช้ Cloud Functions (รุ่นที่ 2) Cloud Functions (รุ่นที่ 1) ไม่ รองรับโหมด Datastore
- ทริกเกอร์จะเชื่อมโยงกับฐานข้อมูลเดียว คุณสร้างทริกเกอร์ที่ตรงกับฐานข้อมูลหลายรายการไม่ได้
- การลบฐานข้อมูลจะไม่ลบทริกเกอร์สำหรับฐานข้อมูลนั้นโดยอัตโนมัติ ทริกเกอร์จะหยุดส่งเหตุการณ์แต่จะยังคงอยู่จนกว่าคุณจะลบทริกเกอร์
- หากเหตุการณ์ที่ตรงกันมีขนาดเกิน ขนาดคำขอสูงสุด ระบบอาจไม่ส่ง
เหตุการณ์ไปยัง Cloud Functions (รุ่นที่ 1)
- ระบบจะบันทึกเหตุการณ์ที่ไม่ได้ส่งเนื่องจากขนาดคำขอในบันทึกของแพลตฟอร์ม และนับรวมกับการใช้งานบันทึกของโปรเจ็กต์
- คุณดูบันทึกเหล่านี้ได้ใน Logs Explorer โดยมีข้อความ "Event cannot deliver to
Cloud function due to size exceeding the limit for รุ่นที่ 1..." ที่มีความรุนแรงระดับ
errorคุณดูชื่อฟังก์ชันได้ในฟิลด์functionNameหาก ฟิลด์receiveTimestampยังอยู่ในช่วง 1 ชั่วโมงนับจากนี้ คุณจะอนุมาน เนื้อหาเหตุการณ์จริงได้โดยการอ่านเอกสารที่เป็นปัญหาด้วย สแนปชอตก่อนและหลังการประทับเวลา - หากต้องการหลีกเลี่ยงการเกิดเหตุการณ์ดังกล่าว คุณสามารถทำดังนี้
- ย้ายข้อมูลและอัปเกรดเป็น Cloud Functions (รุ่นที่ 2)
- ลดขนาดเอกสาร
- ลบ Cloud Functions ที่เป็นปัญหา
- คุณปิดการบันทึกได้โดยใช้ การยกเว้น แต่โปรดทราบว่าระบบจะไม่ส่งเหตุการณ์ที่เป็นปัญหา