Skip to content

dharayush7/fireclass-js

Repository files navigation

@dharayush7/fireclass-js

npm version license types

Type-safe Firestore models for Node.js and Express, powered by firebase-admin. The server-side runtime of the Fireclass suite — it wraps @dharayush7/fireclass-core with a firebase-admin adapter and re-exports everything, so you install one package.

Replaces the deprecated @dharayush7/fireclass. Migration is a one-line change — see Migration.

Install

npm install @dharayush7/fireclass-js firebase-admin class-validator class-transformer

firebase-admin, class-validator, and class-transformer are peer dependencies. Enable decorators in tsconfig.json:

{ "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true } }

Quick start

import "reflect-metadata";
import { getFirestore } from "firebase-admin/firestore";
import { createFireclass, Collection } from "@dharayush7/fireclass-js";
import { IsEmail, IsString, Length, IsInt, Min } from "class-validator";

const { BaseModel } = createFireclass(getFirestore());

@Collection("users")
class User extends BaseModel<User> {
  @IsString() @Length(2, 50) name!: string;
  @IsEmail() email!: string;
  @IsInt() @Min(0) age!: number;

  constructor(data?: Partial<User>) {
    super(data);
    Object.assign(this, data);
  }
}

// create (validates first) — returns the new document id
const id = await new User({ name: "Ada", email: "ada@example.com", age: 36 }).save();

// query — typed operators: equals, gt, gte, lt, lte, in, notIn, arrayContains, ...
const adults = await User.findMany({
  where: { age: { gte: 18 } },
  orderBy: { name: "asc" },
  limit: 20,
});

// read / update / delete
const user = await User.findById(id);
user!.age = 37;
await user!.save();            // has id -> merge update
await user!.delete();

A complete, runnable Express CRUD API is in examples/express-api.

API

createFireclass(firestore)

Returns { BaseModel, adapter }. Call once with a firebase-admin Firestore instance and reuse BaseModel. Instances have save() / delete(); the class has findById / findMany / findOne / count / deleteById / deleteMany. See the core API for the full surface and query operators (all re-exported here).

fireclassErrorHandler(options?)

Express error middleware. Turns ValidationFailedError into a 400 with field-level details, other Fireclass errors into a 400 (configurable), and forwards everything else.

app.use(fireclassErrorHandler());

AdminAdapter

The firebase-admin implementation of the core FirestoreAdapter (exported for advanced use / testing). Provides batchDelete and aggregate count; realtime subscribe is intentionally omitted (that lives in fireclass-react).

Migration

From the deprecated @dharayush7/fireclass:

- import { getBaseModel } from "@dharayush7/fireclass";
+ import { getBaseModel } from "@dharayush7/fireclass-js";

getBaseModel(firestore) still works (it returns createFireclass(firestore).BaseModel). Note the query operator is equals (the old docs incorrectly showed equal).

License

MIT © Ayush Dhar