如何使用Firebase处理自动服务器端计算? [英] How do I use Firebase to handle automatic server-side calculations?

查看:102
本文介绍了如何使用Firebase处理自动服务器端计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我的问题应该重申为:我如何将这些行为重构为CRUD,这是Firebase擅长的?



我知道CRUD运行良好。我也看到Firebase声明式安全模型如何让我确保服务器端的安全性,它应该存在于哪里。



假设我有订阅服务。每次人们注册一项服务时,他们都需要自动将到期订单项添加到其帐户中。简单来说:

  / users / john 
/ services / goodstuff

所以 john 可以注册 goodstuff

对于服务器后端,我会 POST / services / goodstuff / members ,例如,让 POST 处理程序的一部分在 john 的帐户中添加一个您欠的订单项,确保没有人可以加入 goodstuff 而不被标记为欠款。在bsp应用中,那些服务器端逻辑不存在,我将如何重构应用程序获得相同的有效行为?

解决方案

2017年10月10日) 虽然下面概述的体系结构仍然有效,可用于将Firebase与任何现有基础架构,Firebase刚刚发布了 Firebase云端函数,它允许您在Google上运行JavaScript函数服务器响应Firebase事件(例如数据库更改,用户登录等等)。

一个可能的解决方案(未经测试,不好意思;但它应该是正确的想法):

  {
rules:{
users :{
$ user:{
/ *)当他们创建自己的用户记录时,他们必须在接下来的30天内写出一个'到期'
*。 * /
.write:!data.exists()& newData.child('due')。isNumber()& newData.child('due')。val() < now +(30 * 24 * 60 * 60 * 1000)
}
},
services:
$ service:{
/ *必须经过认证,截止日期不得通过。 * /
.read:auth!= null&& now< root.child('users /'+ auth.id +'/ due).val()
}





这需要当有人第一次登录并初始化他们的用户/入口,他们必须在接下来的30天内写一个截止日期。然后,当访问任何服务,该到期日期将被验证没有通过。

这就是说,另一种选择是旋转一个微小的后端服务来处理某种商业逻辑。 Firebase擅长保护,存储和同步数据。但是,如果你的业务逻辑复杂,你可能想考虑一个微小的后端进程。 Firebase具有 REST API 以及 Node.JS JVM 客户端,因此运行与Firebase集成的自有后端代码非常简单。


Perhaps my question should be restated as: how do I refactor those behaviours into CRUD, which is what Firebase excels at?

I get that CRUD works well. I also see how the Firebase declarative security model allows me to ensure proper security server-side, where it should exist.

Let's say I have a subscription service. Each time a person signs up for a service, they need to automatically have a "due" line item added to their account. In simple terms:

/users/john
/services/goodstuff

So john can sign up for goodstuff, I might let him in for 30 days without paying, but will remind him when 30 days is up, "hey, you need to pay or else you lose your subscription to goodstuff."

With a server back-end, I would POST to /services/goodstuff/members, e.g., have part of the POST handler add a "you owe" line item to john's account, ensuring that no one can join goodstuff without being marked as owing.

In a Firebase BaaS app, where those server-side logics don't exist, how would I refactor the app to get the same effective behaviour?

解决方案

Update (March 10, 2017): While the architecture I outline below is still valid and can be used to combine Firebase with any existing infrastructure, Firebase just released Cloud Functions for Firebase, which allows you to run JavaScript functions on Google's servers in response to Firebase events (such as database changes, users signing in and much more).

One potential solution (untested, sorry; but it should be the right idea):

{
  "rules": {
    "users": {
      "$user": {
        /* When they create their user record, they must write a 'due' that's
         * within the next 30 days. */
        ".write": "!data.exists() && newData.child('due').isNumber() && newData.child('due').val() < now + (30*24*60*60*1000)"
      }
    },
    "services":
      "$service": {
        /* Must be authenticated and due date must not be passed. */
        ".read": "auth != null && now < root.child('users/' + auth.id + '/due).val()" 
      }
    }
  }
}

This would require that when somebody logs in for the first time and initializes their users/ entry, they'd have to write a due date in the next 30 days. And then when accessing any service, that due date would be verified to have not passed.

That said, another option is to just spin up a tiny backend service to handle this sort of business logic. Firebase excels at protecting, storing, and synchronizing data. But if you have complicated business logic, you might want to think about spinning up a tiny backend process. Firebase has a REST api as well as Node.JS and JVM clients, so it's really easy to run your own backend code that integrates with Firebase.

这篇关于如何使用Firebase处理自动服务器端计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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