Create a Seed File for Cloud Firestore in Node.js

Firebase’s Cloud Firestore is a NoSQL cloud database that allows for flexible data models and efficient querying. With Firestore, you can add new data directly through the Firebase console at any point in your app’s development, without worrying about meeting database constraints like you would with a relational database management system. It allows developers to get their app up and running quickly, and to adapt their data structures as they go. However, if you’re starting a project and need a foundation of test data to work with, or would like to periodically clear the database and refresh your data to a starting point, a seed file that you can call from the command line might be handy.

If you’re like me and are switching over from a RDBMS to the freedoms of a non-relational database, it might be difficult to conceptualize how your data will look before you really get into the creation of your app. Before writing a seed file, it’s important to get familiar with Firestore’s collection and document data structure and have initialized Firestore both in the Firebase console and in your local environment. When you’re ready, here’s how to approach creating a seed file for Cloud Firestore in node.js.

Setting Up

Let’s say we want to create a note-taking app with a homepage that renders all of our past notes. Before we start, we’ll want to spend some time sketching out the structure of the data we’ll need to store and query. As you develop this, be careful to avoid nested data, as this could complicate queries later on, and arrays, where it’s tricky to update individual elements and generally becomes slower to query as more elements are added on. The good news is that you can always change this structure as you go.

When you have an idea of the initial data you want and how it’s structured, then we’re ready to begin by building out our data objects. These will represent the documents that we’re adding to each collection. For our simple note collection, we’ll want to seed data for each note’s title, a timestamp for when it was created, and some content.

In a new seed.js file, we’ll start by importing Firestore from where you’ve initialized it in your app. Here I’m also importing faker.js, a JavaScript library that will help me generate some instances of unique fake data. We’ll start our seed function, making sure to export it so we can later call it in other parts of our app.

Build Out Your Data

Let’s create an outline for our data objects. In this example, each note is given unique content with the help of faker. We can use faker.lorem to fill in a note’s title and content with lorem ipsum words and sentences, and then combine faker with Firebase’s built in Timestamp to use as a createdAt value.

Inside of our new seed function, we’ll store the collection title in its own variable, and then fill an empty array with the number of documents we want to store in this collection. Lastly, we’ll map over this array and populate it with our data object. Here I’m populating noteTemplate with 10 individual note objects.

So now to add all our new data to Firestore. Since we need to write a lot of data at once, this is a good use case for a batched write.

What is a Batched Write?

A batched write is what allows you to update many collections at once in Firestore without compromising the data; if one write fails the whole batch fails. By grouping the data we want to send together, we can guarantee that our write is never accidentally interrupted, leaving our database with incomplete data. It’s important to note that Firestore only allows you to write up to 500 documents in each batch of writes.

Create a Function for Batched Writes

Now we can start an async batched write function, addToCollection. It will take a collectionKey and an arrayOfObjectsToAdd. Then we are able to find the collection by its key and start our batch.

For each object in our array, we’ll want to create a new document with a unique ID and use our object as its data fields. We can accomplish that by using .doc() on our collection and then passing our new document reference and object to batch.set().

The last step in this function is to wait for batch.commit() to do its thing. If it goes through, we can rest assured that all of our data is safe and sound in Firestore. I’ll add a console.log so we can see the start of the seeding process. Here’s the whole batched write function:

In our seed function, we can now call our addToCollection, passing in our collection key and data object. You can imagine how you might expand your seed file, by including other collection keys and objects here and calling addToCollection after each of them. By commenting out any combination of these invocations, you can adjust which collections are being added to or not being added to every time you run your seed file.

Add the Seed File to Your Firebase Config

Finally, the way we can run our seed file is by simply importing it into the index file where we initialized Firebase and then call it. This will allow it to run whenever we start up our app in development and testing. When we’re done with the seed file, just comment out the seed line and use your data in Firestore as needed. If you need to reset or adjust your data, just delete the necessary documents or collections, comment this line back in, and start up your local development environment as normal.

There you have it, a successful seed file that will fill your Firestore with test data configured to your needs!

A full stack developer that loves creative and analytical challenges.

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