Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 22, 2022 09:31 am GMT

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

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

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");

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");

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});

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();

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'});});

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 :).


Original Link: https://dev.to/ikramkharbouch/how-to-create-and-connect-to-a-firestore-database-in-an-express-app--4j84

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To