npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

firebase-shell

v0.0.2

Published

<div><img height="141" src="https://cloud.githubusercontent.com/assets/1671025/25864392/31541d12-34e7-11e7-9bed-aec2c1a2ef5d.png" /></div>

Downloads

5

Readme

Firebase Shell

Build Status npm version

A Node.js command line tool for executing queries against a Firebase database using an SQL-like syntax.

Why?

The Firebase UI console is great if you know the exact path of the data you want to view, but it doesn't provide any way to run a query against your data. The Firebase SDK has a nice API but if you want to query some data on the command line then you end up writing a non-trivial amount of code.

This tool aims to make it easy to query your Firebase data from the command line.

TLDR

$ select * from customers

┌──────────────────────┬────────────────────────────┐
│ Key                  │ Value                      │
├──────────────────────┼────────────────────────────┤
│ bsttRb+xm220Ig==     │ {                          │
│                      │   "name": "Sally",         │
│                      │   "age": 16,               │
│                      │ }                          │
├-─────────────────────┼────────────────────────────┤
│ HbJxvi0hUYIGpw==     │ {                          │
│                      │   "name": "Barry",         │
│                      │   "age": 31,               │
│                      │ }                          │
└──────────────────────┴────────────────────────────┘
Results: 2

Installing

firebase-shell can be installed locally or globally, whatever you prefer.

Global

$ npm install firebase-shell -g
$ firebase-shell -c path/to/credentials.json

Local

npm install firebase-shell
./node_modules/.bin/firebase-shell --credentils my-credentials.json

You could also add an entry to the scripts section of your package.json allowing you to use npm run to open a shell. For example:

{
    "scripts": {
        "fbs": "firebase-shell --credentials path/to/credentials.json"
    }
}

CLI Options

At minimum you need to provide a path to a credentials file for your Firebase database. This can be provided in the --credentials option or via the GOOGLE_APPLICATION_CREDENTIALS environment variable.

If you don't provide a value for the --url option then the project id will be extracted from the credentials file and the following url will be used.https://${PROJECT_ID}.firebaseio.com/. I think that this should be ok for most projects.

You can see all options by using --help:

$ firebase-shell --help

  Usage: firebase-shell [options]

  Options:

    -h, --help                      output usage information
    -c --credentials <credentials>  Path to your Firebase credentials file
    -u --url <databaseUrl>          URL of your Firebase database
    -a --auth <auth>                Auth uid variable override
    -o --output <output>            The format to use when outputing results. Options are table, json, and jsonPretty. Defaults to table.
    -q --query <query>              An optional query to run. If provided the result will be send to stdout and the program will be exited.

--auth

The value of the --auth option (if provided) will be used for the uid property of the databaseAuthVariableOverride option. So --auth admin would result in the app being initialized in the following way:

const admin = firebase.initializeApp({
    databaseAuthVariableOverride: {
        uid: 'admin',
    },
})

Pipeing output into another program

One use case might be to pipe the results of your query into another program, for example jq. You can acheive this by using the --query and --output options. For example:

$ firebase-shell --query 'select * from customers where age >= 18' --output json | jq '..|.name?|strings'
"Harry"
"Barry"

Running Queries

The following examples assume a database containing this data.

{
    "customers": {
        "A2103LLk1q04rA==": {
            "name": "Sally",
            "age": 16
        },
        "bsttRb+xm220Ig==": {
            "name": "Harry",
            "age": 20
        },
        "HbJxvi0hUYIGpw==": {
            "name": "Barry",
            "age": 31
        }
    }
}

For a good example of how firebase-shell makes it easy to query your data let's start with the following query.

$ select * from customers where age >= 18 limit 10

Which is equivalent to the following code.

admin
    .database()
    .ref('customers')
    .orderByChild('age')
    .startsAt(18)
    .limitToFirst(10)
    .on('value')
    .then(snapshot => {
        console.log(snapshot.val());
    });

The output of the query would look something like this:

┌──────────────────────┬────────────────────────────┐
│ Key                  │ Value                      │
├──────────────────────┼────────────────────────────┤
│ bsttRb+xm220Ig==     │ {                          │
│                      │   "name": "Harry",         │
│                      │   "age": 20,               │
│                      │ }                          │
├-─────────────────────┼────────────────────────────┤
│ HbJxvi0hUYIGpw==     │ {                          │
│                      │   "name": "Barry",         │
│                      │   "age": 31,               │
│                      │ }                          │
└──────────────────────┴────────────────────────────┘
Results: 2

SELECT <fields...>

You can easily imagine a situation where each customer object starts to get quite big as more fields are added. If you only want to see certain fields you can filter the resulting data by providing a comma-separated list of paths. In this example we actually don't know what the first part of the path will be as the keys are auto-generated uid's for each customer, but this is ok as you can make sections of a path match any key by using [].

$ select []/name from customers

┌──────────────────────┬────────────────────────────┐
│ Key                  │ Value                      │
├──────────────────────┼────────────────────────────┤
│ A2103LLk1q04rA==     │ {                          │
│                      │   "name": "Sally",         │
│                      │ }                          │
├-─────────────────────┼────────────────────────────┤
│ bsttRb+xm220Ig==     │ {                          │
│                      │   "name": "Harry",         │
│                      │ }                          │
├-─────────────────────┼────────────────────────────┤
│ HbJxvi0hUYIGpw==     │ {                          │
│                      │   "name": "Barry",         │
│                      │ }                          │
└──────────────────────┴────────────────────────────┘
Results: 2

SELECT accepts a comma-separated list of these paths. A path can contain multiple match-all ([]) sections.

The reason for using [] as the way of specifying a "match-all" part of a path is that Firebase doesn't allow [ or ] characters in keys, so there can never be a real key that is [].

FROM <path>

This is probably the simplest clause. The path value is used to create the root Reference that all subsequent operations will be performed against.

WHERE <path> <operator> <value>

A WHERE clause will cause an orderByChild operation on the query using the given path. Possible operators are:

Operator | Method ------------ | ------------- = or == | equalTo <= | startAt >= | endAt

The value can be a string literal (using single or double quotes), a number, true, false, or null.

LIMIT <count>

A LIMIT clause will cause a limitToFirst or limitToLast operation on the query depending on whether count is a positive negative number. For example LIMIT 5 would result in .limitToFirst(5) being called on the query whereas LIMIT -5 would result in .limitToLast(5) being used.