| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from sqlalchemy import select |
| 6 |
|
| 7 |
from litestar import Litestar, post |
| 8 |
from litestar.di import NamedDependency |
| 9 |
from litestar.plugins.sqlalchemy import SQLAlchemyInitPlugin, SQLAlchemySyncConfig |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from typing import Tuple |
| 13 |
|
| 14 |
from sqlalchemy import Engine |
| 15 |
from sqlalchemy.orm import Session |
| 16 |
|
| 17 |
|
| 18 |
@post("/", sync_to_thread=True) |
| 19 |
def handler(db_session: NamedDependency[Session], db_engine: NamedDependency[Engine]) -> Tuple[int, int]: |
| 20 |
one = db_session.execute(select(1)).scalar() |
| 21 |
|
| 22 |
with db_engine.begin() as conn: |
| 23 |
two = conn.execute(select(2)).scalar() |
| 24 |
|
| 25 |
return one, two |
| 26 |
|
| 27 |
|
| 28 |
config = SQLAlchemySyncConfig(connection_string="sqlite:///sync.sqlite") |
| 29 |
plugin = SQLAlchemyInitPlugin(config=config) |
| 30 |
app = Litestar(route_handlers=[handler], plugins=[plugin]) |