0
0
NestJSframework~10 mins

Schema definition in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Schema definition
Define Schema Class
Add Decorators (@Schema, @Prop)
Create Schema Factory
Use Schema in Module
Inject Model in Service
Perform DB Operations
This flow shows how to define a schema class with decorators, create a schema factory, and use it in NestJS modules and services for database operations.
Execution Sample
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema()
export class Cat extends Document {
  @Prop() name: string;
  @Prop() age: number;
}

export const CatSchema = SchemaFactory.createForClass(Cat);
Defines a Cat schema with name and age properties using decorators and creates a schema factory for use in NestJS.
Execution Table
StepActionCode LineResult
1Import decorators and Documentimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose';Decorators and Document class available
2Define Cat class with @Schema decorator@Schema() export class Cat extends Document { ... }Cat class marked as schema
3Add @Prop() to name property@Prop() name: string;name property registered in schema
4Add @Prop() to age property@Prop() age: number;age property registered in schema
5Create schema factoryexport const CatSchema = SchemaFactory.createForClass(Cat);CatSchema created from Cat class
6Use CatSchema in module importsMongooseModule.forFeature([{ name: Cat.name, schema: CatSchema }])Schema registered in module
7Inject Cat model in serviceconstructor(@InjectModel(Cat.name) private catModel: Model<Cat>) {}Model ready for DB operations
8Perform DB operationsthis.catModel.create({ name: 'Tom', age: 3 })New Cat document saved in DB
9ExitEnd of schema definition and usage flowSchema fully defined and usable
💡 Schema class defined, schema factory created, and schema integrated into NestJS module and service for database use.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7Final
Cat classundefinedDefined as schema classUsed to create CatSchemaUsed as model injection tokenUsed for DB operations
CatSchemaundefinedundefinedCreated from Cat classRegistered in moduleUsed in service for DB
catModelundefinedundefinedundefinedInjected Model<Cat>Used to create documents
Key Moments - 3 Insights
Why do we use @Schema() decorator on the class?
The @Schema() decorator marks the class as a schema definition so NestJS knows to create a Mongoose schema from it, as shown in step 2 and step 5.
What does @Prop() do on class properties?
@Prop() tells NestJS which properties to include in the schema. Without it, the property won't be part of the database schema, as seen in steps 3 and 4.
How is the schema connected to the database model?
The schema factory creates a schema from the class (step 5), which is then registered in the module (step 6) and injected as a model in the service (step 7) for DB operations.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the CatSchema created?
AStep 3
BStep 5
CStep 7
DStep 2
💡 Hint
Check the 'Action' column for 'Create schema factory' in the execution table.
According to variable_tracker, what is the state of catModel after step 7?
AUndefined
BSchema class
CInjected Model<Cat>
DSchema factory
💡 Hint
Look at the 'catModel' row under 'After Step 7' in variable_tracker.
If we remove @Prop() from a property, what happens in the execution flow?
AProperty is not part of the schema
BSchema creation fails
CProperty is still included in schema
DModel injection fails
💡 Hint
Refer to key_moments about the role of @Prop() and steps 3-4 in execution_table.
Concept Snapshot
NestJS Schema Definition:
- Use @Schema() on a class to mark it as a schema.
- Use @Prop() on properties to include them in the schema.
- Create schema with SchemaFactory.createForClass(Class).
- Register schema in module with MongooseModule.forFeature.
- Inject model in service with @InjectModel(Class.name).
- Use model for DB operations.
Full Transcript
In NestJS, schema definition starts by importing necessary decorators and Document from mongoose. You define a class and decorate it with @Schema() to mark it as a schema. Each property you want in the database schema is decorated with @Prop(). Then, you create a schema factory using SchemaFactory.createForClass with your class. This schema is registered in your module using MongooseModule.forFeature. In your service, you inject the model using @InjectModel with the class name. This model lets you perform database operations like creating or querying documents. The execution table shows each step from defining the class to using the model. The variable tracker follows how the class, schema, and model variables change through these steps. Key moments clarify why decorators are needed and how schema connects to the model. The quiz tests understanding of when schema is created, model injection, and the role of @Prop(). This process helps you organize your data structure clearly and use it easily in your NestJS app.