UNPKG

axiodb

Version:

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

85 lines (84 loc) 3.69 kB
import { ErrorInterface, SuccessInterface } from "../../config/Interfaces/Helper/response.helper.interface"; /** * The DeleteOperation class is used to delete a document from a collection. * This class provides methods to delete a single document that matches the base query. */ export default class DeleteOperation { protected readonly collectionName: string; private readonly baseQuery; private readonly path; private readonly isEncrypted; private readonly encryptionKey; private readonly ResponseHelper; private readonly cryptoInstance?; private readonly Converter; private allDataWithFileName; private sort; constructor(collectionName: string, path: string, baseQuery: object | any, isEncrypted?: boolean, encryptionKey?: string); /** * Deletes a single document that matches the base query. * * This method: * 1. Loads all raw data from buffers * 2. Searches for documents matching the base query * 3. Selects the first matching document (applying sort if provided) * 4. Deletes the file associated with the selected document * * @returns {Promise<object>} A response object containing either: * - Success: { message: "Data deleted successfully", deleteData: object } * - Error: An error message if no data found or deletion fails * * @throws Will propagate any errors from underlying operations */ deleteOne(): Promise<SuccessInterface | ErrorInterface>; /** * Deletes multiple documents that match the base query. * * This method: * 1. Searches for documents matching the base query * 2. Deletes each matching file * 3. Returns success with the deleted data or an error * * @returns {Promise<SuccessInterface | ErrorInterface>} A promise that resolves to either: * - Success with a success message and the deleted data * - Error if: * - No matching data is found * - Any file deletion operation fails * - The initial buffer data loading fails */ deleteMany(): Promise<SuccessInterface | ErrorInterface>; /** * Loads all buffer raw data from the specified directory. * * This method performs the following steps: * 1. Checks if the directory is locked. * 2. If the directory is not locked, it lists all files in the directory. * 3. Reads each file and decrypts the data if encryption is enabled. * 4. Stores the decrypted data in the `AllData` array. * 5. If the directory is locked, it unlocks the directory, reads the files, and then locks the directory again. * * @returns {Promise<SuccessInterface | ErrorInterface>} A promise that resolves to a success or error response. * * @throws {Error} Throws an error if any operation fails. */ private LoadAllBufferRawData; /** * Deletes a file from the specified path. * * This method checks if the directory is locked before attempting to delete the file. * If the directory is locked, it tries to unlock it, delete the file, and then lock it again. * * @param fileName - The name of the file to be deleted * @returns A response object indicating success or failure * Success response: { status: true, message: "File deleted successfully" } * Error response: { status: false, message: <error message> } * @private */ private deleteFile; /** * to be sorted to the query * @param {object} sort - The sort to be set. * @returns {DeleteOperation} - An instance of the DeleteOperation class. */ Sort(sort: object | any): DeleteOperation; }