Sample Header Ad - 728x90

How to create a flexible table schema for storing messages from different chats?

8 votes
3 answers
41939 views
Help please solve the following situation: There are two kinds of API where message history is stored, it's **Zopim ** and **Chat2Desc** (**import into Postman **). While these two but can then others appear. And my database with the users table: Table users id , email, phone, ... In **Zopim**, users are identified via **email**, and in **Chat2Desc** through the **phone**. For me, these two fields are important, whatever the chat was and how many would not be. That is, if I receive an email or a user's phone in messages, I make a request to my database (table users) to identify my user. And in principle, even the structure of chat rooms is not important.I'll somehow choose them.And here's how to properly save them, so much so that I had one structure for everyone. And that's what I came up with (Something I don't like, especially the chat_clients table): enter image description here **Explanation:** **Table chats (Data for chat):** 1. client_id - indicates the id of the chat_clients table 2. duration - the duration of the chat (120 seconds) 3. system_type - stores the name of the chat (**Zopim, Chat2Desc**, ...) 4. created_at - creation date **Table chat_clients (information about users who were in the chat):** 1. is_agent - 0 | 1: 1 => my user, 0 => not my 2. user_id - is the user id. Contains either id from the users table or empty. 3. assigned_data - those initials under which users were in the chat 4. bean_module - it does not matter (information about my user) 5. unique_col - There will either be a email (from Zopim) or a phone (from Chat2Desc, Or I think to store the id of the users table). Will guarantee the uniqueness of the values. The users_id + unique_col bunch is unique (UNIQUE KEY user_id_unique_col_UQ (user_id, unique_col)) **Table chat_messages:** 1. text - the text of the message. 2. client_id - indicates the id of the chat_clients table 3. chat_id - indicates the id of the table chats 4. file_id - indicates the id of the chat_files table 5. transport - the value will be for Chat2Desc (**Viber, WhatsApp,** ...), for Zopim, so it's not empty, Zopim Table chat_files Information about the transferred files in the chat. Analogical tables may be not to store additional information. In the future I'm going to select the history of correspondence for each user. **Q:** How can I improve the structure of tables to get more flexibility ? Thank you in advance.
Asked by Vanya Avchyan (141 rep)
Dec 1, 2017, 02:00 PM
Last activity: Nov 11, 2019, 12:13 PM