figuring out the db structure + fetching all the data
This commit is contained in:
@@ -1,16 +1,33 @@
|
||||
from fastapi import FastAPI
|
||||
from google.cloud import firestore
|
||||
|
||||
app = FastAPI()
|
||||
db = firestore.Client.from_service_account_json('fastapi-firestore-filipriec-824499994d80.json')
|
||||
|
||||
|
||||
# Get all collections
|
||||
print("Collections:")
|
||||
for col in db.collections():
|
||||
print(f" - {col.id}")
|
||||
@app.get("/products")
|
||||
async def get_products():
|
||||
docs = db.collection('somarina').stream()
|
||||
|
||||
result = []
|
||||
for d in docs:
|
||||
item = {
|
||||
"id": d.id,
|
||||
"title": d.to_dict().get("somarina_title", "Unknown"),
|
||||
"value": d.to_dict().get("somarina_value", 0)
|
||||
}
|
||||
result.append(item)
|
||||
|
||||
return result
|
||||
|
||||
# Get first document to see fields
|
||||
docs = list(col.limit(1).stream())
|
||||
if docs:
|
||||
print(f" Fields: {list(docs[0].to_dict().keys())}")
|
||||
print(f" Example: {docs[0].to_dict()}")
|
||||
print()
|
||||
@app.get("/structure")
|
||||
async def get_structure():
|
||||
result = []
|
||||
for col in db.collections():
|
||||
for doc in col.stream():
|
||||
result.append({
|
||||
"collection": col.id,
|
||||
"id": doc.id,
|
||||
"data": doc.to_dict()
|
||||
})
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user