用于维护计数器和聚合的 Firebase 控制服务器 [英] Firebase control server for maintaining counters and aggregates

查看:22
本文介绍了用于维护计数器和聚合的 Firebase 控制服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 已知问题 firebase 没有简单的方法来计算项目.我计划创建一个严重依赖计数和其他聚合的应用程序.我担心按照此处建议的规则创建此应用程序的计数器将非常复杂且难以维护.

所以我想到了这个模式:

我将保留一个服务器来侦听输入到数据库中的所有项目,该服务器将更新所有计数器和聚合.服务器将持有一个特殊管理员的 UID,只有他才能更新计数器.

这样,用户就不必下载整个节点来获取计数,而且我也不必处理由客户端维护计数器引起的问题.

这种模式有意义吗?我错过了什么吗?

解决方案

Firebase 最近发布了 Cloud Functions.如文档所述:

<块引用>

Cloud Functions 是一个托管的、私有的、可扩展的 Node.js 环境您可以在其中运行 JavaScript 代码.

使用 Cloud Functions,您无需创建自己的服务器.您可以简单地编写 JavaScript 函数并将其上传到 Firebase.Firebase 将负责在事件发生时触发函数.

例如,假设您想计算帖子中的点赞数.你应该有一个类似于这个的结构:

<代码>{帖子":{随机密钥":{"likes_count":5,喜欢":{用户X":真,用户Y":真,userZ":真,...}}}}

你的 JavaScript 函数会写成这样:

const functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp(functions.config().firebase);//在单独的属性中跟踪喜欢"子列表的长度.export.countlikes = functions.database.ref('/posts/$postid/likes').onWrite(event => {返回 event.data.ref.parent().child('likes_count').set(event.data.numChildren());});

每次在 likes 节点上有新的写入时,此代码都会增加 likes_count 变量.

此示例可在 GitHub 上找到.>

It's a known issue that firebase doesn't have easy way to count items. I'm planning to create an app that relies heavily on counts and other aggregates. I fear creating this app's counters with the rules as suggested here will be incredibly complex and hard to maintain.

So I thought about this pattern:

I will keep a server that will listen to all items entered in the database and this server will update all counters and aggregates. The server will hold the UID of a special admin that only he can update counters.

This way, users will not have to download entire nodes in order to get a count, plus I won't have to deal with issues that arise from maintaining counters by clients.

Does this pattern make sense? Am I missing something?

解决方案

Firebase has recently released Cloud Functions. As mentioned on the documentation:

Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code.

With Cloud Functions, you don't need to create your own Server. You can simply write JavaScript functions and upload it to Firebase. Firebase will be responsible for triggering functions whenever an event occurs.

For example, let's say you want to count the number of likes in a post. You should have a structure similar to this one:

{
  "Posts" : {
    "randomKey" : {
      "likes_count":5,
      "likes" : {
      "userX" : true,
      "userY" : true,
      "userZ" : true,
      ...
    }
    }
  }
}

And your JavaScript function would be written like this:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// Keeps track of the length of the 'likes' child list in a separate attribute.
exports.countlikes = functions.database.ref('/posts/$postid/likes').onWrite(event => {
  return event.data.ref.parent().child('likes_count').set(event.data.numChildren());
});

This code increases the likes_count variable every time there is a new write on the likes node.

This sample is available on GitHub.

这篇关于用于维护计数器和聚合的 Firebase 控制服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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