Firestore数据库:如何删除引用最初已删除文档的文档?

huangapple go评论60阅读模式
英文:

Firestore Database: How to delete documents that reference an initially deleted document?

问题

我正在使用Google Firestore数据库为我正在开发的应用程序。数据库布局如下:

用户

---电子邮件

---姓名

---等等...

帖子

---[用户ID]

---图像

---等等...

当我删除一个用户时,我希望所有引用该用户ID的帖子也被删除。我正在使用Google Firestore数据库和Flutterflow,并且用户将从我正在构建的应用程序中删除他们的帐户。

我尝试过使用“删除用户数据”扩展,但我无法正确配置它以使其工作。也许它不适用于这种情况?

英文:

I am using google Firestore Database for an app i am working on. The database layout is as follows:

User

---email

---name

---etc...

Post

---[User ID]

---image

---etc...

When i delete a user, i want all posts that reference the users ID to also be deleted. I am using Google Firestore Database alongside Flutterflow, and the users will be deleting their account from the app that i am building.

I have tried to use the Delete User Data extension but i could not configure it correctly to work. Perhaps it is not made for this scenario?

答案1

得分: 1

The extension deletes documents associated with a specific user ID when the user is deleted from Firebase Authentication. In your case, you want to delete documents from a collection when a master document is deleted in another collection.

一种可能的解决方案是使用后台触发的 Cloud Function,在从 User Firestore 集合中删除文档时触发,然后删除 Post 集合中的所有相应文档。

在你的情况下,可以参考以下代码示例:

exports.deleteUsers = functions
    .firestore
    .document('User/{docId}')
    .onDelete(async (snap, context) => {

        try {

            const db = admin.firestore();
            let batch = db.batch();

            const querySnapshot = await db.collection("Post").where("ArtistReference", "==", context.params.docId).get();

            querySnapshot.forEach((doc) => {
                batch.delete(doc.ref);
            });

            return batch.commit();

        } catch (error) {
            //...
        }

    });

请注意,批量写入最多可以包含 500 个操作。如果有超过 500 个与用户对应的 "Post" 文档的情况,你应该使用 Promise.all()

英文:

> I have tried to use the Delete User Data extension but i could not
> configure it correctly to work. Perhaps it is not made for this
> scenario?

New answer

The extension deletes docs associated to a specific user ID when the user is deleted from Firebase Authentication. In your case you want to delete documents from a collection when a (master) document is deleted in another collection.

One possibility in your case would be to use a background triggered Cloud Function that is triggered when a document is deleted from the User Firestore collection and that deletes all the corresponding docs in the Post collection.

Something along the following lines:

exports.deleteUsers = functions
    .firestore
    .document('User/{docId}')
    .onDelete(async (snap, context) => {

        try {

            const db = admin.firestore();
            let batch = db.batch();

            const querySnapshot = await db.collection("Post").where("ArtistReference", "==", context.params.docId).get();

            querySnapshot.forEach((doc) => {
                batch.delete(doc.ref);
            });

            return batch.commit();

        } catch (error) {
            //...
        }

    });

Note that a batched write can only contain up to 500 operations. If you have cases with more than 500 "Post" docs corresponding to a user, you should use Promise.all().


Erroneous answer

(The extension deletes docs associated to a specific user ID when the user is deleted from Firebase Authentication and not when another Firestore doc is deleted)

As explained in the extension documentation, in your case (a document field is used to associate the user UID with the document) you need to enable the auto discovery and configure the extension as follows:

> Auto discovery search fields
>
> If auto discovery is enabled, specify what document fields are used to associate the UID with the document. The extension will delete
> documents where the value for one or more of these fields matches the
> deleting user’s UID.

huangapple
  • 本文由 发表于 2023年5月10日 19:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定