Skip to content

Dat0319/nestjs-template-api

Repository files navigation

NestJS Product API

A robust REST API built with NestJS, PostgreSQL, Sequelize ORM, and Docker Compose. Features authentication and product management with Swagger documentation.

Features

  • 🚀 NestJS Framework - Scalable Node.js server-side applications
  • 🐘 PostgreSQL Database - Reliable relational database
  • 🔗 Sequelize ORM - Promise-based Node.js ORM
  • 🐳 Docker Compose - Easy deployment and development
  • 📚 Swagger Documentation - Interactive API documentation
  • 🔐 JWT Authentication - Secure user authentication
  • Input Validation - Request validation with class-validator
  • 🛡️ Guards & Strategies - Passport.js integration

API Flows

Authentication Flow

  • POST /auth/register - Register new user
  • POST /auth/login - User login
  • GET /auth/profile - Get user profile (protected)

Product Flow

  • POST /products - Create product (protected)
  • GET /products - Get all products (protected)
  • GET /products/my-products - Get current user's products (protected)
  • GET /products/:id - Get product by ID (protected)
  • PATCH /products/:id - Update product (protected, owner only)
  • DELETE /products/:id - Delete product (protected, owner only)

Quick Start

Prerequisites

  • Docker and Docker Compose
  • Node.js 18+ (for local development)

Using Docker Compose (Recommended)

  1. Clone and setup

    git clone <your-repo>
    cd nestjs-product-api
    cp .env.example .env
  2. Start the application

    docker-compose up -d
  3. Access the application

Local Development

  1. Install dependencies

    npm install
  2. Setup PostgreSQL database

    # Start only PostgreSQL with Docker
    docker-compose up postgres -d
  3. Configure environment

    cp .env.example .env
    # Edit .env with your configuration
  4. Start development server

    npm run start:dev

Environment Variables

Variable Description Default
DATABASE_HOST PostgreSQL host localhost
DATABASE_PORT PostgreSQL port 5432
DATABASE_USER PostgreSQL username postgres
DATABASE_PASSWORD PostgreSQL password password
DATABASE_NAME PostgreSQL database name nestjs_db
JWT_SECRET JWT signing secret your-secret-key-here
PORT Application port 3000

API Usage Examples

1. Register User

curl -X POST http://localhost:3000/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "password123",
    "firstName": "John",
    "lastName": "Doe"
  }'

2. Login

curl -X POST http://localhost:3000/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "password123"
  }'

3. Create Product (with JWT token)

curl -X POST http://localhost:3000/products \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "name": "iPhone 14",
    "description": "Latest iPhone",
    "price": 999.99,
    "stock": 50
  }'

4. Get All Products

curl -X GET http://localhost:3000/products \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Database Schema

Users Table

  • id - Primary key
  • email - Unique user email
  • password - Hashed password
  • firstName - User's first name
  • lastName - User's last name
  • createdAt - Creation timestamp
  • updatedAt - Update timestamp

Products Table

  • id - Primary key
  • name - Product name
  • description - Product description
  • price - Product price (decimal)
  • stock - Stock quantity
  • userId - Foreign key to users table
  • createdAt - Creation timestamp
  • updatedAt - Update timestamp

Project Structure

src/
├── auth/
│   ├── dto/
│   │   ├── login.dto.ts
│   │   └── register.dto.ts
│   ├── entities/
│   │   └── user.entity.ts
│   ├── guards/
│   │   └── jwt-auth.guard.ts
│   ├── strategies/
│   │   ├── jwt.strategy.ts
│   │   └── local.strategy.ts
│   ├── auth.controller.ts
│   ├── auth.module.ts
│   └── auth.service.ts
├── product/
│   ├── dto/
│   │   ├── create-product.dto.ts
│   │   └── update-product.dto.ts
│   ├── entities/
│   │   └── product.entity.ts
│   ├── product.controller.ts
│   ├── product.module.ts
│   └── product.service.ts
├── app.module.ts
└── main.ts

Development Scripts

# Development
npm run start:dev

# Build
npm run build

# Production
npm run start:prod

# Testing
npm run test
npm run test:watch
npm run test:cov

# Linting
npm run lint

Docker Commands

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

# Rebuild and start
docker-compose up --build -d

# Reset database
docker-compose down -v
docker-compose up -d

Security Features

  • Password Hashing - bcryptjs for secure password storage
  • JWT Authentication - Stateless authentication tokens
  • Input Validation - class-validator for request validation
  • Authorization Guards - Route protection with JWT guards
  • CORS Enabled - Cross-origin resource sharing configured

Contributing

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

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published