File: sqlalchemy_sync_dependencies.py

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (29 lines) | stat: -rw-r--r-- 781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy import select

from litestar import Litestar, post
from litestar.plugins.sqlalchemy import SQLAlchemyInitPlugin, SQLAlchemySyncConfig

if TYPE_CHECKING:
    from typing import Tuple

    from sqlalchemy import Engine
    from sqlalchemy.orm import Session


@post("/", sync_to_thread=True)
def handler(db_session: Session, db_engine: Engine) -> Tuple[int, int]:
    one = db_session.execute(select(1)).scalar()

    with db_engine.begin() as conn:
        two = conn.execute(select(2)).scalar()

    return one, two


config = SQLAlchemySyncConfig(connection_string="sqlite:///sync.sqlite")
plugin = SQLAlchemyInitPlugin(config=config)
app = Litestar(route_handlers=[handler], plugins=[plugin])