Skip to content

Commit ec87331

Browse files
author
Elad Ben-Israel
authored
feat(aws-dynamodb): allow specifying partition/sort keys in props (#1054)
Adds an option to specify `partitionKey` and/or `sortKey` when the table is initialized. This is identical to calling `addPartitionKey` and `addSortKey`. Fixes #1051
1 parent 2b6180d commit ec87331

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

packages/@aws-cdk/aws-dynamodb/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
## AWS DynamoDB Construct Library
22

3-
Add a DynamoDB table to your stack like so:
3+
Here is a minimal deployable DynamoDB table definition:
44

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

88
const table = new dynamodb.Table(stack, 'Table', {
9-
// You can leave this out to automatically generate a name.
10-
tableName: 'MyTableName',
11-
12-
// If you leave these out they default to 5
13-
readCapacity: 100,
14-
writeCapacity: 10,
15-
})
9+
partitionKey: { name: 'id', type: dynamodb.AttributeType.String }
10+
});
1611
```
1712

13+
### Keys
14+
15+
You can either specify `partitionKey` and/or `sortKey` when you initialize the
16+
table, or call `addPartitionKey` and `addSortKey` after initialization.
17+
1818
### Configure AutoScaling for your table
1919

2020
You can have DynamoDB automatically raise and lower the read and write capacities

packages/@aws-cdk/aws-dynamodb/lib/table.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ export interface TableProps {
8787
* @default undefined, TTL is disabled
8888
*/
8989
ttlAttributeName?: string;
90+
91+
/**
92+
* Partition key attribute definition. This is eventually required, but you
93+
* can also use `addPartitionKey` to specify the partition key at a later stage.
94+
*/
95+
partitionKey?: Attribute;
96+
97+
/**
98+
* Table sort key attribute definition. You can also use `addSortKey` to set
99+
* this up later.
100+
*/
101+
sortKey?: Attribute;
90102
}
91103

92104
export interface SecondaryIndexProps {
@@ -158,8 +170,8 @@ export class Table extends Construct {
158170
private readonly secondaryIndexNames: string[] = [];
159171
private readonly nonKeyAttributes: string[] = [];
160172

161-
private tablePartitionKey: Attribute | undefined = undefined;
162-
private tableSortKey: Attribute | undefined = undefined;
173+
private tablePartitionKey?: Attribute;
174+
private tableSortKey?: Attribute;
163175

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

191203
this.scalingRole = this.makeScalingRole();
192204

205+
if (props.partitionKey) {
206+
this.addPartitionKey(props.partitionKey);
207+
}
208+
209+
if (props.sortKey) {
210+
this.addSortKey(props.sortKey);
211+
}
193212
}
194213

195214
/**

packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,38 @@ export = {
123123
test.done();
124124
},
125125

126+
'hash + range key can also be specified in props'(test: Test) {
127+
const app = new TestApp();
128+
129+
new Table(app.stack, CONSTRUCT_NAME, {
130+
partitionKey: TABLE_PARTITION_KEY,
131+
sortKey: TABLE_SORT_KEY
132+
});
133+
134+
const template = app.synthesizeTemplate();
135+
136+
test.deepEqual(template, {
137+
Resources: {
138+
MyTable794EDED1: {
139+
Type: 'AWS::DynamoDB::Table',
140+
Properties: {
141+
AttributeDefinitions: [
142+
{ AttributeName: 'hashKey', AttributeType: 'S' },
143+
{ AttributeName: 'sortKey', AttributeType: 'N' }
144+
],
145+
KeySchema: [
146+
{ AttributeName: 'hashKey', KeyType: 'HASH' },
147+
{ AttributeName: 'sortKey', KeyType: 'RANGE' }
148+
],
149+
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
150+
}
151+
}
152+
}
153+
});
154+
155+
test.done();
156+
},
157+
126158
'point-in-time recovery is not enabled'(test: Test) {
127159
const app = new TestApp();
128160
new Table(app.stack, CONSTRUCT_NAME)
@@ -1167,7 +1199,7 @@ export = {
11671199
'"grantFullAccess" allows the principal to perform any action on the table ("*")'(test: Test) {
11681200
testGrant(test, [ '*' ], (p, t) => t.grantFullAccess(p));
11691201
}
1170-
}
1202+
},
11711203
};
11721204

11731205
class TestApp {

0 commit comments

Comments
 (0)