【发布时间】:2018-07-17 00:13:10
【问题描述】:
我正在使用 firebase 制作博客应用。
我想知道数据结构的最佳实践。
据我所知,有两种情况。 (我正在使用本机反应)
案例一:
posts
-postID
-title,content,author(userID),createdDate,favoriteCount
favorites
-userID
-favoriteList
-postID(onlyID)
-postID(onlyID)
在这种情况下,例如,当我们需要获取最喜欢的帖子时。
firebase.firestore().collection(`favorites/${userID}/favoriteList`)
.get()
.then((snapshot) => {
snapshot.forEach((favorite) => {
firebase.firestore().collection(`favorites/`).doc(`${favorite.id}`)
.get()
.then((post) => {
myPostList.push(post.data())
});
});
在这种情况下,我们无法通过createdDate 订购最喜欢的帖子。所以,需要对客户端进行排序。即使是这样,我们也不使用limit()函数。
案例2:
posts
-postID
-title,content,author(userID),createdDate,favoriteCount
favorites
-userID
-favoriteList
-postID
-title,content,author(userID),createdDate,favoriteCount
-postID
-title,content,author(userID),createdDate,favoriteCount
firebase.firestore().collection(`favorites/${userID}/favoriteList`).orderBy('createdDate','desc').limit(30)
.get()
.then((snapshot) => {
snapshot.forEach((post) => {
myPostList.push(post.data())
});
});
在这种情况下,当收藏的帖子被作者修改时, 我们必须更新所有喜欢的帖子。 (例如,如果 100 位用户将帖子收藏为收藏,我们必须更新到 100 条数据。)
(而且我不确定我们是否可以通过事务增加 favoritecount,完全相同。)
我认为如果我们使用firebase.batch(),我们可以管理它。但我认为它似乎效率低下。
看来这两种方式都不完美。你知道这个案例的最佳实践吗?
【问题讨论】:
-
您应该以最适合您要执行的查询的方式构建数据。
-
@DougStevenson 谢谢你的回应。嗯...我发现这是个案。谢谢
标签: javascript firebase react-native nosql google-cloud-firestore