0
votes

I am trying to build a structure that allows users to access only their own posts.

I have two possible ways in mind, and I'm not sure either of them is best practice. My end goal is to allow a user to read and write to all of their own posts only.

Approach A - Posts/Users (Not sure this can work from a security standpoint)

{
  "posts" : {
    "001" : {
      "text" : "note 1",
      "userID" : "user1"
    },
    "002" : {
      "text" : "note 2",
      "userID" : "user1"
    },
    "003" : {
      "text" : "note 3",
      "userID" : "user2"
    }
  }
}

Approach B - Users/Posts (Not as flat as Approach A)

{
  "users" : {
    "user1" : {
      "posts" : {
        "001" : {
        "text" : "note 1",
        },
        "002" : {
          "text" : "note 2",
        },
      }
    },
    "user2" : {
      "posts" : {
        "003" : {
        "text" : "note 3",
        },
    },
  }
}

I'm trying to follow the guidelines to keep data flat. In reality the post items will have more than just text, but not extend more than 2 levels deep. Thanks for any advice you can give!

1
NoSQL data models depend on the use-cases of your app. There is no n'th normal form to strive for, just a data model that allows you to efficiently implement your use-cases. Sometimes it'll be approach A, sometimes approach B. But more frequently it will be a combination of both, duplicating the data upon write to allow efficient reading for all use-cases. I recommend reading NoSQL data modeling and watching Firebase for SQL developers. - Frank van Puffelen

1 Answers

0
votes

I would have

{
"Users": {
    "user1uniqueRef": {
        "postIDUniqueRef1": {
            "text" : "Here is someText"
            "imageURL" : "https:// etc etc etc"
        "postIDUniqueRef2": {
            "text" : "Here is someText"
            "imageURL" : "https:// etc etc etc"
    "user2uniqueRef": {
       "postIDUniqueRef1": {
           "text" : "Here is someText"
           "imageURL" : "https:// etc etc etc"
        }
    }
}