dubswayAgenticV2 / app /database.py
peace2024's picture
Upload 23 files
6d01d5b
raw
history blame
830 Bytes
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker, declarative_base
from dotenv import load_dotenv
# Load .env variables
load_dotenv()
# Ensure your DATABASE_URL is in correct asyncpg format
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
raise RuntimeError("DATABASE_URL is not set in environment.")
# Create the async engine
engine = create_async_engine(
DATABASE_URL, echo=True, future=True # Set echo=False in production
)
# Session factory
AsyncSessionLocal = sessionmaker(
bind=engine, class_=AsyncSession, expire_on_commit=False
)
# Base class for models
Base = declarative_base()
# Dependency for routes to get the async session
async def get_db():
async with AsyncSessionLocal() as session:
yield session