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

@sortd-io/firesql

v2.0.3

Published

Query Cloud Firestore for Firebase using SQL

Downloads

2

Readme

FireSQL - Query Firestore using SQL syntax

What is FireSQL?

FireSQL is a library built on top of the official Firebase SDK that allows you to query Cloud Firestore using SQL syntax. It's smart enough to issue the minimum amount of queries necessary to the Firestore servers in order to get the data that you request.

On top of that, it offers some of the handy utilities that you're used to when using SQL, so that it can provide a better querying experience beyond what's offered by the native querying methods.

Installation

Just add firesql and firebase to your project:

npm install firesql firebase
# or
yarn add firesql firebase

If you want to receive realtime updates when querying, then you will also need to install rxjs and rxfire:

npm install firesql firebase rxjs rxfire
# or
yarn add firesql firebase rxjs rxfire

Usage

// You can either query the collections at the root of the database...
const dbRef = firebase.firestore();

// ... or the subcollections of some document
const docRef = firebase.firestore().doc('someDoc');

// And then just pass that reference to FireSQL
const fireSQL = new FireSQL(dbRef);

// Use `.query()` to get a one-time result
fireSQL.query('SELECT * FROM myCollection').then(documents => {
  documents.forEach(doc => {
    /* Do something with the document */
  });
});

// Use `.rxQuery()` to get an observable for realtime results.
// Don't forget to import "firesql/rx" first (see example below).
fireSQL.rxQuery('SELECT * FROM myCollection').subscribe(documents => {
  /* Got an update with the documents! */
});

Examples

One-time result (Promise)

import { FireSQL } from 'firesql';
import firebase from 'firebase/app';
import 'firebase/firestore';

firebase.initializeApp({ /* ... */ });

const fireSQL = new FireSQL(firebase.firestore());

const citiesPromise = fireSQL.query(`
  SELECT name AS city, country, population AS people
  FROM cities
  WHERE country = 'USA' AND population > 700000
  ORDER BY country, population DESC
  LIMIT 10
`);

citiesPromise.then(cities => {
  for (const city of cities) {
    console.log(
      `${city.city} in ${city.country} has ${city.people} people`
    );
  }
});

Realtime updates (Observable)

import { FireSQL } from 'firesql';
import firebase from 'firebase/app';
import 'firesql/rx'; // <-- Important! Don't forget
import 'firebase/firestore';

firebase.initializeApp({ /* ... */ });

const fireSQL = new FireSQL(firebase.firestore());

const cities$ = fireSQL.rxQuery(`
  SELECT city, category, AVG(price) AS avgPrice
  FROM restaurants
  WHERE category IN ("Mexican", "Indian", "Brunch")
  GROUP BY city, category
`);

cities$.subscribe(results => {
  /* REALTIME AGGREGATED DATA! */
});

Limitations

  • Only SELECT queries for now. Support for INSERT, UPDATE, and DELETE might come in the future.
  • No support for JOINs.
  • LIMIT doesn't accept an OFFSET, only a single number.
  • No support for aggregate function COUNT.
  • If using GROUP BY, it cannot be combined with ORDER BY nor LIMIT.
  • No support for negating conditions with NOT.
  • Limited LIKE. Allows for searches in the form of WHERE field LIKE 'value%', to look for fields that begin with the given value; and WHERE field LIKE 'value', which is functionally equivalent to WHERE field = 'value'.

Nested objects

You can access nested objects by using backticks around the field path. For example, if you have a collection "products" with documents like this:

{
  productName: "Firebase Hot Sauce",
  details: {
    available: true,
    stock: 42
  }
}

You could do the following queries:

SELECT *
FROM products
WHERE `details.stock` > 10
SELECT productName, `details.stock` AS productStock
FROM products
WHERE `details.available` = true

Getting the document IDs

You can use the special field __name__ to refer to the document ID (its key inside a collection). For convenience, you might want to alias it:

SELECT __name__ AS docId, country, population
FROM cities

If you always want to include the document ID, you can specify that as a global option to the FireSQL class:

const fireSQL = new FireSQL(ref, { includeId: true}); // To include it as "__name__"
const fireSQL = new FireSQL(ref, { includeId: 'fieldName'}); // To include it as "fieldName"

You can also specify that option when querying. This will always take preference over the global option:

fireSQL.query(sql, { includeId: 'id'}); // To include it as "id"
fireSQL.query(sql, { includeId: false}); // To not include it

When querying it's also possible to use the document as a search field by using __name__ directly. For example, you could search for all the documents whose IDs start with Hello:

SELECT *
FROM cities
WHERE __name__ LIKE 'Hello%'

Note: You will need to specify the includeId option if you want to obtain the document IDs when doing a SELECT * query.

