Atu
Atu

Reputation: 937

Keeping track of fan-out relationships in Firebase Database

How do you keep track of the fan-out paths?
I'm not sure if I should call it a relationship. Anyway...
I'll take the following structure as an example. Say we have some form of social app. In which every user adds their friend and gives nicknames.

  "friends": {
    "user2": {
      "user1": {
        "nickname": "wife",
        "uid": "user1"
      }
    },
    "user3": {
      "user1": {
        "nickname": "bff",
        "uid": "user1"
      }
    }
  }
  "users": {
    "user1": {
        "name": "Rubia Castangia",
        "gender": "Female",
        "age": 22
    },
    "user2": {
        "name": "Roxy Brightling",
        "gender": "Male",
        "age": 23
    },
    "user3":{
        "name": "Carole Delgardillo",
        "gender": "Female",
        "age": 35
    }
  }

Let's say we add timeline & followers feature to the app with the following structure

"timeline": {
    "user2": {
        "post1234": {
            "text": "Feeling bored",
            "author_nick": "wife",
            "uid": "user1"
        }
    },
    "user3": {
        "post1235": {
            "text": "Feeling bored",
            "author_nickname": "bff",
            "uid": "user1"
        }
    }
},
"followers": {
    "user1": {
        "user2": true,
        "user3": true
    }
}

So after that we add another feature.... and it goes on.
So the relationships are e.g: /timeline/userId/postId/author_nick must be updated when user2 updates the nickname for friendId here: /friends/userId/friendId/nickname

As the app becomes more complex, I tend to forget which paths must I fan-out. Is there any specific tools are you people using to keep track of the relations between paths to fan-out?

Upvotes: 0

Views: 96

Answers (1)

Itai Hanski
Itai Hanski

Reputation: 8700

No tools are needed - just model your database structure according to your needs and keep it denormalized and as flat as possible.

How about something like this:

users
  user1
    *user_data*
  user2
    *user_data*
    following
      user1: true
  user3
    *user_data*

nicknames
  user2
    user1: wife 
    user4: friendo
  user3
    user1: bff 

posts
   user1
     post1
       text: "bla"

And roughly this is what you can do:

  • user2 logs in
  • fetch the user object
  • fetch the nickname map for that user
  • for every person in the "following" map: fetch that user's posts
  • When displaying a post content, search for a nickname mapping, otherwise display the username

Simple enough?

Upvotes: 1

Related Questions