Creating a Schema for Firebase Cloud Firestore

Creating a Schema for Firebase Cloud Firestore

Structuring a schemaless database

Firebase is Google's mobile platform that helps you quickly develop high-quality apps and grow your business.

Prerequisites

  • Firebase CLI
  • Firebase Functions
  • TypeScript

Cloud Firestore is a subset of firebase which provides a remote database with real-time updates. The special thing about firestore is that it is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps - at a global scale. That means that query execution times are a lot faster and lighter. It also means that you can't define a schema for your documents(tables) and your database can take any structure.

For the sake of proper models, you usually want your data to follow the same format. To do that from the back end means you'll have to enter the data in the same structure for each new entry. This can get tiring if you have a large data structure and a lot of fields to input. You have to keep entering the data with the right keys and with the correct spelling for each new entry.

This was the problem I faced when starting out. In this article, I'll introduce a way to keep your data structure for new entries.

To achieve this, we would use another subset of firebase called firebase functions. This is where you can write code to run the main logic of your backend. I would not go into the setup and deployment of firebase functions in this article

In this article, we would be writing the cloud functions in Typescript. Once you've set up the cloud functions with Typescript, we'll jump right into the code.

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();

export const addProjectSchema = functions.firestore
    .document("/collection/{id}")
    .onCreate((snapShot, context) => {
      return admin
          .firestore()
          .collection("collection")
          .doc(context.params.id)
          .set({
            name: "_",
            email: "_",
            gender: "_",
            age: 0,
            title: context.params.id,
          });
    });

Here we are importing firebase functions and firebase admin to use functions to perform admin-level writes on our firestore. Next, we define a function to be executed on the onCreate event of a document ("{id}") added to a collection ("collection"). In the code block, we set the schema of the document (where "context.params.id" is equal to the wildcard id "{id}" of any document created).

And that's it, it's as simple as that. The next time you create a document in that collection, you'll have your document structured with the set placeholders (notice that I didn't use empty strings as placeholders because firestore would consider them as null).

download.png

Firebase is definitely an awesome platform. As a mobile developer with no experience in back-end development, I totally fell in love with firebase. I encourage you to check out the awesome germs firebase has at firebase docs