igorbenav / fastcrud

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

[WIP] Filters in Automatic Endpoints #87

Closed igorbenav closed 1 month ago

igorbenav commented 1 month ago

This is a work in progress, but it will close #15. This PR adds optional filters to automatic endpoints.

The usage idea after it is merged is something like:

Passing Filters as a Dict to crud_router

from fastapi import FastAPI
from fastcrud import crud_router, FilterConfig
from myapp.models import MyModel
from myapp.schemas import CreateMyModel, UpdateMyModel
from myapp.database import async_session

app = FastAPI()

router = crud_router(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModel,
    update_schema=UpdateMyModel,
    filter_config=FilterConfig(filters={"id": None, "name": "default"})
)
# Adds CRUD routes with filtering capabilities
app.include_router(router, prefix="/mymodel")

Explanation:

Passing Filters as Keyword Arguments to EndpointCreator

It's also possible to use FilterConfig with keyword arguments or with EndpointCreator instead of crud_router:

from fastapi import FastAPI
from fastcrud import EndpointCreator, FilterConfig
from myapp.models import MyModel
from myapp.schemas import CreateMyModel, UpdateMyModel
from myapp.database import async_session

app = FastAPI()

# Using FilterConfig with keyword arguments
endpoint_creator_kw = EndpointCreator(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModel,
    update_schema=UpdateMyModel,
    filter_config=FilterConfig(id=None, name="default")
)
# Adds CRUD routes with filtering capabilities
endpoint_creator_kw.add_routes_to_router()
# Include the internal router into the FastAPI app
app.include_router(endpoint_creator_kw.router, prefix="/mymodel")

Of course one could also pass as keyword arguments to crud_router or as a dict to EndpointCreator.

Notes:

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (adf9ab0) to head (7bbe7db).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #87 +/- ## ========================================== Coverage 100.00% 100.00% ========================================== Files 66 68 +2 Lines 4544 4819 +275 ========================================== + Hits 4544 4819 +275 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

igorbenav commented 1 month ago

Btw @AndreGuerra123 @samuel-favarin-dbc you guys might be interested in taking a look at this

igorbenav commented 1 month ago

@JakNowy not for this iteration, but for the next one we should get supported_filters list somewhere easy to access outside FastCRUD so we can also use advanced filters in the automatic endpoints.

PaleNeutron commented 2 weeks ago

The filter is wonderful! But can it be applied on joined models?

For example, filter user by user.company.name == 'A' ? Or filter company by user's name, company.users.any(User.name == 'B')