【问题标题】:How to obtain DB Keys before push in Firebase Admin Python SDK如何在推送 Firebase Admin Python SDK 之前获取数据库密钥
【发布时间】:2018-05-10 09:20:04
【问题描述】:

我在 AWS Lambda 函数中使用 Firebase Admin Python SDK。 我想在一次更新中将多个对象推入数据库。

 for mess in arrayMessages:

    ...

    newMessageKey = root.child('.../messages').push().key

    messages_updates[newMessageKey] = {
            'author': 'Bob',
            'dateTime': d,
            'text': mess,
    }

    messagesKeys.append(newMessageKey)

    ...

root.child(''.../messages').set(messages_updates)

“...push().key”方法立即在数据库上创建密钥(然后直接在一个命令中移动是有意义的,但会失去更新的效率)。 在不推送的情况下进行更新,插入增量整数键(平凡序列 0,1,2 ...)

Android 客户端 SDK 类似(设计用于离线获取密钥),是否有解决方案可以在数据库上创建对象之前获取密钥?

【问题讨论】:

    标签: python firebase firebase-realtime-database aws-lambda firebase-admin


    【解决方案1】:

    截至 9 月2021是:https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push

    引用@JW_ 的评论。

    旧答案:

    无法使用管理 SDK。 但是有a python port of the JS code that is used to generate the IDs client-side

    来自 firebase 团队的 Michael Lehenbauer 描述了实现 in a blog article。在那里您还可以找到link to a GitHub gist of the implementation,其中有一条评论,其中包含指向上述 python 实现的链接。

    【讨论】:

    【解决方案2】:

    以下内容会有帮助吗?

    new_message = root.child('.../messages').push({
        'author': 'Bob',
        'dateTime': d,
        'text': mess,
    })
    messagesKeys.append(new_message.key)
    

    基于实时数据库的 REST API 的数据库客户端(如 Python Admin SDK),不支持在客户端生成 ID。所以这是你得到的最佳选择。 FWIW,像上面所做的那样进行 2 个 API 调用也不错。 SDK 将在底层重用相同的 HTTP 连接,并尽量降低延迟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2019-05-09
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多