UNPKG

pg-transactional-outbox

Version:

A PostgreSQL based transactional outbox and inbox pattern implementation to support exactly once message processing (with at least once message delivery).

129 lines 5.54 kB
import { ClientConfig } from 'pg'; import { Env } from './env-settings'; export type OutboxOrInbox = 'outbox' | 'inbox'; export type FullListenerConfig = Required<ListenerConfig> & { settings: FullListenerSettings; }; export type FullListenerSettings = Required<ListenerSettings>; export interface ListenerConfig { /** * Defines if this listener is used for the transactional outbox or inbox * handling */ outboxOrInbox: OutboxOrInbox; /** * Database connection details for the message handler logic. The user needs * update permission to the outbox or inbox table. Uses the dbListenerConfig * if it is not provided. */ dbHandlerConfig?: ClientConfig; /** * The "pg" library based settings to initialize the PostgreSQL connection for * the logical replication listener (with replication permissions) or the * polling listener. */ dbListenerConfig: ClientConfig; /** Listener specific configurations */ settings: ListenerSettings; } export interface ListenerSettings { /** The database schema name where the table is located */ dbSchema: string; /** The database table of the outbox/inbox */ dbTable: string; /** * Outbox message sender or the inbox message handlers that do not finish can * block further messages from being processed/sent. The timeout * (in milliseconds) ensures to continue with the next items. Default is 15s. */ messageProcessingTimeoutInMs?: number; /** * The maximum number of attempts to handle an incoming message. * Defaults to 5 which means a message is handled once initially and up to * four more times for retries. */ maxAttempts?: number; /** * Enable max poisonous attempts protection. Might be disabled when using it for the * outbox scenario. Defaults to true. */ enableMaxAttemptsProtection: boolean; /** * Defines the maximum number of times a message is going to be processed * when it is (likely) a poisonous message which is causing a server crash. * This is used in the default poisonous message retry strategy. * It defaults to three. */ maxPoisonousAttempts?: number; /** * Poisonous message protection is enabled by default or if you set this to * true. Enabling it will take a little bit more time but will prevent an * infinite service crash loop if there is a poisonous message. */ enablePoisonousMessageProtection: boolean; /** * The maximum number of retries when a message could not be found. * Defaults to 0 which means it tries to find the message only once but will * not try it again. */ maxMessageNotFoundAttempts?: number; /** * The time to wait before trying to find a not (yet) found message again. * Defaults to 10ms. */ maxMessageNotFoundDelayInMs?: number; /** * Time in milliseconds between the execution of the old message cleanups. * Leave it undefined or zero to disable automatic message cleanup. */ messageCleanupIntervalInMs?: number; /** Delete messages where the processed field is older than this in seconds */ messageCleanupProcessedInSec?: number; /** Delete messages where the abandoned field is older than this in seconds */ messageCleanupAbandonedInSec?: number; /** Delete messages where the created field is older than this in seconds */ messageCleanupAllInSec?: number; } export declare const applyDefaultListenerConfigValues: (config: ListenerConfig) => FullListenerConfig; export declare const fallbackEnvPrefix = "TRX_"; export declare const inboxEnvPrefix = "TRX_INBOX_"; export declare const outboxEnvPrefix = "TRX_OUTBOX_"; /** * Loads the environment variables into the listener settings object. It * supports reading an inbox specific setting or a general one. * Please use the `getXxxxListenerEnvTemplate` functions to get a list of all * the outbox and inbox relevant settings for the replication or polling * listener. * @example * TRX_DB_SCHEMA=trx_schema * TRX_INBOX_DB_TABLE=inbox_table * TRX_MESSAGE_PROCESSING_TIMEOUT_IN_MS=30000 * @param env The process.env variable or a custom object. * @returns The listener settings object with filled with the ENV variables */ export declare const getInboxListenerSettings: (env?: Env) => ListenerSettings; /** * Loads the environment variables into the listener settings object. It * supports reading an outbox specific setting or a general one. * Please use the `getXxxxListenerEnvTemplate` functions to get a list of all * the outbox and outbox relevant settings for the replication or polling * listener. * @example * TRX_DB_SCHEMA=trx_schema * TRX_OUTBOX_DB_TABLE=outbox_table * TRX_MESSAGE_PROCESSING_TIMEOUT_IN_MS=30000 * @param env The process.env variable or a custom object. * @returns The listener settings object with filled with the ENV variables */ export declare const getOutboxListenerSettings: (env?: Env) => ListenerSettings; /** * Creates the available env variables and their default values for the basic * inbox listener env variables. */ export declare const getInboxListenerEnvTemplate: (defaultOverrides?: Record<string, string>) => string; /** * Creates the available env variables and their default values for the basic * outbox listener env variables. */ export declare const getOutboxListenerEnvTemplate: (defaultOverrides?: Record<string, string>) => string; //# sourceMappingURL=listener-config.d.ts.map