Skip to content

Commit

Permalink
feat(aws-dynamodb): allow specifying partition/sort keys in props (#1054
Browse files Browse the repository at this point in the history
)

Adds an option to specify `partitionKey` and/or `sortKey` when the
table is initialized. This is identical to calling `addPartitionKey`
and `addSortKey`.

Fixes #1051
  • Loading branch information
Elad Ben-Israel committed Oct 31, 2018
1 parent 2b6180d commit ec87331
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
16 changes: 8 additions & 8 deletions packages/@aws-cdk/aws-dynamodb/README.md
@@ -1,20 +1,20 @@
## AWS DynamoDB Construct Library

Add a DynamoDB table to your stack like so:
Here is a minimal deployable DynamoDB table definition:

```ts
import dynamodb = require('@aws-cdk/aws-dynamodb');

const table = new dynamodb.Table(stack, 'Table', {
// You can leave this out to automatically generate a name.
tableName: 'MyTableName',

// If you leave these out they default to 5
readCapacity: 100,
writeCapacity: 10,
})
partitionKey: { name: 'id', type: dynamodb.AttributeType.String }
});
```

### Keys

You can either specify `partitionKey` and/or `sortKey` when you initialize the
table, or call `addPartitionKey` and `addSortKey` after initialization.

### Configure AutoScaling for your table

You can have DynamoDB automatically raise and lower the read and write capacities
Expand Down
23 changes: 21 additions & 2 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Expand Up @@ -87,6 +87,18 @@ export interface TableProps {
* @default undefined, TTL is disabled
*/
ttlAttributeName?: string;

/**
* Partition key attribute definition. This is eventually required, but you
* can also use `addPartitionKey` to specify the partition key at a later stage.
*/
partitionKey?: Attribute;

/**
* Table sort key attribute definition. You can also use `addSortKey` to set
* this up later.
*/
sortKey?: Attribute;
}

export interface SecondaryIndexProps {
Expand Down Expand Up @@ -158,8 +170,8 @@ export class Table extends Construct {
private readonly secondaryIndexNames: string[] = [];
private readonly nonKeyAttributes: string[] = [];

private tablePartitionKey: Attribute | undefined = undefined;
private tableSortKey: Attribute | undefined = undefined;
private tablePartitionKey?: Attribute;
private tableSortKey?: Attribute;

private readonly tableScaling: ScalableAttributePair = {};
private readonly indexScaling = new Map<string, ScalableAttributePair>();
Expand Down Expand Up @@ -190,6 +202,13 @@ export class Table extends Construct {

this.scalingRole = this.makeScalingRole();

if (props.partitionKey) {
this.addPartitionKey(props.partitionKey);
}

if (props.sortKey) {
this.addSortKey(props.sortKey);
}
}

/**
Expand Down
34 changes: 33 additions & 1 deletion packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts
Expand Up @@ -123,6 +123,38 @@ export = {
test.done();
},

'hash + range key can also be specified in props'(test: Test) {
const app = new TestApp();

new Table(app.stack, CONSTRUCT_NAME, {
partitionKey: TABLE_PARTITION_KEY,
sortKey: TABLE_SORT_KEY
});

const template = app.synthesizeTemplate();

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

test.done();
},

'point-in-time recovery is not enabled'(test: Test) {
const app = new TestApp();
new Table(app.stack, CONSTRUCT_NAME)
Expand Down Expand Up @@ -1167,7 +1199,7 @@ export = {
'"grantFullAccess" allows the principal to perform any action on the table ("*")'(test: Test) {
testGrant(test, [ '*' ], (p, t) => t.grantFullAccess(p));
}
}
},
};

class TestApp {
Expand Down

0 comments on commit ec87331

Please sign in to comment.