@ecopex/ecopex-framework
v1.0.13
Published
Javascript Framework for API and Admin Panel
Maintainers
Readme
SB System
A Node.js system built with Fastify, Knex, and Ajv for automatic routing and database management with PM2 process management.
Features
- Fastify: High-performance web framework
- Knex: SQL query builder with migrations and seeds
- Ajv: JSON schema validation
- MySQL: Database support
- PM2: Process management for production
- Microservices: Separate admin and API services
- Automatic Routing: Dynamic route loading from directory structure
Project Structure
sb_system/
├── workers/
│ ├── admin.js # Admin service worker
│ └── api.js # API service worker
├── routes/
│ ├── admin/
│ │ └── users/
│ │ ├── route.js # Route definitions
│ │ ├── handler.js # Route handlers
│ │ └── validation.js # Validation schemas
│ └── api/
├── database/
│ ├── migrations/
│ └── seeds/
├── config/
│ └── database.js
├── utils/
│ ├── routeLoader.js
│ └── validator.js
├── logs/ # PM2 log files
├── ecosystem.config.js # PM2 configuration
└── package.jsonInstallation
- Install dependencies:
npm install- Set up environment variables:
# Copy the example and configure your settings
cp env.example .env- Configure your environment in
.env:
# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=sb_system
# Server Configuration
API_PORT=3000
ADMIN_PORT=3001
HOST=0.0.0.0
# Optional: Internationalization
DEFAULT_LOCALE=en
SUPPORTED_LOCALES=en,tr,es- Run database migrations:
npm run migrate- Seed the database (optional):
npm run seedNote: See ENVIRONMENT.md for complete environment variable documentation.
Usage
Development
# Start both services with PM2
npm run dev
# Or start individual services for development
npm run dev:api # API service only
npm run dev:admin # Admin service onlyProduction
# Start all services
npm start
# Or start with production environment
npm run prodPM2 Management
# View status
npm run status
# View logs
npm run logs
# Restart services
npm run restart
# Stop services
npm run stop
# Reload services (zero-downtime)
npm run reload
# Delete all services
npm run deleteServices
API Service (Port 3000)
- Health Check:
GET http://localhost:3000/health - Documentation:
GET http://localhost:3000/docs - Welcome:
GET http://localhost:3000/ - Language Management:
GET /api/language- Get supported languagesPOST /api/language/set- Set preferred languageGET /api/language/detect- Detect language from headers
Admin Service (Port 3001)
- Health Check:
GET http://localhost:3001/health - Documentation:
GET http://localhost:3001/docs - User Management:
GET /admin/users- Get all users (with pagination)GET /admin/users/:id- Get user by IDPOST /admin/users- Create new userPUT /admin/users/:id- Update userDELETE /admin/users/:id- Delete user
Adding New Routes
- Create a new directory under
routes/admin/orroutes/api/ - Add three files:
route.js- Define your routeshandler.js- Implement route handlersvalidation.js- Define validation schemas
Example Route Structure
// routes/admin/products/route.js
module.exports = [
{
method: 'GET',
url: '',
handlerName: 'getAllProducts',
validationName: 'getAllProductsValidation',
schema: {
// Fastify schema definition
}
}
];
// routes/admin/products/handler.js
class ProductHandler {
static async getAllProducts(request, reply) {
// Handler implementation
}
}
module.exports = ProductHandler;
// routes/admin/products/validation.js
module.exports = {
getAllProductsValidation: {
// Ajv validation schema
}
};Database
The system uses Knex.js for database operations with MySQL. Migrations and seeds are included for easy setup.
Available Commands
npm run migrate- Run migrationsnpm run migrate:rollback- Rollback migrationsnpm run seed- Run seeds
Multi-Language Support
The system includes comprehensive internationalization (i18n) support with automatic language detection and validation error translation.
Supported Languages
- English (en) - Default
- Turkish (tr) - Türkçe
- Spanish (es) - Español
Language Detection
The system automatically detects language from:
Accept-LanguageheaderX-Languageheaderlangquery parameter- Falls back to default locale
Language Endpoints
# Get supported languages
GET /api/language
# Set preferred language
POST /api/language/set
{
"language": "tr"
}
# Detect language from headers
GET /api/language/detectUsage Examples
# Request with Turkish language
curl -H "Accept-Language: tr" http://localhost:3000/api/language
# Request with custom language header
curl -H "X-Language: es" http://localhost:3000/admin/users
# Request with query parameter
curl "http://localhost:3000/admin/users?lang=tr"Validation
The system uses Ajv for JSON schema validation with multi-language error messages. Validation schemas are defined in the validation.js files and automatically applied to routes.
License
MIT
