Adr*_*ith 11 tree schema firebase google-cloud-firestore
我正在尝试将Reddit/HackerNews样式的注释树作为项目的一部分来实现,并且我正在尝试将Firestore作为数据库解决方案.但是,我不确定通过文档正确的设计阅读.在SQL数据库中,我会使用数字键,如:
0
1.0
1.1
1.1.1
0.0
Run Code Online (Sandbox Code Playgroud)
代表我的树.但是,像这样的数字键似乎是Firebase反模式.另一条路线是在json中使用实际树,其中一个帖子表示如下:
{
uid: 'A0000',
content: 'foo',
children: [
{uid:..., content:..., children: []}]
}
Run Code Online (Sandbox Code Playgroud)
但据说在Firestore中,深树很糟糕.据我所知,深树不好的原因是你必须取出整个东西,但就我而言,我不确定这是不是一个问题.提取帖子的客户端将获取根内容节点和前20个子树.这可能是一个相当大的提取,但不是疯狂的.
有谁知道实现这种结构的良好标准方法?
额外:以下是客户端处理结构后应该看起来更详细的表达方式.
{
uid: 0,
title: 'Check out this cat!',
body: 'It\'s pretty cute! This **text** is [markdown](link), so it can have ' +
'links and *stuff*. Yay!',
poster: {
uid: 0,
name: 'VivaLaPanda',
aviUrl: 'badlink',
},
posted: '2018-03-28',
children: [{
uid: 0,
body: 'This is a comment, it\'s angry!',
poster: {
uid: 0,
name: 'VivaLaPanda',
aviUrl: 'badlink',
},
posted: '2018-03-20',
children: [{
uid: 0,
body: 'This is a comment, it\'s neutral!',
poster: {
uid: 0,
name: 'Steve',
aviUrl: 'badlink',
},
posted: '2018-03-20',
children: [{
uid: 0,
body: 'This is a comment, it\'s neutral!',
poster: {
uid: 0,
name: 'Craig',
aviUrl: 'badlink',
},
posted: '2018-04-10',
children: []
}, ]
}, ]
},
{
uid: 0,
body: 'This is a comment, it\'s happy!',
poster: {
uid: 0,
name: 'Craig',
aviUrl: 'badlink',
},
posted: '2018-03-28',
children: []
},
]
};Run Code Online (Sandbox Code Playgroud)
编辑:
虽然我已将此作为回答,因为是一个答案,我仍然看到的东西更优雅/有效的真正的兴趣.
EDIT2:
对于后代:我最终决定任何Firebase解决方案都是无可救药的,并且只使用了DGraph来获取数据,Firebase坐在前面用于Auth.
如果有很多人互相评论,您的孩子的方法可能会非常混乱。更好的方法是每个评论的以下结构:
// single comment
postUid // <- random generated by firebase
{
postedBy: userUid
postedTime: timestamp
postIsChildOfUid: postUid // <- reference to an other post (optional if the comment didn't respond to another comment(top-level comment))
}
Run Code Online (Sandbox Code Playgroud)
这甚至根本不需要嵌套:)。您现在可以使用这种方法轻松生成评论树,但这必须是客户端。但这应该很容易!
这很难,因为您拥有的结构是自然递归的。显而易见的选项是每条评论都是集合中的一个新文档,并且每条评论都作为单个文档进行回复。
每个评论作为一个新文档可以像这样工作。每条评论都有一个“postId”属性,用于指示它属于哪个帖子。一些评论,即对其他评论的回复,有一个“replyToId”。结合这两个属性,您的客户端应用程序可以:
但这里的逻辑显然很复杂。
| 归档时间: |
|
| 查看次数: |
973 次 |
| 最近记录: |