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).
105 lines • 5.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfigSettingsEnvTemplate = exports.getConfigSettings = exports.constantToCamel = exports.getEnvVariableBoolean = exports.getEnvVariableNumber = exports.getEnvVariableString = void 0;
/**
* Get a string from the environment variable.
* @throws Error if the variable is not found or empty and no default value was provided.
*/
const getEnvVariableString = (env, field, fallbackField, defaultValue) => {
var _a;
const value = (_a = env[field]) !== null && _a !== void 0 ? _a : env[fallbackField];
if (typeof value !== 'string' || value === '') {
if (defaultValue) {
return defaultValue;
}
throw new Error(`The environment variable ${field} must be a non-empty string.`);
}
return value;
};
exports.getEnvVariableString = getEnvVariableString;
/**
* Get a string from the environment variable.
* @throws Error if the variable is not found or empty and no default value was provided.
*/
const getEnvVariableNumber = (env, field, fallbackField, defaultValue) => {
const value = Number((0, exports.getEnvVariableString)(env, field, fallbackField, `${defaultValue}`));
if (Number.isNaN(value)) {
if (defaultValue) {
return defaultValue;
}
throw new Error(`The environment variable ${field} must be a number.`);
}
return value;
};
exports.getEnvVariableNumber = getEnvVariableNumber;
/**
* Get a boolean from the environment variable. The true/1 value return true, false/0 return false. Everything else throws an error.
* @throws Error if the variable is not found or empty and no default value was provided.
*/
const getEnvVariableBoolean = (env, field, fallbackField, defaultValue) => {
const value = (0, exports.getEnvVariableString)(env, field, fallbackField, `${defaultValue}`).toLowerCase();
if (value === 'true' || value === '1') {
return true;
}
if (value === 'false' || value === '0') {
return false;
}
if (defaultValue) {
return defaultValue;
}
throw new Error(`The environment variable ${field} must be a number.`);
};
exports.getEnvVariableBoolean = getEnvVariableBoolean;
/**
* Transforms a constant formatted value to snake case formatted one
* @param constantStr The CONSTANT_VALUE to convert to constantValue
* @returns the formatted value
*/
const constantToCamel = (constantStr) => {
return constantStr
.toLowerCase()
.replace(/_([a-z0-9])/g, (_, char) => char.toUpperCase());
};
exports.constantToCamel = constantToCamel;
/**
* Loads the configuration settings from the ENV variables into the settings object.
* @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_INBOX_").
* @param envPrefixFallback The fallback prefix if the other is not found. Useful for defining settings that should be used for both outbox and inbox.
* @param env The process.env variable or a custom object
* @returns The parsed configuration object.
*/
const getConfigSettings = (map, envPrefix, envPrefixFallback, env = process.env) => {
const settings = {};
for (const s of map) {
const key = (0, exports.constantToCamel)(s.constantName);
settings[key] = s.func(env, `${envPrefix}${s.constantName}`, `${envPrefixFallback}${s.constantName}`, s.default);
}
return settings;
};
exports.getConfigSettings = getConfigSettings;
/**
* Shows the available env variables and their default values.
* @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_INBOX_").
* @param envPrefixFallback The fallback prefix if the other is not found. Useful for defining settings that should be used for both outbox and inbox.
* @param defaultOverrides Default values for the overrides.
* @returns A string with all the ENV config keys and their default values.
*/
const getConfigSettingsEnvTemplate = (map, envPrefix, envPrefixFallback, defaultOverrides) => {
var _a, _b;
let result = '';
for (const s of map) {
const val = (_b = (_a = defaultOverrides === null || defaultOverrides === void 0 ? void 0 : defaultOverrides[`${envPrefix}${s.constantName}`]) !== null && _a !== void 0 ? _a : defaultOverrides === null || defaultOverrides === void 0 ? void 0 : defaultOverrides[s.constantName]) !== null && _b !== void 0 ? _b : s.default;
const commentKey = s.skipFallback
? `${envPrefix}${s.constantName}`
: `${envPrefixFallback}${s.constantName}`;
const quotedVal = typeof val === 'string' ? `"${val}"` : val;
result += `# | ${commentKey} | ${typeof val} | ${quotedVal} | ${s.description} |
${commentKey}=${val}
`;
}
return result;
};
exports.getConfigSettingsEnvTemplate = getConfigSettingsEnvTemplate;
//# sourceMappingURL=env-settings.js.map