Impelementing aging in a Firebase real time database

后端 未结 1 1111
执念已碎
执念已碎 2021-02-11 11:30

Unfortunately, Firebase doesn\'t have out of the box an aging mechanism (delete old entries automatically). So, I am trying to implement one. However, I am stuck between two dec

1条回答
  •  灰色年华
    2021-02-11 12:03

    Deleting outdated items from the client has been covered before. See:

    • Firebase chat - removing old messages
    • How to delete firebase data after "n" days
    • Delete firebase data older than 2 hours

    To secure this operation so that only outdated items can be removed, you can use Firebase Database security rules. Something like:

    {
      "rules": {
        "messages": {
          "$message": {
            // only messages older than an hours can be remove
            ".write": "newData.exists() || data.child('timestamp').val() < (now - 3600000)",
          }
        }
      }
    }
    

    Running your own code on Firebase's servers can now be done with Cloud Functions for Firebase. There is also a sample that shows how to delete older data with Cloud Functions.

    0 讨论(0)
提交回复
热议问题