是否可以在 Firestore 上添加一个层,这样更改就不会直接提交?

Is it possible to add a layer on Firestore so the changes won't be committed directly?

如我的标题所述,是否可以在 Cloud Firestore 数据库之上添加一个新层,这样用户所做的所有更改都不会直接提交到数据库中?

有 2 种模式可用于保持 'Backend as a Service' 体验,并且不需要设置诸如专用 VM 或 Kubernetes 集群之类的东西;请记住,您始终可以像使用传统文档数据库一样使用 Cloud Firestore。

间接写入队列

使用安全规则,仅对名为 'write_queue' 的集合和 none 其他集合启用新文档创建:

match /write_queue/{doc} {
  allow create: if true;
}

在此集合上,配置 Cloud Functions 以在创建时触发:

exports.myFunctionName = functions.firestore
    .document('write_queue/{writeId}').onCreate((document, context) => {
      // ... Your code here
    });

此函数然后可以从用户处获取此间接写入并执行您想要的任何处理,最后将其写入数据库中预期的实际集合。

通过云函数写入

与其让用户写入数据库,不如配置安全规则使其成为只读的。使用 HTTP Cloud Functions 作为端点,用户可以向其发出任何写入,而不是直接写入数据库。此 Cloud Function 将执行您想要的任何处理,并最终将其写入数据库中预期的实际集合。