zhaoqingang
2025-02-06 bcc63761bdc4c1604c9275a3c5cdf8a483ad5611
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
 
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.config.config import settings
 
DATABASE_URL = os.getenv('POSTGRESQL_DATABASE_URL') or settings.postgresql_database_url
 
engine = create_async_engine(DATABASE_URL, echo=False)
PostgresqlSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession)
PostgresqlBase = declarative_base()
 
 
async def get_pdb() -> AsyncSession:
    async with PostgresqlSessionLocal() as session:
        # yield session
        try:
            yield session
        finally:
            session.close()