ScorpDB
ScorpDB is a powerful, modular Node.js database library designed to handle complex database operations with ease. It comes with features such as dynamic model management, transaction handling, caching, RBAC (Role-Based Access Control), GraphQL integration, health checks, and much more. ScorpDB is perfect for developers looking to build scalable, secure, and maintainable applications.
Features
- Dynamic Model System: Simplify database operations with reusable models.
- Transaction Management: Ensure data consistency with robust transaction handling.
- Caching: Improve performance with a Redis-based caching system.
- RBAC: Implement secure, role-based access control.
- GraphQL Integration: Query your database using GraphQL with ease.
- Health Checks: Monitor system health with built-in database and cache status checks.
- Query Profiling: Measure and optimize database query performance.
- Event System: Emit and listen for system-wide events.
- Localization: Support for multiple languages.
- Backup and Restore: Easily back up and restore your database.
- AI-Based Query Optimization: Optimize your SQL queries using AI.
- Job Queue: Manage background tasks with a Bull-based job queue.
- Rate Limiting: Protect APIs from overuse with request rate limiting.
- Scheduler: Schedule recurring tasks using cron expressions.
- Migration Management: Seamlessly manage database schema changes.
Installation
Install ScorpDB via npm:
npm install scorpdb
npm install scorpdb -g
Setup
Create a scorp.config.json file in your project root to define your database configuration:
{
"database": {
"host": "localhost",
"port": 3306,
"user": "root",
"password": "your-password",
"database": "your-database"
}
}
Available scorp cli commands;
- scorp help Display a list of available commands.
- scorp install Setup the ScorpDB module and configuration file.
- scorp config view Display the current database configuration.
- scorp config reset Reset the database configuration file.
- scorp npm update Update ScorpDB to the latest version.
Usage
Import the Library
const ScorpDB = require('scorpdb');
Database Connection
Connect to the database:
(async () => {
try {
await ScorpDB.Database.connect();
console.log('Database connected!');
} catch (error) {
console.error('Connection error:', error.message);
}
})();
Disconnect from the database:
await ScorpDB.Database.disconnect();
Dynamic Models
Define and use models for database operations:
const userModel = new ScorpDB.Model('users');
const users = await userModel.findAll();
console.log(users);
await userModel.insert({ name: 'John Doe', email: 'john@example.com' });
await userModel.update(1, { name: 'John Smith' });
await userModel.delete(1);
Transactions
Handle database transactions:
const transaction = ScorpDB.Transaction;
await transaction.start();
try {
await ScorpDB.Database.query('INSERT INTO accounts (balance) VALUES (?)', [100]);
await transaction.commit();
console.log('Transaction committed.');
} catch (error) {
await transaction.rollback();
console.error('Transaction rolled back:', error.message);
}
Caching
Use Redis for caching:
await ScorpDB.Cache.set('key', 'value');
const value = await ScorpDB.Cache.get('key');
console.log('Cached value:', value);
RBAC (Role-Based Access Control)
Define roles and permissions:
ScorpDB.RBAC.addRole('admin', ['create', 'read', 'update', 'delete']);
const access = ScorpDB.RBAC.hasAccess('admin', 'create');
console.log('Admin access to create:', access);
GraphQL Integration
Query your database using GraphQL:
const query = `
{
hello
}
`;
const result = await ScorpDB.GraphQL.executeQuery(query);
console.log(result);
Health Checks
Monitor the health of the database and caching systems:
const status = await ScorpDB.HealthCheck.getStatus();
console.log('System Health:', status);
Scheduler
Schedule recurring tasks:
ScorpDB.Scheduler.schedule('taskName', '0 3 * * *', () => {
console.log('Task executed at 3 AM!');
});
Backup and Restore
Back up and restore your database:
ScorpDB.Backup.backup('backup.sql');
ScorpDB.Backup.restore('backup.sql');
AI-Based Query Optimization
Optimize your SQL queries using AI:
const originalQuery = 'SELECT * FROM users WHERE email LIKE "%@example.com"';
const optimizedQuery = await ScorpDB.AIOptimizer.optimizeQuery(originalQuery);
console.log('Optimized Query:', optimizedQuery);
Job Queue
Manage background tasks:
const emailQueue = new ScorpDB.JobQueue('emailQueue');
emailQueue.addJob({ email: 'test@example.com', message: 'Hello!' });
emailQueue.process(5, async (job) => {
console.log(`Sending email to ${job.data.email}`);
});
Migration Management
Apply or rollback migrations:
await ScorpDB.Migrations.runMigrations();
await ScorpDB.Migrations.rollbackMigration('001_create_users_table.js');
Contribution
We welcome contributions to improve ScorpDB. Please feel free to open issues or submit pull requests.
License
ScorpDB is open-source software licensed under the MIT License.