Skip to content

BackendWorks/post

Repository files navigation

Post Service

A microservice for managing blog posts and content in the NestJS microservices architecture.

πŸš€ Features

  • Post Management: Full CRUD operations for blog posts
  • User Integration: Integration with Auth Service for user authentication
  • Search & Filtering: Advanced search and filtering capabilities
  • Pagination: Efficient pagination for large datasets
  • Soft Delete: Soft delete functionality with audit trails
  • gRPC Microservice: Inter-service communication via gRPC
  • REST API: HTTP endpoints for post operations
  • Database Integration: PostgreSQL with Prisma ORM
  • Caching: Redis-based caching for performance
  • Internationalization: Multi-language support with nestjs-i18n
  • API Documentation: Swagger/OpenAPI documentation
  • Health Checks: Built-in health monitoring
  • Security: JWT authentication and authorization

πŸ—οΈ Architecture

Technology Stack

  • Framework: NestJS 10.x
  • Language: TypeScript 5.x
  • Database: PostgreSQL with Prisma ORM
  • Cache: Redis with cache-manager
  • Authentication: JWT with Passport.js
  • API Documentation: Swagger/OpenAPI
  • Microservice: gRPC communication
  • Validation: class-validator and class-transformer
  • Testing: Jest

Service Structure

src/
β”œβ”€β”€ app/                    # Application bootstrap
β”œβ”€β”€ common/                 # Shared modules and utilities
β”‚   β”œβ”€β”€ config/            # Configuration management
β”‚   β”œβ”€β”€ constants/         # Application constants
β”‚   β”œβ”€β”€ decorators/        # Custom decorators
β”‚   β”œβ”€β”€ dtos/             # Data Transfer Objects
β”‚   β”œβ”€β”€ enums/            # Application enums
β”‚   β”œβ”€β”€ filters/          # Exception filters
β”‚   β”œβ”€β”€ guards/           # Authentication guards
β”‚   β”œβ”€β”€ interceptors/     # Response interceptors
β”‚   β”œβ”€β”€ interfaces/       # TypeScript interfaces
β”‚   β”œβ”€β”€ middlewares/      # Request middlewares
β”‚   └── services/         # Shared services
β”œβ”€β”€ generated/            # gRPC generated code
β”œβ”€β”€ languages/            # i18n translation files
β”œβ”€β”€ modules/             # Feature modules
β”‚   └── post/            # Post management module
β”œβ”€β”€ services/            # External service integrations
β”‚   └── auth/            # Auth service integration
└── protos/              # gRPC protocol buffers

πŸ“‹ Prerequisites

  • Node.js >= 18.0.0
  • npm >= 9.0.0
  • PostgreSQL
  • Redis
  • Auth Service (for authentication)

πŸ› οΈ Installation

  1. Clone the repository

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

    npm install
  3. Environment Configuration Create .env and .env.docker files with the following variables:

    # App Configuration
    NODE_ENV=development
    APP_NAME=NestJS Post Service
    APP_DEBUG=false
    APP_CORS_ORIGINS=http://localhost:3000
    
    # HTTP Configuration
    HTTP_ENABLE=true
    HTTP_HOST=0.0.0.0
    HTTP_PORT=9002
    HTTP_VERSIONING_ENABLE=false
    HTTP_VERSION=1
    
    # Database Configuration
    DATABASE_URL=postgresql://username:password@localhost:5432/post_db
    
    # Redis Configuration
    REDIS_URL=redis://localhost:6379
    REDIS_KEY_PREFIX=post:
    REDIS_TTL=3600
    
    # gRPC Configuration
    GRPC_URL=0.0.0.0:50052
    GRPC_PACKAGE=post
    
    # Auth Service Configuration (for gRPC communication)
    AUTH_SERVICE_URL=0.0.0.0:50051
    
    # Monitoring (Optional)
    SENTRY_DSN=your-sentry-dsn
  4. Database Setup

    # Generate Prisma client
    npm run prisma:generate
    
    # Run migrations
    npm run prisma:migrate
    
    # (Optional) Open Prisma Studio
    npm run prisma:studio
  5. Generate gRPC code

    npm run proto:generate

πŸš€ Running the Service

Development Mode

npm run dev

Production Mode

npm run build
npm start

Docker (if available)

docker build -t post-service .
docker run -p 9002:9002 post-service

πŸ“‘ API Endpoints

Post Management Endpoints

Public Endpoints

  • GET /posts - List all posts (paginated)
  • GET /posts/:id - Get post by ID

Protected Endpoints

  • POST /posts - Create new post
  • PUT /posts/:id - Update post
  • DELETE /posts/:id - Delete post
  • POST /posts/bulk-delete - Bulk delete posts

Query Parameters

Pagination

  • page (number): Page number (default: 1)
  • limit (number): Items per page (default: 10, max: 100)

