Here is a rewritten version of the article:

Building a REST API with Firebase Cloud Functions, TypeScript, and Firestore

Firebase is a leading solution for building serverless applications. It offers developers a way to easily develop scalable, high-quality apps without worrying about the cost of setting up and maintaining servers over a long term. In this article, we’ll build a REST API with Firebase Cloud Functions, TypeScript, and Firestore.

Setting Up Firebase

To get started, you’ll need to have Node.js installed on your computer. If you already have it installed, running the following command on your terminal should return your current Node.js version. We’ll be using version 12 in our app.

Creating a Firestore Database

With our billing plan upgraded, let’s create a Firestore database for our app. Navigate to the Cloud Firestore option on the left pane, then click on the Create database button. We’ll use the Start in production mode option for this demo.

Writing Our First Cloud Function

To write our first Cloud Function, we’ll first sign in to our Firebase account using the Firebase CLI. After successful authentication, create a project directory on your computer and, inside it, run the following command to initialize Firebase functions.

Deploying Our First Function

To deploy the default helloworld function, navigate to the functions directory on your terminal and run the following command. If the deployment is successful, you should receive a response that looks like this.

Creating a Service Account for Our App

In order to access our Firestore database and admin tools from our app, we’ll need to create a service account. To do so, click on the gear icon next to Project Overview in the left pane, then navigate to Service accounts, select Node.js, and click on the Generate new private key button.

Working with Firestore

Firestore is a NoSQL database for mobile, web, and server development. It offers a seamless integration with Firebase cloud functions, and its flexible and scalable nature makes it a good fit for building web APIs.

Adding Entries to Our Firestore Database

With our database config set up, we can now add functionality for creating journal entries. Inside the./functions/src directory, let’s create a new file named entryController.ts. We’ll use this file to house all the controllers for our entry.

Getting Entries from Our Firestore Database

Now that we’ve seen how to add entries to our Firestore database using Cloud Functions, let’s see how we can get data from it. For this, we’ll create a new function named getAllEntries in the entryController.ts file.

Updating and Deleting Entries

To add the functionality for updating entries, we’ll create a new function named updateEntry in the entryController.ts file. We’ll also create a function for deleting entries.

Understanding Firebase Realtime Database

The Realtime Database is a NoSQL database for mobile, web, and server development. It also offers a seamless integration with Firebase cloud functions, and its flexible and scalable nature makes it a good fit for building web APIs.

Working with the Realtime Database

Just as with the Firestore, data can be created, deleted, and manipulated from the Firebase console directly, or through Cloud Functions. We’ll update our firebase.ts file to point at our newly created Realtime Database, instead of the Firestore.

Adding Entries to Our Realtime Database

Now that we’ve seen how to set up the Realtime Database with Firebase Functions, let’s go over the first of the CRUD operations; i.e., creating — or adding — data into our database.

Getting Entries from Our Realtime Database

Now that we’ve gone over a basic creation flow, let’s look at the second CRUD operation and figure out how we can read, or get, all of the entries at once.

Updating and Deleting Entries from Our Realtime Database

Last but not least, let’s review how to delete and update calls.

When to Use Firebase vs. Realtime Database

Now that we understand how to use both the Cloud Firestore and the Realtime Database, let’s go over some of the main differences between both of these tools, and consider which one of the two is better suited for some of the most common situations.

Leave a Reply

Your email address will not be published. Required fields are marked *