DEV Community

Alex Mammay
Alex Mammay

Posted on • Edited on

Firebase Emulator Suite: Using production data inside the emulator

So you have a lot of info in your live (prod/test) database that you want to use with the new fancy firebase emulator UI. Here is how i went upon wiring up the data.

  1. Create storage bucket to sync into
    • gsutil mb gs://[targetstoragebucket]
  2. Export your prod database into a storage bucket.
    • export the database with this command https://cloud.google.com/sdk/gcloud/reference/beta/firestore/export as a sample if you want to sync the whole database you can do gcloud beta firestore export gs://[targetstoragebucket] and you can pick and choose root level collections by doing gcloud beta firestore export gs://[targetstoragebucket] --collection-ids='customers','orders'
  3. Create seed-data folder in your working directory mkdir seed-data
  4. Once the export is done, we can sync it down to our filesystem. gsutil rsync -r gs://[targetstoragebucket] seed-data/
  5. Now here is where we need to do a bit of patching, if we just try to run firebase emulators:start --only firestore --import seed-data it will error on us, so lets do a bit of cleaning up
  6. lets cd seed-data and then mkdir firestore_export and touch firebase-export-metadata.json and then finally move our export into the firestore export folder, in my case it was mv 2020-07-12T02:45:39_47881/ firestore_export.
  7. create content of firebase-export-metadata.json file, so lets open it in a editor and point out some important bits
{
  "version": "8.5.0",
  "firestore": {
    "version": "1.11.4",
    "path": "firestore_export",
      "metadata_file": "firestore_export/2020-07-12T02:45:39_47881/2020-07-12T02:45:39_47881.overall_export_metadata"
  }
}

make sure to note the version field (version of firebase-tools you are using, and the metadata file, this is that folder we used the mv command for.

and just as final screen shot of the important stuff in case you want to compare.
Screenshot

now if we boot up the emulator with firebase emulators:start --only firestore --import seed-data lets do a compare of the live database and our emulator

Woow!

success

Top comments (1)

Collapse
 
uppergoal profile image
Michael Boutin

Thanks Alex! Saved me a lot of time.