Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Database Configuration
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=magento
DB_USER=root
DB_PASSWORD=

# Server Configuration
PORT=3000
NODE_ENV=development

# Magento Configuration
MAGENTO_BASE_PATH=/var/www/html/magento/

# Application Configuration
CACHE_ENABLED=true
LOG_LEVEL=info
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Dependencies
node_modules/
package-lock.json
yarn.lock

# Environment variables
.env
.env.local
.env.*.local

# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Build artifacts
dist/
build/
tmp/

# Testing
coverage/
.nyc_output/

# Misc
*.bak
*.tmp
302 changes: 302 additions & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
# Architecture Improvements Documentation

## Overview

This document describes the architectural improvements made to the NodeJento project to enhance maintainability, scalability, and code quality.

## What Was Improved

### 1. Configuration Management

**Before:**
- Hardcoded credentials in `app.js`
- No environment variable support
- Connection strings embedded in code

**After:**
- Environment-based configuration using `dotenv`
- Centralized configuration in `src/config/index.js`
- `.env.example` file for easy setup
- Support for different environments (development, production)

**Usage:**
```javascript
const config = require('./src/config');
const sequelize = new Sequelize(config.database);
```

### 2. Error Handling

**Before:**
- Only one catch block for database connection
- No error handling in async routes
- No error middleware
- Silent failures

**After:**
- Comprehensive error handling middleware
- `asyncHandler` wrapper for async routes
- Custom `AppError` class
- Proper error responses with status codes
- Environment-specific error details

**Usage:**
```javascript
const { asyncHandler, AppError } = require('./src/middleware/errorHandler');

app.get('/product/:sku', asyncHandler(async (req, res) => {
// Your code here - errors are automatically caught
}));
```

### 3. Service Layer Architecture

**Before:**
- All logic in route handlers
- Data transformation mixed with routing
- No separation of concerns

**After:**
- **EavService**: Manages EAV attribute loading and caching
- **ProductService**: Handles business logic for products
- **ProductTransformer**: Transforms raw data to API format
- Clear separation between routes, services, and data access

**Benefits:**
- Easier to test
- Reusable business logic
- Better code organization
- Single Responsibility Principle

### 4. Caching Improvements

**Before:**
- Unbounded `requestCache` object (memory leak risk)
- No cache expiration
- No cache management

**After:**
- `Cache` utility class with TTL support
- Maximum cache size limit (prevents memory leaks)
- Cache statistics and monitoring
- Cache management endpoints

**Features:**
```javascript
const cache = new Cache({
maxSize: 100,
defaultTTL: 300000, // 5 minutes
enabled: true
});

cache.set('key', value, 60000); // Cache for 1 minute
const data = cache.get('key');
const stats = cache.getStats(); // Get cache statistics
```

### 5. Project Structure

**Before:**
```
nodejento/
├── Models/
├── app.js
├── config.js
└── test.js
```

**After:**
```
nodejento/
├── Models/ (existing models)
├── src/
│ ├── config/
│ │ └── index.js (centralized configuration)
│ ├── services/
│ │ ├── EavService.js (EAV data management)
│ │ ├── ProductService.js (business logic)
│ │ └── ProductTransformer.js (data transformation)
│ ├── middleware/
│ │ └── errorHandler.js (error handling)
│ └── utils/
│ └── Cache.js (caching utility)
├── app.js (original, kept for compatibility)
├── app-improved.js (new improved version)
├── config.js (updated with env support)
├── .env.example (environment template)
└── .gitignore (proper ignore rules)
```

### 6. Dependencies

**Before:**
```json
{
"mysql2": "^2.2.5", // 2+ years outdated
"sequelize": "^6.6.5" // 2+ years outdated
}
```

**After:**
```json
{
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mysql2": "^3.9.8",
"sequelize": "^6.35.2"
},
"devDependencies": {
"nodemon": "^3.0.2"
}
}
```

### 7. Package Scripts

**New scripts added:**
```json
{
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
}
}
```

## Migration Guide

### For Existing Users

1. **Install new dependencies:**
```bash
npm install
```

2. **Create `.env` file from template:**
```bash
cp .env.example .env
```

3. **Configure your environment:**
Edit `.env` and set your database credentials and other settings.

4. **Use the improved app:**
```bash
# Development mode with auto-reload
npm run dev

# Production mode
npm start
```

### Backward Compatibility

- The original `app.js` is preserved for backward compatibility
- Use `app-improved.js` for new projects or when ready to migrate
- Can run both versions side by side on different ports

## New API Endpoints

### Product by SKU
```
GET /product/:sku?store_ids=0,1
```

Returns a single product by SKU with full EAV data.

### Products by multiple SKUs
```
GET /nodejento?skus=24-MB01,24-MB04&store_ids=0,1
```

Returns multiple products with full EAV data.

### Cache Statistics
```
GET /cache/stats
```

Returns cache performance metrics:
```json
{
"size": 45,
"maxSize": 100,
"hits": 150,
"misses": 50,
"hitRate": "75.00%",
"enabled": true
}
```

### Clear Cache
```
POST /cache/clear
```

Clears all cached data.

## Key Benefits

### 1. Maintainability
- Clear separation of concerns
- Easier to understand and modify
- Better organized code

### 2. Testability
- Services can be unit tested independently
- Mock dependencies easily
- Better test coverage potential

### 3. Scalability
- Services can be extracted to microservices
- Caching prevents database overload
- Better resource management

### 4. Security
- No hardcoded credentials
- Environment-based configuration
- Proper error handling (no stack traces in production)

### 5. Performance
- Improved caching strategy
- Memory leak prevention
- Better database connection pooling

### 6. Developer Experience
- Hot reload with nodemon
- Clear error messages
- Better documentation
- Environment templates

## Best Practices Implemented

1. **Environment Variables**: All configuration through environment variables
2. **Error Handling**: Comprehensive error handling at all levels
3. **Separation of Concerns**: Clear boundaries between layers
4. **Dependency Injection**: Services receive dependencies, not global state
5. **Caching**: Proper cache management with TTL and size limits
6. **Logging**: Structured logging with request IDs
7. **Graceful Shutdown**: Proper cleanup on SIGTERM
8. **API Design**: RESTful endpoints with proper HTTP status codes

## Next Steps (Future Improvements)

1. **Testing**: Add unit and integration tests with Jest or Mocha
2. **Logging**: Implement structured logging (Winston, Pino)
3. **Documentation**: Add OpenAPI/Swagger documentation
4. **Monitoring**: Add APM integration (New Relic, DataDog)
5. **Database**: Add connection retry logic and read replicas support
6. **Rate Limiting**: Add rate limiting middleware for API protection
7. **CORS**: Add CORS support for web clients
8. **Authentication**: Add JWT-based authentication for secure endpoints

## Support

For questions or issues with the new architecture:
- Create an issue on GitHub
- Email: yegorshytikov@gmail.com

## License

Same as the main project (ISC)
Loading