Does Firebase Work With Drizzle ORM?
You can use Drizzle ORM with Firebase, but only with Firestore or Cloud SQL—not Realtime Database—and it requires careful architectural decisions.
Quick Facts
How Firebase Works With Drizzle ORM
Firebase and Drizzle ORM can work together, but with important limitations. Drizzle is a SQL-first ORM designed for traditional SQL databases, while Firebase's primary offering—Realtime Database—is NoSQL and document-based. However, if you use Firebase's Cloud SQL connector or Firestore with a separate Cloud SQL instance, Drizzle becomes viable. The typical architecture involves using Firebase Authentication and other Firebase services (hosting, storage) alongside a Cloud SQL database managed by Drizzle ORM. You'd handle auth through Firebase Admin SDK and queries through Drizzle. The developer experience is smooth for the SQL layer, but you're essentially using Firebase as a backend service suite rather than a unified database solution. This works well for teams wanting Firebase's infrastructure but needing relational data—common in e-commerce and SaaS apps.
Best Use Cases
Firebase Cloud Function with Drizzle ORM
npm install firebase-admin drizzle-orm @google-cloud/sql-connector pgimport * as functions from 'firebase-functions';
import { drizzle } from 'drizzle-orm/node-postgres';
import { users } from './schema';
import { Connector } from '@google-cloud/sql-connector';
import postgres from 'pg';
const connector = new Connector();
const pool = new postgres.Pool({
user: 'postgres',
password: process.env.DB_PASSWORD,
database: 'myapp',
max: 5,
});
const db = drizzle(pool);
export const createUser = functions.https.onCall(async (data, context) => {
if (!context.auth) throw new functions.https.HttpsError('unauthenticated', 'Auth required');
const result = await db.insert(users).values({
email: data.email,
firebaseUid: context.auth.uid,
}).returning();
return result[0];
});Known Issues & Gotchas
Firebase Realtime Database has no SQL interface—Drizzle cannot query it directly
Fix: Use Cloud SQL instead, or accept that Realtime Database queries must bypass Drizzle entirely
Connection pooling and Firebase Cloud Functions have timeout constraints that can interrupt long Drizzle queries
Fix: Keep queries optimized, use serverless-compatible connection pools (PgBouncer), or implement query timeouts
Firebase Auth tokens and Drizzle database credentials are separate concerns requiring dual authentication logic
Fix: Use Firebase Admin SDK in backend functions to validate tokens before executing Drizzle queries; never expose DB credentials to client
Mixing Firestore and Cloud SQL can create eventual consistency issues and data synchronization challenges
Fix: Clearly separate concerns—use Firestore for real-time data, Cloud SQL+Drizzle for transactional/relational data
Alternatives
- •Supabase + Drizzle ORM: PostgreSQL-first with built-in Auth and Drizzle support
- •Firebase Firestore + Prisma: NoSQL-native with better Firebase integration
- •PlanetScale + Drizzle: MySQL serverless optimized for Drizzle, pair with Firebase Auth separately
Resources
Related Compatibility Guides
Explore more compatibility guides