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

https://github.com/quickheaven/nestjs-ultimate-masterclass


https://github.com/quickheaven/nestjs-ultimate-masterclass

docker jwt nestjs postgresql typeorm typescript

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

          

# NestJS Ultimate Masterclass

A comprehensive NestJS application demonstrating best practices for building scalable APIs with authentication, database integration, and testing.

## Features

- RESTful API endpoints for task management
- User authentication with JWT
- Role-based authorization
- Database integration with TypeORM and PostgreSQL
- Request validation with class-validator
- Configuration management with @nestjs/config
- Comprehensive testing suite (unit and e2e)
- Docker support

## Prerequisites

- Node.js (v18+)
- PostgreSQL
- npm/yarn

## Installation

```bash
npm install
```

## Configuration

Create a `.env` file in the root directory with the following variables:

```env
# App
PORT=3000
APP_MESSAGE_PREFIX=Hello

# Database
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_DATABASE=tasks
DB_SYNC=false

# Auth
JWT_SECRET=your-secret-key
JWT_EXPIRES_IN=60m
```

## Running the Application

```bash
# Development
npm run start:dev

# Production
npm run start:prod
```

## Database Migrations

```bash
# Generate migration
npm run migration:generate src/migrations/MigrationName

# Run migrations
npm run migration:run
```

## Testing

```bash
# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov
```

## API Endpoints

### Authentication
- `POST /auth/register` - Register new user
- `POST /auth/login` - Login user
- `GET /auth/profile` - Get user profile
- `GET /auth/admin` - Admin only endpoint

### Tasks
- `GET /tasks` - List all tasks (with pagination and filters)
- `POST /tasks` - Create new task
- `GET /tasks/:id` - Get single task
- `PATCH /tasks/:id` - Update task
- `DELETE /tasks/:id` - Delete task
- `POST /tasks/:id/labels` - Add labels to task
- `DELETE /tasks/:id/labels` - Remove labels from task

## Project Structure

```
src/
├── app.module.ts # Main application module
├── main.ts # Application entry point
├── common/ # Shared resources
├── config/ # Configuration modules
├── tasks/ # Tasks module
│ ├── entities/
│ ├── dto/
│ └── controllers/
├── users/ # Users module
│ ├── entities/
│ ├── dto/
│ └── controllers/
└── migrations/ # Database migrations
```

## Docker Support

Build and run the application using Docker:

```bash
# Build image
docker build -t nestjs-app .

# Run container
docker run -p 3000:3000 nestjs-app
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Why NestJS?
It scales from small to enterprise apps.
It has clear structure which is better for maintainability.
It is great for team development.
It has a very rich ecosystem.
It has support GraphQL, WebSockets and microservices.
It has a strong community support.

Provider is anything that can be injected as a dependency.

#### Creating Module

`nest g module`

#### Creating Controller of the module.

`nest g controller tasks tasks --flat --no-spec`

#### Creating a Service

`nest g service tasks tasks --flat --no-spec`

#### Validations
`npm i --save class-validator class-transformer`

#### Mapped Types
`npm i --save @nestjs/mapped-types`

#### NestJS Config
`npm i --save @nestjs/config`

#### Validating Configuration with Joi
`npm install --save joi`

#### TypeORM Integration
`npm install --save @nestjs/typeorm typeorm pg`

#### Installing Authentication Dependencies
`npm install @nestjs/jwt @nestjs/passport passport passport-jwt bcrypt`
`npm install install --save-dev @types/passport-jwt @types/passport @types/bcrypt @types/jsonwebtoken`
`node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"`

#### Unit Test
`npm run test`
`npm run test watch`

#### End-2-End Test

`npm run test:e2e`

```typescript
@Controller('auth')
@UseInterceptors(ClassSerializerInterceptor)
@SerializeOptions({
strategy: 'excludeAll',
})
```
```typescript
@Expose()
```

#### TypeORM Migration
`npm run migration:generate src/migrations/InitialMigration`