88from fastapi import Depends , Request , Response
99from fastapi .concurrency import contextmanager_in_threadpool
1010from fastapi .responses import PlainTextResponse
11+ from pydantic import BaseModel
1112from sqlalchemy import engine_from_config , text
1213from sqlalchemy .engine import Connection , Engine
1314from sqlalchemy .ext .declarative import DeferredReflection
3839_session_factories : dict [str , sessionmaker ] = {}
3940
4041
42+ class _EngineConfig (BaseModel ):
43+ """Engine configuration with typed defaults and bool coercion."""
44+
45+ hide_parameters : bool = True
46+
47+ class Config :
48+ extra = "allow"
49+
50+
4151class Base (DeclarativeBase , DeferredReflection ):
4252 __abstract__ = True
4353
@@ -50,11 +60,31 @@ def get_envvar_prefix(key: str) -> str:
5060 return envvar_prefix
5161
5262
63+ def _get_engine_config (
64+ envvar_prefix : str ,
65+ ) -> dict [str , Union [str , bool ]]:
66+ """Build engine config dict with opinionated defaults and type coercion."""
67+ lowercase_env : dict [str , Union [str , bool ]] = {
68+ k .lower (): v for k , v in os .environ .items ()
69+ }
70+ lowercase_env .pop (f"{ envvar_prefix } warn_20" , None )
71+
72+ overrides = {
73+ k [len (envvar_prefix ) :]: v
74+ for k , v in lowercase_env .items ()
75+ if k .startswith (envvar_prefix )
76+ }
77+ config = _EngineConfig (** overrides ) # type: ignore[arg-type]
78+ for param , value in config .dict ().items ():
79+ lowercase_env [f"{ envvar_prefix } { param } " ] = value
80+
81+ return lowercase_env
82+
83+
5384def new_engine (key : str = _DEFAULT_SESSION_KEY ) -> Union [Engine , Connection ]:
5485 envvar_prefix = get_envvar_prefix (key )
55- lowercase_environ = {k .lower (): v for k , v in os .environ .items ()}
56- lowercase_environ .pop (f"{ envvar_prefix } warn_20" , None )
57- return engine_from_config (lowercase_environ , prefix = envvar_prefix )
86+ config = _get_engine_config (envvar_prefix )
87+ return engine_from_config (config , prefix = envvar_prefix )
5888
5989
6090def startup (key : str = _DEFAULT_SESSION_KEY ):
0 commit comments