@grandlinex/core
    Preparing search index...

    Class XUtil

    Utility class providing helper functions for timing, worker pools, file system operations, string manipulation, entity metadata handling, environment setup, command execution, and file downloading.

    Index

    Constructors

    Methods

    • Parameters

      • dirPath: string
      • Optionalexclude: string[]

      Returns Promise<number>

    • Converts a camelCase string to snake_case.

      Parameters

      • str: string

        The camelCase string to convert.

      Returns string

      The converted snake_case string. If the input string is empty, the same empty string is returned.

    • Creates multiple folders if they do not already exist.

      Parameters

      • ...path: string[]

        One or more folder paths to create.

      Returns boolean

      true if all folders were created successfully or already existed; false if any folder failed to be created.

    • Creates a folder at the specified path if it does not already exist.

      Parameters

      • path: string

        The path of the folder to create.

      Returns boolean

      Always returns true.

    • Downloads a file from the specified URL and writes it to the given path.

      Parameters

      • url: string

        The URL from which to download the file.

      • path: string

        The destination file path without extension. The file extension is derived from the response MIME type.

      • OptionalextMap: Map<string, string> = MineTypeMap

        A map that associates MIME types with file extensions. Used to determine the file extension to append.

      Returns Promise<{ name: string; size: number; type: string } | null>

      A promise that resolves to an object containing the file name, size in bytes, and MIME type, or null if the download fails.

    • Returns the relation metadata for a given entity.

      Type Parameters

      Parameters

      • entity: T

        The entity instance for which to retrieve relation metadata.

      • Optionalschema: string

        Optional database schema name to associate with the relation.

      Returns { key: string; relation: string; schema: string | undefined }

      An object containing:

      • key: the fixed key name 'e_id'.
      • relation: the table name derived from the entity.
      • schema: the optional schema name if provided.
    • Returns the column properties for a relation to the specified entity.

      Type Parameters

      Parameters

      • entity: T

        The entity to which the column will reference.

      • Optionalschema: string

        Optional schema name for the foreign key.

      Returns ColumnProps

      An object containing the column data type and foreign key definition.

    • Executes the specified command with optional arguments and options.

      Parameters

      • cmd: string

        The command to execute.

      • Optionalargs: string[]

        Optional list of arguments to pass to the command.

      • Optionaloptions: ExecutableOptions

        Optional execution options.

      Returns Promise<XExecResult>

      A promise that resolves with the execution result.

    • Retrieves the class and table names for the supplied entity.

      Parameters

      • entity: IEntity

        The entity instance from which to extract metadata.

      Returns ClassNameInterface

      An object containing the class name and the corresponding table name derived from the entity metadata.

    • Returns the current timestamp as a formatted string.

      Returns string

      A string in the format "YYYY-MM-DD HH:mm:ss" representing the current date and time.

    • Formats a number as a two‑digit string. If the provided number is less than 10, a leading zero is added.

      Parameters

      • num: number

        The number to format.

      Returns string

      The formatted number as a string, padded with a leading zero if necessary.

    • Removes the folder at the given path if it exists.

      Parameters

      • path: string

        The file system path of the folder to delete.

      Returns boolean

      Returns true after attempting the removal. The method performs the deletion only when the folder is present; otherwise it simply returns true.

    • Sets up the environment by creating the specified folder hierarchy.

      Parameters

      • basePath: string[]

        The components that form the base path for the environment.

      • config: string[]

        The sequence of folder names to be appended to the base path, creating nested directories.

      • Optionalother: string[]

        Optional additional folder names that will be created inside the final configuration directory.

      Returns string[]

      An array containing the final configuration path after all directories have been created.

    • Pauses execution for the specified duration.

      Parameters

      • ms: number

        The number of milliseconds to wait before resolving the promise.

      Returns Promise<unknown>

      A promise that resolves after the specified delay.

    • Creates a worker factory that processes an array of items using a pool of workers.

      Type Parameters

      • X
      • F

      Parameters

      • count: number

        The maximum number of workers that will run concurrently.

      • arr: X[]

        The array of input data items to be processed.

      • mapper: (arg: WDat<X>) => Promise<WDat<F>>

        A function that receives an element wrapped in a WDat object and returns a promise that resolves to a WDat containing the mapped result.

      Returns Promise<F[]>

      A promise that resolves when all workers have finished processing, with the value returned by workerFactoryWithProducer.

    • Creates a set of concurrent workers that fetch items from a producer, process them with a consumer, and collects the results.

      Type Parameters

      • T

        The type of data produced by the producer function.

      • F

        The type of data produced by the consumer function.

      Parameters

      • count: number

        The number of worker instances to run concurrently.

      • producer: () => Promise<WDat<T> | null>

        An async function that yields a WDat<T> object or null to indicate no more items.

      • consumer: (arg: WDat<T>) => Promise<WDat<F>>

        An async function that receives a WDat<T> item and returns a processed WDat<F> object.

      Returns Promise<F[]>

      A promise that resolves to an array containing the F values extracted from the processed WDat<F> objects.

    • Processes a stream of data asynchronously using a producer and a consumer.

      Type Parameters

      • T

        The type of the input data produced by the producer.

      • F

        The type of the output data produced by the consumer.

      Parameters

      • producer: () => Promise<WDat<T> | null>

        A function that asynchronously returns a WDat<T> instance or null when no more items are available to process.

      • consumer: (arg: WDat<T>) => Promise<WDat<F>>

        A function that consumes a WDat<T> object and asynchronously returns a WDat<F> instance containing the processed data.

      • oMap: Map<number, F>

        A map that will receive the results from the consumer. The key is the i property of the WDat object and the value is the processed data.

      Returns Promise<void>

      Resolves when the producer has returned null and all produced items have been consumed and stored in the provided map.