mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 12:20:38 +04:00
Connect to database and add first models
This commit is contained in:
3
database/__init__.py
Normal file
3
database/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .crud import *
|
||||
from .schemas import *
|
||||
from .database import get_session, init_models
|
||||
8
database/crud.py
Normal file
8
database/crud.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
|
||||
from .models import *
|
||||
|
||||
|
||||
async def get_user(db: AsyncSession, user_id: int):
|
||||
return await db.get(User, user_id)
|
||||
26
database/database.py
Normal file
26
database/database.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
DATABASE_URL = "sqlite+aiosqlite:///./dev_database.db"
|
||||
# DATABASE_URL = "postgresql://user:password@postgresserver/db"
|
||||
|
||||
engine = create_async_engine(
|
||||
DATABASE_URL, connect_args={"check_same_thread": False}, echo=True
|
||||
)
|
||||
async_session = sessionmaker(
|
||||
engine, class_=AsyncSession, expire_on_commit=False)
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
async def get_session() -> AsyncSession: # type: ignore
|
||||
# Dependency
|
||||
async with async_session() as session: # type: ignore
|
||||
yield session
|
||||
|
||||
|
||||
async def init_models():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
37
database/models.py
Normal file
37
database/models.py
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .database import Base
|
||||
|
||||
|
||||
class Game(Base):
|
||||
__tablename__ = "games"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
title = Column(String)
|
||||
description = Column(String)
|
||||
language = Column(String)
|
||||
version = Column(String)
|
||||
download_size = Column(String)
|
||||
upload_date = Column(String)
|
||||
|
||||
system = Column(String)
|
||||
processor = Column(String)
|
||||
memory = Column(String)
|
||||
graphics = Column(String)
|
||||
storage = Column(String)
|
||||
|
||||
owner_id = Column(Integer, ForeignKey("users.id"))
|
||||
owner = relationship("User", back_populates="games")
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
email = Column(String, unique=True)
|
||||
name = Column(String)
|
||||
hash_of_password = Column(String)
|
||||
|
||||
games = relationship("Game", back_populates="owner")
|
||||
45
database/schemas.py
Normal file
45
database/schemas.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class GameBase(BaseModel):
|
||||
title: str
|
||||
description: str | None = None
|
||||
language: str | None = None
|
||||
version: str | None = None
|
||||
download_size: str | None = None
|
||||
upload_date: str | None = None
|
||||
|
||||
system: str | None = None
|
||||
processor: str | None = None
|
||||
memory: str | None = None
|
||||
graphics: str | None = None
|
||||
storage: str | None = None
|
||||
|
||||
|
||||
class GameCreate(GameBase):
|
||||
pass
|
||||
|
||||
|
||||
class Game(GameBase):
|
||||
id: int
|
||||
owner_id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
email: str
|
||||
name: str
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
|
||||
class User(UserBase):
|
||||
id: int
|
||||
games: list[Game] = []
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
Reference in New Issue
Block a user