Setting up Firebase Cloud Firestore for a Node.js Server
This article is the eleventh part of Week of Web(#WOW). If you don’t know what #WOW is here’s a card to it.
In this article, we will be creating our database and linking it to our ToDo list application. Then we will be writing Firestore commands in our NodeJS application to add, read and delete data in our database.
But before we start developing let's get the theory right
What is a Database?
A database is an organised collection of data. Well If you ask what data is then it could be any piece of information like text, number, picture or even a byte.
A database helps us organise this data so that it can be easily found, updated or used such that it provides us with some meaningful actions or even just information. In our case, we will be using it to store our tasks so that our web app can retain the list even when our page is refreshed.
There are many types of databases but here, we will be using a NoSQL database here.
To understand NoSQL, let’s know what a SQL database is.
What is a SQL Database?
SQL stands for Structured Query Language for storing, managing and reading data stored in a tabular fashion with rows and columns.
It is the standard language for all Relational database management systems(RDBMS). In an RDBMS all the entities have some relationship defined among themselves. A few popular RDBMS are MySQL, Microsoft Access, Oracle and Postgres.
What is a NoSQL Database?
A NoSQL database is a non-tabular way of storing and managing data. Unlike SQL it uses non-relational methods. This doesn't mean that NoSQL can not store relationship well, just that it stores them in a different fashion. In fact, it is easier to model relationships in a NoSQL database as data do not have to be broken into tables.
There are many ways data can be structured in NoSQL databases. Firebase Cloud Firestore uses a Collection Document-based type. Here each document contains pairs of fields and values in JSON like objects. These objects can contain various type of data like strings, numbers, booleans, arrays or even objects again.
Such several documents are stored under a collection entity. The twist here is that a collection can have subcollections as well along with other documents. And like this, we can access a document at a time in a collection.
And that’s all the prerequisite we need for Firebase Cloud Firestore
What is Firebase Cloud Firestore?
As the name suggests, Firebase Cloud Firestore helps us take a NoSQL document database into the clouds where it could be accessed and managed by any device in the world without the need for a server. But of course, we will be using the Node.js backend server we created using express.js earlier to access it. In case you missed it here’s a card to it.
Now let’s get our hands dirty and continue with our ToDo list Application!
1. Creating a Firebase Project
To get started with Firebase Cloud Firestore we will have to create a project in Firebase. So let's head over to the firebase console by clicking the link here.
Now click on Create a project
button and give a unique name to your project.
If you want you may turn on Google Analytics though we won't be using it in this tutorial.
And that's all your new project will be created.
2. Creating Firestore Database
Now you will be redirected to the Project Overview page. Here, under the build dropdown select Firestore Database
. And then click on Create Database button
.
For the security rules, select Start in test mode
, click on next and select nam5
in cloud firestore location. And it will create a database for you.
Now click on Start collection, name our collection todo
and now we will add a document in it to initialise our database structure.
To let firebase automatically generate an id for each document click on Auto-ID
, in the field section type task
, type section will be string
and value will be the task, so let's put it as Learn Database.
And now click on save
.
And with that, our Cloud Firestore database is created and structured.
3. Setting up Firestore Admin SDK in our Node.js Server
Now that our database is created let’s link it with our Node.js application using Firebase Admin SDK. Head over to project settings
and then the service accounts
tab.
Here click on Generate new private key
. This will download a file containing API Keys to access your firebase service account. Do not share it with others. Or else they will be able to access your firebase service account.
Now rename the file serviceAccountKey.json
and place it in the root of our project directory.
Notice that we have created db.js
as well here. In this file, we will configure our Firebase Admin SDK by importing our serviceAccountKey.json
.
Make sure to add serviceAccountKey.json
to .gitignore
file so that it is not publicly available on your GitHub profile when you push the code.
And now our Node.js server can access our cloud firestore database by importing
db.js
Open a terminal and install firebase-admin
using npm in our project.
npm install firebase-admin
And import both the modules in our index.js
file
const admin = require("firebase-admin");const db = require("./db");
Now our index.js file should look something like this.
And that’s how we set up Firebase Cloud Firestore for a Node.js Application.
In the next article, we will learn how to write CRUD operations in Firebase Cloud Firestore and create separate APIs for each using Node.js. Also, we will be testing these APIs using Postman.
This is Vinayak Tekade from Coder’s Capsule in collaboration with Developer Student Clubs Amrita School of Engineering Bengaluru signing off.
Follow me on LinkedIn and Twitter.
Looking forward to learn, teach and grow together. Check us out on Instagram for more similar content.