rea*_*lph 6 denormalization relationship firebase
我的Firebase中有两个项目:providers而且services,我正在尝试使用Firebase推荐的扁平化架构方法找出构建和建立关系的最佳方法.
我的数据看起来像这样:
{
"services" : {
"hip_replacement" : {
"title" : "Hip Replacement"
}
},
"providers" : {
"the_blue_hospital" : {
"title" : "The Blue Hospital"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想将这两个项目链接在一起,这样如果您要访问髋关节置换页面,蓝色医院会显示在它下面,如果您要访问蓝色医院页面,髋关节置换将显示在其下方.基本上是一种双向关系.
构建这样的东西最好的方法是什么?我在考虑以下几点:
{
"services": {
"hip_replacement": {
"title": "Hip Replacement",
"providers": {
"the_blue_hospital": true,
"the_red_hospital": true
}
},
...
},
"providers": {
"the_blue_hospital": {
"title": "The Blue Hospital",
},
"the_red_hospital": {...
},
"the_green_hospital": {...
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这个或更优雅的解决方案?任何帮助表示赞赏.
提前致谢!
Jam*_*mes 11
Firebase中联接数据的问题在于您以牺牲其他用户为代价来优化某些读取或更新用例.在上面的示例中,创建或删除服务和提供程序之间的关系需要对每个"表"进行两次单独的更新.这没有什么不妥,但这不是唯一的出路.
对于适度大小的数据集,您可以使用"连接表"将服务映射到提供程序,类似于在关系数据库世界中可能执行的操作.数据可能如下所示:
{
"services": {
"hip_replacement": {}
},
"providers": {
"the_blue_hospital": {...},
"the_red_hospital": {...},
"the_green_hospital": {...}
},
"serviceProviders": {
"-JqD5JX0RUDTXsu7Ok3R": {
"provider": "the_blue_hospital",
"service": "hip_replacement"
}
"-JqDoKfyJqPkQlCXDvFM": {
"provider": "the_green_hospital",
"service": "hip_replacement"
}
"-JbE7Ji_JRz2bHgBdMWQ": {
"provider": "the_blue_hospital",
"service": "hip_replacement"
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法有利有弊:
临
精读
你应该考虑:
| 归档时间: |
|
| 查看次数: |
9708 次 |
| 最近记录: |