figuring out the db structure + fetching all the data
This commit is contained in:
1
fastapi_proj/.gitignore
vendored
1
fastapi_proj/.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
kysuce-vyuka-firestore-21b77f3ea979.json
|
kysuce-vyuka-firestore-21b77f3ea979.json
|
||||||
*.json
|
*.json
|
||||||
|
__pycache__/
|
||||||
|
|||||||
@@ -1,16 +1,33 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
from google.cloud import firestore
|
from google.cloud import firestore
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
db = firestore.Client.from_service_account_json('fastapi-firestore-filipriec-824499994d80.json')
|
db = firestore.Client.from_service_account_json('fastapi-firestore-filipriec-824499994d80.json')
|
||||||
|
|
||||||
|
|
||||||
# Get all collections
|
@app.get("/products")
|
||||||
print("Collections:")
|
async def get_products():
|
||||||
for col in db.collections():
|
docs = db.collection('somarina').stream()
|
||||||
print(f" - {col.id}")
|
|
||||||
|
|
||||||
# Get first document to see fields
|
result = []
|
||||||
docs = list(col.limit(1).stream())
|
for d in docs:
|
||||||
if docs:
|
item = {
|
||||||
print(f" Fields: {list(docs[0].to_dict().keys())}")
|
"id": d.id,
|
||||||
print(f" Example: {docs[0].to_dict()}")
|
"title": d.to_dict().get("somarina_title", "Unknown"),
|
||||||
print()
|
"value": d.to_dict().get("somarina_value", 0)
|
||||||
|
}
|
||||||
|
result.append(item)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
@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