connect database
This commit is contained in:
4
app/infrastructure/database/__init__.py
Normal file
4
app/infrastructure/database/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .models import Base
|
||||
from .session import async_session, engine, get_session
|
||||
|
||||
__all__ = ["Base", "async_session", "engine", "get_session"]
|
||||
22
app/infrastructure/database/crud.py
Normal file
22
app/infrastructure/database/crud.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from .models import User
|
||||
|
||||
|
||||
async def get_user_by_tg_id(session: AsyncSession, user_tg_id: int) -> User | None:
|
||||
result = await session.execute(select(User).where(User.tg_id == user_tg_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def create_user(
|
||||
session: AsyncSession,
|
||||
tg_id: int,
|
||||
fullname: str,
|
||||
phone: str = "",
|
||||
role: str = "user",
|
||||
) -> User:
|
||||
user = User(tg_id=tg_id, fullname=fullname, role=role, phone=phone)
|
||||
session.add(user)
|
||||
await session.commit()
|
||||
return user
|
||||
@@ -1,13 +0,0 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||
|
||||
from config.config import DATABASE_URL
|
||||
|
||||
engine = create_async_engine(DATABASE_URL, echo=True)
|
||||
Base = declarative_base()
|
||||
async_session = sessionmaker(bind=engine, expire_on_commit=False, class_=AsyncSession)
|
||||
|
||||
|
||||
async def get_session() -> AsyncSession:
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
13
app/infrastructure/database/init_db.py
Normal file
13
app/infrastructure/database/init_db.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import asyncio
|
||||
|
||||
from .models import Base
|
||||
from .session import engine
|
||||
|
||||
|
||||
async def init_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(init_db())
|
||||
@@ -6,16 +6,17 @@ from sqlalchemy import (
|
||||
ForeignKey,
|
||||
func,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.orm import Mapped, declarative_base, mapped_column
|
||||
|
||||
from app.db.database import Base
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "user"
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
tg_id: Mapped[int] = mapped_column(BigInteger, unique=True, nullable=False)
|
||||
fullname: Mapped[str] = mapped_column()
|
||||
tg_id: Mapped[int] = mapped_column(BigInteger)
|
||||
role: Mapped[str] = mapped_column(default="user")
|
||||
phone: Mapped[str] = mapped_column(nullable=True)
|
||||
|
||||
|
||||
|
||||
20
app/infrastructure/database/session.py
Normal file
20
app/infrastructure/database/session.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncIterator
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from config.config import settings
|
||||
|
||||
engine = create_async_engine(settings.database_url, echo=True)
|
||||
async_session = sessionmaker(
|
||||
bind=engine,
|
||||
expire_on_commit=False,
|
||||
class_=AsyncSession,
|
||||
)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def get_session() -> AsyncIterator[AsyncSession]:
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user