Import seed data for your Firebase emulators

Jorge Vergara
InstructorJorge Vergara
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

Emulator data is lost on restart since it is all in memory. In this lesson we'll take advantage of Firebase's emulator export script and --import flag so that we will always have seeded data when we start our app.

Instructor: I'm going to create an account for my application, jorge@jorgevergara.co. When I click Create Account, you can see that I move into the application. If I go into the emulator in authentication, you can see that the user is here.

Whenever I stop the emulator and then start the emulator again, it all starts from scratch. As you can see, there is no user anymore. If I go into my application, I get a bunch of errors because my user doesn't really exist.

It's the same with Firestore, it's the same with everything else we're using. All of this data is in memory. Whenever you start, you just start again from scratch. That can be a big pain when building applications. We can take advantage of some Firebase tooling that we have to make sure that we can always start with seed data.

First, let's go back into the application and create a user account. Now I can go back and I can see, again, my account is here. I'm going to go to Firestore. Now you see in the database, I have the data about that user. I'm going to start creating a couple of events. This is my birthday. Now I'm going to create something else. You can see that those events are being added to the database here.

Now that we have some seed data, and we can see it here in the application, both in the database and in the user's console, we are going to export this data. It's really important that you leave the console running. You don't want to terminate the emulator session.

We are going to use another terminal window, and we're going to type, firebase emulators export, and we are going to give a path to the data that we want to export. By convention, the documentations shows you to do it in emulators.backup, but this is entirely up to you.

You see that, it says here, "Export complete." You can see that actually inside of the emulator, it says we received an export request, and it exported all of the data. Now if we open our file explorer, we can see here a new folder called emulators backup. In here, we have the backup for the authentication, the firestore export, and so on. If you are using any other APIs and you have data in those APIs, you will have that data here.

Now you can see that I can stop the emulator. I can start it again. I just wanted to show you that when I start it again, the data is completely gone. It's not here because it's running in memory.

We have a different command or a different flag we pass to that command to start the emulator with the seed data. We're going to say first, let's clear this so that we can properly see. We're going to say Firebase emulators start. We're going to import whatever it is that we exported.

In this case, it's import Firebase emulator backup. Now it all starts and as you can see, we had nothing here, and it added both of the events we created. If I go into authentication, here I have my user back. This is a good way you can add seed data.

You can even add these to your source control and that way all of the people in your team can use this data, and they don't have to start with the emulator from scratch every time they want to use it.