Search & Filtering

  • search (string): Search in title and content
  • sortBy (string): Sort field (default: createdAt)
  • sortOrder (string): Sort order - 'asc' or 'desc' (default: desc)

Post List Specific

  • authorId (string): Filter by author ID
  • page (number): Page number
  • limit (number): Items per page

Health Check

  • GET /health - Service health status
  • GET / - Service information

πŸ”Œ gRPC Services

PostService

  • CreatePost - Create a new post
  • GetPost - Get a single post by ID
  • GetPosts - Get paginated list of posts
  • UpdatePost - Update an existing post
  • DeletePost - Delete a post

πŸ—„οΈ Database Schema

Post Model

CREATE TABLE posts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  title VARCHAR NOT NULL,
  content TEXT NOT NULL,
  images TEXT[] DEFAULT '{}',
  created_by UUID NOT NULL,
  updated_by UUID,
  deleted_by UUID,
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW(),
  deleted_at TIMESTAMP,
  is_deleted BOOLEAN DEFAULT false
);

πŸ”§ Configuration

The service uses a modular configuration system with environment-specific settings:

App Configuration

  • Name: Service name and display information
  • Environment: Development, staging, production
  • Debug: Debug mode settings
  • CORS: Cross-origin resource sharing settings

HTTP Configuration

  • Port: HTTP server port (default: 9002)
  • Host: HTTP server host
  • Versioning: API versioning settings

Database Configuration

  • URL: PostgreSQL connection string
  • Migrations: Database migration settings

Redis Configuration

  • URL: Redis connection string
  • Key Prefix: Cache key prefix
  • TTL: Cache time-to-live

gRPC Configuration

  • URL: gRPC server address
  • Package: Protocol buffer package name

Auth Service Integration

  • URL: Auth service gRPC address for token validation

πŸ“Š Data Models

Post Entity

interface Post {
  id: string;
  title: string;
  content: string;
  images: string[];
  createdBy: string;
  updatedBy?: string;
  deletedBy?: string;
  createdAt: Date;
  updatedAt: Date;
  deletedAt?: Date;
  isDeleted: boolean;
}

Post Response DTO

interface PostResponseDto {
  id: string;
  title: string;
  content: string;
  createdBy: UserResponseDto;
  images: string[];
  createdAt: Date;
  updatedAt: Date;
  updatedBy?: UserResponseDto;
  deletedBy?: UserResponseDto;
  deletedAt?: Date;
  isDeleted: boolean;
}

πŸ§ͺ Testing

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:cov

πŸ“š API Documentation

When running in development mode, Swagger documentation is available at:

http://localhost:9002/docs

πŸ”’ Security Features

  • JWT Authentication: Secure token-based authentication
  • Role-based Access Control: User authorization
  • Input Validation: Request validation with class-validator
  • Helmet Security: Security headers
  • CORS Protection: Cross-origin request protection
  • Soft Delete: Audit trail for deleted posts

πŸ“Š Monitoring

  • Health Checks: Built-in health monitoring endpoints
  • Sentry Integration: Error tracking and monitoring
  • Logging: Structured logging
  • Metrics: Performance metrics collection

πŸ”„ Service Integration

Auth Service Integration

The Post Service integrates with the Auth Service via gRPC for:

  • Token Validation: Validate JWT tokens
  • User Information: Get user details for post creation/updates
  • Authorization: Check user permissions

Integration Points

  • gRPC Client: Connects to Auth Service for authentication
  • User Context: Extracts user information from JWT tokens
  • Audit Trail: Tracks user actions on posts

πŸš€ Deployment

Environment Variables

Ensure all required environment variables are set in your deployment environment.

Database Migrations

Run database migrations before starting the service:

npm run prisma:migrate:prod

Health Checks

The service provides health check endpoints for load balancers and monitoring systems.

Service Dependencies

Ensure the Auth Service is running and accessible before starting the Post Service.

πŸ”§ Development

Available Scripts

# Development
npm run dev              # Start in development mode
npm run build           # Build the application
npm run start           # Start in production mode

# Database
npm run prisma:generate # Generate Prisma client
npm run prisma:migrate  # Run migrations
npm run prisma:studio   # Open Prisma Studio

# gRPC
npm run proto:generate  # Generate gRPC code

# Code Quality
npm run lint            # Run ESLint
npm run format          # Format code with Prettier

# Testing
npm test                # Run tests
npm run test:watch      # Run tests in watch mode

Code Structure

The service follows a modular architecture with clear separation of concerns:

  • Controllers: Handle HTTP requests and responses
  • Services: Business logic and data processing
  • DTOs: Data transfer objects for API contracts
  • Interfaces: TypeScript interfaces for type safety
  • Guards: Authentication and authorization guards
  • Interceptors: Response transformation and logging

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License.

πŸ†˜ Support

For support and questions, please contact the development team or create an issue in the repository.

About

Post service

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages