mirror of
https://github.com/aykhans/portfolio-blog.git
synced 2025-09-08 15:10:45 +00:00
First Commit
This commit is contained in:
0
src/app/db/__init__.py
Normal file
0
src/app/db/__init__.py
Normal file
1
src/app/db/base.py
Normal file
1
src/app/db/base.py
Normal file
@@ -0,0 +1 @@
|
||||
from app.db.base_class import Base
|
19
src/app/db/base_class.py
Normal file
19
src/app/db/base_class.py
Normal 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
37
src/app/db/session.py
Normal 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
|
||||
)
|
Reference in New Issue
Block a user