|
1 | 1 | import type { StandardSchemaV1 } from '@standard-schema/spec';
|
2 | 2 | import type {
|
| 3 | + AttributeValue, |
3 | 4 | DynamoDBRecord,
|
4 | 5 | KinesisStreamRecord,
|
5 | 6 | SQSRecord,
|
| 7 | + StreamRecord, |
6 | 8 | } from 'aws-lambda';
|
7 | 9 | import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
|
8 | 10 | import { EventType, SchemaType } from './constants.js';
|
@@ -180,6 +182,35 @@ class BatchProcessor extends BasePartialBatchProcessor {
|
180 | 182 | );
|
181 | 183 | throw new Error('Unsupported schema type');
|
182 | 184 | }
|
| 185 | + if (eventType === EventType.DynamoDBStreams) { |
| 186 | + const extendedSchemaParsing = parse(record, undefined, schema, true); |
| 187 | + if (extendedSchemaParsing.success) |
| 188 | + return extendedSchemaParsing.data as DynamoDBRecord; |
| 189 | + if (schema['~standard'].vendor === SchemaType.Zod) { |
| 190 | + const { DynamoDBMarshalled } = await import( |
| 191 | + '@aws-lambda-powertools/parser/helpers/dynamodb' |
| 192 | + ); |
| 193 | + const { DynamoDBStreamRecord, DynamoDBStreamChangeRecordBase } = |
| 194 | + await import('@aws-lambda-powertools/parser/schemas/dynamodb'); |
| 195 | + const extendedSchema = DynamoDBStreamRecord.extend({ |
| 196 | + dynamodb: DynamoDBStreamChangeRecordBase.extend({ |
| 197 | + // biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema |
| 198 | + OldImage: DynamoDBMarshalled<StreamRecord['OldImage']>( |
| 199 | + schema as any |
| 200 | + ).optional(), |
| 201 | + // biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema |
| 202 | + NewImage: DynamoDBMarshalled<StreamRecord['NewImage']>( |
| 203 | + schema as any |
| 204 | + ).optional(), |
| 205 | + }), |
| 206 | + }); |
| 207 | + return parse(record, undefined, extendedSchema); |
| 208 | + } |
| 209 | + console.warn( |
| 210 | + 'The schema provided is not supported. Only Zod schemas are supported for extension.' |
| 211 | + ); |
| 212 | + throw new Error('Unsupported schema type'); |
| 213 | + } |
183 | 214 | console.warn(
|
184 | 215 | `The event type provided is not supported. Supported events: ${Object.values(EventType).join(',')}`
|
185 | 216 | );
|
|
0 commit comments