axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
159 lines • 8.14 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandHandler = void 0;
const command_types_1 = require("../types/command.types");
const protocol_1 = require("../config/protocol");
const keys_1 = require("../config/keys");
// Handlers
const DB_handler_1 = __importDefault(require("./handlers/DB.handler"));
const Collection_handler_1 = __importDefault(require("./handlers/Collection.handler"));
const Operation_handler_1 = __importDefault(require("./handlers/Operation.handler"));
/**
* Command Handler - Main dispatcher for TCP commands
* Routes commands to appropriate handlers
*/
class CommandHandler {
constructor(axioDB) {
this.axioDB = axioDB;
// Initialize handlers
this.dbHandler = new DB_handler_1.default(axioDB);
this.collectionHandler = new Collection_handler_1.default(axioDB);
this.operationHandler = new Operation_handler_1.default(axioDB);
}
/**
* Handle incoming TCP request
*/
handleRequest(context) {
return __awaiter(this, void 0, void 0, function* () {
const { request } = context;
try {
// Validate request structure
protocol_1.MessageValidator.validateRequest(request);
// Validate command-specific parameters
protocol_1.MessageValidator.validateParams(request.command, request.params);
// Route to appropriate handler
return yield this.routeCommand(request);
}
catch (error) {
return this.createErrorResponse(request.id, keys_1.StatusCode.BAD_REQUEST, error instanceof Error ? error.message : String(error));
}
});
}
/**
* Route command to appropriate handler
*/
routeCommand(request) {
return __awaiter(this, void 0, void 0, function* () {
const { command, params, id } = request;
try {
switch (command) {
// Connection commands
case command_types_1.CommandType.PING:
return this.handlePing(id);
case command_types_1.CommandType.DISCONNECT:
return this.handleDisconnect(id);
// Database commands
case command_types_1.CommandType.CREATE_DB:
return yield this.dbHandler.handleCreateDB(id, params);
case command_types_1.CommandType.DELETE_DB:
return yield this.dbHandler.handleDeleteDB(id, params);
case command_types_1.CommandType.DB_EXISTS:
return yield this.dbHandler.handleDBExists(id, params);
case command_types_1.CommandType.GET_INSTANCE_INFO:
return yield this.dbHandler.handleGetInstanceInfo(id);
// Collection commands
case command_types_1.CommandType.CREATE_COLLECTION:
return yield this.collectionHandler.handleCreateCollection(id, params);
case command_types_1.CommandType.DELETE_COLLECTION:
return yield this.collectionHandler.handleDeleteCollection(id, params);
case command_types_1.CommandType.COLLECTION_EXISTS:
return yield this.collectionHandler.handleCollectionExists(id, params);
case command_types_1.CommandType.GET_COLLECTION_INFO:
return yield this.collectionHandler.handleGetCollectionInfo(id, params);
// CRUD operations
case command_types_1.CommandType.INSERT_DOCUMENT:
return yield this.operationHandler.handleInsertDocument(id, params);
case command_types_1.CommandType.INSERT_MANY_DOCUMENTS:
return yield this.operationHandler.handleInsertManyDocuments(id, params);
case command_types_1.CommandType.QUERY_DOCUMENTS:
return yield this.operationHandler.handleQueryDocuments(id, params);
case command_types_1.CommandType.QUERY_BY_ID:
return yield this.operationHandler.handleQueryById(id, params);
case command_types_1.CommandType.UPDATE_DOCUMENT_BY_ID:
return yield this.operationHandler.handleUpdateById(id, params);
case command_types_1.CommandType.UPDATE_DOCUMENTS_BY_QUERY:
return yield this.operationHandler.handleUpdateByQuery(id, params);
case command_types_1.CommandType.DELETE_DOCUMENT_BY_ID:
return yield this.operationHandler.handleDeleteById(id, params);
case command_types_1.CommandType.DELETE_DOCUMENTS_BY_QUERY:
return yield this.operationHandler.handleDeleteByQuery(id, params);
case command_types_1.CommandType.AGGREGATE:
return yield this.operationHandler.handleAggregate(id, params);
case command_types_1.CommandType.TOTAL_DOCUMENTS:
return yield this.operationHandler.handleTotalDocuments(id, params);
// Index operations
case command_types_1.CommandType.CREATE_INDEX:
return yield this.operationHandler.handleCreateIndex(id, params);
case command_types_1.CommandType.DROP_INDEX:
return yield this.operationHandler.handleDropIndex(id, params);
// Transaction operations (future - not implemented)
case command_types_1.CommandType.BEGIN_TRANSACTION:
case command_types_1.CommandType.COMMIT_TRANSACTION:
case command_types_1.CommandType.ROLLBACK_TRANSACTION:
return this.createErrorResponse(id, keys_1.StatusCode.NOT_IMPLEMENTED, 'Transaction support not implemented yet');
default:
return this.createErrorResponse(id, keys_1.StatusCode.BAD_REQUEST, keys_1.ErrorMessage.UNKNOWN_COMMAND);
}
}
catch (error) {
return this.createErrorResponse(id, keys_1.StatusCode.INTERNAL_SERVER_ERROR, error instanceof Error ? error.message : String(error));
}
});
}
/**
* Handle PING command
*/
handlePing(requestId) {
return {
id: requestId,
statusCode: keys_1.StatusCode.OK,
message: keys_1.SuccessMessage.PING_PONG,
data: { timestamp: Date.now() },
};
}
/**
* Handle DISCONNECT command
*/
handleDisconnect(requestId) {
return {
id: requestId,
statusCode: keys_1.StatusCode.OK,
message: keys_1.SuccessMessage.DISCONNECTED,
};
}
/**
* Create error response
*/
createErrorResponse(requestId, statusCode, message) {
return {
id: requestId,
statusCode,
message,
error: message,
};
}
}
exports.CommandHandler = CommandHandler;
//# sourceMappingURL=CommandHandler.js.map