Python firebase 数据库浅获取数据

Python firebase database shallow get of data

据我们所知,Firebase 实时数据库 is one big JSON tree

Because the Firebase Realtime Database allows nesting data up to 32 levels deep, you might be tempted to think that this should be the default structure. However, when you fetch data at a location in your database, you also retrieve all of its child nodes.

现在我需要进入的数据库具有以下结构:

root
|
|- category 1
|  |
|  |
|  |-sub category 1
|  |  |
|  |  |- record 1
|  |  |  |- (...)
|  |  |
|  |  |- record 2
|  |  |   |- (...)
|  |  |
|  |  |- (...) record n
|  |
|  |
|  |- (...) sub category n
|
| - (...) category n

每个类别都有数千个子类别,每个子类别都有数千条记录。

有没有什么方法可以使用 python 和官方 firebase-admin python SDK 遍历实时数据库并仅获取数据库的浅表副本(又名键)然后获取某些行?

我想避免告诉 DB 架构师重构 DB 以将记录数据非规范化到另一个节点

现在我只找到了使用官方 SDK 获取密钥及其中所有相应数据的方法:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Fetch the service account key JSON file contents
cred = credentials.Certificate('firebase-adminsdk.json')
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://<app_name>.firebaseio.com/'
})

ref = db.reference('category 1')
print(ref.get())

Python Admin SDK 支持对引用的浅读取。

print(ref.get(shallow=True))

查看文档:https://firebase.google.com/docs/reference/admin/python/firebase_admin.db#reference