The Firebase CLI: now with database commands
With the latest Firebase CLI release, it can do much more than just hosting. The latest release gives you the power to read and write data from your Firebase database.
These new data commands simplify tasks like seeding, exporting, and even transferring data from one Firebase database to another. Now you can spend less time writing data seeding scripts and more time developing your app.
This article will cover a few tricks to do some common data operation tasks.
Data seeding
To save data from the command-line use the data:set
command.
firebase data:set /messages messages.json -f my-firebase-db
The first argument is the path you want to write the data to and the second is the JSON file to read from. The last argument is the Firebase to execute the operation against. If you use a /
it will save to the root and overwrite existing data. This is perfect for seeding your database.
firebase data:set / seed.json -f my-firebase-db
When your database loses its test data or things get out of whack, you can reseed your database with a simple JSON file.
You'll get asked if you really want to overwrite the data in your database. To skip this message and move with confidence you can provide the -y
flag.
firebase data:set / seed.json -f my-firebase-db -y
-f is for Firebase
Most developers have a healthy fear of flags labeled -f. But with Firebase, -f
is your friend.
The -f
flag enables you to specify which Firebase database you’re running the data command against.
firebase data:set /status status.json -f other-firebase-db
This command saves the contents of status.json
into the status path of the other-firebase-db
Firebase database.
The -f
flag opens up a larger set of possibilities, like transferring data to another Firebase database.
Data exports
Reading data from the CLI is also a simple one-liner.
firebase data:get /messages -f my-firebase-db
The data:get
command works just like the data:set
command. You can also provide a JSON file to store the data locally.
firebase data:get /messages > messages.json -f my-firebase-db
If your Firebase database is under 256mb, this is a great way to create a seed to work from.
firebase data:get / > seed.json -f my-firebase-db
Formatting JSON
You'll notice that your JSON comes back unformatted which isn't the best for human eyes. The data:get
command allows you to pipe the result to another command like Python’s json.tool
(which comes installed on standard OSX instances).
firebase data:get /messages -f my-firebase-db | python -m json.tool
The |
is the symbol for piping the output to another source. In this case the json.tool
module consumes the piped results from the data:get
command.
If you’re looking for something even more readable, try using the npm module prettyjson. This module formats JSON into a colored YAML format that’s easy to read from the command-line.
davideast: age: 27 happy: true name: David East erlichbachman: age: 34 happy: false name: Erlich Bachman
The prettyjson
module isn't bundled with the CLI so you'll have to install it on your machine. But if you prefer another formatting module, the CLI will pipe the results there too.
npm install -g prettyjson
Data transfers
Transferring data from one Firebase database to another is again a simple one-liner.
firebase data:get / -f my-firebase-db | firebase data:set / -f another-firebase -y
This command is especially helpful if you need to move data to another environment. See our previous blog post for more tips on managing multiple environments with the CLI.
Default Firebase database
If the project directory contains a firebase.json
file, the commands will default to the Firebase database in the JSON file. To create a firebase.json
file just use the command:
firebase init
Now when you run commands within that directory you can omit the -f
flag.
firebase data:get /messages messages.json
bash functions
If you find yourself repeating a set of commands it’s probably time to make a bash function. Save your function to your .bash_profile
and you’ll be able to access them from anywhere in your console.
If you commonly transfer data between Firebase databases the function below makes it simple.
function transfer_to() { local master_db="${1}" local dest_db="${2}" local path="${3:-/}" firebase data:get "$path" -f "$master_db" | firebase data:set "$path" -f "$dest_db" -y }
To use the function just call the transfer_to
command with the destination Firebase database.
transfer_to dev-firebase-db staging-firebase-db
Another useful function is for formatting data.
function formatted() { local db="${1}" local path="${2:-/}" firebase data:get "$path" -f "$db" | python -m json.tool }
The formatted
function takes in a Firebase database and the specified path. The outputted JSON is piped to Python’s json.tool.
formatted my-firebase-db /users
You can check out the community project firebase-dot-files on GitHub to contribute your tricks.
The Firebase CLI is more than just hosting. How do you seed your Firebase database? Drop us a comment and let us know if you’re using the new CLI features in your workflow.