Firestore Synchronous Import Code

Introduction

In case you have under 500 documents to write to Google’s Firebase Firestore, a synchronous write is the way to go. The first step is to get your data in JavaScript Object Notation, JSON, format. Most spreadsheet applications such as Microsoft Excel or Google Sheets offer an export data as JSON option. I use Google Sheets and an add-on called “Export Sheet Data.” Add a header row which will identify each column of data, for example column A is “id” and column B is “url”. Export your data into a text editor and save the file with a .json file extension.

{
"AA": {
"id": "AA",
"url": "https://firebasestorage.googleapis.com/v0/b/blahblah/imageAA.jpg"
},
"BB": {
"id": "BB",
"url": "https://firebasestorage.googleapis.com/v0/b/blahblah/imageBB.jpg"
},
"CC": {
"id": "CC",
"url": "https://firebasestorage.googleapis.com/v0/b/blahblah/imageCC.jpg"
}
}

Setup and Installation

Firestore offers a couple of JavaScript based methods to import data, namely the JavaScript (Web) SDK and the Firebase Admin (Node) SDK. The Admin SDK is the proper tool for this job. Installing the Admin SDK is easy. Install Node on your workspace then install the Firebase Admin SDK. Finally, in the Firebase web console, generate a service account key and save it in your local node directory. You are ready to go.

Node Script

The script below is run from your local Node workspace and uses the service account key to authenticate to the Firebase system. A unique key is assigned to each document and all documents are pushed to Firestore at once.

var admin = require("firebase-admin");
var serviceAccount = require("./your-service-account-secret-key.json");
var fs = require('fs');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://your-project-id.firebaseio.com"
});
var firestore = admin.firestore();
var writeBatch = firestore.batch();
var obj = JSON.parse(fs.readFileSync('your-file.json'));
console.log("working…");
for (var key in obj) {
var ref = firestore.collection("your-collection-name").doc();
writeBatch.set(ref, obj[key]);
}
writeBatch.commit().then(function () {
console.log("done.");
})
.catch(function(error) {
console.error("Error writing batch: ", error);
});

Troubleshooting

If you run into problems be sure and validate your JSON file. JSON is very picky. If you are missing one comma or quote, the whole process will fail.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ronnie Royston

Tulane Graduate (Philosophy '97), Cisco Certified Internetworking Expert# 6824, USPA Master Skydiver (D# 20776).