什么 Firebase 规则将防止基于其他字段的集合中的重复项? [英] What Firebase rule will prevent duplicates in a collection based on other fields?

查看:24
本文介绍了什么 Firebase 规则将防止基于其他字段的集合中的重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,它允许用户创建项目,然后允许其他用户订阅这些项目.我正在努力制定一个规则,以防止用户多次订阅一个项目.

I'm creating an application which lets users create items and then allow other users to subscribe to those items. I'm struggling to craft a rule that will prevent users from subscribing more than once to an item.

这是我的数据结构示例(匿名,因此是OMITTED"值):

Here is an example of my data structure (anonymized, hence the "OMITTED" values):

{
    "OMITTED" : {
        "name" : "Second",
        "body" : "this is another",
        "userName" : "Some User",
        "userId" : "OMITTED",
        "created" : 1385602708464,
        "subscribers" : {
            "OMITTED" : {
                "userName" : "Some User",
                "userId" : "OMITTED"
            }
        }
    }
}

这是我目前的 Firebase 规则:

Here are my Firebase rules at present:

{
  "rules": {
    ".read": true,
    ".write": "auth != null",
    "items": {
      "$item": {
        ".write": "!data.exists()",
        ".validate": "newData.hasChildren(['name', 'body', 'userId', 'userName']) && newData.child('userId').val() == auth.id",
        "subscribers": {
          "$sub": {
            ".validate": "newData.hasChildren(['userId', 'userName']) && newData.child('userId').val() != data.child('userId').val()"
          }
        }
      }
    }
  }
}

如何防止用户多次订阅?我需要什么规则来防止基于 userIdsubscribers 列表中的重复用户?

How can I prevent users from subscribing more than once? What is the rule I need to prevent duplicate users within the subscribers list based on userId?

推荐答案

由于安全规则无法迭代记录列表以找到包含某位数据的记录,这里的技巧是通过 ID 存储记录这允许轻松访问.有一篇关于反规范化的很棒的文章为这种做法提供了一些很好的见解.

Since security rules can't iterate a list of records to find the one containing a certain bit of data, the trick here is to store the records by an ID which allows for easy access. There is a great article on denormalization which offers some good insights into this practice.

在这种情况下,如果您的用例允许,您可能只想切换数据结构,以便按用户的 id 存储记录,而不是将 ID 作为值存储在记录中,如下所示:

In this case, if your use case allows, you may simply want to switch your data structure so that records are stored by the user's id, rather than storing the ID as a value in the record, like so:

/users/user_id/items/item_id/subscribers/user_id/

事实上,正如您将在非规范化中看到的那样,您甚至可以从更远的拆分中受益,具体取决于您的数据的确切大小以及您以后将如何阅读:

In fact, as you'll see in denormalization, you may even benefit from splitting things out even farther, depending on the exact size of your data and how you'll be reading it later:

/users/user_id
/items/user_id/item_id
/subscribers/item_id/user_id

在这两种格式中的任何一种中,您现在都可以通过以下方式防止重复并很好地锁定安全性:

In either of these formats, you can now prevent duplicates and lock down security rather nicely with something like this:

{
   "users": {
      "$user_id": { ".write": "auth.id === $user_id" }
   },
   "subscribers": {
      "$subscriber_id": { ".write": "auth.id === $subscriber_id" }
   }
}

这篇关于什么 Firebase 规则将防止基于其他字段的集合中的重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