PIbd-32_Kashin_M.I_API_Cour.../database.py

33 lines
1.1 KiB
Python
Raw Normal View History

from typing import Optional
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy import Column, DateTime, Enum
from sqlalchemy.sql import func
from datetime import datetime
from enums import TypeMood, TypeModel
engine = create_async_engine("sqlite+aiosqlite:///questions.db")
new_session = async_sessionmaker(engine, expire_on_commit=False)
class Model(DeclarativeBase):
pass
class QuestionOrm(Model):
__tablename__ = "questions"
id: Mapped[int] = mapped_column(primary_key=True)
email_user: Mapped[str]
type_mood: Mapped[TypeMood] = mapped_column(Enum(TypeMood))
type_model: Mapped[TypeModel] = mapped_column(Enum(TypeModel))
question: Mapped[str]
answer: Mapped[Optional[str]]
question_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
async def create_tables():
async with engine.begin() as conn:
await conn.run_sync(Model.metadata.create_all)
async def delete_tables():
async with engine.begin() as conn:
await conn.run_sync(Model.metadata.drop_all)