Question

I am developing a platform with JSON API using Python Flask. In some cases I need to join three tables. How to join tables with a array of IDs gave me some guidance but I need a solution beyond it.

Let's assume we have three tables for a messaging app.

  1. Accounts
  2. Conversations
  3. Messages
  4. Message Readers

Accounts table snippet

{
    "id": "account111",
    "name": "John Doe",
},

Conversations table snippet

{
    "id": "conversation111",
    "to": ["account111", "account222", "account333"], // accounts who are participating the conversation
    "subject": "RethinkDB",
}

Messages table snippet

{
    "id": "message111",
    "text": "I love how RethinkDB does joins.",
    "from": "account111", // accounts who is the author of the message
    "conversation": "conversation111"
}

Message Readers table snippet

{
    "id": "messagereader111",
    "message": "message111",
    "reader": "account111",
}

My question is "What's the magic query to get the document below when I receive a get request on an account document with id="account111"?"

{
    "id": "account111",
    "name": John Doe,
    "conversations": [            // 2) Join account table with conversations
        {
           "id": "conversation111",
           "name": "RethinkDB",
           "to": [                     // 3) Join conversations table with accounts
               {
                    "id": "account111",
                    "name": "John Doe",
               },
               {
                    "id": "account222",
                    "name": "Bobby Zoya",
               },
               {
                    "id": "account333",
                    "name": "Maya Bee",
               },
           ]
           "messages": [        // 4) Join conversations with messages
               {
                   "id": "message111",
                   "text": "I love how RethinkDB does joins.",
                   "from": {        // 5) Join messages with accounts
                        "id": "account111",
                        "name": "John Doe",
                   },
                   "message_readers": [
                       {
                           "name": "John Doe",
                           "id": "account111",
                       }
                   ],
               },
           ],
        },
    ],
}

Any guidance or advice would be fantastic. JavaScript or Python code would be awesome.

Was it helpful?

Solution

I had a hard time understanding what you want (you have multiple documents with the id 111), but I think this is the query you are looking for

Python query:

r.table("accounts").map(lambda account: 
    account.merge({
        "conversations": r.table("conversations").filter(lambda conversation: 
            conversation["to"].contains(account["id"])).coerce_to("array").map(lambda conversation:
            conversation.merge({
                "to": conversation["to"].map(lambda account: 
                    r.table("accounts").get(account)).pluck(["id", "name",]).coerce_to("array"),
                "messages": r.table("messages").filter(lambda message:
                    message["conversation"] == conversation["id"]).coerce_to("array").map(lambda message:
                    message.merge({
                        "from": r.table("accounts").get(message["from"]).pluck(["id", "name",]),
                        "readers": r.table("message_readers").filter(lambda message_reader:
                            message["id"] == message_reader["message"]).coerce_to("array").order_by(r.desc("created_on")),
                    })).order_by(r.desc("created_on"))
            })).order_by(r.desc("modified_on"))
    })).order_by("id").run(db_connection)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top