File size: 2,304 Bytes
97c535b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
"""
Module: models.base
Description: Base model for SQLAlchemy ORM
Author: Anderson H. Silva
Date: 2025-01-25
License: Proprietary - All rights reserved
"""
from datetime import datetime
from typing import Any
import uuid
from sqlalchemy import Column, DateTime, String
from sqlalchemy.ext.declarative import as_declarative, declared_attr
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func
class Base(DeclarativeBase):
"""Base class for all database models."""
pass
class BaseModel(Base):
"""
Base model with common fields for all tables.
Includes:
- UUID primary key
- Created/updated timestamps
- Common methods
"""
__abstract__ = True
# Use UUID for all primary keys
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
# Timestamps
created_at = Column(DateTime, nullable=False, server_default=func.now())
updated_at = Column(DateTime, nullable=False, server_default=func.now(), onupdate=func.now())
@declared_attr
def __tablename__(cls) -> str:
"""Generate table name from class name."""
# Convert CamelCase to snake_case
name = cls.__name__
return ''.join(['_' + c.lower() if c.isupper() else c for c in name]).lstrip('_')
def __repr__(self) -> str:
"""String representation."""
return f"<{self.__class__.__name__}(id={self.id})>"
def to_dict(self, include_sensitive: bool = False) -> dict:
"""
Convert model to dictionary.
Args:
include_sensitive: Include sensitive fields
Returns:
Dictionary representation
"""
# Default implementation - can be overridden
result = {}
for column in self.__table__.columns:
value = getattr(self, column.name)
if isinstance(value, datetime):
value = value.isoformat()
result[column.name] = value
return result
@classmethod
def from_dict(cls, data: dict) -> "BaseModel":
"""
Create instance from dictionary.
Args:
data: Dictionary data
Returns:
Model instance
"""
return cls(**data) |