A comprehensive shopping cart backend API built with NestJS, MongoDB, and TypeScript. This project implements a robust product management system with full CRUD operations, validation, error handling, and comprehensive testing.
- Product Management: Complete CRUD operations for products
- Domain-Driven Design: Clean architecture with value objects and entities
- Validation: Comprehensive input validation using class-validator
- Error Handling: Custom exception handling with proper HTTP status codes
- API Documentation: Auto-generated Swagger/OpenAPI documentation
- Testing: Comprehensive test suite with 69.94% code coverage
- Type Safety: Full TypeScript support with strict typing
- MongoDB Integration: Mongoose ODM for database operations
- JSEND Response Format: Standardized API response format
- Entities:
Product
entity with business logic - Value Objects:
Price
andQuantity
with validation - Exceptions: Custom domain exceptions
- Services: Business logic implementation
- DTOs: Data transfer objects for API contracts
- Controllers: HTTP request/response handling
- Repositories: Data access abstraction
- MongoDB Schema: Database model definitions
- Configuration: Environment-based configuration
- Node.js (v18 or higher)
- pnpm (recommended) or npm
- MongoDB (v5 or higher)
- Docker (optional, for containerized development)
-
Clone the repository
git clone <repository-url> cd BE-NestJS
-
Install dependencies
pnpm install # or npm install
-
Environment Setup
cp .env.example .env
Configure your environment variables:
# Database MONGODB_URI=mongodb://localhost:27017/shopping-cart # Application PORT=3000 NODE_ENV=development API_PREFIX=api/v1 CORS_ORIGIN=http://localhost:4321
-
Start MongoDB
# Using Docker docker run -d -p 27017:27017 --name mongodb mongo:latest # Or start your local MongoDB instance mongod
pnpm start:dev
# or
npm run start:dev
pnpm build
pnpm start:prod
# or
npm run build
npm run start:prod
# Build and run with Docker Compose
docker-compose up --build
# Or using the development Dockerfile
docker build -f Dockerfile.dev -t shopping-cart-backend .
docker run -p 3000:3000 shopping-cart-backend
Once the application is running, access the interactive API documentation:
- Swagger UI: http://localhost:3000/api/v1/docs
- JSON Schema: http://localhost:3000/api/v1/docs-json
pnpm test
# or
npm test
pnpm test:cov
# or
npm run test:cov
pnpm test:watch
# or
npm run test:watch
The test suite provides comprehensive coverage:
- Overall Coverage: 69.94%
- Value Objects: 98.82%
- Product Entity: 86.76%
- Product Service: 83.63%
- Controllers: 100%
Method | Endpoint | Description |
---|---|---|
GET |
/api/v1/products |
Get all products |
GET |
/api/v1/products/:id |
Get product by ID |
POST |
/api/v1/products |
Create new product |
PATCH |
/api/v1/products/:id |
Update product |
DELETE |
/api/v1/products/:id |
Delete product (soft delete) |
POST /api/v1/products
Content-Type: application/json
{
"name": "iPhone 15 Pro",
"description": "Latest iPhone with advanced camera system",
"sku": "IPH15PRO-128-BLK",
"price": 999.99,
"currency": "USD",
"stockQuantity": 100,
"category": "electronics",
"images": ["https://example.com/image1.jpg"],
"brand": "Apple",
"isActive": true
}
PATCH /api/v1/products/:id
Content-Type: application/json
{
"name": "iPhone 15 Pro Max",
"price": 1099.99,
"stockQuantity": 50
}
src/
βββ config/ # Configuration files
β βββ app.config.ts
β βββ database.config.ts
β βββ index.ts
βββ products/ # Product module
β βββ controllers/ # HTTP controllers
β βββ dto/ # Data transfer objects
β βββ entities/ # Domain entities
β βββ repositories/ # Data access layer
β βββ schemas/ # MongoDB schemas
β βββ services/ # Business logic
βββ shared/ # Shared utilities
β βββ decorators/ # Custom decorators
β βββ entities/ # Base entities
β βββ exceptions/ # Custom exceptions
β βββ interceptors/ # Response interceptors
β βββ responses/ # Response utilities
β βββ value-objects/ # Value objects
βββ app.module.ts # Root module
βββ main.ts # Application entry point
test/ # Test files
βββ integration/ # Integration tests
βββ unit/ # Unit tests
βββ setup.ts # Test setup
Variable | Description | Default |
---|---|---|
PORT |
Server port | 3000 |
NODE_ENV |
Environment | development |
MONGODB_URI |
MongoDB connection string | mongodb://localhost:27017/shopping-cart |
API_PREFIX |
API route prefix | api/v1 |
CORS_ORIGIN |
CORS allowed origin | http://localhost:4321 |
The application uses MongoDB with Mongoose ODM. The connection is configured in src/config/database.config.ts
.
- Uses
class-validator
for DTO validation - Comprehensive validation rules for all fields
- Custom validation messages
- Custom domain exceptions
- Global exception filter
- Standardized error responses
- Proper HTTP status codes
All API responses follow the JSEND format:
{
"status": "success|fail|error",
"data": { ... },
"message": "Optional message"
}
docker build -t shopping-cart-backend .
docker run -p 3000:3000 shopping-cart-backend
- Set
NODE_ENV=production
- Configure production MongoDB URI
- Set appropriate CORS origins
- Configure logging and monitoring
- Follow TypeScript best practices
- Use ESLint and Prettier for code formatting
- Write comprehensive tests for new features
- Follow domain-driven design principles
- Create feature branches from
main
- Write tests for new functionality
- Ensure all tests pass
- Create pull requests for code review
- Write unit tests for business logic
- Write integration tests for API endpoints
- Maintain high test coverage
- Use descriptive test names
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.
For support and questions:
- Create an issue in the repository
- Check the API documentation
- Review the test files for usage examples
- Initial release
- Complete product management API
- Comprehensive test suite
- Swagger documentation
- Docker support