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

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

问题描述

这是一个已知问题,firebase没有简单的方法来计算项目。我打算创建一个应用程序,严重依赖计数和其他聚合。我担心建立这个应用程序的计数器与建议这里将是非常复杂和难以维护。

所以我想到了这种模式:

我会保留一个服务器,它将监听所有输入的项目在数据库中,此服务器将更新所有计数器和聚合。服务器将拥有只有他可以更新计数器的特殊管理员的UID。

通过这种方式,用户不需要下载整个节点来获得计数,而且也不必处理由客户端维护计数器引起的问题。



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

解决方案

Firebase最近发布了云端函数。正如文档所述:



< blockquote>

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

使用云功能,您无需创建自己的服务器。您只需编写JavaScript函数并将其上传到Firebase即可。每当发生事件时,Firebase将负责触发功能。



例如,假设您要计算某个帖子中的喜欢数。你应该有一个类似于这个的结构:

$ p $ $ b $ {
Posts:{
randomKey :{
likes_count:5,
likes:{
userX:true,
userY:true,
userZ:真,
...
}
}
}
}

你的JavaScript函数可以这样写:

$ $ $ $ $ $ $ const函数= require('firebase-功能');
const admin = require('firebase-admin');
admin.initializeApp(functions.config()。firebase);

//在一个单独的属性中跟踪喜欢子列表的长度。
exports.countlikes = functions.database.ref('/ posts / $ postid / likes')。onWrite(event => {
return event.data.ref.parent()。child(' (event.data.numChildren());
});

这段代码每次都增加 likes_count 变量在 likes 节点上有新的写入。



本示例可在 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天全站免登陆