Jakub Pastuszuk
Jakub Pastuszuk

Reputation: 1038

Python firebase database shallow get of data

As we know, Firebase Realtime Database 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.

Right now the database I need to get into have following structure:

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

each category has thousands of subcategories in in and each subcategory have thousands of records.

Is there any way to iterate through Realtime Database using python and official firebase-admin python SDK and get only shallow copies (aka keys) of database to then fetch certain rows?

I would like to avoid telling DB architect to remodel DB to denormalize record data to another node

right now I've only found way to fetch keys with all corresponding data in them using official 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())

Upvotes: 1

Views: 1968

Answers (1)

Hiranya Jayathilaka
Hiranya Jayathilaka

Reputation: 7438

Python Admin SDK does support shallow reads on references.

print(ref.get(shallow=True))

See the documentation: https://firebase.google.com/docs/reference/admin/python/firebase_admin.db#reference

Upvotes: 5

Related Questions