This project provides a microservice with SQL-like operations emulated across multiple backend adapters storages, via different access interfaces.
This project is a microservice designed to execute SQL-like operations across multiple backends (traditional databases, Redis, Kafka, and blockchain platforms such as Hedera Hashgraph and Hyperledger Fabric). The service provides a unified interface for handling SQL queries and supports multiple access protocols (via REST, gRPC, WebSocket, MQTT), ensuring versatility and ease of integration.
- Multi-Backend SQL Execution
The microservice routes SQL queries to different backends based on the adapter specified in the request. Supported backends include:
- Relational Databases (e.g., SQLite, MySQL, PostgreSQL) using knex.
- Redis for key-value store emulation.
- Kafka for publish-subscribe messaging with SQL-like operations.
- Hedera Hashgraph for ledger-based SQL operations via smart contracts.
- Hyperledger Fabric for distributed ledger operations using chaincode.
- Ethereum chain for distributed ledger operations using chaincode smart contracts.
- Protocol Support The service supports multiple communication protocols interfaces, to be accessed from:
- REST API: Accessible via HTTP endpoints.
- gRPC: High-performance, secure communication with authentication via metadata.
- WebSockets: Real-time, bidirectional communication for SQL execution.
- MQTT: Lightweight messaging protocol for IoT and pub/sub applications.
- Security
- JWT Authentication: Ensures secure access to all interfaces (REST, gRPC, WebSocket, MQTT).
- Input Validation: Validates SQL queries and adapter specifications to prevent injection attacks.
- Rate Limiting: Limits the number of requests per client to prevent abuse.
- Encryption: Option to use HTTPS/TLS for REST, gRPC, and WebSocket protocols.
- SQL Parsing
- Uses node-sql-parser library for accurate parsing of SQL queries into structured Abstract Syntax Trees (AST).
- Supports SQL operations (depending on backend adapter used)
- Logging and Monitoring
- Centralized logging with Winston for tracking requests, responses, and errors.
- Separate log files for combined logs (combined.log) and errors (error.log).
- Extensibility
- Modular architecture enables the addition of new adapters or backends with minimal effort.
- Easily extendable to support additional SQL operations or protocols.
-
JWT Authentication
- Secure JWT token generation and validation
- Environment variable based secret management
- Token expiration and rotation support
-
HTTPS Support
- Production-ready HTTPS server configuration
- Self-signed certificate generation for development
- Automatic fallback to HTTP if HTTPS fails
-
Input Validation and Sanitization
- SQL query validation to prevent injection attacks
- Input sanitization to prevent XSS attacks
- Parameter validation for all API endpoints
-
Rate Limiting
- Configurable rate limiting for all endpoints
- Stricter rate limiting for authentication endpoints
- Protection against brute force attacks
- Clone the repository:
git clone https://github.com/yourusername/universal-sql-vmm.git
cd universal-sql-vmm
- Install dependencies:
npm install
- Configure environment variables:
cp .env.example .env
# Edit .env with your configuration
- Initialize the database:
node migrations/initDb.js
- Start the server:
# Development mode
npm run dev
# Production mode
NODE_ENV=production npm start
To enable HTTPS in production:
- Set the following environment variables in your
.env
file:
NODE_ENV=production
USE_HTTPS=true
HTTPS_PORT=3443
HTTPS_KEY_PATH=/path/to/key.pem
HTTPS_CERT_PATH=/path/to/cert.pem
- Generate SSL certificates:
- For production, use certificates from a trusted CA
- For development/testing, you can generate self-signed certificates:
mkdir -p certs openssl genrsa -out certs/key.pem 2048 openssl req -new -key certs/key.pem -out certs/csr.pem -subj "/CN=localhost/O=Universal SQL VMM/C=US" openssl x509 -req -days 365 -in certs/csr.pem -signkey certs/key.pem -out certs/cert.pem
For secure JWT authentication:
- Set the following environment variables in your
.env
file:
JWT_SECRET=your-secure-random-secret
JWT_EXPIRATION=1h
- Generate a secure random secret:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
curl -X POST http://localhost:3000/execute \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"sql": "SELECT * FROM users",
"adapter": "database"
}'
Connect to ws://localhost:3000
and send:
{
"sql": "SELECT * FROM users",
"adapter": "database"
}
Use a gRPC client to connect to localhost:50051
and call the Execute
method.
Publish to the sql/request
topic and subscribe to sql/response
.
Run tests:
# Run all tests
npm run test:all
# Run specific tests
npm run test:unit
npm run test:integration
npm run test:functional
npm run test:sql
npm run test:sql:comprehensive
npm run test:load
npm run test:e2e
-
Connection Pooling
- Implemented database connection pooling
- Configurable pool size and timeout settings
- Improved connection reuse and reliability
-
Query Caching
- Added caching for frequent SQL queries
- Configurable TTL and cache size
- Cache statistics for monitoring
-
Timeout Management
- Configurable timeouts for all external services
- Environment variable based configuration
- Graceful error handling for timeouts
-
Docker Support
- Dockerfile for containerization
- docker-compose.yml for local deployment
- Multi-stage builds for smaller images
-
Kubernetes Deployment
- Deployment, Service, and Ingress configurations
- ConfigMaps and Secrets for configuration
- Horizontal Pod Autoscaler for scaling
-
Cloud Deployment Guides
- AWS (ECS, EKS) deployment instructions
- Azure (ACI, AKS) deployment instructions
- Google Cloud (Cloud Run, GKE) deployment instructions
-
Enhanced JWT Security
- Proper JWT secret management with environment variables
- Secure token generation and validation
- Token expiration and rotation support
-
Improved Error Handling
- Centralized error handling system
- Typed errors for better categorization
- Consistent error responses across all interfaces
-
Enhanced Input Validation
- Comprehensive SQL validation
- Input sanitization for all parameters
- Protection against SQL injection and XSS attacks
-
HTTPS Support
- Production-ready HTTPS server
- Self-signed certificate generation for development
- Automatic fallback to HTTP if HTTPS fails
-
Comprehensive Testing
- Unit tests for all components
- Integration tests for interfaces
- End-to-end tests with real services
- Load testing for performance validation
-
Documentation
- Architecture diagrams and explanations
- Security documentation
- Production readiness checklist
- Deployment guides for various environments
For detailed architecture information, including component diagrams and explanations, see ARCHITECTURE.md.
For a checklist of items to ensure production readiness, see PRODUCTION.md.
For detailed security information, see SECURITY.md.
For detailed deployment instructions for Docker, Kubernetes, and cloud environments, see DEPLOYMENT.md.
Apache License 2.0 - See LICENSE for details.