Firebase规则将根据其他字段防止重复收藏中的哪些内容? [英] What Firebase rule will prevent duplicates in a collection based on other fields?

查看:128
本文介绍了Firebase规则将根据其他字段防止重复收藏中的哪些内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是我的数据结构的一个例子(匿名,因此 OMITTED值):

  {
OMITTED:{
name:Second ,
body:this is another,
userName:Some User,
userId:OMITTED,
created:1385602708464,
订阅者:{
OMITTED:{
userName:某用户,
userId:OMITTED
}
}
}
}

以下是我目前使用的Firebase规则: / b>

  {
rules:{
.read:true,
。写:auth!= null,
items:{
$ item:{
.write:!data.exists(),
.validate:newData.hasChildren(['name','body','userId','userNa我'])&& newData.child('userId').val()== auth.id,
subscribers:{
$ sub:{
.validate:newData。 hasChildren(['userId','userName'])&& val()用户名'userId')。val()!= data.child('userId')。val()
}
}
}
} $ b $如何防止用户订阅不止一次?如何防止用户订阅不止一次?什么是规则我需要基于 userId


来防止在订阅者由于安全规则不能遍历记录列表以找到包含特定位数据的记录,因此这里的技巧是通过一个ID来存储记录允许轻松访问。有一篇关于非规范化<在这种情况下,如果您的用例允许,您可能只想切换您的数据结构,以便记录存储在用户的ID中,而不是将该ID存储为记录中的值,就像这样:

  / users / user_id / items / item_id /订阅者/ user_id / 

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

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



在这两种格式中,您现在可以防止重复并锁定安全性,而不会像这样:

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


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.

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"
            }
        }
    }
}

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()"
          }
        }
      }
    }
  }
}

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?

解决方案

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.

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天全站免登陆