@aws-lambda-powertools/batch
Version:
The batch processing package for the Powertools for AWS Lambda (TypeScript) library.
130 lines (129 loc) • 4.66 kB
JavaScript
import '@aws/lambda-invoke-store';
import { shouldUseInvokeStore } from '@aws-lambda-powertools/commons/utils/env';
/**
* Manages storage of batch processing state with automatic context detection.
*
* This class abstracts the storage mechanism for batch processing state,
* automatically choosing between InvokeStore (when in Lambda context) and
* fallback instance variables (when outside Lambda context). The decision is
* made at runtime on every method call to support Lambda's concurrent execution
* isolation.
*/
class BatchProcessingStore {
#recordsKey = Symbol('powertools.batch.records');
#handlerKey = Symbol('powertools.batch.handler');
#optionsKey = Symbol('powertools.batch.options');
#failureMessagesKey = Symbol('powertools.batch.failureMessages');
#successMessagesKey = Symbol('powertools.batch.successMessages');
#batchResponseKey = Symbol('powertools.batch.batchResponse');
#errorsKey = Symbol('powertools.batch.errors');
#fallbackRecords = [];
#fallbackHandler = () => { };
#fallbackOptions;
#fallbackFailureMessages = [];
#fallbackSuccessMessages = [];
#fallbackBatchResponse = {
batchItemFailures: [],
};
#fallbackErrors = [];
getRecords() {
if (!shouldUseInvokeStore()) {
return this.#fallbackRecords;
}
if (globalThis.awslambda?.InvokeStore === undefined) {
throw new Error('InvokeStore is not available');
}
const store = globalThis.awslambda.InvokeStore;
return store.get(this.#recordsKey) ?? [];
}
setRecords(records) {
if (!shouldUseInvokeStore()) {
this.#fallbackRecords = records;
return;
}
if (globalThis.awslambda?.InvokeStore === undefined) {
throw new Error('InvokeStore is not available');
}
const store = globalThis.awslambda.InvokeStore;
store.set(this.#recordsKey, records);
}
getHandler() {
if (!shouldUseInvokeStore()) {
return this.#fallbackHandler;
}
return (globalThis.awslambda?.InvokeStore?.get(this.#handlerKey) ?? (() => { }));
}
setHandler(handler) {
if (!shouldUseInvokeStore()) {
this.#fallbackHandler = handler;
return;
}
globalThis.awslambda?.InvokeStore?.set(this.#handlerKey, handler);
}
getOptions() {
if (!shouldUseInvokeStore()) {
return this.#fallbackOptions;
}
return globalThis.awslambda?.InvokeStore?.get(this.#optionsKey);
}
setOptions(options) {
if (!shouldUseInvokeStore()) {
this.#fallbackOptions = options;
return;
}
globalThis.awslambda?.InvokeStore?.set(this.#optionsKey, options);
}
getFailureMessages() {
if (!shouldUseInvokeStore()) {
return this.#fallbackFailureMessages;
}
return (globalThis.awslambda?.InvokeStore?.get(this.#failureMessagesKey) ?? []);
}
setFailureMessages(messages) {
if (!shouldUseInvokeStore()) {
this.#fallbackFailureMessages = messages;
return;
}
globalThis.awslambda?.InvokeStore?.set(this.#failureMessagesKey, messages);
}
getSuccessMessages() {
if (!shouldUseInvokeStore()) {
return this.#fallbackSuccessMessages;
}
return (globalThis.awslambda?.InvokeStore?.get(this.#successMessagesKey) ?? []);
}
setSuccessMessages(messages) {
if (!shouldUseInvokeStore()) {
this.#fallbackSuccessMessages = messages;
return;
}
globalThis.awslambda?.InvokeStore?.set(this.#successMessagesKey, messages);
}
getBatchResponse() {
if (!shouldUseInvokeStore()) {
return this.#fallbackBatchResponse;
}
return (globalThis.awslambda?.InvokeStore?.get(this.#batchResponseKey) ?? { batchItemFailures: [] });
}
setBatchResponse(response) {
if (!shouldUseInvokeStore()) {
this.#fallbackBatchResponse = response;
return;
}
globalThis.awslambda?.InvokeStore?.set(this.#batchResponseKey, response);
}
getErrors() {
if (!shouldUseInvokeStore()) {
return this.#fallbackErrors;
}
return (globalThis.awslambda?.InvokeStore?.get(this.#errorsKey) ?? []);
}
setErrors(errors) {
if (!shouldUseInvokeStore()) {
this.#fallbackErrors = errors;
return;
}
globalThis.awslambda?.InvokeStore?.set(this.#errorsKey, errors);
}
}
export { BatchProcessingStore };