RGS
RGS

Reputation: 4253

firebase work with the user nick name or the user id?

I'm new in firebase, I know in mysql we have a table users with a unique id (eg: 1, 2...).

If I have a table follow and I want to record who follows who, I add:

follow

1 3 // user_id 1 follows user_id 3
3 2
3 1

I'd like the best approuch in firebase real time database, I have a json users, with unique key too and username. And I have json follow:

follow:

CMypCAlwMXPeM8X07P0sKsO
 |
 --- randomkey
        |
        --- follow: UFPRK8GG4tMvCejEfqWiCE220Mv2

in other words, user CMypCAlwMXPeM8X07P0sKsO follow user UFPRK8GG4tMvCejEfqWiCE220Mv2.

In firebase, should I work with this unique users keys (like mysql) or should I use usernames? like:

josh
 |
 ... randomkey
        |
        --- follow: marie

thanks!

Upvotes: 0

Views: 69

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

To model followers between users in Firebase Realtime Database, I typically end up with three top-level lists:

users: {
  uid1: { ... },
  uid2: { ... },
  uid3: { ... }
},
followers: {
  uid1: {
    uid2: true
  },
  uid2: {
    uid3: true,
  },
  uid3: {
    uid1: true
  }
},
followees: {
  uid1: {
    uid3: true
  },
  uid2: {
    uid1: true,
  },
  uid3: {
    uid2: true
  }
}

So you're essentially splitting your single follow table into two top-level lists: one for each "direction" of the relationship.

Also see:

Upvotes: 1

Related Questions