3
3
from typing import Any , Dict , List , Literal , Optional , Type , Union
4
4
5
5
from pydantic import BaseModel , Field , field_validator
6
+
6
7
from aws_lambda_powertools .shared .dynamodb_deserializer import TypeDeserializer
7
8
8
9
_DESERIALIZER = TypeDeserializer ()
9
10
10
11
11
12
class DynamoDBStreamChangedRecordModel (BaseModel ):
12
- ApproximateCreationDateTime : Optional [float ] = Field ( # AWS sends this as Unix epoch float
13
+ ApproximateCreationDateTime : Optional [datetime ] = Field ( # AWS sends this as Unix epoch float
13
14
default = None ,
14
15
description = "The approximate date and time when the stream record was created (Unix epoch time)." ,
15
- examples = [1693997155.0 ]
16
- )
17
- Keys : Dict [str , Any ] = Field (
18
- description = "Primary key attributes for the item." ,
19
- examples = [{"Id" : {"N" : "101" }}]
16
+ examples = [1693997155.0 ],
20
17
)
18
+ Keys : Dict [str , Any ] = Field (description = "Primary key attributes for the item." , examples = [{"Id" : {"N" : "101" }}])
21
19
NewImage : Optional [Union [Dict [str , Any ], Type [BaseModel ], BaseModel ]] = Field (
22
20
default = None ,
23
21
description = "The item after modifications, in DynamoDB attribute-value format." ,
24
- examples = [{
25
- "Message" : {"S" : "New item!" },
26
- "Id" : {"N" : "101" }
27
- }]
22
+ examples = [{"Message" : {"S" : "New item!" }, "Id" : {"N" : "101" }}],
28
23
)
29
24
OldImage : Optional [Union [Dict [str , Any ], Type [BaseModel ], BaseModel ]] = Field (
30
25
default = None ,
31
26
description = "The item before modifications, in DynamoDB attribute-value format." ,
32
- examples = [{
33
- "Message" : {"S" : "Old item!" },
34
- "Id" : {"N" : "100" }
35
- }]
36
- )
37
- SequenceNumber : str = Field (
38
- description = "A unique identifier for the stream record." ,
39
- examples = ["222" ]
40
- )
41
- SizeBytes : int = Field (
42
- description = "The size of the stream record, in bytes." ,
43
- examples = [26 ]
27
+ examples = [{"Message" : {"S" : "Old item!" }, "Id" : {"N" : "100" }}],
44
28
)
29
+ SequenceNumber : str = Field (description = "A unique identifier for the stream record." , examples = ["222" ])
30
+ SizeBytes : int = Field (description = "The size of the stream record, in bytes." , examples = [26 ])
45
31
StreamViewType : Literal ["NEW_AND_OLD_IMAGES" , "KEYS_ONLY" , "NEW_IMAGE" , "OLD_IMAGE" ] = Field (
46
- description = "The type of data included in the stream record." ,
47
- examples = ["NEW_AND_OLD_IMAGES" ]
32
+ description = "The type of data included in the stream record." , examples = ["NEW_AND_OLD_IMAGES" ]
48
33
)
49
34
50
35
@field_validator ("Keys" , "NewImage" , "OldImage" , mode = "before" )
51
36
def deserialize_field (cls , value ):
52
- return {k : _DESERIALIZER .deserialize (v ) for k , v in value .items ()} if value else value
37
+ return {k : _DESERIALIZER .deserialize (v ) for k , v in value .items ()}
53
38
54
39
55
40
class UserIdentity (BaseModel ):
56
41
type : Literal ["Service" ] = Field (
57
42
default = "Service" ,
58
43
description = "The type of identity that made the request, which is always 'Service' for DynamoDB streams." ,
59
- examples = ["Service" ]
44
+ examples = ["Service" ],
60
45
)
61
- principalId : str = Field (
46
+ principalId : Literal [ "dynamodb.amazonaws.com" ] = Field (
62
47
description = "The unique identifier for the principal that made the request." ,
63
- examples = ["dynamodb.amazonaws.com" ]
48
+ examples = ["dynamodb.amazonaws.com" ],
64
49
)
65
50
66
51
67
52
class DynamoDBStreamRecordModel (BaseModel ):
68
- eventID : str = Field (
69
- description = "A unique identifier for the event." ,
70
- examples = ["1" ]
71
- )
53
+ eventID : str = Field (description = "A unique identifier for the event." , examples = ["1" ])
72
54
eventName : Literal ["INSERT" , "MODIFY" , "REMOVE" ] = Field (
73
- description = "The type of operation that was performed on the item." ,
74
- examples = ["INSERT" ]
55
+ description = "The type of operation that was performed on the item." , examples = ["INSERT" ]
75
56
)
76
- eventVersion : str = Field (
77
- default = "1.0" ,
78
- description = "The version of the stream record format." ,
79
- examples = ["1.0" ]
80
- )
81
- eventSource : str = Field (
57
+ eventVersion : float = Field (default = "1.0" , description = "The version of the stream record format." , examples = ["1.0" ])
58
+ eventSource : Literal ["aws:dynamodb" ] = Field (
82
59
default = "aws:dynamodb" ,
83
60
description = "The source of the event, which is always 'aws:dynamodb' for DynamoDB streams." ,
84
- examples = ["aws:dynamodb" ]
85
- )
86
- awsRegion : str = Field (
87
- description = "The AWS region where the stream record was generated." ,
88
- examples = ["us-west-2" ]
61
+ examples = ["aws:dynamodb" ],
89
62
)
63
+ awsRegion : str = Field (description = "The AWS region where the stream record was generated." , examples = ["us-west-2" ])
90
64
eventSourceARN : str = Field (
91
65
description = "The Amazon Resource Name (ARN) of the DynamoDB stream." ,
92
- examples = ["arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000" ]
66
+ examples = ["arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000" ],
93
67
)
94
68
dynamodb : DynamoDBStreamChangedRecordModel = Field (
95
69
description = "Contains the details of the DynamoDB stream record." ,
96
- examples = [{
97
- "ApproximateCreationDateTime" : 1693997155.0 ,
98
- "Keys" : {"Id" : {"N" : "101" }},
99
- "NewImage" : {"Message" : {"S" : "New item!" }, "Id" : {"N" : "101" }},
100
- "OldImage" : {"Message" : {"S" : "Old item!" }, "Id" : {"N" : "100" }},
101
- "SequenceNumber" : "222" ,
102
- "SizeBytes" : 26 ,
103
- "StreamViewType" : "NEW_AND_OLD_IMAGES"
104
- }]
70
+ examples = [
71
+ {
72
+ "ApproximateCreationDateTime" : 1693997155.0 ,
73
+ "Keys" : {"Id" : {"N" : "101" }},
74
+ "NewImage" : {"Message" : {"S" : "New item!" }, "Id" : {"N" : "101" }},
75
+ "OldImage" : {"Message" : {"S" : "Old item!" }, "Id" : {"N" : "100" }},
76
+ "SequenceNumber" : "222" ,
77
+ "SizeBytes" : 26 ,
78
+ "StreamViewType" : "NEW_AND_OLD_IMAGES" ,
79
+ }
80
+ ],
105
81
)
106
82
userIdentity : Optional [UserIdentity ] = Field (
107
83
default = None ,
108
84
description = "Information about the identity that made the request." ,
109
- examples = [{"type" : "Service" , "principalId" : "dynamodb.amazonaws.com" }]
85
+ examples = [{"type" : "Service" , "principalId" : "dynamodb.amazonaws.com" }],
110
86
)
111
87
112
88
113
89
class DynamoDBStreamModel (BaseModel ):
114
90
Records : List [DynamoDBStreamRecordModel ] = Field (
115
91
description = "A list of records that contain the details of the DynamoDB stream events." ,
116
- examples = [{
117
- "eventID" : "1" ,
118
- "eventName" : "INSERT" ,
119
- "eventVersion" : "1.0" ,
120
- "eventSource" : "aws:dynamodb" ,
121
- "awsRegion" : "us-west-2" ,
122
- "eventSourceARN" : "arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000" ,
123
- "dynamodb" : {
124
- "ApproximateCreationDateTime" : 1693997155.0 ,
125
- "Keys" : {"Id" : {"N" : "101" }},
126
- "NewImage" : {"Message" : {"S" : "New item!" }, "Id" : {"N" : "101" }},
127
- "OldImage" : {"Message" : {"S" : "Old item!" }, "Id" : {"N" : "100" }},
128
- "SequenceNumber" : "222" ,
129
- "SizeBytes" : 26 ,
130
- "StreamViewType" : "NEW_AND_OLD_IMAGES"
131
- },
132
- "userIdentity" : {"type" : "Service" , "principalId" : "dynamodb.amazonaws.com" }
133
- }]
134
- )
92
+ examples = [
93
+ {
94
+ "eventID" : "1" ,
95
+ "eventName" : "INSERT" ,
96
+ "eventVersion" : "1.0" ,
97
+ "eventSource" : "aws:dynamodb" ,
98
+ "awsRegion" : "us-west-2" ,
99
+ "eventSourceARN" : "arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000" ,
100
+ "dynamodb" : {
101
+ "ApproximateCreationDateTime" : 1693997155.0 ,
102
+ "Keys" : {"Id" : {"N" : "101" }},
103
+ "NewImage" : {"Message" : {"S" : "New item!" }, "Id" : {"N" : "101" }},
104
+ "OldImage" : {"Message" : {"S" : "Old item!" }, "Id" : {"N" : "100" }},
105
+ "SequenceNumber" : "222" ,
106
+ "SizeBytes" : 26 ,
107
+ "StreamViewType" : "NEW_AND_OLD_IMAGES" ,
108
+ },
109
+ "userIdentity" : {"type" : "Service" , "principalId" : "dynamodb.amazonaws.com" },
110
+ }
111
+ ],
112
+ )
0 commit comments