UNPKG

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

209 lines 10.6 kB
"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")); const server_2 = __importDefault(require("../tcp/config/server")); /** * Class representing the AxioDB database. * @param {AxioDBOptions} options - Configuration options for AxioDB * @param {boolean} options.GUI - Enable/disable HTTP GUI server (port 27018). Defaults to false. * @param {string} options.RootName - Custom name for the database root folder. Defaults to "AxioDB". * @param {string} options.CustomPath - Custom path for database storage. Defaults to current directory. * @param {boolean} options.TCP - Enable/disable TCP server (port 27019). Defaults to false. * @returns {AxioDB} - Returns the instance of AxioDB. * @example * const db = new AxioDB({ GUI: true, RootName: "MyDB", CustomPath: "./data" }); */ class AxioDB { constructor(options = {}) { this.GUI = Keys_1.General.DBMS_GUI_Enable; this.TCP = false; if (AxioDB._instance) { throw new Error("Only one instance of AxioDB is allowed."); } AxioDB._instance = this; const { GUI, RootName, CustomPath, TCP } = options; // Default Vlaues 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 this.DatabaseMap = new Map(); // Initialize the DatabaseMap this.GUI = GUI !== undefined ? GUI : Keys_1.General.DBMS_GUI_Enable; // Set GUI option this.TCP = TCP !== undefined ? TCP : false; // Set TCP option } /** * 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}`); } } if (this.GUI) { outers_1.Console.green("Starting AxioDB Control Server..."); (0, server_1.default)(this); // Start the web Control Server with the AxioDB instance } if (this.TCP) { outers_1.Console.green("Starting AxioDB TCP Server..."); (0, server_2.default)(this); // Start the TCP Server with the AxioDB instance } }); } /** * Gets the current path of the indexation operation. * @returns {string} The current path. */ get GetPath() { return this.currentPATH; } /** * 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); // Store database metadata in the DatabaseMap // Note: The DatabaseMap is now storing an object instead of a Database instance this.DatabaseMap.set(DBName, { DatabaseName: DBName, path: 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. */ getInstanceInfo() { return __awaiter(this, void 0, void 0, function* () { const totalDatabases = yield this.folderManager.ListDirectory(path_1.default.resolve(this.currentPATH)); // First 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, TotalSize: parseFloat(totalSize.data), TotalDatabases: `${totalDatabases.data.length} Databases`, ListOfDatabases: totalDatabases.data, DatabaseMap: this.DatabaseMap, 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"); } }); } /** * Checks if a database with the given name exists * * @param DBName - The name of the database to check for existence * @returns A Promise that resolves to a boolean indicating whether the database exists * * @example * ```typescript * const exists = await indexation.isDatabaseExists('myDatabase'); * if (exists) { * console.log('Database exists'); * } * ``` */ isDatabaseExists(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); return exists.statusCode === outers_1.StatusCodes.OK; }); } // 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); this.DatabaseMap.delete(DBName); // Remove from DatabaseMap 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