UNPKG

@tmlmobilidade/utils

Version:

A collection of utility functions and helpers for the TML Mobilidade Go monorepo, providing common functionality for batching operations, caching, HTTP requests, object manipulation, permissions, and more.

82 lines (81 loc) 3.07 kB
interface BatchWriterParams<T> { /** * The maximum number of items to hold in memory * before flushing to the database. * @required */ batch_size: number; /** * How long, in milliseconds, data should be kept in memory before * flushing to the database. If this feature is enabled, a flush will * be triggered even if the batch is not full. Disabled by default. * @default disabled */ batch_timeout?: number; /** * How long to wait, in milliseconds, after the last write operation * before flushing the data to the database. This can be used to prevent * items staying in memory for too long if the batch size is not reached * frequently enough. Disabled by default. * @default disabled */ idle_timeout?: number; /** * The insert function to use for inserting data into the batch. * @required */ insertFn: (data: T[]) => Promise<void>; /** * Maximum number of retries for transient insert errors. * @default 3 */ max_retries?: number; /** * Base delay in milliseconds for exponential backoff. * @default 1000 */ retry_base_delay_ms?: number; /** * The title of this BatchWriter instance, * used to identify the source of the logs. * @required */ title: string; } export declare class BatchWriter<T> { private params; private dataBucketAlwaysAvailable; private dataBucketFlushOps; private batchTimeoutTimer; private idleTimeoutTimer; private sessionTimer; constructor(params: BatchWriterParams<T>); /** * Flushes the current batch of data. * This method is called internally when the batch size or timeouts are reached, * but can also be called manually if needed. * @param callback Optional callback to execute after the flush is complete, receiving the flushed data as a parameter */ flush(callback?: (data?: T[]) => Promise<void>): Promise<void>; /** * Helper method to perform insert operations with retry logic for transient errors. * This method will attempt to insert the data using the provided insert function, * and if an error occurs, it will retry the operation with exponential backoff * until the maximum number of retries is reached. * @param data The data to insert. * @returns A promise that resolves when the insert operation is successful, or rejects if all retries fail. */ private insertWithRetry; /** * Write data to the batch. * @param data The data to write. * @param options Options for the write operation (reserved for future use). * @param writeCallback Callback function to call after the write operation is complete. * @param flushCallback Callback function to call after the flush operation is complete. */ write(data: T | T[], { flushCallback, writeCallback }?: { flushCallback?: (data?: T[]) => Promise<void>; writeCallback?: () => Promise<void>; }): Promise<void>; } export {};