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
2 changes: 1 addition & 1 deletion 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.29",
"version": "1.0.30",
"description": "Create stream processors with AWS Lambda functions.",
"keywords": [
"aws",
Expand Down
4 changes: 3 additions & 1 deletion src/connectors/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class Connector {
removeUndefinedValues = true,
timeout = Number(process.env.DYNAMODB_TIMEOUT) || Number(process.env.TIMEOUT) || 1000,
retryConfig = defaultRetryConfig,
throwConditionFailure = false,
additionalClientOpts = {},
...opt
}) {
this.debug = (msg) => debug('%j', msg);
this.tableName = tableName || /* istanbul ignore next */ 'undefined';
this.client = Connector.getClient(pipelineId, debug, convertEmptyValues, removeUndefinedValues, timeout, additionalClientOpts);
this.retryConfig = retryConfig;
this.throwConditionFailure = throwConditionFailure;
this.opt = opt;
}

Expand Down Expand Up @@ -73,7 +75,7 @@ class Connector {
return this._sendCommand(new UpdateCommand(params), ctx)
.catch((err) => {
/* istanbul ignore else */
if (err.name === 'ConditionalCheckFailedException') {
if (err.name === 'ConditionalCheckFailedException' && !this.throwConditionFailure) {
return {};
}
/* istanbul ignore next */
Expand Down
3 changes: 2 additions & 1 deletion src/sinks/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ export const updateDynamoDB = ({
parallel = Number(process.env.UPDATE_PARALLEL) || Number(process.env.PARALLEL) || 4,
timeout = Number(process.env.DYNAMODB_TIMEOUT) || Number(process.env.TIMEOUT) || 1000,
removeUndefinedValues = true,
throwConditionFailure = false,
step = 'save',
...opt
} = {}) => {
const connector = new Connector({
pipelineId, debug, tableName, timeout, removeUndefinedValues, ...opt,
pipelineId, debug, tableName, timeout, removeUndefinedValues, throwConditionFailure, ...opt,
});

const invoke = (uow) => {
Expand Down
60 changes: 58 additions & 2 deletions test/unit/flavors/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { expect } from 'chai';
import sinon from 'sinon';

import { KmsConnector, MOCK_GEN_DK_RESPONSE } from 'aws-kms-ee';
import { DynamoDBDocumentClient, UpdateCommand } from '@aws-sdk/lib-dynamodb';
import { ConditionalCheckFailedException } from '@aws-sdk/client-dynamodb';
import { mockClient } from 'aws-sdk-client-mock';

import {
initialize, initializeFrom,
Expand All @@ -17,16 +20,21 @@ import {
import {
updateExpression, timestampCondition,
} from '../../../src/sinks/dynamodb';
import { DynamoDBConnector } from '../../../src/connectors';
import { DynamoDBConnector, EventBridgeConnector } from '../../../src/connectors';

import { update } from '../../../src/flavors/update';

describe('flavors/update.js', () => {
let mockDdb;

beforeEach(() => {
sinon.stub(DynamoDBConnector.prototype, 'update').resolves({});
});

afterEach(sinon.restore);
afterEach(() => {
sinon.restore();
mockDdb?.restore();
});

it('should execute', (done) => {
sinon.stub(DynamoDBConnector.prototype, 'query').resolves([]);
Expand Down Expand Up @@ -157,6 +165,54 @@ describe('flavors/update.js', () => {
})
.done(done);
});

it('should optionally throw conditional check', (done) => {
sinon.restore();
sinon.stub(EventBridgeConnector.prototype, 'putEvents').resolves({});
mockDdb = mockClient(DynamoDBDocumentClient);
mockDdb.on(UpdateCommand).rejects(new ConditionalCheckFailedException({}));

const events = toDynamodbRecords([
{
timestamp: 1572832690,
keys: {
pk: '1',
sk: 'thing',
},
newImage: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
ttl: 1549053422,
timestamp: 1548967022000,
},
},
]);

const rule = {
id: 'updateThrow',
flavor: update,
eventType: /thing-*/,
toUpdateRequest: () => ({}),
throwConditionFailure: true,
};

initialize({
...initializeFrom([
rule,
]),
}, { ...defaultOptions, AES: false })
.assemble(fromDynamodb(events), true)
.collect()
// .tap((collected) => console.log(JSON.stringify(collected, null, 2)))
.tap((collected) => {
expect(collected.length).to.equal(1);
expect(collected[0].event.tags.pipeline).to.equal('updateThrow');
expect(collected[0].event.type).to.equal('fault');
expect(collected[0].event.err.name).to.equal('ConditionalCheckFailedException');
})
.done(done);
});
});

const toUpdateRequest = (uow) => ({
Expand Down
Loading