260223:1415 20260223 nextJS & nestJS Best pratices
All checks were successful
Build and Deploy / deploy (push) Successful in 4m44s
All checks were successful
Build and Deploy / deploy (push) Successful in 4m44s
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: Organize by Feature Modules
|
||||
impact: CRITICAL
|
||||
impactDescription: "3-5x faster onboarding and development"
|
||||
tags: architecture, modules, organization
|
||||
---
|
||||
|
||||
## Organize by Feature Modules
|
||||
|
||||
Organize your application into feature modules that encapsulate related functionality. Each feature module should be self-contained with its own controllers, services, entities, and DTOs. Avoid organizing by technical layer (all controllers together, all services together). This enables 3-5x faster onboarding and feature development.
|
||||
|
||||
**Incorrect (technical layer organization):**
|
||||
|
||||
```typescript
|
||||
// Technical layer organization (anti-pattern)
|
||||
src/
|
||||
├── controllers/
|
||||
│ ├── users.controller.ts
|
||||
│ ├── orders.controller.ts
|
||||
│ └── products.controller.ts
|
||||
├── services/
|
||||
│ ├── users.service.ts
|
||||
│ ├── orders.service.ts
|
||||
│ └── products.service.ts
|
||||
├── entities/
|
||||
│ ├── user.entity.ts
|
||||
│ ├── order.entity.ts
|
||||
│ └── product.entity.ts
|
||||
└── app.module.ts // Imports everything directly
|
||||
```
|
||||
|
||||
**Correct (feature module organization):**
|
||||
|
||||
```typescript
|
||||
// Feature module organization
|
||||
src/
|
||||
├── users/
|
||||
│ ├── dto/
|
||||
│ │ ├── create-user.dto.ts
|
||||
│ │ └── update-user.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ └── user.entity.ts
|
||||
│ ├── users.controller.ts
|
||||
│ ├── users.service.ts
|
||||
│ ├── users.repository.ts
|
||||
│ └── users.module.ts
|
||||
├── orders/
|
||||
│ ├── dto/
|
||||
│ ├── entities/
|
||||
│ ├── orders.controller.ts
|
||||
│ ├── orders.service.ts
|
||||
│ └── orders.module.ts
|
||||
├── shared/
|
||||
│ ├── guards/
|
||||
│ ├── interceptors/
|
||||
│ ├── filters/
|
||||
│ └── shared.module.ts
|
||||
└── app.module.ts
|
||||
|
||||
// users.module.ts
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User])],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService, UsersRepository],
|
||||
exports: [UsersService], // Only export what others need
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
||||
// app.module.ts
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forRoot(),
|
||||
UsersModule,
|
||||
OrdersModule,
|
||||
SharedModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
```
|
||||
|
||||
Reference: [NestJS Modules](https://docs.nestjs.com/modules)
|
||||
Reference in New Issue
Block a user