25 lines
660 B
Python
25 lines
660 B
Python
"""Declarative base for SQLAlchemy models."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from enum import StrEnum
|
|
from typing import TypeVar
|
|
|
|
from sqlalchemy.orm import DeclarativeBase, declared_attr
|
|
|
|
EnumT = TypeVar("EnumT", bound=StrEnum)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
"""Base class that configures naming conventions."""
|
|
|
|
@declared_attr.directive
|
|
def __tablename__(cls) -> str: # type: ignore[misc] # noqa: N805 - SQLAlchemy expects cls
|
|
return cls.__name__.lower()
|
|
|
|
|
|
def enum_values(enum_cls: type[EnumT]) -> list[str]:
|
|
"""Return enum member values to keep DB representation stable."""
|
|
|
|
return [member.value for member in enum_cls]
|