Backend Template

Production-ready NestJS backend template with comprehensive features and documentation.

Version: 1.0

๐Ÿš€ Quick Start

Example :
# Install dependencies
npm install

# Setup environment
cp .env.example .env  # Configure your environment variables

# Initialize database
npm run db:setup
npm run db:init

# Start development server
npm run start:dev

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

๐Ÿ“š Documentation

Comprehensive API documentation is available through Compodoc:

Example :
# Generate documentation
npm run doc

# Generate and serve documentation (available at http://localhost:3001)
npm run doc:serve

# Watch mode - auto-regenerate on file changes
npm run doc:watch

Documentation URL: http://localhost:3001 (when served)

The documentation includes:

  • All modules, controllers, and services
  • Complete API endpoint reference
  • Dependency graphs and relationships
  • Code examples and usage patterns
  • Coverage reports

๐Ÿ›  Tech Stack

Technology Version Purpose
Node.js v22 (LTS) Runtime environment
NestJS v11 Backend framework
PostgreSQL v16 Primary database
Redis v7 Caching layer
Elasticsearch v8.11 Search engine
TypeScript v5.7 Programming language
JWT - Authentication

๐Ÿ”ง Environment Setup

Create a .env file in the root directory:

Example :
# Database
DB_HOST="localhost"
DB_USERNAME="your_username"
DB_PASSWORD="your_password"
DB_NAME="your_database"

# Redis
REDIS_HOST="127.0.0.1"
REDIS_PORT="6379"

# Elasticsearch
ELASTICSEARCH_NODE="http://localhost:9200"
ELASTICSEARCH_USERNAME=""
ELASTICSEARCH_PASSWORD=""

# JWT
JWT_SECRET="your_secure_secret_key_here"
JWT_REFRESH_SECRET="your_refresh_secret_key_here"
JWT_COOKIE_NAME="elnr.sid"

# API Configuration
API_TITLE="Your API Title"
API_DESCRIPTION="Your API Description"
API_VERSION="1.0.0"

# Server
PORT="3000"

โš ๏ธ Important: Change JWT secrets and cookie name for production!

๐Ÿ“ฆ Features

Core Features

  • โœ… Authentication & Authorization: JWT-based auth with refresh tokens
  • โœ… Database: TypeORM with PostgreSQL, migrations, and seeding
  • โœ… Caching: Redis integration for performance optimization
  • โœ… Search: Full-text search with Elasticsearch
  • โœ… Internationalization: Multi-language support (i18n)
  • โœ… API Documentation: Swagger/OpenAPI + Compodoc
  • โœ… Validation: Class-validator for DTOs
  • โœ… Error Handling: Global exception filters
  • โœ… Logging: Structured logging
  • โœ… CRON Jobs: Scheduled task support
  • โœ… Testing: Unit and E2E test setup

Elasticsearch Integration

Full Elasticsearch integration with:

  • Index management (create, delete, update mappings)
  • Document CRUD operations
  • Advanced search capabilities
  • Bulk operations support
  • Health monitoring endpoints

Start Elasticsearch with Docker:

Example :
docker-compose -f docker-compose.elasticsearch.yml up -d

Access:

  • Elasticsearch: http://localhost:9200
  • Kibana (optional): http://localhost:5601

Database Management

Example :
# Setup database
npm run db:setup

# Run migrations
npm run migration:run

# Generate new migration
npm run migration:generate -- src/migrations/MigrationName

# Seed database
npm run seed

# Initialize (migrations + seed)
npm run db:init

# Reset database (โš ๏ธ destroys all data)
npm run db:reset

Translation Management

Example :
# Extract and update translations
npm run translate

๐Ÿš€ Available Scripts

Development

  • npm run start:dev - Start in watch mode
  • npm run start:debug - Start with debugger
  • npm run build - Build for production
  • npm run start:prod - Run production build

Testing

  • npm run test - Run unit tests
  • npm run test:watch - Run tests in watch mode
  • npm run test:cov - Generate coverage report
  • npm run test:e2e - Run end-to-end tests

Documentation

  • npm run doc - Generate documentation
  • npm run doc:serve - Serve documentation on port 3001
  • npm run doc:watch - Watch and auto-regenerate docs

Code Quality

  • npm run lint - Lint and fix code
  • npm run format - Format code with Prettier

๐Ÿ— Project Structure

Example :
src/
โ”œโ”€โ”€ app.module.ts              # Root module
โ”œโ”€โ”€ main.ts                    # Application entry point
โ”œโ”€โ”€ common/                    # Shared utilities
โ”‚   โ”œโ”€โ”€ decorators/           # Custom decorators
โ”‚   โ”œโ”€โ”€ entities/             # Base entities
โ”‚   โ”œโ”€โ”€ enums/                # Shared enums
โ”‚   โ”œโ”€โ”€ interceptors/         # Global interceptors
โ”‚   โ””โ”€โ”€ pipes/                # Custom pipes
โ”œโ”€โ”€ config/                    # Configuration files
โ”œโ”€โ”€ database/                  # Database setup & utilities
โ”œโ”€โ”€ elasticsearch/            # Elasticsearch integration
โ”œโ”€โ”€ i18n/                     # Internationalization
โ”œโ”€โ”€ migrations/               # Database migrations
โ”œโ”€โ”€ modules/                  # Feature modules
โ”‚   โ”œโ”€โ”€ auth/                # Authentication
โ”‚   โ”œโ”€โ”€ users/               # User management
โ”‚   โ””โ”€โ”€ ...                  # Other features
โ”œโ”€โ”€ redis/                    # Redis integration
โ”œโ”€โ”€ seed/                     # Database seeders
โ””โ”€โ”€ swagger/                  # API documentation setup

๐Ÿ”’ Security

  • JWT-based authentication with refresh tokens
  • Password hashing with bcrypt
  • HTTP-only cookies for token storage
  • Input validation and sanitization
  • CORS configuration
  • Rate limiting ready
  • Security headers

๐Ÿงช Testing

Example :
# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

๐Ÿ“– API Documentation

Swagger/OpenAPI

Available at: http://localhost:3000/api

Compodoc

Generate and view comprehensive code documentation:

Example :
npm run doc:serve

Access at: http://localhost:3001

๐Ÿค Contributing

  1. Follow the existing code style
  2. Add JSDoc comments to all public methods
  3. Write tests for new features
  4. Update documentation as needed
  5. Run linter before committing: npm run lint

๐Ÿ“„ License

UNLICENSED - Private use only


Need help? Check the generated documentation at http://localhost:3001 or review the inline JSDoc comments in the code.

results matching ""

    No results matching ""