DEV Community

Cheryl M
Cheryl M

Posted on • Originally published at cherylm.hashnode.dev on

Importing JSON data into Firestore

Code on gitlab: https://gitlab.com/test-projects47/firestore-import-json

Creating a Firestore database

  1. Register for a firebase account and go to Firebase Console (https://console.firebase.google.com/)

  2. Add Project

  3. Click Continue when the project is ready, which takes you back to the console

  4. Add a new app (Web app in this case) to the project

  5. Copy firebaseConfig in the next screen for later use.

  6. Continue to console

  7. Click into Cloud Firestore and create a database

  8. Pick a mode and select a region and "Enable"

Setup a service account

  1. We will authenticate the app using a service account key.

  2. Go to the Service account page ( Project Overview -> settings -> users and permissions ->service accounts)

  3. "Generate new private key", which will save a JSON file to your computer

The Code

https://gitlab.com/test-projects47/firestore-import-json

  1. start a new node.js project
yarn init

Enter fullscreen mode Exit fullscreen mode
  1. Add dependencies
yarn add firebase-admin
yarn add dotenv

Enter fullscreen mode Exit fullscreen mode
  1. Include the following files in the folder

  2. In the .env file, put the path of the service account private key in the variable GOOGLE_APPLICATION_CREDENTIALS

  3. (Optional) Firebase config variables in .env

  4. Here (index.js) we insert the data in data.json into a collection called "users", with an automatically generated document id.

  5. run the code

Top comments (0)