Node.js - Setting Up & Using Google Cloud Firestore

Google Cloud Firestore is a serverless, cloud-native NoSQL cloud database service. It can be accessed from mobile apps, web apps, or other Google Cloud services. Since it's a NoSQL database, you are not required to define schema. You can have multiple collections and each document in a collections can have different fields.

In this tutorial, I'm going to show you how to setup Cloud Firestore so that it can be accessed from Node.js application, along with some basic operations.

Preparation

1. Create or select a Google Cloud project

A Google Cloud project is required to use this service. Open Google Cloud console, then create a new project or select existing project

2. Enable billing for the project

Like other cloud platforms, Google requires you to enable billing for your project. If you haven't set up billing, open billing page.

3. Enable Cloud Firestore API

To use an API, you must enable it first. Open this page to enable Cloud Firestore API.

4. Set up service account for authentication

As for authentication, you need to create a new service account. Create a new one on the service account management page and download the credentials, or you can use your already created service account.

In your .env file, you have to add a new variable because it's needed by the library we are going to use.

GOOGLE_APPLICATION_CREDENTIALS=/path/to/the/credentials

You need to install gcloud on your computer and get authenticated to access your account.

Dependencies

This tutorial uses @google-cloud/firestore and also dotenv for loading environment. Add the following dependencies to your package.json and run npm install

  "@google-cloud/firestore": "~0.19.0"
  "dotenv": "~4.0.0"

Code

First, we create an instance of Firestore by passing two parameters: projectId and keyFilename which is the path to the file containing credentials. Then, create a document object which refers to the document in Firestore we are going to modify.

firestore.js

  require('dotenv').config();

  const Firestore = require('@google-cloud/firestore');

  const firestore = new Firestore({
    projectId: 'your-project-id',
    keyFilename: 'path/to/google-cloud-keyfile,
  });

  const document = firestore.doc('items/first-item');

Insert a Document

You can insert a document using the following code.

firestore.js

  document.set({
    name: 'The First Item',
    description: 'This item is useless',
  }).then(() => {
    // Handle if document successfully created
  });

To view the database, open Firestore console.

Update a Document

Below is the example of how to update an existing document. It only modifies the value of fields you pass.So, if you don't pass a field when updating a doument, the value of the field will be unchanged.

firestore.js

  document.update({
    description: 'This item is bad',
  }).then(() => {
    // Handle if document successfully updated
  });

Read a Document

You can read the details of an existing document using the following code.

firestore.js

  document.get().then(doc => {
    console.log(doc.data());
  });

Delete a Document

To delete a document, use the following code.

firestore.js

  document.delete().then(() => {
    // Handle if document successfully deleted.
  });

That's how to integrate Google Cloud Firestore database with Node.js application.