@aws-lambda-powertools/batch
Version:
The batch processing package for the Powertools for AWS Lambda (TypeScript) library.
117 lines (116 loc) • 3.82 kB
JavaScript
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
import { BatchProcessingError } from './errors.js';
/**
* Process records in a batch asynchronously 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 asynchronously and in parallel.
*
* **Process batch triggered by SQS**
*
* @example
* ```typescript
* import {
* BatchProcessor,
* EventType,
* processPartialResponse,
* } from '@aws-lambda-powertools/batch';
* import type { SQSRecord, SQSHandler } from 'aws-lambda';
*
* const processor = new BatchProcessor(EventType.SQS);
*
* const recordHandler = async (record: SQSRecord): Promise<void> => {
* const payload = JSON.parse(record.body);
* };
*
* export const handler: SQSHandler = async (event, context) =>
* processPartialResponse(event, recordHandler, processor, {
* context,
* });
* ```
*
* **Process batch triggered by Kinesis Data Streams*
*
* @example
* ```typescript
* import {
* BatchProcessor,
* EventType,
* processPartialResponse,
* } from '@aws-lambda-powertools/batch';
* import type { KinesisStreamHandler, KinesisStreamRecord } from 'aws-lambda';
*
* const processor = new BatchProcessor(EventType.KinesisDataStreams);
*
* const recordHandler = async (record: KinesisStreamRecord): Promise<void> => {
* const payload = JSON.parse(record.kinesis.data);
* };
*
* export const handler: KinesisStreamHandler = async (event, context) =>
* processPartialResponse(event, recordHandler, processor, {
* context,
* });
* ```
*
* **Process batch triggered by DynamoDB Streams**
*
* @example
* ```typescript
* import {
* BatchProcessor,
* EventType,
* processPartialResponse,
* } from '@aws-lambda-powertools/batch';
* import type { DynamoDBRecord, DynamoDBStreamHandler } from 'aws-lambda';
*
* const processor = new BatchProcessor(EventType.DynamoDBStreams);
*
* const recordHandler = async (record: DynamoDBRecord): Promise<void> => {
* const payload = record.dynamodb.NewImage.Message.S;
* };
*
* export const handler: DynamoDBStreamHandler = async (event, context) =>
* processPartialResponse(event, recordHandler, processor, {
* context,
* });
* ```
*
* @param eventType The type of event to process (SQS, Kinesis, DynamoDB)
*/
class BatchProcessor extends BasePartialBatchProcessor {
/**
* Handle a record asynchronously 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
*/
async processRecord(record) {
try {
const data = this.toBatchType(record, this.eventType);
const result = await this.handler(data, this.options?.context);
return this.successHandler(record, result);
}
catch (error) {
return this.failureHandler(record, error);
}
}
/**
* @throws {BatchProcessingError} This method is not implemented for synchronous processing.
*
* @param _record The record to be processed
*/
processRecordSync(_record) {
throw new BatchProcessingError('Not implemented. Use asyncProcess() instead.');
}
}
export { BatchProcessor };