Skip to main content

Posts

Showing posts with the label TypeORM

Hexagonal Architecture in NestJS: Decoupling TypeORM from Domain Entities

  The Coupling Trap The default NestJS + TypeORM pattern leads to a deceptive trap. You start by defining a User class, decorate it with  @Entity() , add a few  @Column()  decorators, and then write methods like  isSubscriptionActive()  directly on that class. It looks efficient until you try to unit test your business logic. Suddenly, you cannot instantiate a  User  without dragging in TypeORM's metadata storage. Your domain logic is now tightly coupled to your persistence infrastructure. If you change your database from SQL to Mongo, or just rename a column, you risk breaking business rules that shouldn't care about storage details. This is the "Active Record" anti-pattern in complex systems: it conflates  what the data is  (Domain) with  how the data is stored  (Infrastructure). The Root Cause: Mixing Concerns Under the hood, TypeORM decorators modify the class prototype to register metadata. When you inject a TypeORM reposi...