Gil
Gil

Reputation: 569

Firebase - proper way to structure the DB

I have an iOS app that is like a social network for music. In the app, users share "posts" about specific music "tracks". I want to know the best way to structure the DB in Firebase, considering that each "post" object references a single "track" object.

Also, when a user submits a new post, I need to check if a track already exists by querying the artist + song title - if the track does not exist, add a new track. If the track exists get the "track_id" to reference in the "post" object.

Upvotes: 0

Views: 212

Answers (2)

Elias L.
Elias L.

Reputation: 11

In this case, you will meet some troubles when you implement the track search features and search users whom follow a track. So generally, you need to fully load at least one table in your client app. I hope this could be a help for your later troubles. Please check the Salada framework on Github. You can use Relation.

Upvotes: 1

Jay
Jay

Reputation: 35658

The challenge here is performing an 'and' query in Firebase as well, that doesn't exist. So you have mush two pieces of data together to then do that query. Here's a structure

artists
   artist_0: Pink Floyd
   artist_1: Billy Thorpe
   artist_2: Led Zeppelin

tracks
   track_id_0: Stairway To Heaven
   track_id_1: Children Of The Sun
   track_id_2: Comfortably Numb

artists_tracks
   artist_0_track_id_2: true
   artist_1_track_id_1: true
   artist_2_track_id_0: true

posts
   post_id_0
      artist_track: artist_1_track_id_1
      post: Billy was one of the most creative musicians of modern times.
   post_id_1
      artist_track: artist_0_track_id_2
      post: The Floyd is the best band evah.

With this structure, if you know the artist and the track name, you can concatenate them and do a simple query in the artists_tracks node for .equalToValue(true) to see if it exists.

The posts in the posts node tie back to those specific artists and tracks.

In some cases you can glue your data together to perform and searches without the extra nodes... like this

stuff
  artist_track: Billy_Thorpe_Children_Of_The_Sun

However, because of the spaces in the names and the varying width of the text it won't work. So that leads to ensuring you include enough digits in the data to handle however many songs and artists so the length stays consistent.

artists_tracks
   artist_00000_track_id_00002: true

Now you can have 50,000 artists and 50,000 tracks.

Upvotes: 0

Related Questions