BlackSheep-SQLAlchemy
Extension for BlackSheep that simplifies the use of SQLAlchemy in the web framework.
pip install blacksheep-sqlalchemy
Important: this library only supports rodi dependency injection
container. However, the implementation can be used for reference to configure
other DI containers to work with SQLAlchemy.
How to use
from blacksheep.server import Application
from blacksheepsqlalchemy import use_sqlalchemy
app = Application()
use_sqlalchemy(app, connection_string="<CONNECTION_STRING>")
After registering SQLAlchemy, services are configured in the application, so they are automatically resolved in any request handler requiring a SQLAlchemy db connection or db session; for example:
@get("/api/countries")
async def get_countries(db_connection) -> List[CountryData]:
"""
Fetches the countries using a database connection.
"""
result = []
async with db_connection:
items = await db_connection.execute(text("SELECT * FROM country"))
for item in items.fetchall():
result.append(CountryData(item["id"], item["name"]))
return result
Services can be injected at any level of the resolution graph, so BlackSheep
and rodi support out of the box the scenario of db connections or db sessions
referenced in the business logic but not directly by the front-end layer
(depending on programmers' preference and their notion of best practices when
building web apps).
Services can be injected in the following ways:
| By alias | By type annotation | Value |
|---|---|---|
| db_connection | AsyncConnection | instance of AsyncConnection (scoped to web request) |
| db_session | AsyncSession | instance of AsyncSession (scoped to web request) |
| db_engine | AsyncEngine | instance of AsyncEngine (singleton) |
For example, using SQLite:
- requires driver:
pip install aiosqlite - connection string:
sqlite+aiosqlite:///example.db
See the tests folder for a working example
using database migrations applied with Alembic, and a documented API that offers methods to fetch, create,
delete countries objects.
Note
BlackSheep is designed to be used in async way, therefore this library
requires the use of an asynchronous driver.
References
Documentation
Please refer to the documentation website.
Metadata
Release files for blacksheep-sqlalchemy 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| blacksheep_sqlalchemy-0.0.4.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| blacksheep_sqlalchemy-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / blacksheep_sqlalchemy-0.0.4.tar.gz
| Download URL | blacksheep_sqlalchemy-0.0.4.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21771ceb919e1a77952cf395f53f40f49bd128ba0f2cea088556598c99c450ee
|
|
BLAKE2b-256 checksum How to use checksums |
cbcb425b1f9123d4ea2e465360cfdea52414c044729b1994d8e24ce0ce25ecdf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / blacksheep_sqlalchemy-0.0.4-py3-none-any.whl
| Download URL | blacksheep_sqlalchemy-0.0.4-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f0202623c9970c104d11b28010d8d16677774bec2fc31ff8ccaa1e1a372d190c
|
|
BLAKE2b-256 checksum How to use checksums |
fe14f1173cd3aef907b1625fd47f042066d48e91d5f93e7648a59f7b36366f4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|