Introduction: Firebase CLI
According to the github page of the firebase-tools project, the firebase cli can be used to:
- Deploy code and assets to your Firebase projects
- Run a local web server for your Firebase Hosting site
- Interact with data in your Firebase database
- Import/Export users into/from Firebase Auth
The web console is fine for a lot of stuff.
But let’s see what we can do with the CLI, and let’s check what we CANNOT do without it.
Assuming you have npm installed, run the following command:
npm install -g firebase-tools
You will now have the command firebase, globally.
to authenticate to your Firebase account (this will open your browser).
List your firebase projects:
You can then select one of them with
firebase use <project>
Cloud Functions Log
A common thing you would like to do is looking at the logs.
This is possible with:
At this point I think there is no way to “listen” for changes in the log, that would be a very useful feature.
gcloud app logs tail
sadly does not help here, even if the current selected project is the firebase one.
If you have some tips about it, I will be more than happy to edit this article.
The functions configuration is handled through some commands:
to retrieve|store|remove|clone project configuration (respectively).
will let you choose which of your functions to emulate locally.
You can then choose the method (get/head/post/put/patch/del/delete/cookie/jar/default) and the test data to start the emulation.
You can then delete one or more cloud functions with:
firestore functions:delete <function_name>
Deploy and serve locally
To serve your project with cloud functions locally, you can run:
When you’re ready to deploy your firebase project, you can run:
With the firebase cli you can both import and export Firebase accounts with the:
firebase auth:import|export <file>
Surprisingly enough, import import accounts from a file, and and export will export accounts to a file.
I will skip this part. It’s very well documented everywhere in the firebase ecosystem.
Here we are.
What we can do with Firestore with our CLI? Almost nothing, I am afraid.
Right now you have only two things we can accomplish.
But we can use gcloud and gsutil to perform other operations, lick exporting / importing data.
Be sure to be logged in with
gcloud auth login
If you are already logged in with different accounts, you can list the accounts with
gcloud auth list
and select one (if the one you need is not the active one) with
gcloud config set account firstname.lastname@example.org
At this point, let’s select our project.
gcloud projects list
to list all the projects and
gcloud config set project project_name
to select our project.
First of all, we need a Storage Bucket.
to see the list of possible buckets to use.
Let’s say that we want to export our data to gs://myproject-abcd.appspot.com/ :
gcloud firestore export gs://myproject-abcd.appspot.com/
You can even export only some collections:
gcloud firestore export gs://myproject-abcd.appspot.com/ --collection-ids=[COLLECTION_ID_1],[COLLECTION_ID_2]
You can imagine it, right?
gcloud firestore import gs://myproject-abcd.appspot.com/2019-12-23T23:54:39_76544/
The operation takes some time (proportional to the data size that has to be imported). You can safely close your terminal, the operation will continue.
Checking operations status
Import and export can take time (import takes time even with a very small database).
To list the operations:
gcloud firestore operations list
And you will see something like:
You can look at your indexes:
This will list the indexes in a JSON-form of array of objects described as the following one:
We cannot perform other operations over indexes.
Collections and documents: delete recursively
It’s easy from the web console to delete a document, and it’s easy to do it programmatically.
It’s easy on the CLI, too.
But the nightmare of the web console (and of the programmatic approach, too) is that it does not exist a simple and fast way to recursively delete a collection.
This is luckily possible with the CLI.
You can recursively delete a collection by using:
firebase firestore:delete --recursive <collection>
Bonus: delete all collections
Now, assuming you are testing something and your firestore is full of garbage, you might want to start from scratch deleting every collection.
This is possible running:
firebase firestore:delete --all-collections
If you are looking at your database in the firebase console, please remember to refresh if the UI is not updated (that means that you still see the root collections).
This concludes the article for now.
I hope that the Firestore-side will be developed with other features and commands, because right now is very limited.
One of the most feature I can think of, generally speaking, would be the chance to “tail” logs in the shell.
I would be more than happy if someone can integrate with useful tools and additional stuff.
The post Exploring Firebase CLI with some Firestore tips appeared first on L.S..