Database-js Interface for Firebase
database-js-firebase is an adapter that provides an SQL-like interface to Google Firebase's NoSQL database, integrating with the `database-js` abstraction layer. Currently at version 1.2.2, the project appears to be abandoned, with the last code commits occurring in 2018 for this package and 2019 for its primary dependency, `database-js`. It translates a subset of SQL commands (SELECT, INSERT, UPDATE, DELETE, CREATE) into Firebase Realtime Database operations and provides Promises for asynchronous execution. Key differentiators include support for WHERE, GROUP BY, LIMIT, and INNER/LEFT/RIGHT JOINs, along with aggregate functions like COUNT and SUM. However, it imposes significant restrictions, such as requiring data storage via Firebase's `ref.push`, limiting authentication to email and password, and restricting interactions with JSON-typed fields. It does not create 'tables' in Firebase, as Firebase is schemaless. Due to its inactivity, it lacks compatibility with newer Firebase SDK versions or modern JavaScript features like ESM.
Common errors
-
Error: Driver not found for protocol 'database-js-firebase:'
cause The `database-js-firebase` package was not installed or correctly registered with `database-js`.fixEnsure both `database-js` and `database-js-firebase` are installed: `npm install database-js database-js-firebase`. -
FirebaseError: Firebase: Error (auth/invalid-email).
cause The email provided in the connection string is not a valid format or does not correspond to an existing Firebase user account.fixVerify the email format and ensure it matches an authenticated user in your Firebase project. -
FirebaseError: Firebase: Error (auth/wrong-password).
cause The password provided in the connection string is incorrect for the specified Firebase user.fixDouble-check the password in your connection string for accuracy. -
Error: SQL command 'OUTER JOIN' not supported.
cause Attempting to use an SQL command (like OUTER JOIN) that this library's SQL interpreter does not support.fixRefactor your SQL query to use only supported commands and join types (INNER, LEFT, RIGHT JOINs).
Warnings
- breaking The project appears to be abandoned, with no significant updates since 2018-2019. This means it is highly unlikely to be compatible with newer Firebase SDK versions, recent Node.js runtimes, or modern JavaScript features (e.g., native ESM support). It will not receive security patches, bug fixes, or new features.
- gotcha The SQL-like interface has significant functional limitations. Notably, `OUTER JOIN` is not supported, and JSON values cannot be directly UPDATEd, INSERTed, or used in WHERE clauses.
- gotcha Authentication is strictly limited to email and password within the connection string. This method is insecure and does not support modern authentication flows such as OAuth, multi-factor authentication, or token-based authentication (e.g., from the Firebase Authentication SDK).
- gotcha Data storage is restricted to Firebase's `ref.push` pattern, which automatically generates unique, sequential keys for new entries. This may not be suitable for use cases requiring specific, user-defined keys or custom sorting.
- gotcha The `CREATE` SQL command in this library does not create a table structure in Firebase, as Firebase Realtime Database is a NoSQL database without a rigid schema. It essentially acts as a no-op for schema definition.
Install
-
npm install database-js-firebase -
yarn add database-js-firebase -
pnpm add database-js-firebase
Imports
- Database
const Database = require('database-js2'); // This refers to an unrelated package or an outdated alias. import { Database } from 'database-js-firebase'; // database-js-firebase is a driver, not the primary class export.import { Database } from 'database-js';
Quickstart
import { Database } from 'database-js';
(async () => {
let connection, statement, rows;
// Set these environment variables or replace with actual values
const firebaseEmail = process.env.FIREBASE_EMAIL ?? 'your-email@example.com';
const firebasePassword = process.env.FIREBASE_PASSWORD ?? 'your-password';
const firebaseProjectId = process.env.FIREBASE_PROJECT_ID ?? 'your-project-id';
const firebaseRootNodePath = process.env.FIREBASE_ROOT_NODE_PATH ?? 'your-root-node-path';
const firebaseApiKey = process.env.FIREBASE_API_KEY ?? 'your-api-key';
const connectionString = `database-js-firebase://${firebaseEmail}:${firebasePassword}@${firebaseProjectId}/${firebaseRootNodePath}?apiKey=${firebaseApiKey}`;
connection = new Database(connectionString);
try {
console.log('Attempting to connect to Firebase Realtime Database...');
// Example: Assume a 'users' collection with 'username' field
statement = await connection.prepareStatement("SELECT * FROM users WHERE username = ?");
rows = await statement.query('dduck');
console.log('Query results:', rows);
} catch (error) {
console.error('An error occurred:', error);
} finally {
if (connection) {
await connection.close();
console.log('Connection closed.');
}
}
})();