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

97 lines (96 loc) 4.68 kB
import Collection from "../Collection/collection.operation"; import { ErrorInterface, SuccessInterface } from "../../config/Interfaces/Helper/response.helper.interface"; /** * Represents a database instance. * This class provides methods to create, delete, and manage collections within a database. */ export default class Database { private name; private readonly path; private fileManager; private folderManager; private ResponseHelper; constructor(name: string, path: string); /** * Creates a new collection inside the specified database. * @param {string} collectionName - Name 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: string, crypto?: boolean, key?: string | undefined): Promise<Collection>; /** * Checks if a collection exists in the database. * @param {string} collectionName - Name of the collection to check. * @returns {Promise<boolean>} - Returns true if the collection exists, false otherwise. **/ isCollectionExists(collectionName: string): Promise<boolean>; /** * 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: string): Promise<SuccessInterface | ErrorInterface | undefined>; /** * 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(): Promise<SuccessInterface | undefined>; /** * Removes the metadata entry for a collection from the collection metadata file. * * Reads the JSON file located at `${this.path}/collection.meta`, validates that the * file exists and contains an array of collection metadata objects, removes any entry * whose `name` matches the provided `collectionName`, and writes the updated array * back to the same file. * * The method returns a SuccessInterface on successful removal (even if no matching * collection was found) or an ErrorInterface describing the failure. * * @param collectionName - The name of the collection whose metadata should be removed. * @returns A promise that resolves to SuccessInterface on success or ErrorInterface on failure. * * @remarks * - If the metadata file does not exist, an ErrorInterface is returned. * - If the metadata file cannot be parsed as a JSON array, an ErrorInterface is returned. * - This method performs I/O using a FileManager instance and uses this.ResponseHelper * to construct success/error responses. It does not throw; failures are reported via * the returned ErrorInterface. * * @example * // Remove the "users" collection metadata * await db.dropCollectionMetadata("users"); */ dropCollectionMetadata(collectionName: string): Promise<SuccessInterface | ErrorInterface>; /** * Adds metadata for a collection to the collection metadata file. * * @param collectionData - The metadata of the collection to add * @returns A Promise that resolves when the operation is complete, or rejects with an error if the collection metadata format is invalid * @private * * This method performs the following operations: * 1. Checks if the collection metadata file exists * 2. If the file doesn't exist, creates it with the provided collection metadata * 3. If the file exists, reads the existing metadata, adds the new collection metadata (if not already present), and writes back to the file * * @throws {Error} If the collection metadata format is invalid */ private AddCollectionMetadata; /** * Retrieves metadata details for a specific collection. * * @param collectionName - The name of the collection to retrieve metadata for * @returns A Promise that resolves to the collection's metadata if found, or undefined if not found * @private * * This method: * 1. Checks if the collection.meta file exists * 2. Reads and parses the metadata file if it exists * 3. Validates that the data is an array * 4. Finds and returns the metadata for the specified collection */ private getCollectionMetaDetails; }