|
| 1 | +import { |
| 2 | + DynamoDBClient, |
| 3 | + BatchWriteItemCommand, |
| 4 | +} from '@aws-sdk/client-dynamodb'; |
| 5 | +import { marshall } from '@aws-sdk/util-dynamodb'; |
| 6 | +import { |
| 7 | + BasePartialProcessor, |
| 8 | + processPartialResponse, |
| 9 | +} from '@aws-lambda-powertools/batch'; |
| 10 | +import type { |
| 11 | + SuccessResponse, |
| 12 | + FailureResponse, |
| 13 | + BaseRecord, |
| 14 | +} from '@aws-lambda-powertools/batch'; |
| 15 | +import type { SQSEvent, Context, SQSBatchResponse } from 'aws-lambda'; |
| 16 | + |
| 17 | +const tableName = process.env.TABLE_NAME || 'table-not-found'; |
| 18 | + |
| 19 | +class MyPartialProcessor extends BasePartialProcessor { |
| 20 | + #tableName: string; |
| 21 | + #client?: DynamoDBClient; |
| 22 | + |
| 23 | + public constructor(tableName: string) { |
| 24 | + super(); |
| 25 | + this.#tableName = tableName; |
| 26 | + } |
| 27 | + |
| 28 | + public async asyncProcessRecord( |
| 29 | + _record: BaseRecord |
| 30 | + ): Promise<SuccessResponse | FailureResponse> { |
| 31 | + throw new Error('Not implemented'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * It's called once, **after** processing the batch. |
| 36 | + * |
| 37 | + * Here we are writing all the processed messages to DynamoDB. |
| 38 | + */ |
| 39 | + public clean(): void { |
| 40 | + // We know that the client is defined because clean() is called after prepare() |
| 41 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 42 | + this.#client!.send( |
| 43 | + new BatchWriteItemCommand({ |
| 44 | + RequestItems: { |
| 45 | + [this.#tableName]: this.successMessages.map((message) => ({ |
| 46 | + PutRequest: { |
| 47 | + Item: marshall(message), |
| 48 | + }, |
| 49 | + })), |
| 50 | + }, |
| 51 | + }) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * It's called once, **before** processing the batch. |
| 57 | + * |
| 58 | + * It initializes a new client and cleans up any existing data. |
| 59 | + */ |
| 60 | + public prepare(): void { |
| 61 | + this.#client = new DynamoDBClient({}); |
| 62 | + this.successMessages = []; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * It handles how your record is processed. |
| 67 | + * |
| 68 | + * Here we are keeping the status of each run, `this.handler` is |
| 69 | + * the function that is passed when calling `processor.register()`. |
| 70 | + */ |
| 71 | + public processRecord(record: BaseRecord): SuccessResponse | FailureResponse { |
| 72 | + try { |
| 73 | + const result = this.handler(record); |
| 74 | + |
| 75 | + return this.successHandler(record, result); |
| 76 | + } catch (error) { |
| 77 | + return this.failureHandler(record, error as Error); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +const processor = new MyPartialProcessor(tableName); |
| 83 | + |
| 84 | +const recordHandler = (): number => { |
| 85 | + return Math.floor(Math.random() * 10); |
| 86 | +}; |
| 87 | + |
| 88 | +export const handler = async ( |
| 89 | + event: SQSEvent, |
| 90 | + context: Context |
| 91 | +): Promise<SQSBatchResponse> => { |
| 92 | + return processPartialResponse(event, recordHandler, processor, { |
| 93 | + context, |
| 94 | + }); |
| 95 | +}; |
0 commit comments