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):
- SELECT
- INSERT
- UPDATE
- DELETE
- 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.
-
SQL Query Parsing: SQL queries are parsed into AST using node-sql-parser. Extracted details include operation type (e.g., SELECT), target table, columns, values, and conditions.
-
Routing: Based on the specified adapter, the query is routed to the appropriate backend (e.g., database, Redis, Kafka, blockchain).
-
Execution: Each backend has its adapter implementing standard CRUD-like operations (SELECT, INSERT, UPDATE, DELETE). Response:
-
The result is returned to the client in a consistent format.
- Distributed SQL Operations Perform SQL operations on traditional databases and distributed ledgers with a single interface.
- Real-Time Applications Use WebSockets or MQTT for real-time SQL execution in applications like IoT or chat systems.
- Blockchain Data Integration Integrate SQL queries with blockchain platforms like Hedera and Hyperledger Fabric for immutable, auditable data storage.
- Caching with Redis Store and query frequently accessed data in Redis using SQL-like syntax.
- Pub/Sub with Kafka Emulate SQL-like operations on Kafka topics for messaging-based workflows.
Backend Frameworks and Libraries Express.js: REST API implementation. gRPC: High-performance RPC framework. node-sql-parser: SQL parsing and validation. Winston: Logging. Backends Relational Databases: SQLite, MySQL, PostgreSQL. Redis: Key-value store. Kafka: Publish-subscribe messaging. Hedera Hashgraph: Distributed ledger via smart contracts. Hyperledger Fabric: Permissioned blockchain via chaincode. Security jsonwebtoken: JWT authentication. helmet: HTTP header security. express-rate-limit: Throttling requests.
-
Unified Interface: A single service to handle SQL operations across diverse backends.
-
Versatile Protocols: Support for REST, gRPC, WebSockets, and MQTT makes it suitable for various client types.
-
Blockchain Integration: SQL support for blockchain backends enables structured query operations on immutable data.
-
High Scalability: Modular architecture and lightweight protocols (e.g., MQTT) allow scaling with ease. Extensible and Future-Proof:
-
New backends and protocols can be added without disrupting existing functionality.
- REST/API
- gRPC
- MQTT
- WebSocket
Adapters:
- SQL DB: MySQL, PostgreSQL, Sqlite (can be extended with other SQL adapters via knex and/or other libs)
- Blockchain: Ethereum, Hedera, Hyperledger (and enforced operations via chain smart contracts code) - only supported operations (eg: DELETE not supported)
- Redis (fast in-memory emulated SQL and data)
- Kafka (distributed SQL DB, via Kafka messaging usage, only supported SQL operations)
-
adapters/ Holds adapters for all backends: dbAdapter.js: SQL database support (SQLite, MySQL, PostgreSQL). redisAdapter.js: Key-value operations mapped to Redis. kafkaAdapter.js: SQL-like operations on Kafka topics. blockchainAdapter.js: Ethereum-compatible blockchain integration. hederaAdapter.js: Hedera Hashgraph integration. hyperledgerAdapter.js: Hyperledger Fabric integration for SQL-like ledger queries.
-
sqlvm/ Handles SQL parsing and routing: sqlInterpreter.js: Routes parsed SQL commands to appropriate adapters.
-
grpc/ Implements gRPC access: grpcServer.js: Handles SQL execution via gRPC. sql_service.proto: gRPC service definition file.
-
mqtt/ Implements MQTT access: mqttServer.js: MQTT client and server for SQL operations.
-
websocket/ Implements WebSocket access: websocketServer.js: WebSocket server for SQL operations.
-
contracts/ Handles blockchain-related files: sqlContract.js: Smart contract for Hyperledger Fabric. connection.json: Connection profile for Hyperledger Fabric. wallets/: Stores identities for Hyperledger Fabric. ethereum contracts hedera contracts
-
logs/ Stores log files: combined.log: All logs. error.log: Errors only.
-
migrations/ For database setup: initDb.js: SQLite schema setup.
-
test/ Holds test cases for unit and integration testing.
-
Root Files .env: Environment variables (e.g., DB credentials, blockchain keys). index.js: Main entry point, initializes REST, WebSocket, gRPC, MQTT servers. logger.js: Winston logger configuration.
- Environment Variables: Add sensitive configurations like blockchain keys, database credentials, and RPC URLs in .env.
- Dependencies: Ensure all required Node.js packages (express, knex, @hashgraph/sdk, fabric-network, etc.) are installed.
- Scripts: Start server: node index.js Initialize database: node migrations/initDb.js
- Documentation: Include detailed setup instructions and usage examples in README.md. This structure ensures the project is modular, maintainable, and scalable.
- Prerequisites Ensure the following tools are installed:
- Node.js (v16 or later): Download Node.js
- npm (comes with Node.js): For dependency management.
- Docker (optional): For running Redis, Kafka, or other backends locally.
- gRPC tools (optional): To test the gRPC interface.
-
Clone the Repository Clone the project repository to your local machine: git clone https://github.com/andreibesleaga/universal-sql-vmm.git cd universal-sql-vmm
-
Install Dependencies Install all necessary packages: npm install
-
Configure Environment Variables Create a .env file in the root directory with the following content (adjust values as needed):
NODE_ENV=development PORT=3000
JWT_SECRET=your-secret-key
HEDERA_OPERATOR_ID=0.0.xxxx HEDERA_OPERATOR_KEY=302e020100300506032b657004220420... HEDERA_CONTRACT_ID=0.0.xxxx HYPERLEDGER_CONNECTION_FILE=./blockchain/connection.json
- Set Up the Database If you're using SQLite, initialize the database schema:
node migrations/initDb.js
- Set Up Redis (Optional) If Redis is required, run Redis locally using Docker:
docker run --name redis -p 6379:6379 -d redis
- Set Up Kafka (Optional) If Kafka is required, run Kafka using Docker Compose. Add the following content to a docker-compose.yml file:
yaml:
version: '3.7' services: zookeeper: image: confluentinc/cp-zookeeper:latest environment: ZOOKEEPER_CLIENT_PORT: 2181 ports: - 2181:2181
kafka: image: confluentinc/cp-kafka:latest depends_on: - zookeeper ports: - 9092:9092 environment: KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092 KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 Run Kafka:
docker-compose up -d
-
Set Up Blockchain Backends If using Hedera Hashgraph or Hyperledger Fabric: Deploy the Hedera Smart Contract or Hyperledger Chaincode as described in their respective sections. Update .env with the blockchain configuration.
-
Run the Microservice Start the application:
npm start Or, for development with auto-restart:
npm run dev The REST server will be running at: http://localhost:3000
- Test the Service REST API Test with curl or any HTTP client (e.g., Postman):
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"
}'
gRPC Use a gRPC client or grpcurl:
grpcurl -plaintext
-d '{"sql": "SELECT * FROM users", "adapter": "database"}'
localhost:50051 SQLService.Execute
WebSocket
Connect to the WebSocket server at ws://localhost:3000 and send a message:
json
{ "sql": "SELECT * FROM users", "adapter": "database" }
MQTT Publish a message to the sql/request topic and listen for responses on the sql/response topic:
mosquitto_pub -h localhost -t sql/request -m '{"sql": "SELECT * FROM users", "adapter": "database"}' mosquitto_sub -h localhost -t sql/response
- Run Tests Run all unit and integration tests:
npm test
- Monitor Logs Check logs for debugging or performance analysis:
tail -f logs/combined.log
- Deployment For deployment:
Use a process manager like PM2 to manage the Node.js application: npm install -g pm2 pm2 start index.js --name universal-sql-vmm Set up a reverse proxy (e.g., NGINX) for handling HTTPS and load balancing.