intermediate
Entities
Map classes to tables through decorators, columns, relations, lifecycle hooks, and database constraints.
TypeORM entities map classes to tables via decorators: columns, relations, indexes, and lifecycle hooks. Persistence feels object-oriented but can mix domain behavior with storage mapping.
@Entity()
export class Order {
@PrimaryGeneratedColumn('uuid') id!: string;
@Column() status!: string;
@ManyToOne(() => Customer, (c) => c.orders) customer!: Customer;
}
On interviews: columns, relations, eager/lazy options, decorators, lifecycle hooks, migrations, metadata reflection costs.
Common pitfalls: decorators replacing constraints and indexes; lazy relations hiding async queries behind property access.
The trade-off is familiar OOP mapping versus hidden I/O in getters.
Checklist:
- Map constraints deliberately.
- Watch lazy relation behavior.
- Keep entity methods from hiding side effects.
- Separate entities from API DTOs.