postgresql_base_model.py 701 B

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