Skip to content

Commit 4ecb29c

Browse files
committed
Prefer event timestamp in ddb trigger events.
1 parent 27f70f7 commit 4ecb29c

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-lambda-stream",
3-
"version": "1.0.34",
3+
"version": "1.1.0",
44
"description": "Create stream processors with AWS Lambda functions.",
55
"keywords": [
66
"aws",

src/from/dynamodb.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const fromDynamodb = (event, {
3434
id: record.eventID,
3535
type: `${calculateEventTypePrefix(record, { skFn, discriminatorFn, eventTypePrefix })}-${calculateEventTypeSuffix(record)}`,
3636
partitionKey: record.dynamodb.Keys[pkFn].S,
37-
timestamp: record.dynamodb.ApproximateCreationDateTime * 1000,
37+
timestamp: deriveTimestamp(record),
3838
tags: {
3939
region: record.awsRegion,
4040
},
@@ -88,6 +88,9 @@ const calculateEventTypeSuffix = (record) => {
8888
return suffix;
8989
};
9090

91+
const deriveTimestamp = (record) =>
92+
record.dynamodb.NewImage?.timestamp || (record.dynamodb.ApproximateCreationDateTime * 1000);
93+
9194
//--------------------------------------------
9295
// global table support - version: 2017.11.29
9396
//--------------------------------------------

test/unit/from/dynamodb.test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,112 @@ describe('from/dynamodb.js', () => {
111111
.done(done);
112112
});
113113

114+
it('should prefer image timestamp if present on INSERT', (done) => {
115+
const events = toDynamodbRecords([
116+
{
117+
timestamp: 1572832690,
118+
keys: {
119+
pk: '1',
120+
sk: 'thing',
121+
},
122+
newImage: {
123+
pk: '1',
124+
sk: 'thing',
125+
discriminator: 'thing',
126+
name: 'n1',
127+
timestamp: 1572832690001
128+
// insert in the current region will not have the awsregion field
129+
},
130+
},
131+
// dynamodb stream emits an extra update event as it adorns the 'aws:rep' global table metadata
132+
// so this extra event should be skipped
133+
{
134+
timestamp: 1572832690,
135+
keys: {
136+
pk: '1',
137+
sk: 'thing',
138+
},
139+
newImage: {
140+
pk: '1',
141+
sk: 'thing',
142+
discriminator: 'thing',
143+
name: 'n1',
144+
awsregion: 'us-west-2',
145+
},
146+
oldImage: {
147+
pk: '1',
148+
sk: 'thing',
149+
discriminator: 'thing',
150+
name: 'n1',
151+
// as mentioned above there was no awsregion field on the insert event
152+
},
153+
},
154+
]);
155+
156+
fromDynamodb(events)
157+
.collect()
158+
.tap((collected) => {
159+
// console.log(JSON.stringify(collected, null, 2));
160+
161+
expect(collected.length).to.equal(1);
162+
expect(collected[0]).to.deep.equal({
163+
record: {
164+
eventID: '0',
165+
eventName: 'INSERT',
166+
eventSource: 'aws:dynamodb',
167+
awsRegion: 'us-west-2',
168+
dynamodb: {
169+
ApproximateCreationDateTime: 1572832690,
170+
Keys: {
171+
pk: {
172+
S: '1',
173+
},
174+
sk: {
175+
S: 'thing',
176+
},
177+
},
178+
NewImage: {
179+
pk: {
180+
S: '1',
181+
},
182+
sk: {
183+
S: 'thing',
184+
},
185+
discriminator: {
186+
S: 'thing',
187+
},
188+
name: {
189+
S: 'n1',
190+
},
191+
},
192+
OldImage: undefined,
193+
SequenceNumber: '0',
194+
StreamViewType: 'NEW_AND_OLD_IMAGES',
195+
},
196+
},
197+
event: {
198+
id: '0',
199+
type: 'thing-created',
200+
partitionKey: '1',
201+
timestamp: 1572832690000,
202+
tags: {
203+
region: 'us-west-2',
204+
},
205+
raw: {
206+
new: {
207+
pk: '1',
208+
sk: 'thing',
209+
discriminator: 'thing',
210+
name: 'n1',
211+
},
212+
old: undefined,
213+
},
214+
},
215+
});
216+
})
217+
.done(done);
218+
});
219+
114220
it('should parse MODIFY record', (done) => {
115221
const events = toDynamodbRecords([
116222
{

0 commit comments

Comments
 (0)