Первые шаги подключения BD к API

This commit is contained in:
2024-07-31 17:58:26 +09:00
parent da2ae23831
commit a2a400fff3
5 changed files with 46 additions and 0 deletions

23
backend-app/database.py Normal file
View File

@ -0,0 +1,23 @@
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession, create_async_engine
from sqlalchemy.orm import DeclarativeBase
class Model(DeclarativeBase):
pass
async_engine=create_async_engine(
url = "mssql+aioodbc://sa:159357"\
"@10.124.30.208:1433/test_db"\
"?driver=ODBC+Driver+17+for+SQL+Server",
connect_args={"check_same_thread": False}
)
async_session = async_sessionmaker(
async_engine,
autoflush=True,
autocommit=False,
expire_on_commit =False
)
# async def get_async_session() -> AsyncSession:
# async with async_session() as session:
# session.

View File

0
backend-app/repo/crud.py Normal file
View File

21
backend-app/repo/model.py Normal file
View File

@ -0,0 +1,21 @@
from database import Model
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import String, Integer, Boolean, ForeignKey
from typing import Optional, Sequence
class UserModel(Model):
__tablename__ ="users"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
firstname: Mapped[str | None]= mapped_column(String(255), nullable=True)
lastname: Mapped[str | None]= mapped_column(String(255), nullable=True)
age: Mapped[int | None] = mapped_column(Integer, nullable=True)
email: Mapped[str | None] = mapped_column(String(255), nullable=True)
login_id: Mapped[int] = mapped_column(Integer, ForeignKey("logins.id"))
class LoginModel(Model):
__tablename__ ="logins"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
login: Mapped[str]= mapped_column(String(255), nullable=False)
password: Mapped[str]= mapped_column(String(255), nullable=False)

View File

@ -0,0 +1,2 @@
from pydantic import BaseModel