forked from grillazz/fastapi-sqlalchemy-asyncpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
30 lines (23 loc) · 780 Bytes
/
database.py
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
30
from collections.abc import AsyncGenerator
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio import async_sessionmaker
from app.config import settings as global_settings
from app.utils.logging import AppLogger
logger = AppLogger.__call__().get_logger()
engine = create_async_engine(
global_settings.asyncpg_url.unicode_string(),
future=True,
echo=True,
)
# expire_on_commit=False will prevent attributes from being expired
# after commit.
AsyncSessionFactory = async_sessionmaker(
engine,
autoflush=False,
expire_on_commit=False,
)
# Dependency
async def get_db() -> AsyncGenerator:
async with AsyncSessionFactory() as session:
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
yield session