Firebase数据上限为儿童 [英] Firebase data cap on children

查看:105
本文介绍了Firebase数据上限为儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,...



<$客户1:{},
客户2:{},
客户3:{}
}

对于我的特定示例,根据客户的付款模式将决定允许使用多少空间。
对于Customer1和Customer2,我只需要最多25MB的空间。任何新的数据将删除旧数据或根本不附加。

我应该为某个客户做些什么,我检查整个数据集的sizeof并防止客户添加新的数据,如果超过一定的数额?

它似乎不是一个理想的解决方案,诚实...

解决方案

在经过一些试验和错误之后,我发现实际上可能对firebase安全规则中的数据设置上限。限制因素是,你只能检查一个原始类型的值(字符串,数字或布尔值) )。当一个节点有孩子的时候,你将不得不检查每个孩子的价值,这意味着你知道你可以期望什么数据。



下面是一个安全的例子规则需要做到这一点。规则只适用于原始类型或最多有3个(指定)原始子元素的节点。

 rules: {
.read:true,
.write:true,
$ key:{
//验证数据没有子元素
.validate:(!newData.hasChildren()&&
//并验证它是否在特定长度内的字符串
(newData.isString()& newData。 val()。length <10)||
//验证它是否是某个范围内的数字
(newData.isNumber()&(newData.val() 10& newData.val()> -10))||
//或者验证它是否为bool
newData.isBoolean())||
// Or rince并重复所有可能的子节点
newData.hasChildren(),
childA:{
.validate:(!newData.hasChildren()&&
(newData.isString()& newData.val()。length< 10)||
(newData.isNumber()&&(newData.val() 10&& newData.val()> -10))||
newData.isBoolean())

childB:{
.validate:(!newData.hasChildren()&&
(newData.isString()& newData.val()。length< 10)||
(newData.isNumber()& amp;(newData.val()< 10& amp ; newData.val()> -10))||
newData.isBoolean())

childC:{
.validate: (newData.hasNild()&&
(newData.isString()& newData.val()。length< 10)||
(newData.isNumber()& &(newData.val()< 10& newData.val()> -10))||
newData.isBoolean())

/ /关闭你不想要的所有子节点
$ other:{.validate:false}
}
}
pre>

这种溶剂的一些缺点是:


  1. 知道你可以预期的数据(每次改变时更新你的规则)

  2. 规则将得到ex显着地扩大你可以拥有的更多的子节点。
  3. 你不能在一个节点上有一个总计上限,而只能在组成一个节点的基元上。例如(childA + childB + childC)较小,那么X是不可能的。
  4. 您不能拥有动态上限。但是,您可以说locationFree的上限为10,而locationPremium的上限为100.但不幸的是,这会使规则的数量增加一倍,用户将根据上限设置不同的写入位置。
  5. ol>

    Is there any way to set a data cap on Firebase children?

    E.g.,...

    {
        Customer1: {},
        Customer2: {},
        Customer3: {}
    }
    

    For my particular example, depending on a customer's payment model will dictate how much space they are allowed to use. Say for Customer1 and Customer2, I only want at most 25MB of space used. Any new data will either remove older data or not get appended at all.

    Should I do something where for a certain Customer, I check the sizeof of the the WHOLE data set and prevent customers from adding in new data if it exceeds a certain amount?

    It doesn't seem like an ideal solution to be honest...

    解决方案

    After some trial and error i found out it is actually possible to have a cap on data within the firebase security rules. But only to a certain extend!

    The limiting factor is that you can only check the value of a primitive type (string, number or bool). When a node has children you would have to check the value of each of that children, this implies you know what data you can expect.

    Below is an example of the security rules needed to accomplish this. The rules are only for a primitive type or a node with a maximum of 3 (specified) primitive children.

    "rules": {
      ".read": true,
      ".write": true,
      "$key":{
        //validate that the data has no children
        ".validate":"(!newData.hasChildren() && 
        //and validate if it is a string within a certain length
        (newData.isString() && newData.val().length < 10) || 
        //or validate if it is a number within a certain range
        (newData.isNumber() && (newData.val() < 10 && newData.val() > -10)) || 
        //or validate if it is a bool
        newData.isBoolean()) || 
        //Or rince and repeat for all possible child nodes
        newData.hasChildren()",
        "childA":{
          ".validate":"(!newData.hasChildren() && 
          (newData.isString() && newData.val().length < 10) || 
          (newData.isNumber() && (newData.val() < 10 && newData.val() > -10)) || 
          newData.isBoolean())"
        },
        "childB":{
          ".validate":"(!newData.hasChildren() && 
          (newData.isString() && newData.val().length < 10) || 
          (newData.isNumber() && (newData.val() < 10 && newData.val() > -10)) || 
          newData.isBoolean())"
        },
        "childC":{
          ".validate":"(!newData.hasChildren() && 
          (newData.isString() && newData.val().length < 10) || 
          (newData.isNumber() && (newData.val() < 10 && newData.val() > -10)) || 
          newData.isBoolean())"
        },
        //dismiss all child nodes you don't want
        "$other": {".validate": false}
      }
    }
    

    Some of the downsides about this sollution are:

    1. You have to know what data you can expect (and update your rules each time this changes)
    2. The rules will get exponentially larger the more child nodes you can have
    3. You can't really have a "total" cap on a node but only on the primitives that make up a node. For example (childA + childB + childC) is smaller then X is not possible.
    4. You can't have a dynamic cap. But you could say locationFree has a cap of 10 and locationPremium has a cap of 100. But unfortunatly this would double the amount of rules needed and users will have different write locations depending on their cap.

    这篇关于Firebase数据上限为儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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