Import CSV Data to Firestore Using GCP and Node.js

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

In this post, you will learn how to import CSV data to a Firestore database using GCP.
Google Cloud Platform (GCP) is a suite of public cloud computing services provided by Google. The platform includes a range of hosted services for compute, storage, and applications that run on Google Hardware.
Google Cloud Platform services can be accessed by software developers, cloud administrators, and other enterprise IT professionals over the public internet or through a dedicated network connection.
Cloud Firestore is a fast, fully managed, serverless, cloud-native NoSQL document database that simplifies storing, syncing, and querying data for your mobile, web, and IoT apps on a global scale.
Its client libraries provide live synchronization and offline support, while its security features and integrations with Firebase and Google Cloud Platform (GCP) accelerate building truly serverless apps.
Open the Google Cloud Console and accept the terms and conditions if you are a new user. Then create a new project named RainfallCSV (you can name your own).

Creating a New Project

Creating a new project RainfallCSV
Once the project is created, click on the menu icon and scroll down to Storage and click FireStore.


Firestore Selection
Once the data is clicked, select the mode as Native Mode.

Selecting Native Mode
Once the Native Mode is selected, choose where to store the data (choose your nearest region) and then click Create Database.

Region Selection
Once the Database is created you will see a screen like below

Firestore Database
Now click the cloud shell icon like below

Activate Cloud Shell
After the shell is created, we will configure the project ID type the below command to check whether the project is configured or not.
gcloud config list project
Now set the project ID using the below command.
gcloud config set project PROEJCTID
gcloud config set project rainfallcsv
Once the project is set, it’s time to write some logic to read CSV data and import it to Firestore.
Click the Launch Editor.

Launch Editor Click
Once the Editor is opened, create a new directory named rainfall in the terminal and then change the directory.
mkdir rainfall
cd rainfall
Initialize npm using the below command in the terminal and fill the details.
npm init
Press Enter for all the options, and you should see a package.json file created with the details below.
{
"name": "rainfallcsvexport",
"version": "1.0.0",
"description": "Rainfall Data conversion",
"main": "index.js",
"scripts": { "test": "echo \"Error: no test specified\" && exit 1" },
"author": "Nidhin",
"license": "ISC"
}
Install the following dependencies.
npm install @google-cloud/firestore
npm install csv-parse
Create a new file named rainfallDataExport.js using the command in the terminal (you can name it anything).
touch rainfallDataExport.js
Copy the below code and paste in the file.
What we have done in the above code is
writeToFirestore()writeToFirestore() uses a batch process in which each of the records is processed in turn and sets a document reference based on the identifier added. At the end of the function, the batch content is written to the database.var docRef = db.collection('rainfall').doc(record.SUBDIVISION);
Now we will upload the CSV file which we are going to process. Click the link to download the Rainfall CSV File.
Upload the CSV file to the rainfall folder by right-clicking the folder name.

Uploading CSV File
Once the file is uploaded to the directory, it’s time to process the data.
In the terminal, run the below command.
Syntax:
node javascript-filename.js csv-filename.csv
So ours will be like:
node rainfallDataExport.js rainfall.csv
Click Enter now, and you should see the data getting imported. In the console, you will get a message like below:
Wrote 4117 records
Once you see the above message in the console, click the MenuButton -> Firestore -> Data and you should see the data imported.

Imported Data
You can view the data from the firebase console also. Go to your Firebase console and click Create a new project and then select the project name from the dropdown (the one we created in GCP console) and click Create.
Go to the Database and click Firestore you can see the data:

Data in Firebase console
You have learned how to export CSV Data to Firestore using GCP.
Happy Learning :)