Firebase Firestore Support

BlazeKit can generate a complete Firebase Firestore controller for your models. The generated code uses the Firebase Client SDK (not Admin SDK) and assumes you have a Firestore instance exported as db.

What BlazeKit Generates

For each model, BlazeKit creates a Firebase Firestore controller with:

  • Firestore Setup — Collection references and Firestore operations imported automatically.
  • CRUD Functions — Create, Read, Update, and Delete operations for the model's collection.

Example Output

Suppose you have a model User:

schema.blaze

model User {
  name: string
  email: string
  age: number
}

BlazeKit will generate the following Firebase Firestore controller:

controllers/user.ts

// Firebase Firestore Setup (auto-generated)
import { db } from "../lib/firebase";
import {
  collection,
  doc,
  getDoc,
  getDocs,
  addDoc,
  updateDoc,
  deleteDoc,
} from "firebase/firestore";
import { User } from "../types";

// CRUD Operations for User

export async function createUser(data: User) {
  const colRef = collection(db, "users");
  const docRef = await addDoc(colRef, data);
  const snapshot = await getDoc(docRef);
  return { id: docRef.id, ...snapshot.data() } as User;
}

export async function getAllUsers() {
  const colRef = collection(db, "users");
  const snapshot = await getDocs(colRef);
  return snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })) as User[];
}

export async function getUserById(id: string) {
  const docRef = doc(db, "users", id);
  const snapshot = await getDoc(docRef);
  if (!snapshot.exists()) return null;
  return { id: snapshot.id, ...snapshot.data() } as User;
}

export async function updateUser(id: string, data: Partial<User>) {
  const docRef = doc(db, "users", id);
  await updateDoc(docRef, data);
  return getUserById(id);
}

export async function deleteUser(id: string) {
  const docRef = doc(db, "users", id);
  await deleteDoc(docRef);
  return { id };
}

Notes

  • BlazeKit expects your Firestore instance to be exported from lib/firebase.ts (or a custom path you provide).
  • Collection names are generated by taking the model name in lowercase and adding an "s" (e.g., Userusers).
  • All documents returned will include an id field alongside the document data.