ioxiocom / firedantic

Database models for Firestore using Pydantic base models.
BSD 3-Clause "New" or "Revised" License
43 stars 14 forks source link

Support collection group queries #35

Open gresnick opened 2 years ago

gresnick commented 2 years ago

Currently it is only possible to get a subcollection from a single document:

class UserStats(AsyncSubModel):
    id: Optional[str]
    purchases: int = 0

    class Collection(AsyncSubCollection):
        # Can use any properties of the "parent" model
        __collection_tpl__ = "users/{id}/stats"

class User(AsyncModel):
    __collection__ = "users"
    name: str

async def get_user_stats(user_id):
    user = await User.get_by_id(user_id)
    stats_model: Type[UserStats] = UserStats.model_for(user)

Ideally, the ability to query all user stats (as supported by firestore's group query):

UserStats.collection.find({"reported_at": {"<=": "datetime.datetime.utcnow()"}})

Side note: It would be awesome to not have to retrieve the user before querying its subcollection

UserStats.model_for(user)

or

UserStats.parent(user_id)