用于社交媒体的Cloud Firestore数据库结构

时间:2018-02-09 19:04:38

标签: firebase firebase-realtime-database nosql google-cloud-firestore

我正在尝试为简单的社交媒体概念创建一个简单的Cloud Firestore数据库结构。

每个用户最多可以有一个帖子(“最喜欢的”帖子除外),可以随时更新。用户的“最喜欢”版本的帖子会保存为单独的帖子,只要用户在其当前版本的帖子上收到的热门数量超过其最喜欢的版本,就可以更新。

我提出了一个结构,但我对NoSQL数据库缺乏经验。这个结构看起来会支持我的系统吗?这些明显的问题也会阻止重要的查询吗?

USERS (collection)
    DocumentID - userId gathered from Authentication uid
        firstName: String
        lastName:  String
        username:  String
        birthdate: Timestamp
        accountCreationDate: Timestamp
        post: postId (when user is created, create a default post document for them since there is only one post per user)
        bestPost:    postId
        FRIENDS (subcollection)
            DocumentID - userId of the friend
                username:  String

POSTS (collection)
    DocumentID - postId randomly generated
        authorUserId:   String
        authorUsername: String
        text:  String   
        imagePath: String
        location:  String
        likesCount: Number
        LIKES (subcollection)
            DocumentID - userID of the liker
                username:  String

朋友子集合存储朋友用户名,因此通过用户名创建该用户的朋友列表很容易。我假设我可以通过将朋友的documentID与用户的documentID相匹配来获取该用户朋友的更多信息。

同样喜欢子集合

1 个答案:

答案 0 :(得分:2)

结构看起来还不错。关于在Cloud Firestore中执行此操作,我会提供一些意见。

更新likesCount

如果帖子喜欢的可能不常见(每秒少于一个),您可以创建一个云功能(由likes子集合.onCreate触发),更新{{1使用事务。确保用户无法在规则范围内修改likesCount

允许用户编辑最喜欢的帖子

您需要构建规则来强制执行此操作。这可能有点复杂,可能需要一些战略思考(除非你已经有了解决方案)。

相关问题