Skip to content

Commit 8b3721a

Browse files
committed
doc: DynamoDBMarshalled function description in user doc
1 parent a1f9b8c commit 8b3721a

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

docs/utilities/parser.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ If you want to extend a schema and transform a JSON stringified payload to an ob
151151
--8<-- "examples/snippets/parser/samples/exampleSqsPayload.json"
152152
```
153153

154+
### DynamoDB Stream event parsing
155+
156+
If you want to parse a DynamoDB stream event with unmarshalling, you can use the helper function `DynamoDBMarshalled`:
157+
158+
=== "DynamoDBStreamSchema with DynamoDBMarshalled"
159+
```typescript hl_lines="17"
160+
--8<-- "examples/snippets/parser/extendDynamoDBStreamSchema.ts"
161+
```
162+
163+
=== "DynamoDBStream event payload"
164+
165+
```json hl_lines="13-20 49-56"
166+
--8<-- "examples/snippets/parser/samples/exampleDynamoDBStreamPayload.json"
167+
```
168+
154169
## Envelopes
155170

156171
When trying to parse your payload you might encounter the following situations:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { DynamoDBMarshalled } from '@aws-lambda-powertools/parser/helpers/dynamodb';
2+
import {
3+
DynamoDBStreamRecord,
4+
DynamoDBStreamSchema,
5+
} from '@aws-lambda-powertools/parser/schemas/dynamodb';
6+
import { z } from 'zod';
7+
8+
const customSchema = z.object({
9+
id: z.string(),
10+
message: z.string(),
11+
});
12+
13+
const extendedSchema = DynamoDBStreamSchema.extend({
14+
Records: z.array(
15+
DynamoDBStreamRecord.extend({
16+
dynamodb: z.object({
17+
NewImage: DynamoDBMarshalled(customSchema).optional(),
18+
}),
19+
})
20+
),
21+
});
22+
23+
type ExtendedDynamoDBStreamEvent = z.infer<typeof extendedSchema>;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"Records": [
3+
{
4+
"eventID": "1",
5+
"eventVersion": "1.0",
6+
"dynamodb": {
7+
"ApproximateCreationDateTime": 1693997155.0,
8+
"Keys": {
9+
"Id": {
10+
"N": "101"
11+
}
12+
},
13+
"NewImage": {
14+
"Message": {
15+
"S": "New item!"
16+
},
17+
"Id": {
18+
"N": "101"
19+
}
20+
},
21+
"StreamViewType": "NEW_AND_OLD_IMAGES",
22+
"SequenceNumber": "111",
23+
"SizeBytes": 26
24+
},
25+
"awsRegion": "us-west-2",
26+
"eventName": "INSERT",
27+
"eventSourceARN": "eventsource_arn",
28+
"eventSource": "aws:dynamodb"
29+
},
30+
{
31+
"eventID": "2",
32+
"eventVersion": "1.0",
33+
"dynamodb": {
34+
"OldImage": {
35+
"Message": {
36+
"S": "New item!"
37+
},
38+
"Id": {
39+
"N": "101"
40+
}
41+
},
42+
"SequenceNumber": "222",
43+
"Keys": {
44+
"Id": {
45+
"N": "101"
46+
}
47+
},
48+
"SizeBytes": 59,
49+
"NewImage": {
50+
"Message": {
51+
"S": "This item has changed"
52+
},
53+
"Id": {
54+
"N": "101"
55+
}
56+
},
57+
"StreamViewType": "NEW_AND_OLD_IMAGES"
58+
},
59+
"awsRegion": "us-west-2",
60+
"eventName": "MODIFY",
61+
"eventSourceARN": "source_arn",
62+
"eventSource": "aws:dynamodb"
63+
}
64+
]
65+
}
66+

packages/parser/src/helpers/dynamodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { type ZodTypeAny, z } from 'zod';
2020
* })
2121
* ),
2222
* });
23+
* type eventSchema = z.infer<typeof extendedSchema>;
2324
* ```
2425
* For example, if you have a DynamoDB stream event like the following:
2526
*

0 commit comments

Comments
 (0)