Code on gitlab: https://gitlab.com/test-projects47/firestore-import-json
Creating a Firestore database
Register for a firebase account and go to Firebase Console (https://console.firebase.google.com/)
Add Project
Click Continue when the project is ready, which takes you back to the console
Add a new app (Web app in this case) to the project
Copy firebaseConfig in the next screen for later use.
Continue to console
Click into Cloud Firestore and create a database
Pick a mode and select a region and "Enable"
Setup a service account
We will authenticate the app using a service account key.
Go to the Service account page ( Project Overview -> settings -> users and permissions ->service accounts)
"Generate new private key", which will save a JSON file to your computer
The Code
https://gitlab.com/test-projects47/firestore-import-json
- start a new node.js project
yarn init
- Add dependencies
yarn add firebase-admin
yarn add dotenv
Include the following files in the folder
In the .env file, put the path of the service account private key in the variable
GOOGLE_APPLICATION_CREDENTIALS
(Optional) Firebase config variables in
.env
Here (index.js) we insert the data in
data.json
into a collection called "users", with an automatically generated document id.run the code
Top comments (0)