Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore - relational data design approaches

I'm totally new to Firebase, and I'm trying to get my head round the best db model design for 'relational' data, both 1-1 and 1-many.

We are using the Firestore db (not the realtime db).

Say we have Projects which can contain many Users, and a User can be in multiple Projects

The UI needs to show a list of Users in a Project which shows things like email, firstname, lastname and department.

What is the best way to store the relationship?

  • An array of User ids in the Project document?
  • A map of Ids in the Project document?

Ive read the above approaches were recommended, but was that for realtime database? Firestore supports Sub Collections, which sound more appropriate...

  • A sub collection of Users in the Project document?
  • A separate collection mapping Project id to User id?
  • A Reference data type? I've read here https://firebase.google.com/docs/firestore/manage-data/data-types about Reference data type, which sounds like what I want, but I cant find any more on it!

If its just a map or array of Ids, how would you then retrieve the remaining data about the user? Would this have to sit in the application UI?

If its a sub collection of Users documents, is there any way to maintain data integrity? If a user changed their name, would the UI / a cloudFunction then have to update every entry of that users name in the Sub collections?

any help / pointers appreciated...

like image 519
Matt Bryson Avatar asked Nov 28 '17 13:11

Matt Bryson


People also ask

Is Firebase firestore a relational database?

Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections.

What type of database is Firebase firestore?

Both Realtime Database and Cloud Firestore are NoSQL Databases. Stores data as one large JSON tree. Simple data is very easy to store.

How do you model a one to many relationship in firestore?

One-to-many using Firestore All the Chat Room instances are part of a “ChatRooms” “Collection” Each Chat Room “Document” has a “Sub-Collection” to hold all the Chat Messages relevant to it, this way establishing a One-to-Many relationship.


1 Answers

The approach for modeling many-to-many relationships in Firestore is pretty much the same as it was in Firebase's Realtime Database, which I've answered here: Many to Many relationship in Firebase. The only difference is indeed that you can store the lookup list in a sub-collection of each project/user.

Looking up the linked item is also the same as before, it indeed requires loading them individually from the client. Such a client-side join is not nearly as slow as you may initially expect, so test it before assuming it can't possibly be fast enough.

Ensuring data integrity can be accomplished by performing batched writes or using transactions. These either completely succeed or completely fail.

like image 165
Frank van Puffelen Avatar answered Oct 20 '22 15:10

Frank van Puffelen