First Commit

This commit is contained in:
2023-09-10 23:50:05 +04:00
commit 6d45d1c604
61 changed files with 19953 additions and 0 deletions

0
src/app/db/__init__.py Normal file
View File

1
src/app/db/base.py Normal file
View File

@@ -0,0 +1 @@
from app.db.base_class import Base

19
src/app/db/base_class.py Normal file
View File

@@ -0,0 +1,19 @@
from typing import Any
from sqlalchemy.ext.declarative import as_declarative, declared_attr
from sqlalchemy import DateTime, Column
from sqlalchemy.sql import func
@as_declarative()
class Base:
__name__: str
id: Any
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# Generate __tablename__ automatically
@declared_attr
def __tablename__(cls) -> str:
return cls.__name__.lower()

37
src/app/db/session.py Normal file
View File

@@ -0,0 +1,37 @@
from sqlalchemy import (
create_engine,
Engine
)
from sqlalchemy.orm import (
sessionmaker,
Session
)
from sqlalchemy.ext.asyncio import (
AsyncEngine,
create_async_engine,
AsyncSession
)
from app.core.config import settings
engine: Engine = create_engine(
str(settings.get_postgres_dsn()),
pool_pre_ping=True
)
SessionLocal: Session = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine
)
async_engine: AsyncEngine = create_async_engine(
str(settings.get_postgres_dsn(_async=True)),
future=True
)
AsyncSessionLocal: AsyncSession = sessionmaker(
autocommit=False,
autoflush=False,
bind=async_engine,
class_=AsyncSession
)