Best way to structure my firebase database

醉酒当歌 提交于 2021-02-07 10:52:42

问题


I'm working on a project where users can post things. But, I'm wondering if my firebase database structure is efficient. Below is how my database looks like so far. I have posts child contains all the post that users will post. and each user will be able to track their own posts by having posts child in uid. Is there a better way of structuring my data? or am I good to go? Any advice would be appreciated!

{
 "posts" : {
     "-KVRT-4z1AUoztWnF-pe" : {
     "caption" : "",
     "likes" : 0,
     "pictureUrl" : "https://firebasestorage.googleapis.com/v0/b/cloub-4fdbd.appspot.com/o/users%2FufTgaqudXeUciW5bGgCSfoTRUw92%2F208222E1-8E20-42A0-9EEF-8AF34F523878.png?alt=media&token=9ec5301e-d913-44ee-81d0-e0ec117017de",
     "timestamp" : 1477946376629,
     "writer" : "ufTgaqudXeUciW5bGgCSfoTRUw92"
     }
 }, 
 "users" : {
     "ufTgaqudXeUciW5bGgCSfoTRUw92" : {
         "email" : "Test1@gmail.com",
         "posts" : {
             "-KVRT-4z1AUoztWnF-pe" : {
                 "timestamp" : 1477946376677
             }
         },
         "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/cloub-4fdbd.appspot.com/o/profile_images%2F364DDC66-BDDB-41A4-969E-397A79ECEA3D.png?alt=media&token=c135d337-a139-475c-b7a4-d289555b94ca",
         "username" : "Test1"
      }
   }
}

回答1:


Working with NoSql Data , your need to take care of few things:-

  • Avoid Nesting The Data Too Deep
  • Flatten your dataStructure as possible
  • Prefer Dictionaries
  • Security Rules [Firebase]

Try this structure:-

users:{
  userID1: {..//Users info..
             posts:{
                 postID1: true,
                 postID2: true,
                 postID3: true,  
                    }
                 },
  userID2: {..//Users info..},
  userID3: {..//Users info..},
  userID4: {..//Users info..},
    },
posts: {
 userID1 :{
  postID1: {..//POST CONTENT },
  postID2: {..//POST CONTENT },
  postID3: {..//POST CONTENT },
   }

  }



回答2:


Keep the data flat and shallow. Store data elsewhere in the tree rather than nest a branch of data under a node that is simply related, duplicating the data if that helps to keep the tree shallow.

The goal is to have fast requests that return only data you need. Consider that every time the tree changes and the client-side listener fires the node and all its children are communicated to the client. Duplication of data across the tree facilitates quick requests with minimal data.

This process of flattening the data is known as "denormalization" and this section of the Firebase Doc does a nice job of providing guidance:

https://firebase.google.com/docs/database/android/structure-data

In your example above I see posts metadata nested under "users", a nested list that grows. Every time something changes under "users" the listener will fire to update the client and all of this data will be transmitted in each response. You could instead consider to fetch the posts data from the "posts" node based on the writer's uuid.



来源:https://stackoverflow.com/questions/40351162/best-way-to-structure-my-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!