An open API service indexing awesome lists of open source software.

https://github.com/zahidrahimoon/nest-boiler


https://github.com/zahidrahimoon/nest-boiler

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          

# NestJS Enterprise Boilerplate


Nest Logo

A production-ready NestJS boilerplate with enterprise-level architecture, following best practices for scalable and maintainable applications.

## ๐ŸŒŸ Features

- โœ… **Enterprise Architecture** - Scalable folder structure with clear separation of concerns
- โœ… **Authentication & Authorization** - JWT-based auth with guards and decorators
- โœ… **Database Integration** - TypeORM with PostgreSQL
- โœ… **API Documentation** - Swagger/OpenAPI integration
- โœ… **Security** - Password hashing with bcrypt, global auth guard
- โœ… **Validation** - Class-validator for DTO validation
- โœ… **Modular Design** - Domain-driven structure ready for expansion
- โœ… **Helper Utilities** - Response and exception helpers
- โœ… **TypeScript** - Full type safety

## ๐Ÿ“ Project Structure

```
src/
โ”œโ”€โ”€ common/ # Shared utilities across modules
โ”‚ โ”œโ”€โ”€ constants/ # Global constants
โ”‚ โ”œโ”€โ”€ decorators/ # Custom decorators
โ”‚ โ”‚ โ”œโ”€โ”€ current-user.decorator.ts
โ”‚ โ”‚ โ””โ”€โ”€ public.decorator.ts
โ”‚ โ”œโ”€โ”€ guards/ # Authentication guards
โ”‚ โ”‚ โ””โ”€โ”€ auth.guard.ts
โ”‚ โ”œโ”€โ”€ helpers/ # Helper utilities
โ”‚ โ”‚ โ”œโ”€โ”€ exceptions/ # Exception helpers
โ”‚ โ”‚ โ””โ”€โ”€ responses/ # Response formatters
โ”‚ โ”œโ”€โ”€ interfaces/ # Shared interfaces
โ”‚ โ”œโ”€โ”€ pipes/ # Custom pipes
โ”‚ โ””โ”€โ”€ serializers/ # Data serializers
โ”œโ”€โ”€ config/ # Configuration modules
โ”‚ โ”œโ”€โ”€ app/ # App configuration
โ”‚ โ”œโ”€โ”€ database/ # Database configuration
โ”‚ โ”‚ โ””โ”€โ”€ postgres/
โ”‚ โ”‚ โ””โ”€โ”€ postgres.module.ts
โ”‚ โ””โ”€โ”€ swagger/ # Swagger configuration
โ”‚ โ””โ”€โ”€ swagger.config.ts
โ”œโ”€โ”€ models/ # Domain models
โ”‚ โ””โ”€โ”€ users/ # User domain
โ”‚ โ”œโ”€โ”€ constants/ # User-specific constants
โ”‚ โ”œโ”€โ”€ dto/ # Data transfer objects
โ”‚ โ”œโ”€โ”€ entities/ # TypeORM entities
โ”‚ โ”œโ”€โ”€ interfaces/ # User interfaces
โ”‚ โ”œโ”€โ”€ serializers/ # User serializers
โ”‚ โ”œโ”€โ”€ users.controller.ts
โ”‚ โ”œโ”€โ”€ users.service.ts
โ”‚ โ”œโ”€โ”€ users.module.ts
โ”‚ โ””โ”€โ”€ users.providers.ts
โ”œโ”€โ”€ authentication/ # Authentication module
โ”‚ โ”œโ”€โ”€ constants/ # Auth constants
โ”‚ โ”œโ”€โ”€ dto/ # Auth DTOs
โ”‚ โ”œโ”€โ”€ authentication.controller.ts
โ”‚ โ”œโ”€โ”€ authentication.service.ts
โ”‚ โ””โ”€โ”€ authentication.module.ts
โ”œโ”€โ”€ providers/ # External service providers
โ”‚ โ””โ”€โ”€ database/ # Database providers
โ”‚ โ””โ”€โ”€ postgres/
โ”‚ โ””โ”€โ”€ postgres.provider.ts
โ”œโ”€โ”€ app.controller.ts
โ”œโ”€โ”€ app.module.ts
โ”œโ”€โ”€ app.service.ts
โ””โ”€โ”€ main.ts
```

## ๐Ÿš€ Getting Started

### Prerequisites

- Node.js (v18 or higher)
- PostgreSQL (v14 or higher)
- npm or yarn

### Installation

1. **Clone the repository**
```bash
git clone git@github.com:zahidrahimoon/nest-boiler.git
cd nest-boiler
```

2. **Install dependencies**
```bash
npm install
```

3. **Environment Setup**

Create a `.env` file in the root directory:

