Spaces:
Sleeping
Sleeping
Update data_fetcher.py
Browse files- data_fetcher.py +7 -4
data_fetcher.py
CHANGED
|
@@ -1,16 +1,19 @@
|
|
| 1 |
# 🔍 data_fetcher.py
|
| 2 |
from firestore_client import get_firestore_client
|
|
|
|
| 3 |
|
| 4 |
db = get_firestore_client()
|
| 5 |
|
| 6 |
# Utility function to convert Gemini-style filters into Firestore queries
|
| 7 |
-
def apply_filters(query_ref, filters):
|
| 8 |
for f in filters:
|
| 9 |
-
|
|
|
|
| 10 |
op = f.get("op", "==")
|
| 11 |
value = f.get("value")
|
| 12 |
-
if field and value is not None:
|
| 13 |
query_ref = query_ref.where(field, op, value)
|
|
|
|
| 14 |
return query_ref
|
| 15 |
|
| 16 |
def fetch_data_from_firestore(instruction):
|
|
@@ -47,7 +50,7 @@ def fetch_data_from_firestore(instruction):
|
|
| 47 |
results[name] = []
|
| 48 |
else:
|
| 49 |
col_ref = db.collection(name)
|
| 50 |
-
col_ref = apply_filters(col_ref, filters)
|
| 51 |
docs = col_ref.limit(limit).stream()
|
| 52 |
results[name] = [doc.to_dict() for doc in docs]
|
| 53 |
except Exception as e:
|
|
|
|
| 1 |
# 🔍 data_fetcher.py
|
| 2 |
from firestore_client import get_firestore_client
|
| 3 |
+
from schema_utils import has_field, resolve_field
|
| 4 |
|
| 5 |
db = get_firestore_client()
|
| 6 |
|
| 7 |
# Utility function to convert Gemini-style filters into Firestore queries
|
| 8 |
+
def apply_filters(query_ref, collection_name, filters):
|
| 9 |
for f in filters:
|
| 10 |
+
raw_field = f.get("field")
|
| 11 |
+
field = resolve_field(collection_name, raw_field) if raw_field else None
|
| 12 |
op = f.get("op", "==")
|
| 13 |
value = f.get("value")
|
| 14 |
+
if field and value is not None and has_field(collection_name, field):
|
| 15 |
query_ref = query_ref.where(field, op, value)
|
| 16 |
+
# else: silently ignore invalid field for this collection
|
| 17 |
return query_ref
|
| 18 |
|
| 19 |
def fetch_data_from_firestore(instruction):
|
|
|
|
| 50 |
results[name] = []
|
| 51 |
else:
|
| 52 |
col_ref = db.collection(name)
|
| 53 |
+
col_ref = apply_filters(col_ref, name, filters)
|
| 54 |
docs = col_ref.limit(limit).stream()
|
| 55 |
results[name] = [doc.to_dict() for doc in docs]
|
| 56 |
except Exception as e:
|