Skip to content

Commit 553fbba

Browse files
committed
Integrated Parser for the DynamoDB event type
1 parent 5c8e34c commit 553fbba

File tree

2 files changed

+367
-3
lines changed

2 files changed

+367
-3
lines changed

packages/batch/src/BatchProcessor.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { StandardSchemaV1 } from '@standard-schema/spec';
22
import type {
3+
AttributeValue,
34
DynamoDBRecord,
45
KinesisStreamRecord,
56
SQSRecord,
7+
StreamRecord,
68
} from 'aws-lambda';
79
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
810
import { EventType, SchemaType } from './constants.js';
@@ -180,6 +182,35 @@ class BatchProcessor extends BasePartialBatchProcessor {
180182
);
181183
throw new Error('Unsupported schema type');
182184
}
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+
}
183214
console.warn(
184215
`The event type provided is not supported. Supported events: ${Object.values(EventType).join(',')}`
185216
);

0 commit comments

Comments
 (0)