Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Filters in Automatic Endpoints #87

Merged
merged 10 commits into from
May 21, 2024
Merged

[WIP] Filters in Automatic Endpoints #87

merged 10 commits into from
May 21, 2024

Conversation

igorbenav
Copy link
Owner

@igorbenav igorbenav commented May 16, 2024

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

  • Create a class to validate passed filters
  • Validate that filters are indeed columns of the db
  • Create function to dynamically add filters to endpoints
  • Accept filter_config in both EndpointCreator and crud_router, dynamically add filters as query parameters
  • Decide whether get_multi and get_paginated only are changed by filter_config
  • Get the type of the query parameters and pass it to FastAPI
  • Tests covering changes
  • Documentation updates

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:

  • The FilterConfig specifies that 'id' should be a query parameter with no default value and 'name' should be a query parameter with a default value of 'default'.
  • When fetching multiple items, you can filter by these parameters.
  • Example GET request: /mymodel/get_multi?id=1&name=example

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:

  • for this first version, get_multi and get_paginated only are getting filters. If people request, we may rethink this
  • for now, we're also accepting a dict being passed instead of a FilterConfig

@igorbenav igorbenav added enhancement New feature or request Automatic Endpoint Related to automatic endpoint creation functionality labels May 16, 2024
@igorbenav igorbenav self-assigned this May 16, 2024
Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@igorbenav
Copy link
Owner Author

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

@igorbenav
Copy link
Owner Author

@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.

@igorbenav igorbenav merged commit 03d4a2d into main May 21, 2024
16 checks passed
@igorbenav igorbenav deleted the endpoint-filters branch May 21, 2024 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Automatic Endpoint Related to automatic endpoint creation functionality enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Automatic Filters for Auto Generated Endpoints
1 participant