Collection group queries

You can easily do collection group queries with FireSQL!

This query will get all documents from any collection or subcollection named "landmarks":

SELECT *
FROM GROUP landmarks

You can read more about collection group queries in the official Firestore documentation.

Array membership queries

It's as simple as using the CONTAINS condition:

SELECT *
FROM posts
WHERE tags CONTAINS 'interesting'

@sortd-io's fork has added the CONTAINS_ANY condition:

SELECT *
FROM posts
WHERE tags CONTAINS_ANY ('interesting', 'cool')

Only one CONTAINS or CONTAINS_ANY condition can be used per query.

You can read more about array membership queries in the official Firestore documentation.

How does FireSQL work?

FireSQL transforms your SQL query into one or more queries to Firestore. Once all the necessary data has been retrieved, it does some internal processing in order to give you exactly what you asked for.

For example, take the following SQL:

SELECT *
FROM cities
WHERE country = 'USA' AND population > 50000

This would get transformed into this single Firestore query:

db.collection('cities')
  .where('country', '==', 'USA')
  .where('population', '>', 50000);

That's pretty straightforward. But what about this one?

SELECT *
FROM cities
WHERE country = 'USA' OR population > 50000

There's no direct way to perform an OR query on Firestore so FireSQL splits that into 2 separate queries:

db.collection('cities').where('country', '==', 'USA');
db.collection('cities').where('population', '>', 50000);

The results are then merged and any possible duplicates are eliminated.

The same principle applies to any other query. Sometimes your SQL will result in a single Firestore query and some other times it might result in several.

For example, take a seemingly simple SQL statement like the following:

SELECT *
FROM cities
WHERE country != 'Japan' AND region IN ('north', 'east', 'west') AND (capital = true OR population > 100000)

This will need to launch a total of 12 concurrent queries to Firestore!

const cities = db.collection('cities');
cities.where('country', '<', 'Japan').where('region', '==', 'north').where('capital', '==', true);
cities.where('country', '<', 'Japan').where('region', '==', 'north').where('population', '>', 100000);
cities.where('country', '<', 'Japan').where('region', '==', 'east').where('capital', '==', true);
cities.where('country', '<', 'Japan').where('region', '==', 'east').where('population', '>', 100000);
cities.where('country', '<', 'Japan').where('region', '==', 'west').where('capital', '==', true);
cities.where('country', '<', 'Japan').where('region', '==', 'west').where('population', '>', 100000);
cities.where('country', '>', 'Japan').where('region', '==', 'north').where('capital', '==', true);
cities.where('country', '>', 'Japan').where('region', '==', 'north').where('population', '>', 100000);
cities.where('country', '>', 'Japan').where('region', '==', 'east').where('capital', '==', true);
cities.where('country', '>', 'Japan').where('region', '==', 'east').where('population', '>', 100000);
cities.where('country', '>', 'Japan').where('region', '==', 'west').where('capital', '==', true);
cities.where('country', '>', 'Japan').where('region', '==', 'west').where('population', '>', 100000);

As you can see, SQL offers a very concise and powerful way to express your query. But as they say, with great power comes great responsibility. Always be mindful of the underlying data model when using FireSQL.

Examples of supported queries:

SELECT *
FROM restaurants
SELECT name, price
FROM restaurants
WHERE city = 'Chicago'
SELECT *
FROM restaurants
WHERE category = 'Indian' AND price < 50
SELECT *
FROM restaurants
WHERE name LIKE 'Best%'
SELECT *
FROM restaurants
WHERE name LIKE 'Best%' OR city = 'Los Angeles'
SELECT *
FROM restaurants
WHERE city IN ( 'Raleigh', 'Nashvile', 'Denver' )
SELECT *
FROM restaurants
WHERE city != 'Oklahoma'
SELECT *
FROM restaurants
WHERE favorite = true
SELECT *
FROM restaurants
WHERE favorite -- Equivalent to the previous one
SELECT *
FROM restaurants
WHERE favorite IS NULL
SELECT AVG(price) AS averagePriceInChicago
FROM restaurants
WHERE city = 'Chicago'
SELECT city, MIN(price), AVG(price), MAX(price)
FROM restaurants
WHERE category = 'Indian'
GROUP BY city
SELECT *
FROM restaurants
WHERE city = 'Memphis' AND ( price < 40 OR avgRating > 8 )
ORDER BY price DESC, avgRating
SELECT *
FROM restaurants
WHERE price BETWEEN 25 AND 150
ORDER BY city, price
LIMIT 10
SELECT *
FROM restaurants
WHERE city = 'Chicago'
UNION
SELECT *
FROM restaurants
WHERE price > 200