@diagramers/api
Version:
Diagramers API - A comprehensive Node.js API template with TypeScript, Firebase Functions, and Socket.io
474 lines (384 loc) ⢠13.2 kB
Markdown
# Diagramers API
A comprehensive, modular, and extensible Node.js API framework built with TypeScript, featuring multi-provider authentication, plugin system, unified response handling, Swagger documentation, Socket.IO real-time communication, and MongoDB support.
## š Features
### Core Features
- **Modular Architecture** - Clean separation of concerns with modules, plugins, and shared components
- **Multi-Provider Authentication** - Internal, Firebase, OAuth, SMS OTP, Email OTP
- **Unified Response Format** - Consistent API responses using the Result model
- **Swagger Documentation** - Auto-generated API documentation
- **Database Seeding** - Comprehensive seeding system with environment-specific data
- **Plugin System** - Extensible plugin architecture for custom functionality
- **Environment Management** - Multi-environment configuration with .env support
- **Audit Logging** - Pluggable audit system supporting multiple backends
- **Socket.IO Integration** - Real-time communication with event handling
- **Auto Database Creation** - Automatically creates collections and indexes on startup
### Database Support
- **MongoDB** - Primary database with Mongoose ODM
- **SQL Databases** - MySQL, PostgreSQL, SQLite, MariaDB (configurable)
- **Connection Pooling** - Optimized database connections
- **Migration Support** - Database schema management
- **Auto-Collection Creation** - Creates collections and indexes automatically
### Authentication & Security
- **JWT Tokens** - Secure token-based authentication
- **Role-Based Access Control** - Granular permission system
- **Rate Limiting** - Built-in request throttling
- **CORS Support** - Configurable cross-origin requests
- **Helmet Security** - Security headers and protection
### Real-Time Communication
- **Socket.IO Server** - WebSocket support with event handling
- **Room Management** - Group-based messaging
- **Event Registration** - Custom event handlers
- **Connection Management** - Client tracking and management
## š¦ Installation
### Option 1: Install API Package Only
```bash
npm install @diagramers/api
```
### Option 2: Install CLI for Project Management
```bash
npm install -g @diagramers/cli
diagramers init api my-new-api
```
### Prerequisites
- Node.js 18+
- npm or yarn
- MongoDB (or other supported database)
## š Quick Start
### 1. Create New Project
```bash
# Using CLI (recommended)
diagramers init api my-new-api
cd my-new-api
# Or manually
npm install @diagramers/api
```
### 2. Set up Environment Variables
```bash
# Copy the example environment file
cp env.example .env.development
# Edit the development environment file
nano .env.development
# Set NODE_ENV to development
echo "NODE_ENV=development" >> .env.development
```
### 3. Install Dependencies
```bash
npm install
```
### 4. Start Development Server
```bash
npm start
```
## š Project Structure
```
src/
āāā core/ # Core application components
ā āāā config/ # Configuration management
ā ā āāā index.ts # Config manager
ā ā āāā interfaces.ts # Configuration interfaces
ā āāā database/ # Database management
ā ā āāā connection.ts # Database connections
ā ā āāā seeder.ts # Database seeding
ā āāā server/ # Server setup
ā ā āāā manager.ts # Server manager with Swagger & Socket.IO
ā āāā logging/ # Logging system
ā āāā app.ts # Application initialization
āāā modules/ # Feature modules
ā āāā users/ # User management module
ā ā āāā entities/ # Data models
ā ā āāā schemas/ # Database schemas
ā ā āāā services/ # Business logic
ā ā āāā controllers/ # HTTP handlers
ā ā āāā routes/ # Route definitions
ā āāā auth/ # Authentication module
ā āāā audit/ # Audit logging module
āāā plugins/ # Plugin system
ā āāā base/ # Base plugin classes
āāā shared/ # Shared utilities
ā āāā types/ # TypeScript types
ā ā āāā result.ts # Unified response model
ā ā āāā base-entity.ts # Base entity interface
ā āāā utils/ # Utility functions
āāā main.ts # Application entry point
```
## āļø Configuration
### Environment Variables
The API uses environment variables for configuration. Copy `env.example` to `.env.development` and customize:
#### Basic Configuration
```bash
NODE_ENV=development
PORT=4000
HOST=localhost
```
#### Logging Configuration
```bash
# Basic Logging
LOG_LEVEL=info # error, warn, info, debug, verbose
LOG_FORMAT=colored # json, simple, colored
LOG_COLORS=true # Enable/disable colored output
# File Logging
LOG_FILE_ENABLED=false # Enable file logging
LOG_FILE_PATH=./logs/app.log # Log file path
LOG_FILE_MAX_SIZE=10m # Max log file size
LOG_FILE_MAX_FILES=5 # Number of log files to keep
# Database Logging
LOG_DATABASE_ENABLED=false # Enable database logging
LOG_DATABASE_COLLECTION=logs # MongoDB collection name
# External Logging Services
LOG_EXTERNAL_ENABLED=false # Enable external logging
LOG_EXTERNAL_TYPE=serilog # serilog, sentry, loggly, papertrail
LOG_EXTERNAL_URL= # External service URL
LOG_EXTERNAL_API_KEY= # API key for external service
LOG_EXTERNAL_APP_NAME= # Application name for external service
```
**Logging Backends Supported:**
- **Console** - Colored output with configurable format
- **File** - JSON or text format with rotation
- **Database** - MongoDB collection for structured logging
- **External Services** - Serilog, Sentry, Loggly, Papertrail
- **Custom Transports** - Extensible transport system
#### Database Configuration
```bash
# MongoDB (Primary)
MONGODB_URI=mongodb://127.0.0.1:27017/diagramers-api
# SQL Database (Alternative)
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=your_password
DB_DATABASE=diagramers
DB_DIALECT=mysql
DB_LOGGING=false
```
#### Socket.IO Configuration
```bash
SOCKETIO_ENABLED=true
SOCKETIO_CORS_ORIGIN=*
SOCKETIO_CORS_METHODS=GET,POST
```
#### Authentication
```bash
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRES_IN=1h
REFRESH_TOKEN_EXPIRES_IN=7d
# Firebase Authentication
FIREBASE_ENABLED=false
FIREBASE_API_KEY=your-api-key
FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
FIREBASE_PROJECT_ID=your-project-id
```
#### Email Configuration
```bash
EMAIL_ENABLED=false
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
EMAIL_FROM=noreply@yourdomain.com
```
#### SMS Configuration (Twilio)
```bash
TWILIO_ENABLED=false
TWILIO_ACCOUNT_SID=your-account-sid
TWILIO_AUTH_TOKEN=your-auth-token
TWILIO_PHONE_NUMBER=+1234567890
```
#### Security Configuration
```bash
CORS_ENABLED=true
CORS_ORIGIN=*
SECURITY_ENABLED=true
RATE_LIMIT_ENABLED=true
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
```
## š ļø Available Scripts
### Development Scripts
```bash
npm start # Start development server
npm run dev # Start development server (alias)
npm run build # Build for production
npm run build:dev # Build for development
npm run clean # Clean build artifacts
```
### Database Scripts
```bash
npm run seed # Run database seeding
npm run seed:force # Force seeding (overwrite existing data)
npm run seed:reset # Reset and reseed database
npm run seed:truncate # Truncate all data and reseed
```
### Module Generation Scripts
```bash
npm run generate:module product # Generate new module
npm run process:template my-api # Process template for new project
npm run cli help # Show CLI help
```
### Code Quality Scripts
```bash
npm run lint # Run ESLint
npm run lint:fix # Fix ESLint issues
npm run type-check # Run TypeScript type checking
npm run format # Format code with Prettier
npm run format:check # Check code formatting
```
### Documentation Scripts
```bash
npm run docs # Generate API documentation
npm run docs:serve # Serve documentation locally
```
### Docker Scripts
```bash
npm run docker:build # Build Docker image
npm run docker:run # Run Docker container
npm run docker:compose # Start with Docker Compose
```
### Monitoring Scripts
```bash
npm run health # Check server health
npm run logs # Tail application logs
npm run logs:clear # Clear log files
npm run monitor # Start monitoring
```
### Deployment Scripts
```bash
npm run deploy # Deploy to default environment
npm run deploy:staging # Deploy to staging
npm run deploy:production # Deploy to production
```
## š Socket.IO Usage
### Server-Side Event Registration
```typescript
import { ServerManager } from '@diagramers/api';
const serverManager = new ServerManager(config, pluginManager);
// Register custom events
serverManager.registerSocketEvent({
event: 'user:join',
handler: (socket, data) => {
socket.join(`room:${data.userId}`);
socket.emit('user:joined', { userId: data.userId });
},
description: 'User joins a room'
});
serverManager.registerSocketEvent({
event: 'message:send',
handler: (socket, data) => {
// Broadcast to room
serverManager.emitToRoom(`room:${data.roomId}`, 'message:received', data);
},
description: 'Send message to room'
});
```
### Client-Side Connection
```javascript
import { io } from 'socket.io-client';
const socket = io('http://localhost:4000');
// Listen for events
socket.on('connect', () => {
console.log('Connected to server');
});
socket.on('message:received', (data) => {
console.log('New message:', data);
});
// Emit events
socket.emit('user:join', { userId: '123' });
socket.emit('message:send', { roomId: 'room1', message: 'Hello!' });
```
## šļø Database Auto-Creation
The API automatically creates database collections and indexes when the server starts:
```typescript
// Collections are created automatically for all registered models
// Indexes are created based on schema definitions
const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
username: { type: String, required: true, index: true }
}, { timestamps: true });
// This will automatically create:
// - 'users' collection
// - Index on 'email' field (unique)
// - Index on 'username' field
```
## š API Documentation
### Swagger UI
Access the interactive API documentation at: `http://localhost:4000/api-docs`
### Socket.IO Info
Get Socket.IO event information at: `http://localhost:4000/api/socket-info`
### Health Check
Check server status at: `http://localhost:4000/health`
## š§ Module Generation
### Using CLI (Recommended)
```bash
# Generate complete module
diagramers extend --module product --crud --fields name,price,category
# Generate table only
diagramers extend --table orders --fields customer_id,amount,status
# Generate relations
diagramers extend --relation user-posts --type mongodb
```
### Using API Scripts
```bash
# Generate module
npm run generate:module product
# Process template
npm run process:template my-api
```
## š Deployment
### Docker Deployment
```bash
# Build and run with Docker
npm run docker:build
npm run docker:run
# Or use Docker Compose
npm run docker:compose
```
### Manual Deployment
```bash
# Build for production
npm run build
# Start production server
NODE_ENV=production npm start
```
### Environment-Specific Deployment
```bash
# Deploy to staging
npm run deploy:staging
# Deploy to production
npm run deploy:production
```
## š Monitoring & Logging
### Log Levels
- `error` - Application errors
- `warn` - Warning messages
- `info` - General information
- `debug` - Debug information
- `verbose` - Detailed debugging
### Log Formats
- `json` - Structured JSON logging
- `simple` - Simple text format
- `colored` - Colored console output
### File Logging
```bash
# Enable file logging
LOG_FILE_ENABLED=true
LOG_FILE_PATH=./logs/app.log
LOG_FILE_MAX_SIZE=10m
LOG_FILE_MAX_FILES=5
```
## š¤ Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## š License
This project is licensed under the MIT License.
## š Support
For support and questions:
- š Check the documentation
- š Open an issue on GitHub
- š¬ Join our community discussions
- š§ Contact: support@diagramers.com
## š Related Packages
- **@diagramers/cli** - Command-line interface for project management
- **@diagramers/admin** - Admin dashboard template
- **@diagramers/shared** - Shared utilities and types