Transaction.getall vs Promise.All在前端

发布于 2025-01-24 02:53:24 字数 539 浏览 0 评论 0 原文

在nodejs中,使用 firebase-admin 库,当您运行交易时,您可以使用称为 getall 的函数来读取多个文档,例如:

runTransaction((t) => {
  const docs = await t.getAll(someDocRef, anotherDocRef);
});

尽管如此,在前端中,您使用JavaScript SDK(或在我的情况下像 @angular/fire一样包装它的库),并且使用 getall ,但是 get get (用于一个文件)。

我的问题是,如果我这样做,我是否会获得 getall 的相同功能:

runTransaction((t) => {
  const docs = await Promise.all([
    t.get(someDocRef),
    t.get(anotherDocRef),
  ])
});

In NodeJS, with the firebase-admin library, when you run a transaction, you can use a function called getAll to read multiple documents, like this:

runTransaction((t) => {
  const docs = await t.getAll(someDocRef, anotherDocRef);
});

Nonetheless, in the frontend you use the Javascript SDK (or a library that wraps it, like @angular/fire in my case), and with it you don't have getAll but get (for one document).

My question is, will I get the same functionality of getAll if I do:

runTransaction((t) => {
  const docs = await Promise.all([
    t.get(someDocRef),
    t.get(anotherDocRef),
  ])
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2025-01-31 02:53:24

交易如何处理并发

  1. 使用客户端SDK时,事务将跟踪其中使用的所有文档。如果这些文档中的任何一个是由外部操作编辑的,则交易将从划痕中重试,以确保使用最新数据。

  2. 另一方面,服务器/管理员SDK将继续并在使用的文档上放置一个悲观的锁。试图更改交易中使用的文档的外部操作将失败,或者在抬起数据库锁之前才能完成。

除了交易中的并发控制外,如果您的问题是关于 在服务器上, Promise.all() get()在客户端上操作的数组完成相似的结果,然后在代码中,两个都将返回单个Promise 可以解决 documentnapshot 的数组,每个文档都被获取。

在这两种情况下,您的结果文档常数只需在承诺解决后即可包含文档数组。我在两个SDK中对此进行了测试。

// Iterating over documents regardless of which SDK is used in the transaction
myDocs.forEach( myDoc => {
    console.log(myDoc.data());
});

//Output in both SDKs
{ lastName: 'Doe', firstName: 'Jane' }
{ lastName: 'Smith', firstName: 'John' }
{ firstName: 'Foo', lastName: 'Bar' }

There is an important difference in how transactions handle concurrency between the client and admin SDKs:

  1. When using the client SDK, a transaction will keep track of all the documents that are used within it. If any of these documents is edited by an external operation, the transaction will retry from scratch ensuring the latest data is used.

  2. On the other hand, the server/admin SDK will go ahead and place a pessimistic lock on the documents that are used. External operations that try to change the documents used within the transaction will fail, or will not complete until the database lock is lifted.

Besides concurrency control in transactions, if your question is about whether getAll() on the server and a Promise.all() array of get() operations on the client accomplish similar results, then in the code both will return a single promise that resolves to an array of DocumentSnapshot with each document being fetched.

In both cases, your resulting docs constant will simply contain the array of documents after the Promise resolves. I tested this in both SDKs.

// Iterating over documents regardless of which SDK is used in the transaction
myDocs.forEach( myDoc => {
    console.log(myDoc.data());
});

//Output in both SDKs
{ lastName: 'Doe', firstName: 'Jane' }
{ lastName: 'Smith', firstName: 'John' }
{ firstName: 'Foo', lastName: 'Bar' }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文