axiodb
Version:
A blazing-fast, lightweight, and scalable nodejs package based DBMS for modern application. Supports schemas, encryption, and advanced query capabilities.
155 lines • 8.05 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
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.AxioDB = void 0;
// Import Libraries
const FileManager_1 = __importDefault(require("../engine/Filesystem/FileManager"));
const FolderManager_1 = __importDefault(require("../engine/Filesystem/FolderManager"));
const Keys_1 = require("../config/Keys/Keys");
const path_1 = __importDefault(require("path"));
const database_operation_1 = __importDefault(require("./Database/database.operation"));
// import startWebServer from "../server/Fastify";
// Helper Classes
const Converter_helper_1 = __importDefault(require("../Helper/Converter.helper"));
const outers_1 = require("outers");
const response_helper_1 = __importDefault(require("../Helper/response.helper"));
const server_1 = __importDefault(require("../server/config/server"));
/**
* Class representing the AxioDB database.
* @param {string} RootName - The name of the root folder.
* @returns {AxioDB} - Returns the instance of AxioDB.
*/
class AxioDB {
constructor(RootName, CustomPath) {
if (AxioDB._instance) {
throw new Error("Only one instance of AxioDB is allowed.");
}
AxioDB._instance = this;
this.RootName = RootName || Keys_1.General.DBMS_Name; // Set the root name
this.currentPATH = path_1.default.resolve(CustomPath || "."); // Set the current path
this.fileManager = new FileManager_1.default(); // Initialize the FileManager class
this.folderManager = new FolderManager_1.default(); // Initialize the FolderManager class
this.Converter = new Converter_helper_1.default(); // Initialize the Converter class
this.ResponseHelper = new response_helper_1.default(); // Initialize the ResponseHelper class
this.initializeRoot(); // Ensure root initialization
}
/**
* Initializes the root directory for the AxioDB.
*
* This method sets the `currentPATH` to include the `RootName` and checks if the AxioDB folder exists.
* If the folder does not exist, it attempts to create it. If the creation fails, an error is thrown.
*
* @throws {Error} If the AxioDB folder cannot be created.
* @returns {Promise<void>} A promise that resolves when the initialization is complete.
*/
initializeRoot() {
return __awaiter(this, void 0, void 0, function* () {
this.currentPATH = path_1.default.join(this.currentPATH, this.RootName); // Correctly set the path
// Check if the AxioDB folder exists
const exists = yield this.folderManager.DirectoryExists(this.currentPATH);
if (exists.statusCode !== outers_1.StatusCodes.OK) {
const Dir_Status = yield this.folderManager.CreateDirectory(this.currentPATH);
if (Dir_Status.statusCode !== outers_1.StatusCodes.OK) {
throw new Error(`Failed to create AxioDB folder: ${Dir_Status.statusCode}`);
}
else {
console.log(`AxioDB folder created at: ${this.currentPATH}`);
}
}
(0, server_1.default)(this); // Start the web Control Server with the AxioDB instance
});
}
/**
* Creates a new database folder and updates the metadata file.
* @param DBName - The name of the database to create.
* @returns The newly created database object.
*/
createDB(DBName) {
return __awaiter(this, void 0, void 0, function* () {
const dbPath = path_1.default.join(this.currentPATH, DBName);
// Check if the database already exists
const exists = yield this.folderManager.DirectoryExists(dbPath);
if (exists.statusCode !== outers_1.StatusCodes.OK) {
yield this.folderManager.CreateDirectory(dbPath);
console.log(`Database Created: ${dbPath}`);
}
const newDB = new database_operation_1.default(DBName, dbPath);
return newDB;
});
}
// Information about the AxioDB instance
/**
* Retrieves information about the databases in the current directory.
*
* This method performs the following operations:
* 1. Lists all directories (databases) in the current path.
* 2. Calculates the total size of the current directory.
* 3. Checks if the data from both operations is returned.
* 4. Logs the number of databases.
* 5. Constructs an object containing the total size, total number of databases, and the list of databases.
* 6. Sends a success response with the constructed object.
*
* @returns {Promise<SuccessInterface | undefined>} A promise that resolves when the database information is successfully retrieved and the response is sent.
*/
getDatabaseInfo() {
return __awaiter(this, void 0, void 0, function* () {
const totalDatabases = yield this.folderManager.ListDirectory(path_1.default.resolve(this.currentPATH));
const totalSize = yield this.folderManager.GetDirectorySize(path_1.default.resolve(this.currentPATH));
// check if all data is returned
if ("data" in totalDatabases && "data" in totalSize) {
const FinalDatabaseInfo = {
CurrentPath: this.currentPATH,
RootName: this.RootName,
MatrixUnits: "MB",
TotalSize: parseInt((totalSize.data / 1024 / 1024).toFixed(4)),
TotalDatabases: `${totalDatabases.data.length} Databases`,
ListOfDatabases: totalDatabases.data,
AllDatabasesPaths: totalDatabases.data.map((db) => path_1.default.join(this.currentPATH, db)),
};
return this.ResponseHelper.Success(FinalDatabaseInfo);
}
else {
return this.ResponseHelper.Error("Failed to get database info");
}
});
}
// Delete Database
/**
* Deletes a database from the current directory.
*
* This method performs the following operations:
* 1. Checks if the database exists.
* 2. Deletes the database if it exists.
* 3. Sends a success response if the database is deleted.
*
* @param {string} DBName - The name of the database to delete.
* @returns {Promise<SuccessInterface>} A promise that resolves when the database is successfully deleted and the response is sent.
*/
deleteDatabase(DBName) {
return __awaiter(this, void 0, void 0, function* () {
const dbPath = path_1.default.join(this.currentPATH, DBName);
const exists = yield this.folderManager.DirectoryExists(dbPath);
if (exists.statusCode === outers_1.StatusCodes.OK) {
yield this.folderManager.DeleteDirectory(dbPath);
return this.ResponseHelper.Success(`Database: ${DBName} deleted successfully`);
}
else {
return this.ResponseHelper.Error(`Database: ${DBName} does not exist`);
}
});
}
}
exports.AxioDB = AxioDB;
//# sourceMappingURL=Indexation.operation.js.map