Documentation
Define a model, save a document, and run a typed query in a few minutes.
This walkthrough uses the Express runtime (@dharayush7/fireclass-js), but the
model API is identical everywhere.
createFireclass takes your firebase-admin Firestore and returns a BaseModel
you extend. Do this once and reuse it.
import "reflect-metadata";
import { createFireclass } from "@dharayush7/fireclass-js";
import { getDb } from "./firebase.js";
export const { BaseModel } = createFireclass(getDb());Keep this file focused on values bound to your Firestore instance. Import decorators and standalone helpers directly from the SDK package where you use them.
Extend BaseModel, tag it with @Collection, and add class-validator
decorators. The constructor pattern is required so hydrated instances get their fields.
import { Collection } from "@dharayush7/fireclass-js";
import { IsEmail, IsInt, Min } from "class-validator";
import { BaseModel } from "../lib/fireclass.js";
@Collection("users")
export class User extends BaseModel<User> {
@IsEmail() email!: string;
@IsInt() @Min(0) age!: number;
constructor(data?: Partial<User>) {
super(data);
Object.assign(this, data);
}
}save() validates first, then writes — returning the new document id.
const user = new User({ email: "ada@example.com", age: 36 });
const id = await user.save(); // throws ValidationFailedError on bad inputFilters, ordering, and results are all typed against your model.
const adults = await User.findMany({
where: { age: { gte: 18 } },
orderBy: { age: "desc" },
limit: 20,
});
const ada = await User.findById(id);
await ada?.delete();That's it
The same BaseModel API — save, findById, findMany, findOne, count,
deleteById, deleteMany — works on the server and, with realtime hooks, in
React. Continue with Models & BaseModel for the complete
lifecycle, or use the CLI to generate the next model.