Skip to content

Commit

Permalink
fix(firestore): Add ID to all documents in Firestore provider (#94)
Browse files Browse the repository at this point in the history
Noticed a bug in the Firestore provider while taking this on a test run
- many of the methods with the Firestore provider do not add the ID to
the document and therefore fail model validation. Additionally, some
places had incorrect conversion of documents into dict (using
`dict(doc)` instead of `doc.to_dict()`). This PR resolves both of these
issues.

---------

Co-authored-by: Anirudh Murali <[email protected]>
  • Loading branch information
anihm136 and Anirudh Murali authored Nov 21, 2023
1 parent 334257c commit 1a02328
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions extension_service/datastore/providers/firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,17 @@ async def get_airport_by_id(self, id: int) -> Optional[models.Airport]:
query = self.__client.collection("airports").where(
filter=FieldFilter("id", "==", id)
)
return models.Airport.model_validate(await query.get().to_dict())
airport_doc = await query.get()
airport_dict = airport_doc.to_dict() | {"id": airport_doc.id}
return models.Airport.model_validate(airport_dict)

async def get_airport_by_iata(self, iata: str) -> Optional[models.Airport]:
query = self.__client.collection("airports").where(
filter=FieldFilter("iata", "==", iata)
)
return models.Airport.model_validate(await query.get().to_dict())
airport_doc = await query.get()
airport_dict = airport_doc.to_dict() | {"id": airport_doc.id}
return models.Airport.model_validate(airport_dict)

async def search_airports(
self,
Expand All @@ -187,14 +191,19 @@ async def search_airports(
query = query.where("name", ">=", name).where("name", "<=", name + "\uf8ff")

docs = query.stream()
airports = [models.Airport.model_validate(dict(doc)) async for doc in docs]
airports = []
async for doc in docs:
airport_dict = doc.to_dict() | {"id": doc.id}
airports.append(models.Airport.model_validate(airport_dict))
return airports

async def get_amenity(self, id: int) -> Optional[models.Amenity]:
query = self.__client.collection("amenities").where(
filter=FieldFilter("id", "==", id)
)
return models.Amenity.model_validate(await query.get().to_dict())
amenity_doc = await query.get()
amenity_dict = amenity_doc.to_dict() | {"id": amenity_doc.id}
return models.Amenity.model_validate(amenity_dict)

async def amenities_search(
self, query_embedding: list[float], similarity_threshold: float, top_k: int
Expand All @@ -203,9 +212,11 @@ async def amenities_search(

async def get_flight(self, flight_id: int) -> Optional[models.Flight]:
query = self.__client.collection("flights").where(
filter=FieldFilter("id", "==", id)
filter=FieldFilter("id", "==", flight_id)
)
return models.Flight.model_validate(await query.get().to_dict())
flight_doc = await query.get()
flight_dict = flight_doc.to_dict() | {"id": flight_doc.id}
return models.Flight.model_validate(flight_dict)

async def search_flights_by_number(
self,
Expand All @@ -218,9 +229,11 @@ async def search_flights_by_number(
.where(filter=FieldFilter("flight_number", "==", number))
)

flights = [
models.Flight.model_validate(dict(doc)) async for doc in query.stream()
]
docs = query.stream()
flights = []
async for doc in docs:
flight_dict = doc.to_dict() | {"id": doc.id}
flights.append(models.Flight.model_validate(flight_dict))
return flights

async def search_flights_by_airports(
Expand All @@ -243,7 +256,10 @@ async def search_flights_by_airports(
query = query.where("arrival_airport", "==", arrival_airport)

docs = query.stream()
flights = [models.Flight.model_validate(dict(doc)) async for doc in docs]
flights = []
async for doc in docs:
flight_dict = doc.to_dict() | {"id": doc.id}
flights.append(models.Flight.model_validate(flight_dict))
return flights

async def close(self):
Expand Down

0 comments on commit 1a02328

Please sign in to comment.