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 readonly collectionMetaPath; 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. * @returns {Promise} - Returns the instance of AxioDB. */ createCollection(collectionName: string): Promise; /** * Checks if a collection exists in the database. * @param {string} collectionName - Name of the collection to check. * @returns {Promise} - Returns true if the collection exists, false otherwise. **/ isCollectionExists(collectionName: string): Promise; /** * Deletes a collection from the database. * @param {string} collectionName - Name of the collection to delete. * @returns {Promise} - Returns a promise. * @throws {Error} - Throws an error if the collection does not exist. */ deleteCollection(collectionName: string): Promise; /** * Lists all collections in the database. * @returns {Promise} - Returns a promise with the list of collections data. * @throws {Error} - Throws an error if the database does not exist. */ getCollectionInfo(): Promise; /** * Removes the metadata entry for a collection from the collection metadata file. * * Replays `collection.meta.jsonl`, drops any entry whose `name` matches the provided * `collectionName`, and rewrites the surviving entries as JSONL. This is the one path * that rewrites the registry - additions are plain appends. * * 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; /** * Reads the JSONL collection registry, one entry per line, last-write-wins per name. * * Streams the file rather than parsing it whole, and tolerates a torn final line from a * crash mid-append - every complete line before it still loads. */ private readCollectionMetadata; /** * 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 * * Appends one JSONL line for a collection that isn't registered yet - the entries already * on disk are never rewritten. The existence check keeps `createCollection()` on an * existing collection (which is idempotent) from growing the file on every call. */ 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 * * Returns undefined when the registry has no line for that collection. */ private getCollectionMetaDetails; }