feat(root):add mysql

This commit is contained in:
shahzeydyfah
2026-02-17 23:52:14 +03:30
parent abb1c01753
commit 189df529d9
4 changed files with 126 additions and 6 deletions
+27
View File
@@ -0,0 +1,27 @@
"""
مدل ORM برای جدول const در MySQL.
"""
from sqlalchemy import Column, Integer, String, Text
from core.service.mysql_orm import Base
class Const(Base):
"""مدل جدول const"""
__tablename__ = "const"
id = Column(Integer, primary_key=True, index=True)
key = Column(String(255), nullable=False, index=True)
title_fa = Column(String(255), nullable=True)
value = Column(Text, nullable=True)
def __repr__(self):
return f"<Const(id={self.id}, key='{self.key}', title_fa='{self.title_fa}')>"
def to_dict(self):
"""تبدیل به dict برای JSON response"""
return {
"id": self.id,
"key": self.key,
"title_fa": self.title_fa,
"value": self.value,
}
+34
View File
@@ -0,0 +1,34 @@
"""
Repository برای جدول const در MySQL - برای استفاده در خروجی اکسل.
"""
from typing import List, Dict, Optional
from sqlalchemy import select
from core.service.mysql_orm import get_mysql_session
from app.models.mysql_const import Const
class MySQLConstRepository:
"""Repository برای خواندن دیتا از جدول const"""
async def get_by_keys(self, keys: List[str]) -> Dict[int, str]:
"""
گرفتن const ها با لیست key ها و برگرداندن dict به صورت {id: title_fa}
مثال: get_by_keys(['nature', 'type']) -> {2: 'طبیعت 2', 9: 'نوع 9'}
"""
async with get_mysql_session() as session:
result = await session.execute(
select(Const).where(Const.key.in_(keys))
)
consts = result.scalars().all()
return {c.id: c.title_fa or c.value or str(c.id) for c in consts}
async def get_by_key(self, key: str) -> Dict[int, str]:
"""گرفتن const ها با یک key و برگرداندن dict به صورت {id: title_fa}"""
return await self.get_by_keys([key])
async def get_all_consts_dict(self) -> Dict[int, str]:
"""گرفتن همه const ها به صورت {id: title_fa} - برای کش کردن"""
async with get_mysql_session() as session:
result = await session.execute(select(Const))
consts = result.scalars().all()
return {c.id: c.title_fa or c.value or str(c.id) for c in consts}