Created
October 2, 2024 08:41
-
-
Save arturboyun/b601b50998865de9d814081b0dfe7f7e to your computer and use it in GitHub Desktop.
Base Class for SQLAlchemy Models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import re | |
import uuid | |
from sqlalchemy import MetaData, func, DateTime, UUID, text | |
from sqlalchemy.ext.asyncio import AsyncAttrs | |
from sqlalchemy.orm import declarative_base, Mapped, mapped_column | |
POSTGRES_INDEXES_NAMING_CONVENTION = { | |
"ix": "%(column_0_label)s_idx", | |
"uq": "%(table_name)s_%(column_0_name)s_key", | |
"ck": "%(table_name)s_%(constraint_name)s_check", | |
"fk": "%(table_name)s_%(column_0_name)s_fkey", | |
"pk": "%(table_name)s_pkey", | |
} | |
metadata = MetaData(naming_convention=POSTGRES_INDEXES_NAMING_CONVENTION) | |
DeclarativeBase = declarative_base(metadata=metadata) | |
class Base(AsyncAttrs, DeclarativeBase): | |
"""Base class for all models.""" | |
__abstract__ = True | |
# id: Mapped[uuid.UUID] = mapped_column( | |
# UUID(as_uuid=True), | |
# primary_key=True, | |
# server_default=text("gen_random_uuid()"), | |
# unique=True, | |
# ) | |
created_at: Mapped[datetime.datetime] = mapped_column( | |
DateTime(timezone=True), | |
default=datetime.datetime.now(datetime.UTC), | |
server_default=func.current_timestamp(), | |
) | |
updated_at: Mapped[datetime.datetime] = mapped_column( | |
DateTime(timezone=True), | |
default=datetime.datetime.now(datetime.UTC), | |
onupdate=datetime.datetime.now(datetime.UTC), | |
) | |
def __init_subclass__(cls, **kwargs): | |
"""Set the table name to the lowercase version of the class name if not set.""" | |
if not cls.__dict__.get("__tablename__", None): | |
cls.__tablename__ = re.sub(r"(?<!^)(?=[A-Z])", "_", cls.__name__).lower() | |
super().__init_subclass__(**kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment