Skip to content

JS library to execute SQL queries against Firebase databases

Notifications You must be signed in to change notification settings

JoeRoddy/firebase-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Firebase SQL

The Firebase SQL library accepts standard SQL queries and executes them as corresponding Firebase queries.

import fbsql from "fbsql";

// async:
const codingBlogs = await fbsql(`select * from blogs where genre = "coding";`);

// or apply a listener:
fbsql(`select * from users where online = true;`, onlineUsers => {
  // handle realtime updates...
});

Installation

npm install --save fbsql

Wait, but why?

Fbsql was extracted from the Firestation desktop app's source code to make issue tracking easier.

This library may be useful for:

  • Developers who prefer SQL syntax to the Firebase API
  • Saving time writing complicated functions that could be achieved with a one line SQL statement
  • Improving code clarity:
// firebase-sql
const codingBlogs = await fbsql(`select * from blogs where genre = "coding";`);

// realtime db
const snapshot = await firebase
  .database()
  .ref("/blogs/")
  .orderByChild("genre")
  .equalTo("coding")
  .once("value");
const codingBlogs = snapshot.val();

// firestore
const doc = await firebase
  .firestore()
  .collection("blogs")
  .where("genre", "==", "coding")
  .get();
const codingBlogs = doc.data();

Setup

Wherever you initialize firebase:

import firebase from "firebase/app"; // import * as firebase from "firebase-admin";
import { configureFbsql } from "fbsql";

firebase.initializeApp({ your config... });
configureFbsql({ app: firebase });

If you run into errors saying app.database() is not a function, you may need to import firebase into the file causing the issue. import firebase from "firebase/app";

Configuration

You have multiple configuration options through the configureFbsql function:

import fbsql, { configureFbsql } from "fbsql";

// pass any combination of options
// below are the defaults
configureFbsql({
  app: null // your firebase app
  isFirestore: false, // use firestore instead of the realtime db?
  shouldCommitResults: true, // commit changes on inserts, updates, deletes?
  shouldExpandResults: false // return a more detailed res obj from queries?
});

About

JS library to execute SQL queries against Firebase databases

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published