Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-lambda-stream",
"version": "1.0.34",
"version": "1.1.0",
"description": "Create stream processors with AWS Lambda functions.",
"keywords": [
"aws",
Expand Down
7 changes: 6 additions & 1 deletion src/from/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const fromDynamodb = (event, {
id: record.eventID,
type: `${calculateEventTypePrefix(record, { skFn, discriminatorFn, eventTypePrefix })}-${calculateEventTypeSuffix(record)}`,
partitionKey: record.dynamodb.Keys[pkFn].S,
timestamp: record.dynamodb.ApproximateCreationDateTime * 1000,
timestamp: deriveTimestamp(record),
tags: {
region: record.awsRegion,
},
Expand Down Expand Up @@ -88,6 +88,11 @@ const calculateEventTypeSuffix = (record) => {
return suffix;
};

const deriveTimestamp = (record) =>
parseInt(record.dynamodb.NewImage?.timestamp?.N, 10) || ddbApproximateCreationTimestamp(record);

export const ddbApproximateCreationTimestamp = (record) => record.dynamodb.ApproximateCreationDateTime * 1000;

//--------------------------------------------
// global table support - version: 2017.11.29
//--------------------------------------------
Expand Down
110 changes: 110 additions & 0 deletions test/unit/from/dynamodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,116 @@ describe('from/dynamodb.js', () => {
.done(done);
});

it('should prefer image timestamp if present', (done) => {
const events = toDynamodbRecords([
{
timestamp: 1572832690,
keys: {
pk: '1',
sk: 'thing',
},
newImage: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'n1',
timestamp: 1572832690001,
// insert in the current region will not have the awsregion field
},
},
// dynamodb stream emits an extra update event as it adorns the 'aws:rep' global table metadata
// so this extra event should be skipped
{
timestamp: 1572832690,
keys: {
pk: '1',
sk: 'thing',
},
newImage: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'n1',
awsregion: 'us-west-2',
},
oldImage: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'n1',
// as mentioned above there was no awsregion field on the insert event
},
},
]);

fromDynamodb(events)
.collect()
.tap((collected) => {
// console.log(JSON.stringify(collected, null, 2));

expect(collected.length).to.equal(1);
expect(collected[0]).to.deep.equal({
record: {
eventID: '0',
eventName: 'INSERT',
eventSource: 'aws:dynamodb',
awsRegion: 'us-west-2',
dynamodb: {
ApproximateCreationDateTime: 1572832690,
Keys: {
pk: {
S: '1',
},
sk: {
S: 'thing',
},
},
NewImage: {
pk: {
S: '1',
},
sk: {
S: 'thing',
},
discriminator: {
S: 'thing',
},
name: {
S: 'n1',
},
timestamp: {
N: '1572832690001',
},
},
OldImage: undefined,
SequenceNumber: '0',
StreamViewType: 'NEW_AND_OLD_IMAGES',
},
},
event: {
id: '0',
type: 'thing-created',
partitionKey: '1',
timestamp: 1572832690001,
tags: {
region: 'us-west-2',
},
raw: {
new: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'n1',
timestamp: 1572832690001,
},
old: undefined,
},
},
});
})
.done(done);
});

it('should parse MODIFY record', (done) => {
const events = toDynamodbRecords([
{
Expand Down