Documentation
Map model classes to Firestore collections and inspect Fireclass collection metadata.
Fireclass decorators attach collection metadata to a model class. Import them directly from the SDK package used by the current runtime.
@Collection(name) maps a model to a top-level Firestore collection.
import { Collection } from "@dharayush7/fireclass-js";
import { IsString } from "class-validator";
import { BaseModel } from "../lib/fireclass.js";
@Collection("posts")
export class Post extends BaseModel<Post> {
@IsString()
title!: string;
constructor(data?: Partial<Post>) {
super(data);
Object.assign(this, data);
}
}The decorator stores "posts" on the model constructor. Every instance and
static method resolves that value before it touches the adapter.
await Post.findMany(); // queries the "posts" collection
await new Post({ title: "Hello" }).save(); // adds to "posts"Use the exact collection name configured in Firestore. Fireclass does not pluralize, normalize, or otherwise change it.
Models must have either @Collection or @Subcollection. Fireclass fails fast
when an undecorated model is constructed or a static model method is called.
export class Draft extends BaseModel<Draft> {}
new Draft();
// MissingCollectionError: No @Collection decorator found for "Draft".This catches configuration mistakes before a read or write reaches Firestore.
@Subcollection(name, { parent }) records a collection name and the parent
model constructor.
import { Subcollection } from "@dharayush7/fireclass-js";
import { BaseModel } from "../lib/fireclass.js";
import { Post } from "./post.js";
@Subcollection("comments", { parent: Post })
export class Comment extends BaseModel<Comment> {
body!: string;
constructor(data?: Partial<Comment>) {
super(data);
Object.assign(this, data);
}
}Nested path resolution is not implemented
The current release stores the parent as metadata only. Model CRUD resolves the
collection as comments; it does not construct a path such as
posts/{postId}/comments. Do not use @Subcollection as a nested-write API yet.
Use the Firebase SDK directly for nested collection paths until a parent-id aware Fireclass API is released.
getCollectionName() returns the stored collection name or undefined.
import {
getCollectionName,
Collection,
} from "@dharayush7/fireclass-js";
@Collection("accounts")
class Account {}
getCollectionName(Account); // "accounts"
getCollectionName(class Undecorated {}); // undefinedThis helper is useful for integrations, logging, and custom adapter tooling. Application CRUD normally does not need to call it.
The Fireclass collection decorators themselves store static metadata and do not
depend on reflect-metadata. Validation decorators do, so a normal Fireclass
application should still load it once in the runtime entry:
import "reflect-metadata";Enable legacy TypeScript decorators and emitted type metadata:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Import decorators from the installed runtime SDK, not from the initialized local Fireclass file:
// Node / Express
import { Collection } from "@dharayush7/fireclass-js";
// Next.js server code
import { Collection } from "@dharayush7/fireclass-ssr";
// React
import { Collection } from "@dharayush7/fireclass-react";The runtime packages re-export the modeling core. The local initialization file
exists only to bind values such as BaseModel and adapter to a Firestore
instance.
Continue with Validation or review the complete Models & BaseModel lifecycle.