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.
Installation
Ok, easy.
Assuming you have npm installed, run the following command:
npm install -g firebase-tools
You will now have the command firebase, globally.
Now, run:
firebase login
to authenticate to your Firebase account (this will open your browser).
Firebase tasks
Seleting projects
List your firebase projects:
firebase list
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:
firebase functions:log
At this point I think there is no way to “listen” for changes in the log, that would be a very useful feature.
The
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.
Configuration
The functions configuration is handled through some commands:
firebase functions:get|set|unset|clone
to retrieve|store|remove|clone project configuration (respectively).
Emulate locally
Running
firebase functions:shell
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.
Delete functions
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:
firebase serve
When you’re ready to deploy your firebase project, you can run:
firebase deploy
Accounts management
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.
Database
I will skip this part. It’s very well documented everywhere in the firebase ecosystem.
Firestore
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.
Indexes (read)
You can look at your indexes:
firebase firestore:indexes
This will list the indexes in a JSON-form of array of objects described as the following one:
{ "indexes": [ { "collectionId": "<collection_id>", "fields": [ { "fieldPath": "<field_name_one>", "mode": "ASCENDING|DESCENDING" }, { "fieldPath": "<field_name_two>", "mode": "ASCENDING|DESCENDING" } ]}, {..} ]}
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).
Conclusion
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..