File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ import os
2+
3+ from pydantic_settings import BaseSettings , SettingsConfigDict
4+
5+
6+ class CARPIFastAPISettings (BaseSettings ):
7+ db_dialect : str
8+ db_api : str
9+ db_hostname : str
10+ db_username : str
11+ db_password : str
12+ db_schema : str
13+ model_config = SettingsConfigDict (
14+ env_file = os .path .join (os .path .dirname (__file__ ), ".env" )
15+ )
16+
17+
18+ _SETTINGS = CARPIFastAPISettings ()
19+
20+
21+ def get_app_settings () -> CARPIFastAPISettings :
22+ return _SETTINGS
Original file line number Diff line number Diff line change 1+ from typing import Annotated , Generator
2+
3+ from fastapi import Depends
4+ from sqlalchemy import Engine
5+ from sqlmodel import Session , SQLModel , create_engine
6+
7+ from app .config import CARPIFastAPISettings
8+
9+ _engine : Engine = None
10+
11+
12+ def init_db_engine (settings : CARPIFastAPISettings ) -> None :
13+ global _engine
14+ _engine = create_engine (
15+ url = f"{ settings .db_dialect } +{ settings .db_api } "
16+ + f"://{ settings .db_username } :{ settings .db_password } "
17+ + f"@{ settings .db_hostname } /{ settings .db_schema } " ,
18+ # echo=True,
19+ )
20+ SQLModel .metadata .create_all (_engine )
21+
22+
23+ def dispose_db_engine () -> None :
24+ _engine .dispose ()
25+
26+
27+ def get_db_session () -> Generator [Session , None , None ]:
28+ with Session (_engine ) as session :
29+ yield session
30+
31+
32+ SessionDep = Annotated [Session , Depends (get_db_session )]
You can’t perform that action at this time.
0 commit comments