feat: add SQLAlchemy async engine and session factory

This commit is contained in:
agatha 2026-03-14 12:44:15 -04:00
parent 1aae932d1e
commit 3c38333e9c
2 changed files with 43 additions and 0 deletions

22
src/proxy_pool/db/base.py Normal file
View File

@ -0,0 +1,22 @@
import uuid
from datetime import datetime
from sqlalchemy import func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class UUIDPrimaryKeyMixin:
id: Mapped[uuid.UUID] = mapped_column(
primary_key=True,
default_factory=uuid.uuid4,
)
class TimestampMixin:
created_at: Mapped[datetime] = mapped_column(
server_default=func.now(),
)

View File

@ -0,0 +1,21 @@
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from proxy_pool.config import Settings
def create_engine(settings: Settings):
return create_async_engine(
settings.database_url,
pool_size=settings.db_pool_size,
max_overflow=settings.db_max_overflow,
echo=settings.log_level == "DEBUG",
)
def create_session_factory(settings: Settings) -> async_sessionmaker[AsyncSession]:
engine = create_engine(settings)
return async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
)