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

123 lines (122 loc) 4.89 kB
import { ErrorInterface, SuccessInterface } from "../../config/Interfaces/Helper/response.helper.interface"; /** * Class representing a read operation. */ export default class Reader { private readonly collectionName; private readonly path; private readonly Converter; private readonly baseQuery; private limit; private skip; private sort; private isEncrypted; private encryptionKey; private cryptoInstance?; private totalCount; private FindOneStatus; private project; private readonly ResponseHelper; private AllData; /** * Creates an instance of Read. * @param {string} collectionName - The name of the collection. * @param {string} path - The data to be read. * @param {object} baseQuery - The base query to be used. * @param {boolean} isEncrypted - The encryption status. * @param {string} encryptionKey - The encryption key. */ constructor(collectionName: string, path: string, baseQuery: object | any, isEncrypted?: boolean, encryptionKey?: string); /** * Generates a comprehensive cache key including collection context * Prevents cache collisions between different collections with same query * * Format: {collectionPath}::{query}::{limit}::{skip}::{sort} * * @returns Cache key string * @private */ private generateCacheKey; /** * Reads the data from a file. * @returns {Promise<any>} A promise that resolves with the response of the read operation. */ exec(): Promise<SuccessInterface | ErrorInterface>; /** * Checks if the query is an exact match on a single indexed field (no operators) */ private isExactIndexMatch; /** * Detects if a regex pattern is a simple prefix match (e.g., /^John/, /^admin@/) * and extracts the prefix for index optimization * * @param regex - The regex pattern (RegExp object or string) * @param options - Optional regex flags (e.g., 'i' for case-insensitive) * @returns Object with isPrefix flag, prefix string, and case-insensitive flag * * @example * detectPrefixPattern(/^John/, 'i') // { isPrefix: true, prefix: 'John', caseInsensitive: true } * detectPrefixPattern(/John/) // { isPrefix: false } * detectPrefixPattern(/^test[0-9]+/) // { isPrefix: false } - complex pattern */ private detectPrefixPattern; /** * Applies sorting if needed and returns data with skip/limit */ private applySortAndReturn; /** * set limit to the query * @param {number} limit - The limit to be set. * @returns {Reader} - An instance of the Reader class. */ Limit(limit: number): Reader; /** * to be skipped to the query * @param {number} skip - The skip to be set. * @returns {Reader} - An instance of the Reader class. */ Skip(skip: number): Reader; /** * to be sorted to the query * @param {object} sort - The sort to be set. * @returns {Reader} - An instance of the Reader class. */ Sort(sort: object | any): Reader; /** * Sets whether to include the total count of matching documents in the result. * * @param count - Boolean flag indicating whether to include the total count * @returns The Reader instance for method chaining */ setCount(count: boolean): Reader; findOne(status?: boolean): Reader; setProject(project: object | any): Reader; /** * 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; /** * Applies skip and limit to the provided data array. * * This method checks if both `limit` and `skip` are defined. If they are, * it slices the `FinalData` array according to the `skip` and `limit` values * and returns the sliced data. If either `limit` or `skip` is not defined, * it returns the original `FinalData` array. * * @param {any[]} FinalData - The array of data to apply skip and limit to. * @returns {Promise<SuccessInterface | ErrorInterface>} - A promise that resolves to a success interface containing the sliced data or the original data. */ private ApplySkipAndLimit; private ApplyProjection; }