File size: 1,809 Bytes
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a8e48
 
 
4a2ab42
 
 
 
 
 
 
58a8e48
 
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a8e48
 
 
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
import uuid

from sqlalchemy import (
    Boolean,
    Column,
    DateTime,
    ForeignKey,
    Index,
    Numeric,
    String,
    Text,
)
from sqlalchemy.orm import relationship

from core.database import Base, utc_now


class EAVEntity(Base):
    __tablename__ = "eav_entities"

    entity_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    entity_type = Column(String, index=True, nullable=False)
    created_at = Column(DateTime, default=utc_now)
    updated_at = Column(DateTime, default=utc_now, onupdate=utc_now)

    values = relationship(
        "EAVValue", back_populates="entity", cascade="all, delete-orphan"
    )


class EAVAttribute(Base):
    __tablename__ = "eav_attributes"

    attribute_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    attribute_name = Column(String, unique=True, nullable=False, index=True)
    data_type = Column(
        String, nullable=False
    )  # 'text', 'numeric', 'boolean', 'json', 'date'
    description = Column(Text)
    is_required = Column(Boolean, default=False)


class EAVValue(Base):
    __tablename__ = "eav_values"

    value_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    entity_id = Column(String, ForeignKey("eav_entities.entity_id"), index=True)
    attribute_id = Column(String, ForeignKey("eav_attributes.attribute_id"), index=True)

    value_text = Column(Text)
    value_numeric = Column(Numeric)
    value_boolean = Column(Boolean)
    value_json = Column(Text)  # Storing JSON as text string
    value_date = Column(DateTime)

    entity = relationship("EAVEntity", back_populates="values")
    attribute = relationship("EAVAttribute")

    __table_args__ = (
        Index("idx_eav_entity_attribute", "entity_id", "attribute_id", unique=True),
    )