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.1.0",
"version": "1.1.1",
"description": "Create stream processors with AWS Lambda functions.",
"keywords": [
"aws",
Expand Down
12 changes: 9 additions & 3 deletions src/from/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const fromDynamodb = (event, {
eventTypePrefix = undefined,
ignoreTtlExpiredEvents = false,
ignoreReplicas = true,
preferApproximateTimestamp = false,
} = {}) => // eslint-disable-line import/prefer-default-export

// prepare the event stream
Expand All @@ -34,7 +35,7 @@ export const fromDynamodb = (event, {
id: record.eventID,
type: `${calculateEventTypePrefix(record, { skFn, discriminatorFn, eventTypePrefix })}-${calculateEventTypeSuffix(record)}`,
partitionKey: record.dynamodb.Keys[pkFn].S,
timestamp: deriveTimestamp(record),
timestamp: deriveTimestamp(record, preferApproximateTimestamp),
tags: {
region: record.awsRegion,
},
Expand Down Expand Up @@ -88,8 +89,13 @@ const calculateEventTypeSuffix = (record) => {
return suffix;
};

const deriveTimestamp = (record) =>
parseInt(record.dynamodb.NewImage?.timestamp?.N, 10) || ddbApproximateCreationTimestamp(record);
const deriveTimestamp = (record, preferApproximateTimestamp) => {
if (preferApproximateTimestamp) {
return ddbApproximateCreationTimestamp(record);
} else {
return parseInt(record.dynamodb.NewImage?.timestamp?.N, 10) || ddbApproximateCreationTimestamp(record);
}
};

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

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 @@ -221,6 +221,116 @@ describe('from/dynamodb.js', () => {
.done(done);
});

it('should prefer approximate timestamp if flag set', (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, { preferApproximateTimestamp: true })
.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: 1572832690000,
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