Simple Firebase Data Structure

258 views
Skip to first unread message

John Healey

unread,
May 28, 2017, 10:04:22 PM5/28/17
to Firebase Google Group
Hello. I've read a few things regarding the firebase data structure best practises but being new to this kind of thing (I'm a front end dev) I'm still not quite getting it.

I have a simple app which categorises movies. You can see recent movies, movies by actor, category, etc. So far I've built it in a flat (maybe?) kind of way:


    movies: {
        movieName: {
            date: 'xxx',
            actors: {
                0: 'actor'
            },
            category: 'category'
        },
        otherMovieName: {
            date: 'xxx',
            actors: {
                0: 'other actor'
            },
            category: 'category'
        }
    },
    actors: {
        actor: {
            name: 'actor',
            movies: {
                movieName: {
                    date: 'xxx',
                    actors: {
                        0: 'actor'
                    },
                    category: 'category'
                }
            }
        },
        otherActor: {
            name: 'other actor',
            movies: {
                otherMovieName: {
                    date: 'xxx',
                    actors: {
                        0: 'other actor'
                    },
                    category: 'category'
                }
            }
        }
    },
    categories: {
        category: {
            name: 'category',
            movies: {
                movieName: {
                    date: 'xxx',
                    actors: {
                        0: 'actor'
                    },
                    category: 'category'
                },
                otherMovieName: {
                    date: 'xxx',
                    actors: {
                        0: 'other actor'
                    },
                    category: 'category'
                }
            }
        }
    }

And that's how I thought Firebase recommended doing it, so that you can access things like: 'cateogories/category/movies/movieName' and it's just one call.

However I've been reading some more and I'm not sure whether a relational (?) kind of thing might be better? Along the lines of:

    movies: {
        movieName: {
            date: 'xxx',
            actors: {
                0: 'actor'
            },
            category: 'category'
        },
        otherMovieName: {
            date: 'xxx',
            actors: {
                0: 'other actor'
            },
            category: 'category'
        }
    },
    actors: {
        actor: {
            name: 'actor',
            movies: {
                0: movieName
            }
        },
        otherActor: {
            name: 'other actor',
            movies: {
                0: otherMovieName
            }
        }
    },
    categories: {
        category: {
            name: 'category',
            movies: {
                0: movieName,
                1: otherMovieName
            }
        }
    }

Which would mean getting the category, then querying the movies based on the key. That's a lot more calls isn't it?

It's not a very big app, but could scale. No writing to db for users except upvotes per movie.

Apologies if obvious, just want to make sure before I put data in...

Thanks.


Kato Richardson

unread,
May 31, 2017, 4:25:31 PM5/31/17
to Firebase Google Group

Hi John,

The most scalable rule of thumb here is to work hard on writes, so that things are easy to read later. In other words, structure the data how it will be read back.

Note that, for your indices, you’ll get into trouble trying to store arrays (e.g. {movies: {0: 'movie1', 1: 'movie2'}}) and you’d probably be better off to store the movies by key with a filler value (e.g. {movies: {movie1: true, movie2: true}}).

But whether you flatten those or not probably just depends on how you’ll read them back. For example, if you want to be able to iterate users without downloading all the movie data, you probably need a more relational format. But you don’t necessarily need that for querying, since you can write queries like firebase.database().ref().child('movies').orderByChild('actors/billpullman').equalTo(true) to fetch movies with a certain actor in them.

Note also that something like an Algolia integration is really what you want for a searchable database. The realtime database is probably usable here, but probably not the best format for search-based data like this (it would be a great tool for synchronizing realtime aspects like comments, voting, et al).

☼, Kato


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/174f1d90-6122-4dc2-9543-1fe5d66d3de5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages