【问题标题】:Is there any way to setup firestore triggers stemming from multiple firestore collections to trigger only one cloud (javascript) function?有什么方法可以设置源自多个 firestore 集合的 firestore 触发器以仅触发一个云(javascript)功能?
【发布时间】:2021-08-26 17:26:54
【问题描述】:

因此,通常,在基本层面上,您经常会看到 Firestore 收集触发的函数编写如下:

exports.myFunction = functions.firestore
  .document('my-collection/{docId}')
  .onWrite((change, context) => { /* ... */ });

话虽如此,我正在运行我的部署有点不同,虽然结构相同。

  • 我有一个exports.main = (content, event),其中:
    • event 持有 EVENT 信息:
    • eventId(例如:66e9e123-d3fg-1234-123a-1234f110b71b-0)
    • eventType(例如:providers/cloud.firestore/eventTypes/document.create)
    • 不支持(例如:{})
    • 参数(例如:{eventId: 1CB89AD0-5AS0-1233-SD​​70-E3B7DS52D23D})
    • 资源(例如:projects/its-the-project-id/databases/(默认)/documents/TheCollection/1CB89AD0-5AS0-1233-SD​​70-E3B7DS52D23D
    • 时间戳(例如:2021-05-19T20:00:24.131151Z)
  • 内容就是数据

我通过我定义的 Terraform 部署功能:

  • 事件触发器:
    • event_type = providers/cloud.firestore/eventTypes/document.create
    • 资源 = ${TheCollection}/{eventId}

如您所见,虽然我这样做的方式肯定需要更多配置,但它非常相似,只是以更支持 GCP 的方式进行配置(如果您愿意的话)

因此,在这种情况下:

  1. 是否有任何可能的方法来拥有多个集合/文档触发器,仅触发 1 个云功能?
    • 假设我有集合 x 和集合 y,我希望它们都触发相同的云功能
  2. 如果有办法,请随时以通常编写的 Firebase-able 方式编写它,我可以尝试将其转换为可在 Firestore 中部署

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions terraform terraform-provider-gcp


    【解决方案1】:

    是的,您可以使用多个通配符来做到这一点:

    exports.myFunction = functions.firestore
      .document('{collectionId}/{docId}')
      .onWrite((change, context) => {
        console.log(context.params.collectionId);
        console.log(context.params.docId);
    });
    

    更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 2020-12-06
      相关资源
      最近更新 更多