starlite-saqlalchemy¶
An API application pattern standing on the shoulders of:
- Starlite: "...a light, opinionated and flexible ASGI API framework built on top of pydantic".
- SQLAlchemy 2.0: "The Python SQL Toolkit and Object Relational Mapper".
- SAQ: "...a simple and performant job queueing framework built on top of asyncio and redis".
- Structlog: "...makes logging in Python faster, less painful, and more powerful".
Usage Example¶
"""The minimal Starlite/starlite-saqlalchemy application."""
from starlite import Starlite, get
from starlite_saqlalchemy import ConfigureApp
@get("/example")
def example_handler() -> dict:
"""Hello, world!"""
return {"hello": "world"}
app = Starlite(route_handlers=[example_handler], on_app_init=[ConfigureApp()])
Check out the Usage section to see all the features this configures on the application!
Pattern¶
sequenceDiagram
Client ->> Controller: Inbound request data
Controller ->> Service: Invoke service with data validated by DTO
Service ->> Repository: View or modify the collection
Repository ->> Service: Detached SQLAlchemy instance(s)
Service ->> Queue: Optionally enqueue an async callback
Service ->> Controller: Outbound data
Controller ->> Client: Serialize via DTO
Queue ->> Worker: Worker invoked
Worker ->> Service: Makes async callback
- Request data is deserialized and validated by Starlite before it is received by controller.
- Controller invokes relevant service object method and waits for response.
- Service method handles business logic of the request and optionally triggers an asynchronous callback.
- Service method returns to controller and response is made to client.
- Async worker makes callback to service object where any async tasks can be performed. Depending on architecture, this may not be the same instance of the application that handled the request.
Motivation¶
A modern, production-ready API has a lot of components. Starlite, the backbone of this library, exposes a plethora of features and functionality that requires some amount of boilerplate and configuration that must be carried from one implementation of an application to the next.
starlite-saqlalchemy
is an example of how Starlite's on_app_init
hook can be utilized to build
application configuration libraries that support streamlining the application development process.
However, more than just an example, this library intends to be an opinionated resource to support the efficient, and consistent rollout of production ready APIs built on top of Starlite.
Use this library if the stack and design decisions suit your taste. If there are improvements or generalizations that could be made to the library to support your use case, we'd love to hear about them. Open an issue or start a discussion.
Backward compatibility and releases¶
This project follows semantic versioning, and we use semantic releases in our toolchain. This means that bug fixes and new features will find there way into a release as soon they hit the main branch, and if we break something, we'll bump the major version number. However, until we hit v1.0, there will be breaking changes between minor versions, but v1.0 is close!