Web Development Concepts with Ebinabo

Web Development Concepts with Ebinabo

Uploading Test Data to Firebase Cloud Firestore

Uploading Test Data to Firebase Cloud Firestore

Subscribe to my newsletter and never miss my upcoming articles

You've built out this really great application and now you want to try it out just before you push it to production.

Putting in data manually can be very hard to do. One simple solution would be to batch upload data to firestore and it's relatively easier than it sounds.

For this tutorial, we'll be creating a database of commerce products. And we would be using Faker.js to help with this.

Install Libraries

The first step would be to install the Firebase Admin SDK.

yarn add firebase-admin faker -D or npm i firebase-admin faker -D

This would add the sdk as a development dependency since we wouldn't want that pushed to production.

Now if you have Firebase functions set up in your project, you can opt to skip this step since you already have the sdk installed and only install fakerjs.

Create a new file in your project, preferably in the root directory or in your functions folder if you have that added to your project. I'll be calling mine seedData.js

Generate a new private key

Project Settings -> Service Accounts -> Generate New Private Key

firebase-tut-1.png

firebase-tut-2.png

firebase-tut-3.png

I like to rename this file to service-account.json. I'll also be putting it in the same directory as seedData.js which makes it easier to access, you don't have to do this if you don't want to as long as you link the files correctly.

From the warning, remember to add the private key to your .gitignore file or delete it all together after you're done to avoid pushing secrets to version control.

var faker = require('faker');
var admin = require("firebase-admin");
var serviceAccount = require("./service-account.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://my-new-project-932a3.firebaseio.com"
})

Put in Data

To upload the data to firestore, the command we would run in the terminal after adding in the rest of the code is

node seedData

Using Faker.js

We're going to iterate over 10 items (feel free to use more) and for each iteration, we would add a new product to the database alongside it's price and description. Then we would log out the id of the products just to see a response.

for (i in [...Array(10).keys()]) {
    admin.firestore()
    .collection('products')
    .add({
        name: faker.commerce.productName(),
        price: faker.commerce.price(),
        description: faker.commerce.productDescription()
    })
    .then(doc => console.log(doc.id))
    .catch(err => console.log(err))
}

And now you have some data in your database to work with from a tiny effort.

Uploaded Data.png

Using a Custom JavaScript Object

It is also possible to use a JavaScript object containing the data you would want in the database. You're likely to run into errors here since Firestore accepts all the JavaScript datatypes.

const sampleData = {...}

for (i in [...sampleData.keys()]) {
    admin.firestore()
    .collection('my-collection')
    .add(sampleData[i])
    .then(doc => console.log(doc.id))
    .catch(err => console.log(err))
}
 
Share this