Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DynamoDB Table Stream #633

Merged
merged 3 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
+ **Property Changes**
- AWS::AppSync::DataSource HttpConfig (__added__)
- AWS::DAX::Cluster SSESpecification (__added__)
- AWS::DynamoDB::Table Stream (__added__)
- AWS::EC2::VPCEndpoint IsPrivateDnsEnabled (__added__)
- AWS::EC2::VPCEndpoint SecurityGroupIds (__added__)
- AWS::EC2::VPCEndpoint SubnetIds (__added__)
Expand Down
12 changes: 10 additions & 2 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface TableProps {
* @default <generated>
*/
tableName?: string;

/**
* The Type of Stream to create.
* @default ''
*/
streamSpecification?: cloudformation.TableResource.StreamSpecificationProperty;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't like to expose the raw CloudFormation data types in this layer since normally there's a way to provide a richer API. This is a great example for this. the [StreamSpecification] property basically has a single string field that can accept a bunch of possible values. Instead of sending users to read the docs and find what are the allowed values (failing only in deploy-time), we can just provide a simple enum:

export enum StreamViewType {
  NewImage = 'NEW_IMAGE',
  OldImage = 'OLD_IMAGE',
  NewAndOldImages = 'NEW_AND_OLD_IMAGES',
  KeysOnly = 'KEYS_ONLY'
}

Also, copy & paste the documentation from the DynamoDB manual as jsdocs on these enum members so users have all the information.

I also don't see any need for a nested structure here. We can use the fact that this is an optional field to determine if streams are enabled. So you can just call this streamViewType and indicate under @default that if this is not defined, DDB streams are disabled.

}

/**
Expand All @@ -43,12 +49,14 @@ export class Table extends Construct {

const readCapacityUnits = props.readCapacity || 5;
const writeCapacityUnits = props.writeCapacity || 5;
const streamViewType = props.streamSpecification;

this.table = new cloudformation.TableResource(this, 'Resource', {
tableName: props.tableName,
keySchema: this.keySchema,
attributeDefinitions: this.attributeDefinitions,
provisionedThroughput: { readCapacityUnits, writeCapacityUnits }
provisionedThroughput: { readCapacityUnits, writeCapacityUnits },
streamSpecification: streamViewType
});

if (props.tableName) { this.addMetadata('aws:cdk:hasPhysicalName', props.tableName); }
Expand Down Expand Up @@ -111,4 +119,4 @@ export enum KeyAttributeType {
Binary = 'B',
Number = 'N',
String = 'S',
}
}
139 changes: 136 additions & 3 deletions packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export = {
Properties: {
AttributeDefinitions: [{ AttributeName: 'hashKey', AttributeType: 'B' }],
KeySchema: [{ AttributeName: 'hashKey', KeyType: 'HASH' }],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
}
}
}
Expand Down Expand Up @@ -52,14 +52,147 @@ export = {
{ AttributeName: 'hashKey', KeyType: 'HASH' },
{ AttributeName: 'sortKey', KeyType: 'RANGE' }
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
}
}
}
});

test.done();
},
'stream is not enabled by default'(test: Test) {
const app = new TestApp();
new Table(app.stack, 'MyTable')
.addPartitionKey('partitionKey', KeyAttributeType.Binary)
.addSortKey('sortKey', KeyAttributeType.Number);
const template = app.synthesizeTemplate();

test.deepEqual(template, {
Resources: {
MyTable794EDED1: {
Type: 'AWS::DynamoDB::Table',
Properties: {
AttributeDefinitions: [
{ AttributeName: 'partitionKey', AttributeType: 'B' },
{ AttributeName: 'sortKey', AttributeType: 'N' }
],
KeySchema: [
{ AttributeName: 'partitionKey', KeyType: 'HASH' },
{ AttributeName: 'sortKey', KeyType: 'RANGE' }
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
}
}
}
});

test.done();
},
'can specify new and old images'(test: Test) {
const app = new TestApp();
const table = new Table(app.stack, 'MyTable', {
tableName: 'MyTable',
readCapacity: 42,
writeCapacity: 1337,
streamSpecification: {streamViewType: 'NEW_AND_OLD_IMAGES'}
});
table.addPartitionKey('partitionKey', KeyAttributeType.String);
table.addSortKey('sortKey', KeyAttributeType.Binary);
const template = app.synthesizeTemplate();

test.deepEqual(template, {
Resources: {
MyTable794EDED1: {
Type: 'AWS::DynamoDB::Table',
Properties: {
AttributeDefinitions: [
{ AttributeName: 'partitionKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'B' }
],
StreamSpecification: { StreamViewType: 'NEW_AND_OLD_IMAGES' },
KeySchema: [
{ AttributeName: 'partitionKey', KeyType: 'HASH' },
{ AttributeName: 'sortKey', KeyType: 'RANGE' }
],
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 },
TableName: 'MyTable'
}
}
}
});

test.done();
},
'can specify new images only'(test: Test) {
const app = new TestApp();
const table = new Table(app.stack, 'MyTable', {
tableName: 'MyTable',
readCapacity: 42,
writeCapacity: 1337,
streamSpecification: {streamViewType: 'NEW_IMAGE'}
});
table.addPartitionKey('partitionKey', KeyAttributeType.String);
table.addSortKey('sortKey', KeyAttributeType.Binary);
const template = app.synthesizeTemplate();

test.deepEqual(template, {
Resources: {
MyTable794EDED1: {
Type: 'AWS::DynamoDB::Table',
Properties: {
KeySchema: [
{ AttributeName: 'partitionKey', KeyType: 'HASH' },
{ AttributeName: 'sortKey', KeyType: 'RANGE' }
],
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 },
AttributeDefinitions: [
{ AttributeName: 'partitionKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'B' }
],
StreamSpecification: { StreamViewType: 'NEW_IMAGE' },
TableName: 'MyTable'
}
}
}
});

test.done();
},
'can specify old images only'(test: Test) {
const app = new TestApp();
const table = new Table(app.stack, 'MyTable', {
tableName: 'MyTable',
readCapacity: 42,
writeCapacity: 1337,
streamSpecification: {streamViewType: 'OLD_IMAGE'}
});
table.addPartitionKey('partitionKey', KeyAttributeType.String);
table.addSortKey('sortKey', KeyAttributeType.Binary);
const template = app.synthesizeTemplate();

test.deepEqual(template, {
Resources: {
MyTable794EDED1: {
Type: 'AWS::DynamoDB::Table',
Properties: {
KeySchema: [
{ AttributeName: 'partitionKey', KeyType: 'HASH' },
{ AttributeName: 'sortKey', KeyType: 'RANGE' }
],
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 },
AttributeDefinitions: [
{ AttributeName: 'partitionKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'B' }
],
StreamSpecification: { StreamViewType: 'OLD_IMAGE' },
TableName: 'MyTable'
}
}
}
});

test.done();
}
},

'when specifying every property'(test: Test) {
Expand Down Expand Up @@ -90,7 +223,7 @@ export = {
ReadCapacityUnits: 42,
WriteCapacityUnits: 1337
},
TableName: 'MyTable'
TableName: 'MyTable',
}
}
}
Expand Down