tofu-api/tofu_api/common/database/mixins.py

24 lines
748 B
Python

from datetime import datetime
from sqlalchemy import Column, func
from sqlalchemy.orm import declarative_mixin
from tofu_api.common.database import Col
from tofu_api.common.database.types import TzDateTime
__all__ = [
'TimestampMixin'
]
@declarative_mixin
class TimestampMixin:
"""
Mixin for database models that provides the "created_at" and "modified_at" columns.
"""
# Created timestamp (automatically set to NOW() once on object creation)
created_at: Col[datetime] = Column(TzDateTime, nullable=False, server_default=func.now())
# Modified timestamp (automatically set to NOW() on each update)
modified_at: Col[datetime] = Column(TzDateTime, nullable=False, server_default=func.now(), onupdate=func.now())