igorbenav / fastcrud

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities.
MIT License
681 stars 57 forks source link

Expose simpler interface to override EndpointCreator methods #118

Open JakNowy opened 3 months ago

JakNowy commented 3 months ago

Let's say you want to use crud.get_joined on your router read_item method. What you end up doing is something like:

class MyItemRouter(EndpointCreator):
    def _read_items(self):
        """Creates an endpoint for reading multiple items from the database."""

        async def endpoint(
            db: SessionDep,
        ) -> list[MissionObjectives]:
            return await self.crud.get_joined(db, <joined_params...>)

        return endpoint

Implementing your own crud methods and calling them in overriden EndpointCreator methods should be common and simple approach, while currently it's quite verbose.

JakNowy commented 3 months ago

I would love to be able to do something like this:

class MyModelRouter(EndpointCreator):
    crud = MyModelCrud #self.crud cannot recognize custom methods when crud passed as param, unless __init__ overriden.

    async def _create_endpoint(
        self,
        my_endpoint_query_params,
        my_endpoint_deps,
    ):
        # my_router_logic...
        # ...
        # ...

        return await self.crud.my_crud_method(self.session)

router = MyModelRouter(
    **other_params
)

resulting in default "get" endpoint and customized "create" one.

Do you think it's going to be implemented in the future?

JakNowy commented 3 months ago

We're getting a lot of experiences with different use cases in our project. I am going to provide a PR with a FastRouter which best meets our needs and observations for implementing customized routes.