Skip to content

Commit e6cc189

Browse files
jungseokleerix0rrr
authored andcommitted
refactor(aws-dynamodb): Attribute type for keys (#720)
BREAKING CHANGE: this patch introduces an interface to represent DynamoDB Attributes, in preparation for upcoming GSI and LSI support. It changes the signature of the `addPartitionKey` and `addSortKey` methods to be consistent across the board.
1 parent 72fec36 commit e6cc189

File tree

6 files changed

+100
-81
lines changed

6 files changed

+100
-81
lines changed

docs/src/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ and sort key **Timestamp**.
135135
writeCapacity: 5
136136
});
137137
138-
table.addPartitionKey('Alias', dynamodb.KeyAttributeType.String);
139-
table.addSortKey('Timestamp', dynamodb.KeyAttributeType.String);
138+
table.addPartitionKey({ name: 'Alias', type: dynamodb.AttributeType.String });
139+
table.addSortKey({ name: 'Timestamp', type: dynamodb.AttributeType.String });
140140
}
141141
}
142142

examples/cdk-examples-typescript/chat-app/dynamodb-posts-table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class DynamoPostsTable extends cdk.Construct {
99
readCapacity: 5, writeCapacity: 5
1010
});
1111

12-
table.addPartitionKey('Alias', dynamodb.KeyAttributeType.String);
13-
table.addSortKey('Timestamp', dynamodb.KeyAttributeType.String);
12+
table.addPartitionKey({ name: 'Alias', type: dynamodb.AttributeType.String });
13+
table.addSortKey({ name: 'Timestamp', type: dynamodb.AttributeType.String });
1414
}
1515
}

examples/cdk-examples-typescript/hello-cdk/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class HelloCDK extends cdk.Stack {
1010
writeCapacity: 1
1111
});
1212

13-
table.addPartitionKey('ID', dynamodb.KeyAttributeType.String);
14-
table.addSortKey('Timestamp', dynamodb.KeyAttributeType.Number);
13+
table.addPartitionKey({ name: 'ID', type: dynamodb.AttributeType.String });
14+
table.addSortKey({ name: 'Timestamp', type: dynamodb.AttributeType.Number });
1515
}
1616
}
1717

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ export interface TableProps {
6666
writeAutoScaling?: AutoScalingProps;
6767
}
6868

69+
export interface Attribute {
70+
/**
71+
* The name of an attribute.
72+
*/
73+
name: string;
74+
75+
/**
76+
* The data type of an attribute.
77+
*/
78+
type: AttributeType;
79+
}
80+
6981
/* tslint:disable:max-line-length */
7082
export interface AutoScalingProps {
7183
/**
@@ -150,13 +162,13 @@ export class Table extends Construct {
150162
}
151163
}
152164

153-
public addPartitionKey(name: string, type: KeyAttributeType): this {
154-
this.addKey(name, type, HASH_KEY_TYPE);
165+
public addPartitionKey(attribute: Attribute): this {
166+
this.addKey(attribute.name, attribute.type, HASH_KEY_TYPE);
155167
return this;
156168
}
157169

158-
public addSortKey(name: string, type: KeyAttributeType): this {
159-
this.addKey(name, type, RANGE_KEY_TYPE);
170+
public addSortKey(attribute: Attribute): this {
171+
this.addKey(attribute.name, attribute.type, RANGE_KEY_TYPE);
160172
return this;
161173
}
162174

@@ -266,7 +278,7 @@ export class Table extends Construct {
266278
return this.keySchema.find(prop => prop.keyType === keyType);
267279
}
268280

269-
private addKey(name: string, type: KeyAttributeType, keyType: string) {
281+
private addKey(name: string, type: AttributeType, keyType: string) {
270282
const existingProp = this.findKey(keyType);
271283
if (existingProp) {
272284
throw new Error(`Unable to set ${name} as a ${keyType} key, because ${existingProp.attributeName} is a ${keyType} key`);
@@ -279,7 +291,7 @@ export class Table extends Construct {
279291
return this;
280292
}
281293

282-
private registerAttribute(name: string, type: KeyAttributeType) {
294+
private registerAttribute(name: string, type: AttributeType) {
283295
const existingDef = this.attributeDefinitions.find(def => def.attributeName === name);
284296
if (existingDef && existingDef.attributeType !== type) {
285297
throw new Error(`Unable to specify ${name} as ${type} because it was already defined as ${existingDef.attributeType}`);
@@ -293,7 +305,7 @@ export class Table extends Construct {
293305
}
294306
}
295307

296-
export enum KeyAttributeType {
308+
export enum AttributeType {
297309
Binary = 'B',
298310
Number = 'N',
299311
String = 'S',

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { App, Stack } from '@aws-cdk/cdk';
2-
import { KeyAttributeType, StreamViewType, Table } from '../lib';
2+
import { AttributeType, StreamViewType, Table } from '../lib';
33

44
const app = new App(process.argv);
55

@@ -12,7 +12,7 @@ const table = new Table(stack, 'Table', {
1212
ttlAttributeName: 'timeToLive'
1313
});
1414

15-
table.addPartitionKey('hashKey', KeyAttributeType.String);
16-
table.addSortKey('rangeKey', KeyAttributeType.Number);
15+
table.addPartitionKey({ name: 'hashKey', type: AttributeType.String });
16+
table.addSortKey({ name: 'rangeKey', type: AttributeType.Number });
1717

1818
process.stdout.write(app.run());

0 commit comments

Comments
 (0)