Cloud Firestore 觸發條件


有了 Cloud Functions,您可以在 Cloud Firestore 中處理事件,而不必更新用戶端程式碼。您可以透過文件快照介面或 Admin SDK 變更 Cloud Firestore。

在一般生命週期中,Cloud Firestore 函式會執行下列操作:

  1. 等待對特定文件的變更。
  2. 在事件發生時觸發並執行它的工作。
  3. 接收包含指定文件中所儲存資料快照的資料物件。針對寫入或更新事件,資料物件包含兩個快照,代表觸發事件前後的資料狀態。

Firestore 執行個體位置和函式位置之間的距離可能會導致網路延遲時間大幅延遲。如要最佳化效能,請考慮在適用情況下指定函式位置

Cloud Firestore 函式觸發條件

Cloud Functions for Firebase SDK 會匯出 functions.firestore 物件,讓您建立與特定 Cloud Firestore 事件相連結的處理常式。

事件類型 觸發條件
onCreate 在第一次寫入文件時觸發。
onUpdate 在文件已經存在且已變更任何值時觸發。
onDelete 在刪除具有資料的文件時觸發。
onWrite 在觸發 onCreateonUpdateonDelete 時觸發。

如果您尚未針對 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} 取代文件 ID:

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。您可以定義任意數量的萬用字元,取代明確的集合或文件 ID,例如:

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 ...
    });

對文件的所有變更觸發函式

如果您不在乎要觸發的事件類型,可以使用含有萬用字元onWrite() 函式監聽 Cloud Firestore 文件中的所有變更。如果使用者建立、更新或刪除,這個函式範例會呼叫 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 資料庫中的特定文件相關聯。您可以透過傳回函式的快照 ref 屬性,以DocumentReference存取該文件。

這個 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 Functions 的 Cloud Firestore 觸發條件有下列限制:

  • 我們不保證排序。快速變更可能會以非預期的順序觸發函式叫用。
  • 事件至少會傳送一次,但單一事件可能會導致多個函式叫用。請避免完全依賴一次性機制,並寫入冪等函式
  • Cloud Firestore (Datastore 模式) 需要 Cloud Functions (第 2 代)。Cloud Functions (第 1 代) 不支援 Datastore 模式。
  • Cloud Functions (第 1 代) 僅適用於「(預設)」資料庫,且不支援 Cloud Firestore 已命名資料庫。請使用 Cloud Functions (第 2 代) 為已命名的資料庫設定事件。
  • 觸發條件與單一資料庫相關聯。您無法建立與多個資料庫相符的觸發條件。
  • 刪除資料庫不會自動刪除該資料庫的任何觸發條件。觸發條件會停止傳送事件,但在您刪除觸發條件前會持續存在。
  • 如果相符的事件超過要求大小上限,該事件可能無法傳送至 Cloud Functions (第 1 代)。
    • 因要求大小而未傳送的事件會記錄在平台記錄中,並計入專案的記錄檔用量。
    • 您可以在記錄檔探索工具中找到這些記錄,並顯示以下訊息:「由於大小超過第 1 代 (第 1 代) 的限制,因此事件無法傳送至 Cloud Functions」(第 1 代嚴重性為 error)。您可以在 functionName 欄位下方找到函式名稱。如果 receiveTimestamp 欄位目前仍在一小時後,則可透過讀取時間戳記前後的快照,讀取相關的文件,藉此推斷實際的事件內容。
    • 如要避免這類頻率,你可以採取下列做法:
      • 遷移並升級至 Cloud Functions (第 2 代)
      • 縮小文件
      • 刪除相關的 Cloud 函式
    • 您可以使用排除來關閉記錄本身,但請注意,系統不會傳送違規事件。