Skip to content

Instantly share code, notes, and snippets.

@hoangddt
Created May 20, 2025 04:06
Show Gist options
  • Save hoangddt/07a624b0187d26b7b66f9e755d6224b9 to your computer and use it in GitHub Desktop.
Save hoangddt/07a624b0187d26b7b66f9e755d6224b9 to your computer and use it in GitHub Desktop.
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from video_processor.core.config import settings
from sqlalchemy import create_engine
async_engine = create_async_engine(
str(settings.ASYNC_SQLALCHEMY_DATABASE_URI),
echo=False,
future=True,
pool_size=32, # Set the number of connections in the pool
max_overflow=64, # Set the maximum number of connections that can be created beyond the pool size
pool_timeout=30, # Set the timeout for getting a connection from the pool
pool_recycle=1800, # Recycle connections after 30 minutes
)
async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
class CourseRepository:
"""Repository for course operations."""
def __init__(self, session: AsyncSession):
self.session = session
async def create(self, course: CourseCreateDto) -> CourseModel:
"""Create a new course."""
db_course = CourseModel(**course.model_dump())
self.session.add(db_course)
await self.session.commit()
await self.session.refresh(db_course)
return db_course
async with async_session() as session:
course_repo = CourseRepository(session)
course = await course_repo.create(CourseCreateDto(
name="Test Course",
description="Test Description",
provider_id="123"
))
print(course)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment