Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RESTful Pokémon API built with Nest.js, MongoDB, and Docker with full CRUD operations and validation.

Notifications You must be signed in to change notification settings

mddiosc/nest-pokedex

Repository files navigation

🚀 Nest.js Pokédex API

Nest Logo

A RESTful API built with Nest.js and MongoDB that provides comprehensive Pokémon information. This application includes full CRUD operations, pagination, data validation, and a seeding system to populate the database.

📋 Table of Contents

✨ Features

  • Complete RESTful API with CRUD operations
  • Data validation with class-validator
  • Integrated pagination
  • MongoDB database with Mongoose
  • Docker Compose for local development
  • Seeding system to populate initial data
  • Global validation pipelines
  • Static files served from /public
  • TypeScript for type safety
  • Modular structure following Nest.js best practices

🛠 Tech Stack

  • Framework: Nest.js v9
  • Runtime: Node.js 18.x
  • Package Manager: pnpm 7.x
  • Database: MongoDB 5
  • ODM: Mongoose
  • Validation: class-validator & class-transformer
  • Containerization: Docker & Docker Compose
  • Language: TypeScript
  • Testing: Jest

📋 Prerequisites

Before running this project, make sure you have installed:

  • Node.js 18.x (recommended to use with Volta)
  • pnpm 7.x
  • Docker and Docker Compose
  • Git

Installation with Volta (Recommended)

This project uses Volta for Node.js and pnpm version management:

# Install Volta (if you don't have it)
curl https://get.volta.sh | bash

# Versions will be configured automatically when cloning the project

🚀 Installation

  1. Clone the repository

    git clone <repository-url>
    cd nest-pokedex
  2. Install dependencies

    pnpm install
  3. Install Nest CLI globally (optional but recommended)

    npm i -g @nestjs/cli

🐳 Database Configuration

Start MongoDB with Docker

# Start MongoDB container in the background
docker-compose up -d

# Verify that the container is running
docker ps

The database will be available at:

  • Host: localhost
  • Port: 27017
  • Name: nest-pokemon

Populate the Database

Once the application is running, you can populate the database with initial data:

# Execute the seed via HTTP request
curl http://localhost:3000/api/v2/seed

Or visit in your browser: http://localhost:3000/api/v2/seed

🏃‍♂️ Running the Application

Development Mode

# Development with automatic reload
pnpm run start:dev

# Development with debug
pnpm run start:debug

Production Mode

# Build the application
pnpm run build

# Run in production
pnpm run start:prod

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

🔌 API Endpoints

Base URL

http://localhost:3000/api/v2

Pokémon Endpoints

Method Endpoint Description Parameters
GET /pokemon Get all Pokémon ?limit=10&offset=0
GET /pokemon/:term Get Pokémon by ID, name or number -
POST /pokemon Create a new Pokémon Body: CreatePokemonDto
PATCH /pokemon/:term Update Pokémon Body: UpdatePokemonDto
DELETE /pokemon/:id Delete Pokémon -

Other Endpoints

Method Endpoint Description
GET /seed Populate database with initial data

Usage Examples

Get all Pokémon with pagination

curl "http://localhost:3000/api/v2/pokemon?limit=20&offset=0"

Get a specific Pokémon

# By name
curl "http://localhost:3000/api/v2/pokemon/pikachu"

# By number
curl "http://localhost:3000/api/v2/pokemon/25"

# By MongoDB ID
curl "http://localhost:3000/api/v2/pokemon/64a5f8b2c1234567890abcde"

Create a new Pokémon

curl -X POST "http://localhost:3000/api/v2/pokemon" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "mew",
    "no": 151
  }'

Update a Pokémon

curl -X PATCH "http://localhost:3000/api/v2/pokemon/mew" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "mew-updated"
  }'

📁 Project Structure

nest-pokedex/
├── 📁 public/                 # Static files
│   ├── index.html
│   └── css/styles.css
├── 📁 src/
│   ├── 📁 common/             # Common module (pipes, DTOs, adapters)
│   │   ├── 📁 adapters/       # HTTP adapters
│   │   ├── 📁 dto/            # Shared DTOs (pagination)
│   │   ├── 📁 interfaces/     # Shared interfaces
│   │   └── 📁 pipes/          # Custom pipes
│   ├── 📁 pokemon/            # Main Pokémon module
│   │   ├── 📁 dto/            # Pokémon-specific DTOs
│   │   ├── 📁 entities/       # MongoDB schemas
│   │   ├── pokemon.controller.ts
│   │   ├── pokemon.service.ts
│   │   └── pokemon.module.ts
│   ├── 📁 seed/               # Seeding module
│   │   ├── 📁 interfaces/     # External data interfaces
│   │   ├── seed.controller.ts
│   │   ├── seed.service.ts
│   │   └── seed.module.ts
│   ├── app.module.ts          # Root module
│   └── main.ts                # Entry point
├── 📁 test/                   # E2E tests
├── docker-compose.yaml        # MongoDB configuration
├── package.json
└── README.md

🔧 Environment Variables

Currently, the application uses hardcoded configuration. For production, consider using environment variables:

# .env (create file if needed)
DATABASE_URL=mongodb://localhost:27017/nest-pokemon
PORT=3000

📜 Available Scripts

# Development
pnpm run start          # Start application
pnpm run start:dev      # Development with watch mode
pnpm run start:debug    # Development with debug

# Build
pnpm run build         # Build for production
pnpm run start:prod    # Run production version

# Code quality
pnpm run format        # Format code with Prettier
pnpm run lint          # Run ESLint

# Testing
pnpm run test          # Run unit tests
pnpm run test:watch    # Tests in watch mode
pnpm run test:cov      # Tests with coverage
pnpm run test:e2e      # End-to-end tests

🧪 Testing

# Unit tests
pnpm run test

# Tests with coverage
pnpm run test:cov

# E2E tests
pnpm run test:e2e

# Tests in watch mode
pnpm run test:watch

🤝 Contributing

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

📄 License

This project is unlicensed.


Built with ❤️ using Nest.js

About

RESTful Pokémon API built with Nest.js, MongoDB, and Docker with full CRUD operations and validation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published