{% if database == 'postgres' -%} from datetime import datetime from sqlalchemy import BigInteger, Column from sqlmodel import Field as SQLField from sqlmodel import SQLModel from .base import created_at_col class User(SQLModel, table=True): __tablename__ = "user_account" id: int | None = SQLField(default=None, primary_key=True) tg_id: int = SQLField(sa_column=Column(BigInteger, unique=True, index=True)) username: str | None = None first_name: str | None = None last_name: str | None = None created_at: datetime | None = created_at_col() {%- else -%} from beanie import Document class User(Document): id: int balance: float = 0 class Settings: name = "users" @classmethod async def get_by_id(cls, id_: int) -> "User | None": return await cls.find_one(cls.id == id_) @classmethod async def get_or_create(cls, id_: int) -> "User": user = await cls.get_by_id(id_) if user is None: user = cls(id=id_) await user.insert() return user async def add_balance(self, amount: float) -> None: self.balance += amount await self.save() async def subtract_balance(self, amount: float) -> bool: if self.balance >= amount: self.balance -= amount await self.save() return True return False {%- endif %}