在Firebase中设置最大子级数

问题描述:

对此还有其他一些(较旧的)问题,但是由于它们已经存在数年了,所以我很想知道是否对此有所更新.我想制作一个彩票动画,用户可以在其中购买彩票.一切进展顺利,但我想确保所售票不超过一千张.它必须在服务器端,这是我的规则:

There are some other (older) questions about this, but since they already are a few years old, I am curious if there have been an update about this. I want to animate a lottery, in which users can buy tickets. Everything is going well, but I want to make sure there are never more than a thousand sold tickets. It has to be server side, and these are my rules:

  "Lottery": {
    ".read": "auth != null",
    ".write": "auth != null",
      "counter": {
        ".validate": "newData.isNumber() && newData.val() > 0 && newData.val() <= 1000 && ((!data.exists() && newData.val() === 1) || (newData.val() === data.val()+1))"
      },
        "tickets":{
          "root.child('Lottery/counter/').val() === someValueYouNeedItToEqual"
        }
  }

我不知道在someValueYouNeedItToEqual处写什么.我对这个系统的运作感到担忧.我的目标是将用户的UID写入服务器,如果该值(我可以在客户端搜索可用的点,该值可以是0到1000之间的Int值)是免费的,则可以被接受.当所有地点都被采集(一个节点中有1000个子节点)时,应拒绝此操作.我希望有人可以帮助我弄清楚所需的验证规则.谢谢.

I do not know what to write at someValueYouNeedItToEqual. I am concerned about the working of this system. My goal is to write the user's UID to the server, and gets accepted if the value (I can search client side for available spots, the value can be a Int between 0 and 1000) is free. It should be rejected when all spots are taken (1000 children in a node). I hope someone can help me out figuring out the needed validation rules. Thank you.

SO上已经有此指南:

There is this guidance already here on SO: Limit number of records that can be written to a path (reference other paths in security rules)

或者您可以使用 Firebase的云功能来实现

Or you could use Cloud Functions for Firebase to implement a database trigger that both:

  1. 随着孩子的来去,增加/减少孩子的数量(为了安全起见,在交易中).
  2. 检查子项计数以确保新子项可用于添加,如果没有,则将其删除(或其他一些子项).