Automate Importing Data to Firestore

Mansi Kalaria
JavaScript in Plain English
3 min readJun 15, 2021

--

Having trouble importing your Google Form responses to Firebase Database? Or finding difficulty in manually uploading the data?

Let’s automate this tedious task with some code.

In this article we will be using Node.js to import JSON data directly to Firestore. Do not worry if you have your data in some other format, we can easily convert it to JSON. You will require data in form of an Excel sheet or CSV format. An Excel sheet can be downloaded in CSV format as shown below.

To convert CSV to JSON, we will use this online tool. And download the JSON file.

Now let us create a new project in Firebase and download the required files. Go to Firebase console and fill the required project name and create a new project. After the project is loaded, move to Project Settings

Project Settings > Service Accounts > Create Service Account and Generate New Private key and download the JSON file.

We have finished setting up our Firebase Project. Next, make sure that you have node installed in your system.

To setup a new node project, create new folder and run npm init to initialise your new Node.js project and fill in required details, for default keep pressing “Enter”.

When the process is finished, it will create a package.json file inside the directory. We will move our data.json (the file that we converted from CSV to JSON) and service.json (the file that we downloaded from Firebase) files to our project directory.

Now we will create our index.js file inside our project directory. And write our code inside it. Fill in the variables according to your project.

You can find the databaseURL from your firebase project under Service Accounts:

Save index.js inside your project directory and now we need to install firebase-admin library.

run >> npm install firebase-admin

Now Lets run our code ;) To run, move out of your project directory and

run >> node <directory_name>

All your data will be written to Firestore database in fraction of seconds.

Conclusion

And that’s it! Our boring task has been automated and can be finished in a matter of few seconds. I hope you have found this useful. If so, be sure to let us know in the comments. Stay tuned for more interesting programming stuff.

More content at plainenglish.io

--

--

Final Year B.Tech student | Upcoming Analyst @Goldman Sachs | GDSC Lead @DDU | Flutter Enthusiast