MCPcopy Create free account
hub / github.com/dbunt1tled/python-fast-api / MyDatabaseConfig

Class MyDatabaseConfig

src/core/db/asmysql.py:9–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9class MyDatabaseConfig:
10 conn = None
11
12 def __init__(
13 self,
14 dsn: str,
15 ):
16 self.dsn = dsn
17 self.database_url = f"{self.dsn}"
18
19 self.engine = create_async_engine(
20 self.database_url,
21 echo=False,
22 pool_pre_ping=True,
23 poolclass=StaticPool,
24 connect_args={"charset": "utf8mb4", "cursorclass": aiomysql.SSCursor},
25 )
26
27 self.async_session_factory = async_sessionmaker(
28 bind=self.engine,
29 class_=AsyncSession,
30 expire_on_commit=False,
31 )
32
33 @asynccontextmanager
34 async def session(self) -> AsyncGenerator[AsyncSession]:
35 async with self.async_session_factory() as session:
36 try:
37 yield session
38 await session.commit()
39 except Exception:
40 await session.rollback()
41 raise
42 finally:
43 await session.close()
44
45 async def close(self) -> None:
46 if self.engine is not None:
47 await self.engine.dispose()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected