我正在使用 Python Flask 开发一个带有 JSON API 的平台。在某些情况下,我需要加入三个表。如何连接具有一组 ID 的表给了我一些指导,但我需要一个解决方案。
假设我们有三个用于消息传递应用程序的表。
- 账户
- 对话
- 留言
- 留言阅读器
帐户表片段
{
"id": "account111",
"name": "John Doe",
},
对话表片段
{
"id": "conversation111",
"to": ["account111", "account222", "account333"], // accounts who are participating the conversation
"subject": "RethinkDB",
}
消息表片段
{
"id": "message111",
"text": "I love how RethinkDB does joins.",
"from": "account111", // accounts who is the author of the message
"conversation": "conversation111"
}
消息阅读器表片段
{
"id": "messagereader111",
"message": "message111",
"reader": "account111",
}
我的问题是“当我收到对 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",
}
],
},
],
},
],
}
任何指导或建议都会很棒。JavaScript 或 Python 代码会很棒。