DEV Community

Cover image for 🔥 Firebase Firestore Migrations 🔥
Mikael
Mikael

Posted on

🔥 Firebase Firestore Migrations 🔥

Firebase is one of the greatest serverless environment for developers to quickly get scalable products up and running, it lets you focus strictly on code and not care too much about what happens in the backend.

Although serverless is great, developers often face use cases that the developers of those tools didn't think about or implement yet.

I came across one of those problems recently and wanted to share to the greater community a nice workaround the community has built for us.

My problem arose when I wanted to migrate the noSQL data I had in one Firebase project to another Firebase project. Google lets you use the gcloud CLI for this purpose and has a very useful gcloud firestore export and gcloud firestore import that normally poses no issue. However, when you start to have nested collections, this tool will fail and only export the first level documents, which is not exactly what it should do...

After searching the internet for a bit, I came across this awesome npm package: firestore-export-import. This package will let you use the command line to do exactly what it does best: export and import data from firestore in JSON format easily. It's really a child's game.

1) install the package:

npm i -g firestore-export-import
Enter fullscreen mode Exit fullscreen mode

2) Export the credentials.json from your Firebase project
Firebase Dashboard > Settings > Service Accounts > New Private Key

3) Export Data

firebase-export -a path/to/credentials.json -b path/to/savefile.json
Enter fullscreen mode Exit fullscreen mode

4) Import Data

firebase-import -a path/to/credentials.json -b path/to/savefile.json
Enter fullscreen mode Exit fullscreen mode

Change the project credentials in between step 3 & 4 to switch the target project.

and.. that's it!

Amazing little package that lets you perform those tasks in a way easier way than the tools Google proposes you to use. Only caveat, make sure to secure your local private keys. If anybody finds them, you will compromise your entire Firebase project.

Code safe!

Top comments (2)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
thanhlm profile image
Thanh Minh

Hey try refiapp.io