@aws-lambda-powertools/batch
Version:
The batch processing package for the Powertools for AWS Lambda (TypeScript) library.
121 lines (120 loc) • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchProcessorSync = void 0;
const BasePartialBatchProcessor_js_1 = require("./BasePartialBatchProcessor.js");
const errors_js_1 = require("./errors.js");
/**
* Process records in a batch synchronously and handle partial failure cases.
*
* The batch processor supports processing records coming from Amazon SQS,
* Amazon Kinesis Data Streams, and Amazon DynamoDB Streams.
*
* Items are processed synchronously and in sequence.
*
* **Process batch triggered by SQS**
*
* @example
* ```typescript
* import {
* BatchProcessorSync,
* EventType,
* processPartialResponseSync,
* } from '@aws-lambda-powertools/batch';
* import type { SQSRecord, SQSHandler } from 'aws-lambda';
*
* const processor = new BatchProcessorSync(EventType.SQS);
*
* const recordHandler = (record: SQSRecord): void => {
* const payload = JSON.parse(record.body);
* };
*
* export const handler: SQSHandler = async (event, context) =>
* processPartialResponseSync(event, recordHandler, processor, {
* context,
* });
* ```
*
* **Process batch triggered by Kinesis Data Streams*
*
* @example
* ```typescript
* import {
* BatchProcessorSync,
* EventType,
* processPartialResponseSync,
* } from '@aws-lambda-powertools/batch';
* import type { KinesisStreamHandler, KinesisStreamRecord } from 'aws-lambda';
*
* const processor = new BatchProcessorSync(EventType.KinesisDataStreams);
*
* const recordHandler = (record: KinesisStreamRecord): void => {
* const payload = JSON.parse(record.kinesis.data);
* };
*
* export const handler: KinesisStreamHandler = async (event, context) =>
* processPartialResponseSync(event, recordHandler, processor, {
* context,
* });
* ```
*
* **Process batch triggered by DynamoDB Streams**
*
* @example
* ```typescript
* import {
* BatchProcessorSync,
* EventType,
* processPartialResponseSync,
* } from '@aws-lambda-powertools/batch';
* import type { DynamoDBRecord, DynamoDBStreamHandler } from 'aws-lambda';
*
* const processor = new BatchProcessorSync(EventType.DynamoDBStreams);
*
* const recordHandler = (record: DynamoDBRecord): void => {
* const payload = record.dynamodb.NewImage.Message.S;
* };
*
* export const handler: DynamoDBStreamHandler = async (event, context) =>
* processPartialResponseSync(event, recordHandler, processor, {
* context,
* });
* ```
*
* @param eventType The type of event to process (SQS, Kinesis, DynamoDB)
* @deprecated Use {@link BasePartialBatchProcessor} instead, this class is deprecated and will be removed in the next major version.
*/
/* v8 ignore start */ class BatchProcessorSync extends BasePartialBatchProcessor_js_1.BasePartialBatchProcessor {
/**
* @throws {BatchProcessingError} This method is not implemented for asynchronous processing.
*
* @param _record The record to be processed
*/
processRecord(_record) {
throw new errors_js_1.BatchProcessingError('Not implemented. Use process() instead.');
}
/**
* Handle a record synchronously with the instance handler provided.
*
* This method implements the abstract method from the parent class,
* and orchestrates the processing of a single record.
*
* First, it converts the record to the appropriate type for the batch processor.
* Then, it calls the handler function with the record data and context.
*
* If the handler function completes successfully, the method returns a success response.
* Otherwise, it returns a failure response with the error that occurred during processing.
*
* @param record The record to be processed
*/
processRecordSync(record) {
try {
const data = this.toBatchType(record, this.eventType);
const result = this.handler(data, this.options?.context);
return this.successHandler(record, result);
}
catch (error) {
return this.failureHandler(record, error);
}
}
} /* v8 ignore stop */
exports.BatchProcessorSync = BatchProcessorSync;