DEV Community

Kyle Trahan
Kyle Trahan

Posted on

Cloud Firestore

I recently joined a group to participate in a hackathon. We decided to do our backend in node and to put Firebase's Firestore to use. This was my first real project working with either of these technologies so it has been a very firehose of information kind of week. I wanted to share a few things about Firestore that I learned as a result.

Starting off with what Firestore can do for you. Firestore is essentially a scalable NoSQL database, which is used via the Cloud. It allows your data to stay in sync over multiple different platforms.

One of the most helpful parts of using Firestore is that you can use the prebuilt firebase authentication to handle your users sign in information with very little hassle. We went through a few hiccups along the way but eventually were able to get a functioning authentication system set up!

From here we needed to start collecting information about our users. We created an application that would eliminate the need for any paper whenever it comes time to renew your license at the DMV. With our app DMVeasy you are able to fill out all the information that is required for any form for this process and then it is automatically generated into pdf format. Also you are able to upload images of all the different requirements such as a copy of your passport, social security card etc. Thus eliminated the waste that results from making photocopies of all of these things.

This brings me into actually using the database. It was a very interesting process trying to learn, which I actually really enjoyed. So you start off making a collection, which is just a container that will hold documents that have similar attributes in them. So for example we have a collection of users, and within that collection there are a bunch of documents identified by a unique key. Then inside that document is where we hold all of our information retaining to that specific user. It works very similarly to creating a table and assigning what columns will be inside that container. One of the really big differences is that in a table all of the data entries will have the same columns. In a collection you can actually set each document in that collection to have whatever attributes you want. Which sounds a bit overwhelming in that your database could fairly quickly become very disorganized, but with a little bit of validating we feel like we are sitting in a pretty good place.

I wanted to talk a little bit about one of the functions I created while going through the project. It has to do with the ability to update a users information. As we only had about 10 days to do the hackathon we were pretty limited on time. This was one place I feel like working with Firestore really thrived. Since I was mostly focused on the backend side of the project and my partners were focused primarily on getting the pdf functionality to work. I was unsure exactly what information the update function would be receiving in the request. So I wanted to create a dynamic solution that could work for any key in the requests body, until we finalized what exactly would be sent back. Heres my solution below:

        const userInformation = {};

        for ( let info in request.body ) {
            if ( info !== "userId") {
                userInformation[info] = request.body[info];
            };
        };

Enter fullscreen mode Exit fullscreen mode

I basically just looped through the request.body object and created a new object that would make key value pairs that match whatever was sent back in the request. I was happy with my solution in order to help us continuing to move forward without getting bogged down by exactly what information we would end up saving in our database. Since Firestore allows you to be so dynamic with your collection information, the two paired very well together. Overall I had a lot of fun working on this project. I might go into a little more detail with the auth process in my next blog. Thanks for taking the time to read this and have a great day!

Top comments (0)