Skip to content

Commit 2f95852

Browse files
committed
Prefer approx timestamp if flag set.
1 parent a4fbc8f commit 2f95852

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
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.1.0",
3+
"version": "1.1.1",
44
"description": "Create stream processors with AWS Lambda functions.",
55
"keywords": [
66
"aws",

src/from/dynamodb.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const fromDynamodb = (event, {
1111
eventTypePrefix = undefined,
1212
ignoreTtlExpiredEvents = false,
1313
ignoreReplicas = true,
14+
preferApproximateTimestamp = false,
1415
} = {}) => // eslint-disable-line import/prefer-default-export
1516

1617
// prepare the event stream
@@ -34,7 +35,7 @@ export const fromDynamodb = (event, {
3435
id: record.eventID,
3536
type: `${calculateEventTypePrefix(record, { skFn, discriminatorFn, eventTypePrefix })}-${calculateEventTypeSuffix(record)}`,
3637
partitionKey: record.dynamodb.Keys[pkFn].S,
37-
timestamp: deriveTimestamp(record),
38+
timestamp: deriveTimestamp(record, preferApproximateTimestamp),
3839
tags: {
3940
region: record.awsRegion,
4041
},
@@ -88,8 +89,13 @@ const calculateEventTypeSuffix = (record) => {
8889
return suffix;
8990
};
9091

91-
const deriveTimestamp = (record) =>
92-
parseInt(record.dynamodb.NewImage?.timestamp?.N, 10) || ddbApproximateCreationTimestamp(record);
92+
const deriveTimestamp = (record, preferApproximateTimestamp) => {
93+
if (preferApproximateTimestamp) {
94+
return ddbApproximateCreationTimestamp(record);
95+
} else {
96+
return parseInt(record.dynamodb.NewImage?.timestamp?.N, 10) || ddbApproximateCreationTimestamp(record);
97+
}
98+
};
9399

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

test/unit/from/dynamodb.test.js

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

224+
it('should prefer approximate timestamp if flag set', (done) => {
225+
const events = toDynamodbRecords([
226+
{
227+
timestamp: 1572832690,
228+
keys: {
229+
pk: '1',
230+
sk: 'thing',
231+
},
232+
newImage: {
233+
pk: '1',
234+
sk: 'thing',
235+
discriminator: 'thing',
236+
name: 'n1',
237+
timestamp: 1572832690001,
238+
// insert in the current region will not have the awsregion field
239+
},
240+
},
241+
// dynamodb stream emits an extra update event as it adorns the 'aws:rep' global table metadata
242+
// so this extra event should be skipped
243+
{
244+
timestamp: 1572832690,
245+
keys: {
246+
pk: '1',
247+
sk: 'thing',
248+
},
249+
newImage: {
250+
pk: '1',
251+
sk: 'thing',
252+
discriminator: 'thing',
253+
name: 'n1',
254+
awsregion: 'us-west-2',
255+
},
256+
oldImage: {
257+
pk: '1',
258+
sk: 'thing',
259+
discriminator: 'thing',
260+
name: 'n1',
261+
// as mentioned above there was no awsregion field on the insert event
262+
},
263+
},
264+
]);
265+
266+
fromDynamodb(events, { preferApproximateTimestamp: true })
267+
.collect()
268+
.tap((collected) => {
269+
// console.log(JSON.stringify(collected, null, 2));
270+
271+
expect(collected.length).to.equal(1);
272+
expect(collected[0]).to.deep.equal({
273+
record: {
274+
eventID: '0',
275+
eventName: 'INSERT',
276+
eventSource: 'aws:dynamodb',
277+
awsRegion: 'us-west-2',
278+
dynamodb: {
279+
ApproximateCreationDateTime: 1572832690,
280+
Keys: {
281+
pk: {
282+
S: '1',
283+
},
284+
sk: {
285+
S: 'thing',
286+
},
287+
},
288+
NewImage: {
289+
pk: {
290+
S: '1',
291+
},
292+
sk: {
293+
S: 'thing',
294+
},
295+
discriminator: {
296+
S: 'thing',
297+
},
298+
name: {
299+
S: 'n1',
300+
},
301+
timestamp: {
302+
N: '1572832690001',
303+
},
304+
},
305+
OldImage: undefined,
306+
SequenceNumber: '0',
307+
StreamViewType: 'NEW_AND_OLD_IMAGES',
308+
},
309+
},
310+
event: {
311+
id: '0',
312+
type: 'thing-created',
313+
partitionKey: '1',
314+
timestamp: 1572832690000,
315+
tags: {
316+
region: 'us-west-2',
317+
},
318+
raw: {
319+
new: {
320+
pk: '1',
321+
sk: 'thing',
322+
discriminator: 'thing',
323+
name: 'n1',
324+
timestamp: 1572832690001,
325+
},
326+
old: undefined,
327+
},
328+
},
329+
});
330+
})
331+
.done(done);
332+
});
333+
224334
it('should parse MODIFY record', (done) => {
225335
const events = toDynamodbRecords([
226336
{

0 commit comments

Comments
 (0)