DEV Community

Cover image for How to create and connect to a firestore database in an express app ?
Ikram Kharbouch
Ikram Kharbouch

Posted on

How to create and connect to a firestore database in an express app ?

As a web developer myself, I understand how hard it can be sometimes to find a simple guide that can help you do a specific thing you want, that's why I thought about making a simple guide to help newbies work with firebase's firestore database using expressjs.

In this guide I am gonna show you all the necessary steps to create an expressjs app, create and connect to a firestore database, and then store some data for testing.

First of all let's create an express app.

Inside a folder run :

npm init && npm install express --save
Enter fullscreen mode Exit fullscreen mode

Create a file named index.js to initialize your express app.

Next we need to install the firesbase-admin module inside of our express app:

npm install firebase-admin --save
Enter fullscreen mode Exit fullscreen mode

The module that we just installed gives us access to some builtin methods that we gonna need to interact with our database, these methods are : initializeApp, cert, and getFirestore, destructure the mentioned methods using :

const express = require("express");
const app = express();

const { initializeApp, cert } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
Enter fullscreen mode Exit fullscreen mode

Next, we need to create a firebase project in order to access to a firestore realtime database:

Image description

After creating a project named Sample-project, we should next create a web app to connect to firestore.

Image description

After creating our webapp, we can then access the project overview to create our firestore database here:

Image description

Next thing we would need to do is to generate a new service account key to interact with our database, you can download here and name it whatever you want in your project, for my case I named it serviceAccountKey.json.

Here we go to our project settings, then service accounts and click on generate new key that is featured on the image below :

Image description

You will get prompted to download your serviceAccountKey, and make sure to do it inside of your project.

Next thing you need to include the key in your express app like this :

const serviceAccount = require("./serviceAccountKey.json");
Enter fullscreen mode Exit fullscreen mode

in order to initialize your app with admin privileges, you need to initialize your app this way :

initializeApp({
  credential: cert(serviceAccount),
  databaseURL: process.env.databaseURL
});
Enter fullscreen mode Exit fullscreen mode

You can get your database URL by simply adding your projectID + ".firebaseio.com"

Image description

You can get your project ID by simply browsing to your project settings as mentioned in the screenshot above.

And finally, we went through all these steps to reach this last one, the one where we get our firestore instance that let's us communicate with our database from our app.

const db = getFirestore();
Enter fullscreen mode Exit fullscreen mode

And now it's time to make our first request creating a new collection and inserting some documents into it for test.

app.post("/testDB", (req, res) => {

let setDoc = 
db.collection('testCollection').doc('testDoc').set(req.body);

res.send({'Message': 'Success'});

});
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed this guide, I am looking for your opinions about it, and if there is anything to add don't hesitate to comment about it, thank you :).

Top comments (7)

Collapse
 
hacker4world profile image
hacker4world

Good blog, but isn't collection and doc functions are imported seperately from firestore ?

Collapse
 
ikramkharbouch profile image
Ikram Kharbouch

They actually come from the cloud firestore node, as listed in their documentation!

Collapse
 
hacker4world profile image
hacker4world

I get it now thank you

Collapse
 
hakki profile image
Hakki

add exception handling for collection related fails : )

Collapse
 
yongchanghe profile image
Yongchang He

Thank you for sharing this!

Collapse
 
marsou001 profile image
Marouane Souda

Loved the article, very helpful. Thank you for sharing!

Collapse
 
ayoubechchetyouy profile image
Ayoub Ech-chetyouy

Great job, thanks