0
0
NestJSframework~10 mins

Entity definition in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Entity definition
Define Entity Class
Add @Entity Decorator
Define Properties with @Column
Add Primary Key with @PrimaryGeneratedColumn
Use Entity in Repository
Database Table Created
CRUD Operations on Entity
This flow shows how to define an entity class with decorators, which maps to a database table used for data operations.
Execution Sample
NestJS
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}
Defines a User entity with an auto-generated id and a name column.
Execution Table
StepActionDecorator/PropertyEffectResulting Entity State
1Define class UserNoneClass createdUser class exists with no decorators
2Add @Entity decorator@Entity()Marks class as DB entityUser class mapped to DB table 'user'
3Add @PrimaryGeneratedColumn to id@PrimaryGeneratedColumn()id is primary key, auto-generatedid property set as PK with auto increment
4Add @Column to name@Column()name is a regular columnname property mapped to DB column
5Compile and runN/AEntity registered with ORMUser entity ready for DB operations
6Use repository to save UserN/AInsert SQL generatedNew user row inserted in DB
7Use repository to find UserN/ASelect SQL generatedUser data retrieved from DB
💡 Entity class fully defined and ready for database CRUD operations
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
User classDefinedMarked as entityid property set as PKname property addedComplete entity with id and name
Key Moments - 3 Insights
Why do we need the @Entity decorator?
The @Entity decorator tells NestJS and TypeORM that this class represents a database table. Without it, the class is just a normal TypeScript class and won't map to the database. See execution_table step 2.
What does @PrimaryGeneratedColumn do?
@PrimaryGeneratedColumn marks a property as the primary key and tells the database to auto-generate its value. This is important for uniquely identifying rows. See execution_table step 3.
Can we have properties without @Column decorator?
Properties without @Column are not saved in the database. Only properties with @Column or special decorators like @PrimaryGeneratedColumn are mapped. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what decorator marks the class as a database entity?
A@Entity()
B@Column()
C@PrimaryGeneratedColumn()
DNo decorator needed
💡 Hint
Check execution_table step 2 where the class is marked as an entity.
At which step is the primary key defined in the entity?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 3 where @PrimaryGeneratedColumn is added.
If we remove @Column from the name property, what happens?
Aname is still saved in the database
Bname becomes the primary key
Cname is not saved in the database
DEntity definition fails to compile
💡 Hint
Refer to key_moments about properties without @Column decorator.
Concept Snapshot
Entity definition in NestJS with TypeORM:
- Use @Entity() to mark a class as a DB table
- Use @PrimaryGeneratedColumn() for auto-increment primary key
- Use @Column() for regular table columns
- Only decorated properties map to DB
- Entity classes enable CRUD via repositories
Full Transcript
In NestJS, defining an entity means creating a class that represents a database table. We add the @Entity decorator to mark the class as an entity. Then, we define properties with decorators like @PrimaryGeneratedColumn for the primary key and @Column for other columns. This setup tells the ORM how to map the class to the database. When running, the ORM creates the table and allows CRUD operations through repositories. Properties without decorators are ignored in the database mapping. This visual trace showed each step from class definition to database usage.