class MessageLikes(BaseModel): # Properties # None # Foreign keys message_id = db.Column(db.Integer, db.ForeignKey('messages.id')) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # N:1 users = db.relationship("Users", back_populates="likes", foreign_keys=user_id) messages = db.relationship("Messages", back_populates="likes", foreign_keys=message_id) # 1:N # None # 1:1 # None # Indexes # None __tablename__ = 'messages_likes' # ========= # Methods === # ========= def __init__(self, **kwargs): super().__init__(**kwargs)
class RoomUsers(BaseModel): # Properties # None # Foreign keys room_id = db.Column(db.Integer, db.ForeignKey('rooms.id')) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # 1:N user = db.relationship("Users", back_populates="rooms", foreign_keys=user_id) room = db.relationship("Rooms", back_populates="users", foreign_keys=room_id) # N:1 # None # 1:1 # None # Indexes # None __tablename__ = 'room_users' # ========= # Methods === # ========= def __init__(self, **kwargs): super().__init__(**kwargs)
class Messages(UUIDBaseModel): # Properties message = db.Column(db.Text) # Foreign keys room_id = db.Column(db.Integer, db.ForeignKey('rooms.id')) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # N:1 user = db.relationship("Users", back_populates="messages", foreign_keys=user_id) room = db.relationship("Rooms", back_populates="messages", foreign_keys=room_id) # 1:N likes = db.relationship("MessageLikes", back_populates="messages") # 1:1 # None # Indexes message_index = Index('message_index', 'uuid') __tablename__ = 'messages' __table_args__ = (message_index, ) # ========= # Methods === # ========= def __init__(self, **kwargs): super().__init__(**kwargs) @hybrid_property def uuid_str(self) -> str: return str(self.uuid) @hybrid_property def like_list(self) -> List: if self.likes: likes: List = [] for like in self.likes: likes.append({ "user_name": like.users.name, "user_uuid": like.users.uuid_str }) return likes return []
class Users(UUIDBaseModel): # Properties name = db.Column(db.String) # Foreign keys # None # N:1 rooms = db.relationship("RoomUsers", back_populates="user") messages = db.relationship("Messages", back_populates="user") # 1:N likes = db.relationship("MessageLikes", back_populates="users") # 1:1 # None # Indexes user_index = Index('user_index', 'uuid') __tablename__ = 'users' __table_args__ = (user_index, ) # ========= # Methods === # ========= def __init__(self, **kwargs): super().__init__(**kwargs) @hybrid_property def uuid_str(self) -> str: return str(self.uuid) @hybrid_property def rooms_list(self) -> List: if self.rooms: room_list: List = [] for room in self.rooms: room_list.append({"name": room.name, "uuid": room.uuid_str}) return room_list return []
class Rooms(UUIDBaseModel): # Properties name = db.Column(db.String) # Foreign keys # None # N:1 users = db.relationship("RoomUsers", back_populates="room") # 1:N messages = db.relationship("Messages", back_populates="room") # 1:1 # None # Indexes room_index = Index('room_index', 'uuid') __tablename__ = 'rooms' __table_args__ = (room_index, ) # ========= # Methods === # ========= def __init__(self, **kwargs): super().__init__(**kwargs) @hybrid_property def uuid_str(self) -> str: return str(self.uuid) @hybrid_property def user_list(self) -> List: if self.users: users_list: List = [] for user_room in self.users: users_list.append({ "name": user_room.user.name, "user_uuid": user_room.user.uuid_str, }) return users_list return [] @hybrid_property def message_list(self) -> List: if self.messages: message_list: List = [] for message in self.messages: message_list.append({ "message": message.message, "user_uuid": message.user.uuid_str, "user_name": message.user.name, "likes": message.like_list }) return message_list return []