48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Index
|
|
from sqlalchemy.dialects.postgresql import ENUM, ARRAY
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
import enum
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class WorldVisibility(enum.Enum):
|
|
PUBLIC = "public"
|
|
PRIVATE = "private"
|
|
FRIENDS_ONLY = "friends_only"
|
|
|
|
|
|
class World(Base):
|
|
__tablename__ = "worlds"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String(255), nullable=False)
|
|
description = Column(Text, nullable=True)
|
|
owner_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
visibility = Column(
|
|
ENUM(
|
|
WorldVisibility,
|
|
name="world_visibility",
|
|
create_type=False,
|
|
values_callable=lambda enum_cls: [e.value for e in enum_cls],
|
|
),
|
|
default=WorldVisibility.PUBLIC,
|
|
)
|
|
category = Column(String(100), nullable=True, index=True)
|
|
tags = Column(ARRAY(String), nullable=True, server_default="{}")
|
|
play_count = Column(Integer, nullable=False, server_default="0", index=True)
|
|
last_played_at = Column(DateTime(timezone=True), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
__table_args__ = (
|
|
Index("ix_worlds_tags", "tags", postgresql_using="gin"),
|
|
)
|
|
|
|
owner = relationship("User", back_populates="worlds")
|
|
assets = relationship("Asset", back_populates="world", cascade="all, delete-orphan")
|
|
rooms = relationship("WorldRoom", back_populates="world", cascade="all, delete-orphan")
|
|
visits = relationship("WorldVisit", back_populates="world", cascade="all, delete-orphan")
|
|
|