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

128 lines 6.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOutboxReplicationListenerEnvTemplate = exports.getInboxReplicationListenerEnvTemplate = exports.getOutboxReplicationListenerSettings = exports.getInboxReplicationListenerSettings = exports.applyDefaultReplicationListenerConfigValues = void 0; const env_settings_1 = require("../common/env-settings"); const listener_config_1 = require("../common/listener-config"); const defaultSettings = { restartDelayInMs: 250, restartDelaySlotInUseInMs: 10000, }; const applyDefaultReplicationListenerConfigValues = (config) => { const listenerConfig = (0, listener_config_1.applyDefaultListenerConfigValues)(config); const filledConfig = Object.assign(Object.assign(Object.assign({}, listenerConfig), config), { settings: Object.assign(Object.assign(Object.assign({}, listenerConfig.settings), defaultSettings), config.settings) }); return filledConfig; }; exports.applyDefaultReplicationListenerConfigValues = applyDefaultReplicationListenerConfigValues; const basicSettingsMap = [ { constantName: 'RESTART_DELAY_IN_MS', default: defaultSettings.restartDelayInMs, func: env_settings_1.getEnvVariableNumber, description: 'When there is a message handling error, how long the listener should wait to restart the processing.', }, { constantName: 'RESTART_DELAY_SLOT_IN_USE_IN_MS', default: defaultSettings.restartDelaySlotInUseInMs, func: env_settings_1.getEnvVariableNumber, description: 'If the replication slot is in used, how long the listener should wait to connect again.', }, ]; const outboxSettingsMap = [ { constantName: 'DB_PUBLICATION', default: 'pg_transactional_outbox_pub', func: env_settings_1.getEnvVariableString, skipFallback: true, description: 'The name of the PostgreSQL publication that should be used for the outbox.', }, { constantName: 'DB_REPLICATION_SLOT', default: 'pg_transactional_outbox_slot', func: env_settings_1.getEnvVariableString, skipFallback: true, description: 'The name of the PostgreSQL replication slot that should be used for the outbox.', }, ]; const inboxSettingsMap = [ { constantName: 'DB_PUBLICATION', default: 'pg_transactional_inbox_pub', func: env_settings_1.getEnvVariableString, skipFallback: true, description: 'The name of the PostgreSQL publication that should be used for the inbox.', }, { constantName: 'DB_REPLICATION_SLOT', default: 'pg_transactional_inbox_slot', func: env_settings_1.getEnvVariableString, skipFallback: true, description: 'The name of the PostgreSQL replication slot that should be used for the inbox.', }, ]; /** * Loads the environment variables into the replication listener settings * object. It supports reading an inbox specific setting or a general one. * Please use the `getInboxReplicationListenerEnvTemplate` functions to get a * list of all the inbox relevant settings for the replication listener. * @example * TRX_DB_SCHEMA=trx_schema * TRX_INBOX_DB_TABLE=inbox_table * TRX_INBOX_DB_REPLICATION_SLOT=pg_transactional_inbox_slot * @param env The process.env variable or a custom object. * @returns The replication listener settings object with filled with the ENV variables */ const getInboxReplicationListenerSettings = (env = process.env) => { const inboxSettings = (0, listener_config_1.getInboxListenerSettings)(env); const repSettings = (0, env_settings_1.getConfigSettings)([...basicSettingsMap, ...inboxSettingsMap], listener_config_1.inboxEnvPrefix, listener_config_1.fallbackEnvPrefix, env); return Object.assign(Object.assign({}, inboxSettings), repSettings); }; exports.getInboxReplicationListenerSettings = getInboxReplicationListenerSettings; /** * Loads the environment variables into the replication listener settings * object. It supports reading an outbox specific setting or a general one. * Please use the `getOutboxReplicationListenerEnvTemplate` functions to get a * list of all the outbox relevant settings for the replication listener. * @example * TRX_DB_SCHEMA=trx_schema * TRX_OUTBOX_DB_TABLE=outbox_table * TRX_OUTBOX_DB_REPLICATION_SLOT=pg_transactional_outbox_slot * @param env The process.env variable or a custom object. * @returns The replication listener settings object with filled with the ENV variables */ const getOutboxReplicationListenerSettings = (env = process.env) => { const outboxSettings = (0, listener_config_1.getOutboxListenerSettings)(env); const repSettings = (0, env_settings_1.getConfigSettings)([...basicSettingsMap, ...outboxSettingsMap], listener_config_1.outboxEnvPrefix, listener_config_1.fallbackEnvPrefix, env); return Object.assign(Object.assign({}, outboxSettings), repSettings); }; exports.getOutboxReplicationListenerSettings = getOutboxReplicationListenerSettings; /** * Shows the available env variables and their default values for the inbox * listener with the replication approach. * @param map A mapping of all the env variables to config settings. * @param envPrefix The prefix for the env variables to check first (e.g. "TRX_INBOX_" or "TRX_"). * @param envPrefixFallback The fallback prefix if the other is not found. Useful for defining settings that should be used for both outbox and inbox. * @returns */ const getInboxReplicationListenerEnvTemplate = (defaultOverrides) => { const il = (0, listener_config_1.getInboxListenerEnvTemplate)(defaultOverrides); const cfg = (0, env_settings_1.getConfigSettingsEnvTemplate)([...basicSettingsMap, ...inboxSettingsMap], listener_config_1.inboxEnvPrefix, listener_config_1.fallbackEnvPrefix, defaultOverrides); return `${il} ${cfg}`; }; exports.getInboxReplicationListenerEnvTemplate = getInboxReplicationListenerEnvTemplate; /** * Shows the available env variables and their default values for the outbox * listener with the replication approach. * @param map A mapping of all the env variables to config settings. * @param envPrefix The prefix for the env variables to check first (e.g. "TRX_OUTBOX_" or "TRX_"). * @param envPrefixFallback The fallback prefix if the other is not found. Useful for defining settings that should be used for both outbox and inbox. * @returns */ const getOutboxReplicationListenerEnvTemplate = (defaultOverrides) => { const ol = (0, listener_config_1.getOutboxListenerEnvTemplate)(defaultOverrides); const cfg = (0, env_settings_1.getConfigSettingsEnvTemplate)([...basicSettingsMap, ...outboxSettingsMap], listener_config_1.outboxEnvPrefix, listener_config_1.fallbackEnvPrefix, defaultOverrides); return `${ol} ${cfg}`; }; exports.getOutboxReplicationListenerEnvTemplate = getOutboxReplicationListenerEnvTemplate; //# sourceMappingURL=config.js.map