Skip to content

Commit a4fbc8f

Browse files
authored
Merge pull request #406 from jgilbert01/issue-ddb-timestamp
Prefer timestamp from ddb record
2 parents 27f70f7 + 6597e3c commit a4fbc8f

4 files changed

Lines changed: 119 additions & 4 deletions

File tree

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: 6 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,11 @@ const calculateEventTypeSuffix = (record) => {
8888
return suffix;
8989
};
9090

91+
const deriveTimestamp = (record) =>
92+
parseInt(record.dynamodb.NewImage?.timestamp?.N, 10) || ddbApproximateCreationTimestamp(record);
93+
94+
export const ddbApproximateCreationTimestamp = (record) => record.dynamodb.ApproximateCreationDateTime * 1000;
95+
9196
//--------------------------------------------
9297
// global table support - version: 2017.11.29
9398
//--------------------------------------------

test/unit/from/dynamodb.test.js

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

114+
it('should prefer image timestamp if present', (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+
timestamp: {
192+
N: '1572832690001',
193+
},
194+
},
195+
OldImage: undefined,
196+
SequenceNumber: '0',
197+
StreamViewType: 'NEW_AND_OLD_IMAGES',
198+
},
199+
},
200+
event: {
201+
id: '0',
202+
type: 'thing-created',
203+
partitionKey: '1',
204+
timestamp: 1572832690001,
205+
tags: {
206+
region: 'us-west-2',
207+
},
208+
raw: {
209+
new: {
210+
pk: '1',
211+
sk: 'thing',
212+
discriminator: 'thing',
213+
name: 'n1',
214+
timestamp: 1572832690001,
215+
},
216+
old: undefined,
217+
},
218+
},
219+
});
220+
})
221+
.done(done);
222+
});
223+
114224
it('should parse MODIFY record', (done) => {
115225
const events = toDynamodbRecords([
116226
{

0 commit comments

Comments
 (0)