forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamodb.py
More file actions
46 lines (36 loc) · 1.68 KB
/
dynamodb.py
File metadata and controls
46 lines (36 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, Any
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
from aws_lambda_powertools.utilities.parser.models import DynamoDBStreamModel
if TYPE_CHECKING:
from aws_lambda_powertools.utilities.parser.types import T
logger = logging.getLogger(__name__)
class DynamoDBStreamEnvelope(BaseEnvelope):
"""DynamoDB Stream Envelope to extract data within NewImage/OldImage
Note: Values are the parsed models. Images' values can also be None, and
length of the list is the record's amount in the original event.
"""
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> list[dict[str, T | None]]:
"""Parses DynamoDB Stream records found in either NewImage and OldImage with model provided
Parameters
----------
data : dict
Lambda event to be parsed
model : type[T]
Data model provided to parse after extracting data using envelope
Returns
-------
list[T | None]
List of dictionaries with NewImage and OldImage records parsed with model provided
"""
logger.debug(f"Parsing incoming data with DynamoDB Stream model {DynamoDBStreamModel}")
parsed_envelope = DynamoDBStreamModel.model_validate(data)
logger.debug(f"Parsing DynamoDB Stream new and old records with {model}")
return [
{
"NewImage": self._parse(data=record.dynamodb.NewImage, model=model),
"OldImage": self._parse(data=record.dynamodb.OldImage, model=model),
}
for record in parsed_envelope.Records
]