@aws-lambda-powertools/batch
Advanced tools
Comparing version
@@ -16,2 +16,3 @@ import type { BaseRecord, BatchProcessingOptions, EventSourceDataClassTypes, FailureResponse, SuccessResponse } from './types.js'; | ||
declare abstract class BasePartialProcessor { | ||
#private; | ||
/** | ||
@@ -69,3 +70,3 @@ * List of errors that occurred during processing | ||
/** | ||
* Process all records with an asyncronous handler | ||
* Process all records with an asynchronous handler | ||
* | ||
@@ -79,5 +80,5 @@ * Once called, the processor will create an array of promises to process each record | ||
/** | ||
* Process a record with an asyncronous handler | ||
* Process a record with an asynchronous handler | ||
* | ||
* An implementation of this method is required for asyncronous processors. | ||
* An implementation of this method is required for asynchronous processors. | ||
* | ||
@@ -84,0 +85,0 @@ * When implementing this method, you should at least call the successHandler method |
@@ -66,3 +66,3 @@ "use strict"; | ||
/** | ||
* Process all records with an asyncronous handler | ||
* Process all records with an asynchronous handler | ||
* | ||
@@ -87,4 +87,7 @@ * Once called, the processor will create an array of promises to process each record | ||
this.prepare(); | ||
const processingPromises = this.records.map((record) => this.processRecord(record)); | ||
const processedRecords = await Promise.all(processingPromises); | ||
// Default to `true` if `processInParallel` is not specified. | ||
const processInParallel = this.options?.processInParallel ?? true; | ||
const processedRecords = processInParallel | ||
? await this.#processRecordsInParallel() | ||
: await this.#processRecordsSequentially(); | ||
this.clean(); | ||
@@ -158,3 +161,19 @@ return processedRecords; | ||
} | ||
/** | ||
* Processes records in parallel using `Promise.all`. | ||
*/ | ||
async #processRecordsInParallel() { | ||
return Promise.all(this.records.map((record) => this.processRecord(record))); | ||
} | ||
/** | ||
* Processes records sequentially, ensuring that each record is processed one after the other. | ||
*/ | ||
async #processRecordsSequentially() { | ||
const processedRecords = []; | ||
for (const record of this.records) { | ||
processedRecords.push(await this.processRecord(record)); | ||
} | ||
return processedRecords; | ||
} | ||
} | ||
exports.BasePartialProcessor = BasePartialProcessor; |
@@ -11,2 +11,3 @@ import type { Context, DynamoDBRecord, KinesisStreamRecord, SQSRecord } from 'aws-lambda'; | ||
* @property throwOnFullBatchFailure The option to throw an error if the entire batch fails | ||
* @property processInParallel Indicates whether the records should be processed in parallel | ||
*/ | ||
@@ -28,2 +29,8 @@ type BatchProcessingOptions<T = BasePartialBatchProcessor> = { | ||
throwOnFullBatchFailure?: boolean; | ||
/** | ||
* Indicates whether the records should be processed in parallel. | ||
* When set to `true`, the records will be processed in parallel using `Promise.all`. | ||
* When set to `false`, the records will be processed sequentially. | ||
*/ | ||
processInParallel?: T extends SqsFifoPartialProcessor ? never : boolean; | ||
}; | ||
@@ -30,0 +37,0 @@ /** |
@@ -16,2 +16,3 @@ import type { BaseRecord, BatchProcessingOptions, EventSourceDataClassTypes, FailureResponse, SuccessResponse } from './types.js'; | ||
declare abstract class BasePartialProcessor { | ||
#private; | ||
/** | ||
@@ -69,3 +70,3 @@ * List of errors that occurred during processing | ||
/** | ||
* Process all records with an asyncronous handler | ||
* Process all records with an asynchronous handler | ||
* | ||
@@ -79,5 +80,5 @@ * Once called, the processor will create an array of promises to process each record | ||
/** | ||
* Process a record with an asyncronous handler | ||
* Process a record with an asynchronous handler | ||
* | ||
* An implementation of this method is required for asyncronous processors. | ||
* An implementation of this method is required for asynchronous processors. | ||
* | ||
@@ -84,0 +85,0 @@ * When implementing this method, you should at least call the successHandler method |
@@ -63,3 +63,3 @@ /** | ||
/** | ||
* Process all records with an asyncronous handler | ||
* Process all records with an asynchronous handler | ||
* | ||
@@ -84,4 +84,7 @@ * Once called, the processor will create an array of promises to process each record | ||
this.prepare(); | ||
const processingPromises = this.records.map((record) => this.processRecord(record)); | ||
const processedRecords = await Promise.all(processingPromises); | ||
// Default to `true` if `processInParallel` is not specified. | ||
const processInParallel = this.options?.processInParallel ?? true; | ||
const processedRecords = processInParallel | ||
? await this.#processRecordsInParallel() | ||
: await this.#processRecordsSequentially(); | ||
this.clean(); | ||
@@ -155,3 +158,19 @@ return processedRecords; | ||
} | ||
/** | ||
* Processes records in parallel using `Promise.all`. | ||
*/ | ||
async #processRecordsInParallel() { | ||
return Promise.all(this.records.map((record) => this.processRecord(record))); | ||
} | ||
/** | ||
* Processes records sequentially, ensuring that each record is processed one after the other. | ||
*/ | ||
async #processRecordsSequentially() { | ||
const processedRecords = []; | ||
for (const record of this.records) { | ||
processedRecords.push(await this.processRecord(record)); | ||
} | ||
return processedRecords; | ||
} | ||
} | ||
export { BasePartialProcessor }; |
@@ -11,2 +11,3 @@ import type { Context, DynamoDBRecord, KinesisStreamRecord, SQSRecord } from 'aws-lambda'; | ||
* @property throwOnFullBatchFailure The option to throw an error if the entire batch fails | ||
* @property processInParallel Indicates whether the records should be processed in parallel | ||
*/ | ||
@@ -28,2 +29,8 @@ type BatchProcessingOptions<T = BasePartialBatchProcessor> = { | ||
throwOnFullBatchFailure?: boolean; | ||
/** | ||
* Indicates whether the records should be processed in parallel. | ||
* When set to `true`, the records will be processed in parallel using `Promise.all`. | ||
* When set to `false`, the records will be processed sequentially. | ||
*/ | ||
processInParallel?: T extends SqsFifoPartialProcessor ? never : boolean; | ||
}; | ||
@@ -30,0 +37,0 @@ /** |
{ | ||
"name": "@aws-lambda-powertools/batch", | ||
"version": "2.8.0", | ||
"version": "2.9.0", | ||
"description": "The batch processing package for the Powertools for AWS Lambda (TypeScript) library.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -177,2 +177,3 @@ # Powertools for AWS Lambda (TypeScript) - Batch Processing Utility | ||
- [Elva](https://elva-group.com) | ||
- [Flyweight](https://flyweight.io/) | ||
- [globaldatanet](https://globaldatanet.com/) | ||
@@ -179,0 +180,0 @@ - [Hashnode](https://hashnode.com/) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
170673
1.47%3748
1.46%198
0.51%