Spaces:
Running
Running
File size: 1,373 Bytes
478dec6 39c120f e22b3b4 478dec6 39c120f | 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import os
import dotenv
from config.constant import EnvPostgresConstants, EnvFilepath
dotenv.load_dotenv(EnvFilepath.ENVPATH)
from contextlib import asynccontextmanager
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import declarative_base
DATABASE_URL = EnvPostgresConstants.CONSTRING.\
replace("postgresql+psycopg2", "postgresql+asyncpg")
engine = create_async_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_recycle=300,
pool_size=5,
max_overflow=10,
)
AsyncSessionLocal = async_sessionmaker(
engine,
expire_on_commit=False,
class_=AsyncSession
)
Base = declarative_base()
# async def get_db():
# async with AsyncSessionLocal() as session:
# try:
# return session
# finally:
# await session.close()
async def get_db():
async with AsyncSessionLocal() as session:
try:
yield session
finally:
await session.close()
# async def get_db_session():
# session = AsyncSessionLocal()
# try:
# return session
# except:
# await session.close()
# raise
# @asynccontextmanager
# async def db_session():
# async with AsyncSessionLocal() as session:
# try:
# yield session
# finally:
# await session.close() |