Documentation
Use the package
🧱 Schema Reference
This page uses the Product and Category schemas as a working example for
how to model your NestJS + Mongoose domain while taking full advantage of
nest-mongoose-crud.
Product schema overview
@Schema({ timestamps: true })
export class Product {
@Prop({ required: true })
name: string;
@Prop()
description: string;
@Prop({ required: true })
price: number;
@Prop({ type: Types.ObjectId, ref: 'Category', required: true })
category: Types.ObjectId;
@Prop()
brand: string;
@Prop({ default: 0 })
stock: number;
@Prop({ default: true })
isActive: boolean;
@Prop([String])
tags: string[];
@Prop([String])
images: string[];
@Prop({ default: 0 })
rating: number;
}
Field categories
name,description,brand- Text fields used for display, search, and filtering.
price,rating,stock- Numeric fields used for sorting, range filtering, and inventory logic.
isActive- Boolean control used for visibility and active/inactive state.
tags,images- Arrays used for discovery and richer product responses.
category- Relationship field referencing the
Categorycollection.
- Relationship field referencing the
Why this schema works well with CRUD helpers
requiredfields such asname,price, andcategorymake validation straightforward for create/update DTOs.isActiveis ideal for filtering visible vs. archived products.- Numeric fields like
price,rating, andstocksupport the advanced query operators used byAPIFeatures. - Array fields support multi-value filtering with comma-separated query parameters.
Category schema overview
@Schema({ timestamps: true })
export class Category {
@Prop({ required: true, unique: true })
name: string;
@Prop()
description: string;
@Prop({ default: true })
isActive: boolean;
@Prop()
image: string;
}
Category field notes
nameis unique and required, making it a strong candidate for a category lookup endpoint.isActivecan be used to disable categories while preserving associated product data.imageallows category listing pages to show a visual avatar or icon.
Recommended DTO shapes
CreateProductDto
export class CreateProductDto {
name: string;
description?: string;
price: number;
category: string;
brand?: string;
stock?: number;
isActive?: boolean;
tags?: string[];
images?: string[];
rating?: number;
}
UpdateProductDto
export class UpdateProductDto {
name?: string;
description?: string;
price?: number;
category?: string;
brand?: string;
stock?: number;
isActive?: boolean;
tags?: string[];
images?: string[];
rating?: number;
}
Practical use cases
- Use
ProductServiceto quickly expose endpoints for product catalogs, inventory management, and storefront product search. - Use
Categorypopulation to return category names inside product list responses without extra client-side queries. - Use
isActive,brand, andtagsto power storefront filters and admin dashboards.
Schema-driven query examples
Filter active products under $100
GET /products?isActive=true&price[lte]=100
Find electronics with strong reviews
GET /products?tags=electronics&rating[gte]=4.0
Search by name and category
GET /products?search=wireless:name,description&category=644d1f1ff4a4b23f8c6f9b67
Load category metadata with product results
GET /products?populate=category:name,image,description