如何在Firebase上展平数据 [英] How to flatten data on Firebase
问题描述
我正在开发一个应用程序,用户可以在其中发布类别内的产品.我正在使用Firebase,并且尝试将数据库中的数据展平,但是我不确定帖子与类别的连接.
I am developing an application where users post products inside categories. I am using firebase and I am trying to flatten the data on the database but I am not sure about the connections of the posts with the categories.
这是数据库设计:
"categories": {
"category1": {
"id": "1",
"name": "computers"
},
"category2": {
"id": "2",
"name": "Furniture"
},
"category3: {
"id": "3",
"names": "Books"
},
}
"users":{
"email": "test@gmail.com"
"posts": {
"post1": {
"id": "1"
"title": "Selling my guitar",
"categories": {
"category1": "true"
}
}
}
也许是这样
"categories": {
"category1": {
"id": "1",
"name": "computers",
"posts": {
"post1": true
}
},
"category2": {
"id": "2",
"name": "Furniture"
},
"category3": {
"id": "3",
"names": "Books"
},
}
"users": {
"userid1": {
"email": "test@gmail.com" ,
"posts": {
"post1": "true"
}
}
},
"posts": {
"post1": {
"id": "1",
"title": "Selling my guitar",
"users": {
"user1" : "true"
},
"categories": {
"category1": "true"
}
}
}
因此,据我了解,我知道帖子应该是一个单独的实体,但是我不确定如何将它们联系起来.
So from what I learnt I know posts should be a separate entity but then I am not sure how to connect them.
让我知道如何改进该数据库.
Let me know how can I improve this database.
推荐答案
平移数据意味着您不混合实体类型.如果您查看当前的/users
节点,现在将获得有关该用户(其电子邮件地址)和节点列表的信息.这是两种不同的实体类型,通常应放在单独的顶层列表中.
Flattening data means that you don't mix entity types. If you look at your current /users
node, you now information about the user there (their email address) and a list of nodes. Those are two distinct entity types and should normally be in separate top-level lists.
"categories": {
"category1": {
"id": "1",
"name": "computers"
},
"category2": {
"id": "2",
"name": "Furniture"
},
"category3: {
"id": "3",
"names": "Books"
},
}
"users":{
"userid1": {
"email": "test@gmail.com"
}
},
"posts": {
"post1": {
"id": "1"
"title": "Selling my guitar",
"categories": {
"category1": "true"
}
}
}
这篇关于如何在Firebase上展平数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!