UNPKG

axiodb

Version:

A blazing-fast, lightweight, and scalable nodejs package based DBMS for modern application. Supports schemas, encryption, and advanced query capabilities.

110 lines 5.83 kB
"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 }); /* eslint-disable @typescript-eslint/no-explicit-any */ const collection_operation_1 = __importDefault(require("../Collection/collection.operation")); const FileManager_1 = __importDefault(require("../../engine/Filesystem/FileManager")); const FolderManager_1 = __importDefault(require("../../engine/Filesystem/FolderManager")); const path_1 = __importDefault(require("path")); // Crypto for hashing const Crypto_helper_1 = require("../../Helper/Crypto.helper"); const response_helper_1 = __importDefault(require("../../Helper/response.helper")); const outers_1 = require("outers"); /** * Represents a database instance. */ class Database { constructor(name, path) { this.name = name; this.path = path; this.fileManager = new FileManager_1.default(); this.folderManager = new FolderManager_1.default(); this.ResponseHelper = new response_helper_1.default(); } /** * Creates a new collection inside the specified database. * @param {string} collectionName - Name of the collection. * @param {boolean} isSchemaNeeded - Whether the collection requires a schema. * @param {object} schema - Schema of the collection. * @param {boolean} crypto - Enable crypto for the collection. * @param {string} key - Key for crypto. * @returns {Promise<AxioDB>} - Returns the instance of AxioDB. */ createCollection(collectionName_1) { return __awaiter(this, arguments, void 0, function* (collectionName, isSchemaNeeded = false, schema, crypto = false, key) { // Check if the collection already exists const collectionExists = yield this.folderManager.DirectoryExists(path_1.default.join(this.path, collectionName)); const collectionPath = path_1.default.join(this.path, collectionName); // If the collection does not exist, create it if (collectionExists.statusCode !== outers_1.StatusCodes.OK) { yield this.folderManager.CreateDirectory(collectionPath); console.log(`Collection Created: ${collectionPath}`); } // if crypto is enabled, hash the collection name if (crypto === true) { const newCryptoInstance = new Crypto_helper_1.CryptoHelper(key); const collection = new collection_operation_1.default(collectionName, collectionPath, isSchemaNeeded, schema, crypto, newCryptoInstance, key); return collection; } else { const collection = new collection_operation_1.default(collectionName, collectionPath, isSchemaNeeded, schema); return collection; } }); } /** * Deletes a collection from the database. * @param {string} collectionName - Name of the collection to delete. * @returns {Promise<void>} - Returns a promise. * @throws {Error} - Throws an error if the collection does not exist. */ deleteCollection(collectionName) { return __awaiter(this, void 0, void 0, function* () { const collectionPath = path_1.default.join(this.path, collectionName); const exists = yield this.folderManager.DirectoryExists(collectionPath); if (exists.statusCode === outers_1.StatusCodes.OK) { yield this.folderManager.DeleteDirectory(collectionPath); return this.ResponseHelper.Success(`Collection: ${collectionName} deleted successfully`); } else { return this.ResponseHelper.Error(`Collection: ${collectionName} does not exist`); } }); } /** * Lists all collections in the database. * @returns {Promise<FinalCollectionsInfo>} - Returns a promise with the list of collections data. * @throws {Error} - Throws an error if the database does not exist. */ getCollectionInfo() { return __awaiter(this, void 0, void 0, function* () { const collections = yield this.folderManager.ListDirectory(this.path); const totalSize = yield this.folderManager.GetDirectorySize(path_1.default.resolve(this.path)); if ("data" in collections && "data" in totalSize) { const FinalCollections = { CurrentPath: this.path, RootName: this.name, MatrixUnits: "MB", TotalCollections: `${collections.data.length} Collections`, TotalSize: parseInt((totalSize.data / 1024 / 1024).toFixed(4)), ListOfCollections: collections.data, AllCollectionsPaths: collections.data.map((collection) => path_1.default.join(this.path, collection)), }; return this.ResponseHelper.Success(FinalCollections); } }); } } exports.default = Database; //# sourceMappingURL=database.operation.js.map