```env
# Application
PORT=3000

# Database
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=your_password
DB_NAME=nestjs_lms

# JWT
JWT_SECRET=your_super_secret_jwt_key
```

4. **Run the application**

```bash
# Development mode with hot-reload
npm run start:dev

# Production mode
npm run start:prod

# Debug mode
npm run start:debug
```

The application will be available at `http://localhost:3000`

## ๐Ÿ“š API Documentation

Swagger documentation is available at: `http://localhost:3000/documentation`

### Available Endpoints

#### Authentication
- `POST /auth/signup` - Register a new user
- `POST /auth/login` - Login with email and password

#### User
- `GET /user/profile` - Get authenticated user profile (requires JWT token)

## ๐Ÿ” Authentication

This boilerplate uses JWT-based authentication with the following flow:

1. **Public Routes** - Use `@Public()` decorator to bypass authentication
2. **Protected Routes** - All routes are protected by default via global `AuthGuard`
3. **Current User** - Use `@CurrentUser()` decorator to access authenticated user data

### Decorators

- `@Public()` - Mark routes as public (no authentication required)
- `@CurrentUser()` - Access authenticated user data in route handlers

## ๐Ÿ—„๏ธ Database

### TypeORM Configuration

The application uses TypeORM with PostgreSQL. Entity registration is done in:
`src/providers/database/postgres/postgres.provider.ts`

### Adding New Entities

1. Create entity in `src/models/{domain}/entities/`
2. Register entity in `src/providers/database/postgres/postgres.provider.ts`

## ๐Ÿงฉ Adding New Features

### Creating a New Model

Follow the domain-driven structure:

```bash
src/models/courses/
โ”œโ”€โ”€ constants/
โ”œโ”€โ”€ dto/
โ”‚ โ”œโ”€โ”€ create-course.dto.ts
โ”‚ โ”œโ”€โ”€ update-course.dto.ts
โ”‚ โ””โ”€โ”€ course-response.dto.ts
โ”œโ”€โ”€ entities/
โ”‚ โ””โ”€โ”€ course.entity.ts
โ”œโ”€โ”€ serializers/
โ”‚ โ””โ”€โ”€ course.serializer.ts
โ”œโ”€โ”€ courses.controller.ts
โ”œโ”€โ”€ courses.service.ts
โ”œโ”€โ”€ courses.module.ts
โ””โ”€โ”€ courses.providers.ts
```

## ๐Ÿ› ๏ธ Helper Utilities

### Response Helper
- `ResponseHelper.success()` - Standardized success responses
- `ResponseHelper.paginated()` - Paginated data responses
- `ResponseHelper.error()` - Error responses

### Exception Helper
- `ExceptionHelper.notFound()` - 404 Not Found
- `ExceptionHelper.badRequest()` - 400 Bad Request
- `ExceptionHelper.unauthorized()` - 401 Unauthorized
- `ExceptionHelper.conflict()` - 409 Conflict

## ๐Ÿงช Testing

```bash
# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov
```

## ๐Ÿ“ฆ Build

```bash
# Build for production
npm run build

# Run production build
npm run start:prod
```

## ๐Ÿ—๏ธ Architecture Principles

### Separation of Concerns
- **Common**: Shared utilities and cross-cutting concerns
- **Config**: Application configuration
- **Models**: Domain-specific business logic
- **Providers**: External service integrations

### Scalability
- Modular design allows independent development
- Clear patterns for adding new features
- Domain-driven structure

### Maintainability
- Predictable file locations
- Consistent naming conventions
- Clear module boundaries

## ๐Ÿ”ง Configuration

### Swagger Configuration
Customize API documentation in `src/config/swagger/swagger.config.ts`

### Database Configuration
Modify database settings in `src/providers/database/postgres/postgres.provider.ts`

## ๐Ÿ“ Code Style

This project follows NestJS best practices and conventions:
- Use DTOs for data validation
- Implement serializers for response transformation
- Use guards for authorization
- Use decorators for metadata
- Follow domain-driven design

## ๐Ÿค Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## ๐Ÿ™ Acknowledgments

- [NestJS](https://nestjs.com/) - The progressive Node.js framework
- [TypeORM](https://typeorm.io/) - ORM for TypeScript
- [Swagger](https://swagger.io/) - API documentation

## ๐Ÿ“ž Support

For questions and support:
- Visit [NestJS Documentation](https://docs.nestjs.com)
- Join [NestJS Discord](https://discord.gg/G7Qnnhy)

## ๐Ÿ‘จโ€๐Ÿ’ป Author

**Muhammad Zahid Ali Rahimoon**

## ๐Ÿ”— Repository

[GitHub Repository](https://github.com/zahidrahimoon/nest-boiler)