feat: add SQLAlchemy async engine and session factory
This commit is contained in:
parent
1aae932d1e
commit
3c38333e9c
22
src/proxy_pool/db/base.py
Normal file
22
src/proxy_pool/db/base.py
Normal 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(),
|
||||
)
|
||||
21
src/proxy_pool/db/session.py
Normal file
21
src/proxy_pool/db/session.py
Normal 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,
|
||||
)
|
||||
Loading…
x
Reference in New Issue
Block a user