@grandlinex/kernel
    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
    • 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 specified by the given paths.

      Parameters

      • ...path: string[]

        The list of directory paths to be created.

      Returns boolean

      Returns true if all folders were successfully created, otherwise false if some already exist.

    • Ensures that the specified folder exists by creating it if it does not.

      Parameters

      • path: string

        The filesystem path of the folder to verify or create.

      Returns boolean

      true if the folder was created during this call; false if the folder already existed.

    • 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>

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

      • OptionalfExt: string

        override extension

      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.

      • Optionallength: number

      Returns string

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

    • Executes a promise with a maximum wait time. If the promise does not resolve or reject before the specified timeout, the returned promise is rejected with an Error.

      Type Parameters

      • T

      Parameters

      • promise: Promise<T>

        The promise to execute.

      • Optionalms: number

        The maximum number of milliseconds to wait before timing out.

      Returns Promise<T | undefined>

      A promise that resolves or rejects with the original promise's outcome, or rejects with a timeout error if the specified time elapses.

    • 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.

    • 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.