Designing Data Models for Firebase

Abdul Malik
Jul 10, 2020 · 4 min read
Firebase Firestore

In this article we will be exploring the art of designing data models for Firebase to make it super easy to store and retrieve them.

When we design a data model in our application for any entity say, a person.

We build a class with its

  • Properties
  • A constructor to initialize the object
  • Its methods (if needed)

Lets look at an example below :

class Person{
int id;
String name;
String phoneNumber;
String address;

// I used named arguments for the constructor available in Dart

Okay, so that was a Data Model, but how does Firebase fit into this?

Storing data in Firestore

Cloud Firestore supports a variety of data types for values: boolean, number, string, geo point, binary blob, and timestamp. ~ Cloud Firestore Data model | Firebase

Great ! So Firestore supports a wide variety of data types, so all we need to do is to convert our object (of the Data Model) into something we can store in a Firestore document. The easiest Data Structure would be Maps.

Instead of creating this Map manually everytime we want to store the object, why not just create a method for it in the class itself?

Meme reference : OOP 100 [ignore this if you’re not into memes boomer]

The method for that would look something like this.

class Person{
int id;
String name;
String phoneNumber;
String address;

Awesome ! now we can convert our “person” object to a Map by simply calling this method before sending the data.

lets see the code.

Lets say there is an object of the Person class called personObject.

Store the person object in a document, whose ID is same as the Person ID [Best Practice]

Firestore

There you go, Stored in Firestore in a single neat line of code. (Spaces were added for readability, all of them are chained methods in a single line)

I want to take a second to explain why we used the personObject’s id as the document ID.

While fetching the document from Firestore, we don’t need to ask for all the documents in that collection and than pick ours, We can directly make a query or ask Firestore to return a document with this particular ID. This decreases your Firestore reads, and is helpful if your application is going to scale and have lots of users, since you have a cap of 50k reads per day for free. After that you pay, and trust me i’ve hit that limit lots of times by writing stupid queries.

Our Firestore Document

Retrieving data from Firestore

Ok, now to the part of retrieving data from Firebase.

When you receive a Document from Firestore, it is a Map from which you have to extract data and create an existing object to use in your application.

For that we will include another Constructor called “FromMap()” to create the object.

Okay, so Dart has Named Constructors but i’m sure any other OOP language would have parameterized constructors so you can use them. This is how our data model will look finally.

class Person{
int id;
String name;
String phoneNumber;
String address;

Now we’re ready to receive data from Firestore and convert it into our objects.

Lets see the code.

// Get the document with the ID of our personObject 

These methods seem trivial, but when i was a beginner i never really understood their importance and their contribution to clean code principles. I use to manually do all of this in a function, which is definitely not a good practice as a developer.

Data models are an integral part of an Application and they should be flawless to ensure you focus on the main parts of the application instead of facing small issues like this issue i solved on StackOverflow a few minutes ago, which inspired me to create this article.

I am a Software Developer who is obsessed with producing clean and maintainable code following well-known Software Architectures. I write about problems i have faced myself and how i improved overtime. If you would like to see more such articles and learn about best practices, make sure to follow me.

Till next time, folks ! astalavista !

The Startup

Get smarter at building your thing. Join The Startup’s +793K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Abdul Malik

Written by

Love converting thoughts into code.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Abdul Malik

Written by

Love converting thoughts into code.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store