0.3.0 Summary
Added
- Ability to create
CustomEndpointCreatorfor more advanced and customized route creation. - Feature to include or exclude specific CRUD endpoints in the
crud_routerusingincluded_methodsanddeleted_methodsparameters. - Tests for all the changes.
- Comprehensive documentation on how to use
CustomEndpointCreatorand selectively include or exclude endpoints.
CustomEndpointCreator
- Feature: Introduces the ability to extend the
EndpointCreatorclass. This allows for the creation of custom routes and the addition of complex logic to the API endpoints. - Documentation: Detailed examples and guidelines added on how to implement and use
CustomEndpointCreatorin your projects.
Selective CRUD Operations
- Feature:
crud_routerfunction now supportsincluded_methodsanddeleted_methodsparameters. This allows developers to specify which CRUD methods should be included or excluded when setting up the router. - Use Cases:
included_methods: Define specific CRUD operations to be included.deleted_methods: Exclude certain CRUD operations from being created.
- Documentation: Updated the documentation to include examples and best practices for using these new parameters.
Detailed Changes
Extending EndpointCreator
You can create a subclass of EndpointCreator and override or add new methods to define custom routes. Here's an example:
Creating a Custom EndpointCreator
from fastcrud import EndpointCreator
# Define the custom EndpointCreator
class MyCustomEndpointCreator(EndpointCreator):
# Add custom routes or override existing methods
def _custom_route(self):
async def custom_endpoint():
# Custom endpoint logic
return {"message": "Custom route"}
return custom_endpoint
# override add_routes_to_router to also add the custom routes
def add_routes_to_router(self, ...):
# First, add standard CRUD routes if you want them
super().add_routes_to_router(...)
# Now, add custom routes
self.router.add_api_route(
path="/custom",
endpoint=self._custom_route(),
methods=["GET"],
tags=self.tags,
# Other parameters as needed
)Adding custom routes
from fastcrud import EndpointCreator
# Define the custom EndpointCreator
class MyCustomEndpointCreator(EndpointCreator):
# Add custom routes or override existing methods
def _custom_route(self):
async def custom_endpoint():
# Custom endpoint logic
return {"message": "Custom route"}
return custom_endpoint
# override add_routes_to_router to also add the custom routes
def add_routes_to_router(self, ...):
# First, add standard CRUD routes if you want them
super().add_routes_to_router(...)
# Now, add custom routes
self.router.add_api_route(
path="/custom",
endpoint=self._custom_route(),
methods=["GET"],
tags=self.tags,
# Other parameters as needed
)Overriding add_routes_to_router
from fastcrud import EndpointCreator
# Define the custom EndpointCreator
class MyCustomEndpointCreator(EndpointCreator):
# Add custom routes or override existing methods
def _custom_route(self):
async def custom_endpoint():
# Custom endpoint logic
return {"message": "Custom route"}
return custom_endpoint
# override add_routes_to_router to also add the custom routes
def add_routes_to_router(self, ...):
# First, add standard CRUD routes if you want them
super().add_routes_to_router(...)
# Now, add custom routes
self.router.add_api_route(
path="/custom",
endpoint=self._custom_route(),
methods=["GET"],
tags=self.tags,
# Other parameters as needed
)Using the Custom EndpointCreator
# Assuming MyCustomEndpointCreator was created
...
# Use the custom EndpointCreator with crud_router
my_router = crud_router(
session=async_session,
model=MyModel,
crud=FastCRUD(MyModel),
create_schema=CreateMyModel,
update_schema=UpdateMyModel,
endpoint_creator=MyCustomEndpointCreator,
path="/mymodel",
tags=["MyModel"],
included_methods=["create", "read", "update"] # Including selective methods
)
app.include_router(my_router)Selective CRUD Operations
You can control which CRUD operations are exposed by using included_methods and deleted_methods. These parameters allow you to specify exactly which CRUD methods should be included or excluded when setting up the router.
Using included_methods
Using included_methods you may define exactly the methods you want to be included.
# Using crud_router with selective CRUD methods
my_router = crud_router(
session=async_session,
model=MyModel,
crud=FastCRUD(MyModel),
create_schema=CreateMyModel,
update_schema=UpdateMyModel,
path="/mymodel",
tags=["MyModel"],
included_methods=["create", "read", "update"] # Only these methods will be included
)
app.include_router(my_router)Using deleted_methods
Using deleted_methods you define the methods that will not be included.
# Using crud_router with selective CRUD methods
my_router = crud_router(
session=async_session,
model=MyModel,
crud=FastCRUD(MyModel),
create_schema=CreateMyModel,
update_schema=UpdateMyModel,
path="/mymodel",
tags=["MyModel"],
deleted_methods=["update", "delete"] # All but these methods will be included
)
app.include_router(my_router)Warning
If included_methods and deleted_methods are both provided, a ValueError will be raised.
Relevant Changes in Documentation
- Advanced Use of EndpointCreator: A new section in the documentation explaining how to extend
EndpointCreatorfor advanced route customization. - Selective CRUD Operations: Documentation on how to use
included_methodsanddeleted_methodsfor more control over the API endpoints. - Examples and Code Snippets: Added practical examples and code snippets to help developers quickly implement these new features.
- Added Installation
- Added Quick-Start
- Turned community stuff into a
Communitypage
What's Changed
- Custom endpoints support by @igorbenav in #8
Full Changelog: v0.2.1...v0.3.0