Firestore Synchronous Import Code

Ronnie Royston
2 min readJan 4, 2023

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.

Once exported you should have a .json file that looks something like below. Save this file in your local node directory.

{
"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.

Follow the Add the Firebase Admin SDK to Your Server instructions provided by Firebase to get Node.js and the Firebase Admin SDK installed on your machine.

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.

What if I need to import more than 500 documents at once? Asynchronous batch writes enables you to write millions of documents at a rate of 500 per second. Firebase Admin SDK Bulk Import Example walks you through that process and includes a sample script.

--

--

Ronnie Royston

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver