How to Migrate and Mock Data in the Firebase Emulator

Setting up a fully functional database in Firebase Emulator

Learn how to populate your local Firestore instance with mock data, and how to replicate data from your live project.

Aziz
Aurora Solutions

--

Photo by Markus Spiske on Unsplash

If you want to make your local emulator testing practical, you will most likely need to work with data. This is a crucial step and one that is hard to find in the standard documentation. We will look at how you can populate your Firestore instance and explore three options:

  1. Mock data using Faker on the backend.
  2. Mock data using Faker by injecting code in the browser console.
  3. Export data from your remote Firestore database (like staging or development) using GCP and import it into the local instance.

Method 1: Mock Data using Faker on the Backend

In this option, we will use Faker, which is a package that allows you to generate random mock data. We will add some code to simulate the data that will run each time you start the Firebase emulators.

  • Start by installing the Faker package which you can do by adding a dependency in your project’s package.json as below and running npm install
    "faker": "^5.4.0";
  • In the file where you are calling initializeApp() for Firebase, check whether the process environment is set to Emulator. If it is set to Emulator, then go ahead and add mock data that you need in your collections. A simple code sample is given below:
Mocking some data for our Users collection in the Firestore emulator

Method 2: Mock Data using Faker Injection

This is a quick scrappy solution and not an ideal one. Skip to method three below if you prefer working with a copy of your development, staging, or production database instead.

  • Open your local Firestore in the browser, go to console in the developer tools, and paste the full code below. Take a look at the code, it is quite simple, and you can play around with it. This code lets you add ten random users.
Sample Faker code
  • You should see a button show up on the top right saying “Add 10 random users”.
Browser screenshot for Faker data
  • Optional: Run the script automatically
    There is the possibility to run this code using a chrome plugin similar to TamperMonkey. We haven’t tested this, but it should be possible to automate this script on a specific condition, like each time you load the local firestore URL.

Method 3: Sync with an Existing Firestore Database

This method requires a few small steps to set up but is likely to create better quality local testing once done.

  • Create backups of required Firestore collections. Ideally, your project already has a way of creating regular backups of your Firestore database, in which case you can skip the first step:
    gcloud firestore export <cloud-storage-path> --collection-ids=[comma separated list of collections]
    e.g.gcloud firestore export gs://my-awesome-project/manual-backups/20201231 --collection-ids=users,companies,employees
  • Copy the backup folder locally(needs gsutil installed)
    gsutil cp -r <cloud-storage-path> .
    e.g
    gsutil cp -r gs://my-awesome-project/manual-backups/20201231 .
  • Create a new file called firebase-export-metadata.json in the folder where the backup has been copied. Edit the file as below. Specify the path of the backup and metadata_file from Firestore export. In our case, the file should look something like this:
Sample file for exporting Firestore data
  • Finally, we can modify the run emulators command to include the import data step:
    firebase emulators:start --import full-path-to-your-backup-folder
    For our sample project, it looks something like this:
    firebase emulators:start --import full-path/my-awesome-project/functions/20201231

That’s it. Your one-time setup should now be complete. After this, you will only need to run the last command to import data into the local emulator while starting the emulator. If you need a new copy of the data, re-export the data from cloud Firestore and then place it in the appropriate folder before running this command.

Important: The emulator will initially load the rules specified in the firestore.rules part of your firebase.json file. Make sure they are up to date.

If you need help setting up an end-to-end Firebase emulator suite for debugging locally, give this guide a read.

--

--