|
| 1 | +[](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/actions/workflows/build.yml) |
| 2 | +[](https://pypi.org/project/BlackSheep-SQLAlchemy/) |
| 3 | +[](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/) |
| 4 | +[](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/blob/main/LICENSE) |
| 5 | + |
1 | 6 | # BlackSheep-SQLAlchemy |
2 | | -Extension for BlackSheep that simplifies the use of SQLAlchemy in the web framework. |
| 7 | +Extension for [BlackSheep](https://github.com/Neoteroi/BlackSheep) that |
| 8 | +simplifies the use of SQLAlchemy in the web framework. |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install blacksheep-sqlalchemy |
| 12 | +``` |
| 13 | + |
| 14 | +## How to use |
| 15 | + |
| 16 | +```python |
| 17 | +from blacksheep.server import Application |
| 18 | +from blacksheepsqlalchemy import use_sqlalchemy |
| 19 | + |
| 20 | +app = Application() |
| 21 | + |
| 22 | +use_sqlalchemy(app, connection_string="<CONNECTION_STRING>") |
| 23 | + |
| 24 | +``` |
| 25 | + |
| 26 | +After registering SQLAlchemy, services are configured in the application, so |
| 27 | +they are automatically resolved in any request handler requiring a SQLAlchemy |
| 28 | +db connections or db sessions; for example: |
| 29 | + |
| 30 | +```python |
| 31 | + |
| 32 | +@get("/api/countries") |
| 33 | +async def get_countries(db_connection) -> List[CountryData]: |
| 34 | + """ |
| 35 | + Fetches the countries using a database connection. |
| 36 | + """ |
| 37 | + result = [] |
| 38 | + async with db_connection: |
| 39 | + items = await db_connection.execute(text("SELECT * FROM country")) |
| 40 | + for item in items.fetchall(): |
| 41 | + result.append(CountryData(item["id"], item["name"])) |
| 42 | + return result |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +Services can be injected at any level of the resolution graph, so `BlackSheep` |
| 47 | +and `rodi` support out of the box the scenario of db connections or db sessions |
| 48 | +referenced in the business logic but not directly by the front-end layer |
| 49 | +(depending on programmers' preference and their notion of best practices when |
| 50 | +building web apps). |
| 51 | + |
| 52 | +Services can be injected in the following ways: |
| 53 | + |
| 54 | +| By alias | By type annotation | Value | |
| 55 | +| ------------- | ------------------ | --------------------------------------------------- | |
| 56 | +| db_connection | AsyncConnection | instance of AsyncConnection (scoped to web request) | |
| 57 | +| db_session | AsyncSession | instance of AsyncSession (scoped to web request) | |
| 58 | +| db_engine | AsyncEngine | instance of AsyncEngine (singleton) | |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +For example, using SQLite: |
| 63 | + |
| 64 | +* requires driver: `pip install aiosqlite` |
| 65 | +* connection string: `sqlite+aiosqlite:///example.db` |
| 66 | + |
| 67 | +See the `tests` folder for a working example using database migrations applied |
| 68 | +with `Alembic`, and a documented API that offers methods to fetch, create, |
| 69 | +delete countries objects. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### Note |
| 74 | +BlackSheep is designed to be used in `async` way, therefore this library |
| 75 | +requires the use of an asynchronous driver. |
| 76 | + |
| 77 | +## References |
| 78 | + |
| 79 | +* [SQLAlchemy - support for asyncio](https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html) |
| 80 | + |
| 81 | +## Documentation |
| 82 | +Please refer to the [documentation website](https://www.neoteroi.dev/blacksheep/). |
0 commit comments