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).
126 lines • 6.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOutboxPollingListenerEnvTemplate = exports.getInboxPollingListenerEnvTemplate = exports.getOutboxPollingListenerSettings = exports.getInboxPollingListenerSettings = exports.applyDefaultPollingListenerConfigValues = void 0;
const env_settings_1 = require("../common/env-settings");
const listener_config_1 = require("../common/listener-config");
const defaultSettings = {
nextMessagesFunctionSchema: 'public',
nextMessagesBatchSize: 5,
nextMessagesLockInMs: 5000,
nextMessagesPollingIntervalInMs: 500,
};
const applyDefaultPollingListenerConfigValues = (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.applyDefaultPollingListenerConfigValues = applyDefaultPollingListenerConfigValues;
const basicSettingsMap = [
{
constantName: 'NEXT_MESSAGES_FUNCTION_SCHEMA',
default: defaultSettings.nextMessagesFunctionSchema,
func: env_settings_1.getEnvVariableString,
description: 'The database schema of the next messages function.',
},
{
constantName: 'NEXT_MESSAGES_BATCH_SIZE',
default: defaultSettings.nextMessagesBatchSize,
func: env_settings_1.getEnvVariableNumber,
description: 'The (maximum) amount of messages to retrieve in one query.',
},
{
constantName: 'NEXT_MESSAGES_LOCK_IN_MS',
default: defaultSettings.nextMessagesLockInMs,
func: env_settings_1.getEnvVariableNumber,
description: 'How long the retrieved messages should be locked before they can be retrieved again.',
},
{
constantName: 'NEXT_MESSAGES_POLLING_INTERVAL_IN_MS',
default: defaultSettings.nextMessagesPollingIntervalInMs,
func: env_settings_1.getEnvVariableNumber,
description: 'How often should the next messages function be executed.',
},
];
const outboxSettingsMap = [
{
constantName: 'NEXT_MESSAGES_FUNCTION_NAME',
default: 'next_outbox_messages',
func: env_settings_1.getEnvVariableString,
skipFallback: true,
description: 'The database function name to get the next batch of outbox messages.',
},
];
const inboxSettingsMap = [
{
constantName: 'NEXT_MESSAGES_FUNCTION_NAME',
default: 'next_inbox_messages',
func: env_settings_1.getEnvVariableString,
skipFallback: true,
description: 'The database function name to get the next batch of inbox messages.',
},
];
/**
* Loads the environment variables into the polling listener settings object. It
* supports reading an inbox specific setting or a general one.
* Please use the `getInboxPollingListenerEnvTemplate` functions to get a
* list of all the inbox relevant settings for the polling listener.
* @example
* TRX_DB_SCHEMA=trx_schema
* TRX_INBOX_DB_TABLE=inbox_table
* TRX_INBOX_NEXT_MESSAGES_FUNCTION_SCHEMA=next_inbox_messages
* @param env The process.env variable or a custom object.
* @returns The polling listener settings object with filled with the ENV variables
*/
const getInboxPollingListenerSettings = (env = process.env) => {
const inboxSettings = (0, listener_config_1.getInboxListenerSettings)(env);
const pollSettings = (0, env_settings_1.getConfigSettings)([...basicSettingsMap, ...inboxSettingsMap], listener_config_1.inboxEnvPrefix, listener_config_1.fallbackEnvPrefix, env);
return Object.assign(Object.assign({}, inboxSettings), pollSettings);
};
exports.getInboxPollingListenerSettings = getInboxPollingListenerSettings;
/**
* Loads the environment variables into the polling listener settings object. It
* supports reading an outbox specific setting or a general one.
* Please use the `getOutboxPollingListenerEnvTemplate` functions to get a
* list of all the outbox relevant settings for the polling listener.
* @example
* TRX_DB_SCHEMA=trx_schema
* TRX_OUTBOX_DB_TABLE=outbox_table
* TRX_OUTBOX_NEXT_MESSAGES_FUNCTION_SCHEMA=next_outbox_messages
* @param env The process.env variable or a custom object.
* @returns The polling listener settings object with filled with the ENV variables
*/
const getOutboxPollingListenerSettings = (env = process.env) => {
const outboxSettings = (0, listener_config_1.getOutboxListenerSettings)(env);
const pollSettings = (0, env_settings_1.getConfigSettings)([...basicSettingsMap, ...outboxSettingsMap], listener_config_1.outboxEnvPrefix, listener_config_1.fallbackEnvPrefix, env);
return Object.assign(Object.assign({}, outboxSettings), pollSettings);
};
exports.getOutboxPollingListenerSettings = getOutboxPollingListenerSettings;
/**
* Shows the available env variables and their default values for the inbox
* listener with the polling 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 getInboxPollingListenerEnvTemplate = (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.getInboxPollingListenerEnvTemplate = getInboxPollingListenerEnvTemplate;
/**
* Shows the available env variables and their default values for the outbox
* listener with the polling 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 getOutboxPollingListenerEnvTemplate = (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.getOutboxPollingListenerEnvTemplate = getOutboxPollingListenerEnvTemplate;
//# sourceMappingURL=config.js.map