Initial commit

This commit is contained in:
2025-12-31 00:40:41 +03:00
parent a615f43983
commit d4898869fa
10 changed files with 258 additions and 35 deletions
+12 -7
View File
@@ -1,9 +1,14 @@
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker,
create_async_engine)
def create_engine(database_url: str) -> AsyncEngine:
return create_async_engine(database_url, echo=False)
def create_session_maker(engine: AsyncEngine) -> async_sessionmaker:
return async_sessionmaker(engine, expire_on_commit=False)
def new_session_maker(db_url: str) -> async_sessionmaker[AsyncSession]:
engine = create_async_engine(
db_url,
pool_size=15,
max_overflow=15,
connect_args={
"timeout": 5,
},
)
return async_sessionmaker(engine, class_=AsyncSession, autoflush=False, expire_on_commit=False)
@@ -0,0 +1,121 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from trudex.infrastructure.database.models import User
class UserDAO:
def __init__(self, session: AsyncSession) -> None:
self.session: AsyncSession = session
async def get_by_id(self, user_id: int) -> User | None:
result = await self.session.execute(
select(User).where(User.id == user_id)
)
return result.scalar_one_or_none()
async def get_all(self) -> list[User]:
result = await self.session.execute(select(User))
return list(result.scalars().all())
async def get_by_group(self, group: int) -> list[User]:
result = await self.session.execute(
select(User).where(User.group == group)
)
return list(result.scalars().all())
async def get_admins(self) -> list[User]:
result = await self.session.execute(
select(User).where(User.is_admin == True)
)
return list(result.scalars().all())
async def create(
self,
user_id: int,
first_name: str,
username: str | None = None,
last_name: str | None = None,
group: int | None = None,
is_admin: bool = False,
) -> User:
user = User(
id=user_id,
username=username,
first_name=first_name,
last_name=last_name,
group=group,
is_admin=is_admin,
)
self.session.add(user)
await self.session.flush()
return user
async def update(
self,
user_id: int,
username: str | None = None,
first_name: str | None = None,
last_name: str | None = None,
group: int | None = None,
is_admin: bool | None = None,
) -> User | None:
user = await self.get_by_id(user_id)
if not user:
return None
if username is not None:
user.username = username
if first_name is not None:
user.first_name = first_name
if last_name is not None:
user.last_name = last_name
if group is not None:
user.group = group
if is_admin is not None:
user.is_admin = is_admin
await self.session.flush()
return user
async def delete(self, user_id: int) -> bool:
user = await self.get_by_id(user_id)
if not user:
return False
await self.session.delete(user)
await self.session.flush()
return True
async def upsert(
self,
user_id: int,
first_name: str,
username: str | None = None,
last_name: str | None = None,
group: int | None = None,
is_admin: bool = False,
) -> User:
user = await self.get_by_id(user_id)
if user:
if username is not None:
user.username = username
if first_name is not None:
user.first_name = first_name
if last_name is not None:
user.last_name = last_name
if group is not None:
user.group = group
if is_admin is not None:
user.is_admin = is_admin
await self.session.flush()
return user
return await self.create(
user_id=user_id,
username=username,
first_name=first_name,
last_name=last_name,
group=group,
is_admin=is_admin,
)
+19 -1
View File
@@ -1,5 +1,23 @@
from sqlalchemy.orm import DeclarativeBase
from datetime import datetime
from typing import final
from sqlalchemy import BigInteger, CheckConstraint, String, func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
@final
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
username: Mapped[str | None] = mapped_column(String(32))
first_name: Mapped[str] = mapped_column(String(64))
last_name: Mapped[str | None] = mapped_column(String(64))
group: Mapped[int | None] = mapped_column(CheckConstraint("group >= 1000 AND group <= 9999"))
is_admin: Mapped[bool] = mapped_column(default=False)
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(server_default=func.now(), onupdate=func.now())