UNPKG

axiodb

Version:

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

91 lines (90 loc) 4.23 kB
import { ErrorInterface, SuccessInterface } from "../../config/Interfaces/Helper/response.helper.interface"; export default class UpdateOperation { 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; private updatedAt; private schema; private readonly Insertion; private readonly isSchemaNeeded; constructor(collectionName: string, path: string, baseQuery: object | any, isSchemaNeeded: boolean, schema: object | any, isEncrypted?: boolean, encryptionKey?: string); /** * Updates a single document that matches the base query. * * This method performs the following operations: * 1. Searches for documents matching the base query * 2. If documents are found, selects the first document (or first after sorting if sort criteria are provided) * 3. Deletes the existing document file * 4. Inserts a new file with updated data using the same document ID * * @param newData - The new data to replace the existing document * @returns A Promise resolving to: * - Success with updated data and previous data if successful * - Error if any step fails * @throws May throw errors during file operations or data processing */ UpdateOne(newData: object | any): Promise<SuccessInterface | ErrorInterface>; /** * Updates multiple documents that match the base query. * * This method performs the following operations: * 1. Searches for documents matching the base query * 2. Deletes the existing documents * 3. Inserts new files with updated data for each document * * @param newData - The new data to replace the existing documents * @returns A Promise resolving to: * - Success with updated data and previous data if successful * - Error if any step fails * @throws May throw errors during file operations or data processing */ UpdateMany(newData: object | any): Promise<SuccessInterface | ErrorInterface>; /** * to be sorted to the query this.createdAt = new Date().toISOString(); this.updatedAt = this.createdAt; // Initially updatedAt is same as createdAt * @param {object} sort - The sort to be set. * @returns {DeleteOperation} - An instance of the DeleteOperation class. */ Sort(sort: object | any): UpdateOperation; /** * 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 deleteFileUpdate; /** * Inserts a document into the collection. * @param {object} data - The data to be inserted. * @returns {Promise<any>} - A promise that resolves with the response of the insertion operation. */ private insertUpdate; }