********
Database
********

MongoDB
=======

SFChat database has one collection "chats": ::

  {
      _id: ObjectId("507f191e810c19729de860ea"),
      status: "ready",
      user_tokens: [
         ObjectId("507f191e810c19729de860eb"),
         ObjectId("507f1f77bcf86cd799439011")
      ],
      messages: [{
         _id: ObjectId("507f191e810c19729de860ed"),
         user_token: ObjectId("507f191e810c19729de860eb"),
         msg: "Hi, how are you?",
         system: false
     }],
     long_polling: [{
        _id: ObjectId("507f191e810c19729de860ed"),
        user_token: ObjectId("507f191e810c19729de860eb"),
        created: ISODate("2012-04-03T02:05:06Z")
     }],
     created: ISODate("2012-04-03T02:05:06Z")
  };


.. raw:: latex

    \newpage
    
Table below displays schema for "chats" collection:

.. tabularcolumns:: |p{4cm}|p{2cm}|p{5cm}|p{3cm}|
.. list-table::
    
    * - **Name**
      - **Type**
      - **Description**
      - **Comment**

    * - chats._id
      - ObjectId
      - Unique key. Automatically generated by MongoDB
      -  
        
    * - chats.status
      - "draft", "ready", "closed"
      - Chat status. It should be one of the list.
      -  
        
    * - chats.use_tokens
      - Array
      - List of "user tokens". Generated by ObjectId().
      - It is used for authentication process.

    * - chats.messages
      - Array
      - Messages container that include income messages
      -  
        
    * - chats.messages._id
      - ObjectId
      - "message token". Generated by ObjectId().
      - 
        
    * - chats.messages.user_token
      - ObjectId
      - "user token"
      - For whom that message is addressed

    * - chats.messages.msg
      - String[140]
      - User’s message
      -  
        
    * - chats.messages.system
      - true, false
      - Indicate is that message system or not
      - 
        
    * - long_polling
      - Array
      - Long polling process container
      -  
      
    * - long_polling._id
      - ObjectId
      - Long polling process identifier
      -  
        
    * - long_polling.user_token
      - ObjectId
      - "user token"
      - 
        
    * - long_polling.created
      - String[UTC]
      - Creation date in UTC format
      - Used to determine close tab or browser

    * - chats.created
      - String[UTC]
      - Creation date in UTC format
      - It’s used to identify old chats and clear them
