简体   繁体   English

基于一组已知键向 Firebase 数据库发出批量请求的最有效方法是什么?

[英]What is the most efficient way to make a batch request to a Firebase DB based on an array of known keys?

I need a solution that makes a Firebase DB API call for multiple items based on keys and returns the data ( children ) of those keys ( in one response ).我需要一种解决方案,该解决方案可以根据键对多个项目进行 Firebase DB API 调用,并返回这些键的数据(子项)(在一个响应中)。

Since I don't need data to come real-time, some sort of standard REST call made once ( rather than a Firebase DB listener ), I think it would be ideal.由于我不需要实时数据,某种标准的 REST 调用一次(而不是 Firebase DB 侦听器),我认为这将是理想的。

The app wouldn't have yet another listener and WebSocket connection open.该应用程序不会再打开另一个侦听器和 WebSocket 连接。 However, I've looked through Firebase's API docs and it doesn't look like there is a way to do this.但是,我查看了 Firebase 的 API 文档,但似乎没有办法做到这一点。

Most of the answers I've seen always suggest making a composite key/index of some sort and filter accordingly using the composite key, but that only works for searching through a range.我见过的大多数答案总是建议创建某种类型的复合键/索引并使用复合键进行相应的过滤,但这仅适用于搜索范围。 Or they suggest just nesting the data and not worrying about redundancy and disk space ( and it's quicker ), instead of retrieving associated data through foreign keys.或者他们建议只嵌套数据而不用担心冗余和磁盘空间(而且速度更快),而不是通过外键检索关联数据。

However, the problem is I am using Geofire and its query method only returns the keys of the items, not the items' data.但是,问题是我使用的是 Geofire,它的查询方法只返回项目的键,而不是项目的数据。 All the docs and previous answers would suggest retrieving data either by the real-time SDK, which I've tried by using the once method or making a REST call for all items and filter with the orderBy , startAt , endAt params and filtering locally by the keys I need.所有文档和以前的答案都建议通过实时 SDK 检索数据,我已经尝试使用once方法或对所有项目进行 REST 调用并使用orderBystartAtendAt参数进行过滤并通过本地过滤我需要的钥匙。

This could work, but the potential overhead of retrieving a bunch of items I don't need only to filter them out locally seems wasteful.这可以工作,但检索一堆我不需要的项目的潜在开销只是在本地过滤掉它们似乎很浪费。 The approach using the once listener seems wasteful too because it's a server roundtrip for each item key.使用once侦听器的方法似乎也很浪费,因为它是每个项目键的服务器往返。 This approach is kind of explained in this pretty good post , but according to this explanation it's still making a roundtrip for each item ( even if it's asynchronously and through the same connection ).这种方法在这篇相当不错的帖子中得到了解释,但根据这个解释,它仍然为每个项目进行往返(即使它是异步的并通过相同的连接)。

This poor soul asked a similar question , but didn't get many helpful replies ( that really address the costs of making n number of server requests ).这个可怜的灵魂问了一个类似的问题,但没有得到很多有用的答复(这确实解决了发出n个服务器请求的成本)。

Could someone, once and for all explain the approaches on how this could be done and the pros/cons?有人可以一劳永逸地解释如何做到这一点的方法以及利弊吗? Thanks.谢谢。

I need a solution that makes a Firebase DB API call for multiple items based on keys and returns the data (children) of those keys (in one response).我需要一个解决方案,该解决方案可以根据键对多个项目进行 Firebase DB API 调用,并返回这些键的数据(子项)(在一个响应中)。

One solution might be to set up a separate server to make ALL the calls you need to your Firebase servers, aggregate them, and send it back as one response.一种解决方案可能是设置一个单独的服务器来向 Firebase 服务器发出您需要的所有调用,将它们聚合起来,然后将其作为一个响应发回。

There exists tools that do this.有工具可以做到这一点。

One of the more popular ones recently spec'd by the Facebook team is GraphQL. Facebook 团队最近指定的一种更流行的方法是 GraphQL。 https://graphql.org/ https://graphql.org/

Behind the scenes, you set up your graphql server to map your queries which would all make separate API calls to fetch the data you need to fit the query.在幕后,你设置你的 graphql 服务器来映射你的查询,这些查询都会进行单独的 API 调用来获取你需要的数据以适应查询。 Once all the API calls have been completed, graphql will then send it back as a response in the form of a JSON object.一旦所有 API 调用完成,graphql 就会以 JSON 对象的形式将其作为响应发回。

Looks like you are looking for Cloud Functions .看起来您正在寻找Cloud Functions You can create a function called from http request and do every database read inside of it.您可以创建一个从 http 请求调用的函数并在其中读取每个数据库。

These function are executed in the cloud and their results are sent back to the caller.这些函数在云端执行,它们的结果被发送回调用者。 HTTP call is one way to trigger a Cloud Function but you can setup other methods (schedule, from the app with Firebase SDK, database trigger...). HTTP 调用是触发 Cloud Functions 函数的一种方式,但您可以设置其他方法(调度、来自具有 Firebase SDK 的应用程序、数据库触发器...)。 The data are not charged until they leave the server (so only in your request response or if you request a database of another region).数据在离开服务器之前不会收费(因此仅在您的请求响应中或如果您请求另一个区域的数据库)。 Cloud Function billing is based on CPU used, number of invocations and running intances, more details on the quota section . Cloud Function 计费基于使用的 CPU、调用次数和运行实例,配额部分的更多详细信息。

You will get something like :你会得到类似的东西:

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

exports.getAllNodes = functions.https.onRequest((req, res) => {
  
  let children = [ ... ]; // get your node list from req
  let promises = [];

  for (const i in children) {
    promises.push(database.ref(children[i]).once('value'));
  }

  Promise.all(promises)
    .then(result => {
      res.status(200).send(result);
    })
    .catch(error => {
      res.status(503).send(error);
    });
});

That you will have to deploy with the firebase CLI.您必须使用 firebase CLI 进行部署。

This is how you can do a one time call to a document in javascript , hope it helps这就是您如何在javascript 中一次性调用文档,希望它有所帮助

 // Get a reference to the database service
 let database = firebase.database();
 // one time call to a document
 database.ref("users").child("demo").get().then((snapshot) => {
     console.log("value of users->demo-> is", snapshot.node_.value_)
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Lodash或Vanilla JS,基于对象键筛选数组的最有效方法是什么? - Using Lodash or vanilla JS what is the most efficient way to filter an array based on object keys? 根据Javascript中的其他信息填充数组的最有效方法是什么? - What is the most efficient way to populate an array based on other information in Javascript? 在 Javascript 中反转数组的最有效方法是什么? - What is the most efficient way to reverse an array in Javascript? 根据另一个数组的内容过滤数组的最有效方法是什么? - What's the most efficient way of filtering an array based on the contents of another array? 根据对象数组中的值对 DOM 元素进行排序的最有效方法是什么? - What is the most efficient way to sort DOM elements based on values in an array of objects? 根据 JavaScript 中的单词过滤复杂 html 字符串数组的最有效方法是什么? - What is the most efficient way to filter an array of complex html strings based on words in JavaScript? 根据 javascript 中的属性名称数组,创建具有属性子集的 object 的最有效方法是什么? - What is the most efficient way of creating an object with a subset of properties, based on an array of property names in javascript? 根据条件拆分数百万个数据数组的最有效方法是什么? - what's the most efficient way to split an array of millions of data based on condition? 将数据存储到Firebase中的多个引用的最有效方法是什么? - What's the most efficient way to store data to multiple refs in firebase? 基于其他数组限制检查数组的最有效方法 - Most efficient way to check array based on other array limitation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM