在Firebase实时数据库中实现老化
不幸的是,Firebase没有开箱即用的老化机制(自动删除旧条目).因此,我正在尝试实现一个.但是,我陷入了两个决定之间:
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 decisions:
- 如果客户获取旧条目,他将自动删除: 认为这对我而言是最简单的,但看起来并不十分 确保我的安全.
- 在服务器上执行一个小的脚本/程序,该脚本/程序将 检查完整的数据库并删除旧条目.
- If a client fetch an old entry, he will delete automatically: I think it is the easiest for my context, but it doesn't look very secure to me.
- Implement a small script/program on a server that will check the full database and delete old entries.
我喜欢第一个解决方案,因为它不包括部署另一个服务器端应用程序,但是我不知道它是否可能带来风险.
I like the first solution, since it doesn't include deploying another server side application but I don't know if it could represent a risk.
您认为最好的是什么? 如何确保第一个解决方案避免所有项目的任何可能删除? 对于第二种解决方案,我不会从计算机上运行脚本,也不想为部署该脚本而另外付费,是否有可能将其部署在Firebase上?
What do you think is the best ? How can I secure the first solution to avoid any possible deletion of all items ? For the second solution, I don't won't to run the script from my computer and I don't want to pay for another sever to deploy the script, it it possible to to deploy it on Firebase ?
以前已经介绍过从客户端删除过时的项目.参见:
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)",
}
}
}
}
现在可以通过 Firebase的云函数在Firebase的服务器上运行自己的代码. .还有一个示例,该示例显示了如何删除较旧的数据具有云功能.
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.