Skip to content

Commit

Permalink
refactor(aws-dynamodb): Attribute type for keys (#720)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jungseoklee authored and rix0rrr committed Sep 18, 2018
1 parent 72fec36 commit e6cc189
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 81 deletions.
4 changes: 2 additions & 2 deletions docs/src/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ and sort key **Timestamp**.
writeCapacity: 5
});
table.addPartitionKey('Alias', dynamodb.KeyAttributeType.String);
table.addSortKey('Timestamp', dynamodb.KeyAttributeType.String);
table.addPartitionKey({ name: 'Alias', type: dynamodb.AttributeType.String });
table.addSortKey({ name: 'Timestamp', type: dynamodb.AttributeType.String });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class DynamoPostsTable extends cdk.Construct {
readCapacity: 5, writeCapacity: 5
});

table.addPartitionKey('Alias', dynamodb.KeyAttributeType.String);
table.addSortKey('Timestamp', dynamodb.KeyAttributeType.String);
table.addPartitionKey({ name: 'Alias', type: dynamodb.AttributeType.String });
table.addSortKey({ name: 'Timestamp', type: dynamodb.AttributeType.String });
}
}
4 changes: 2 additions & 2 deletions examples/cdk-examples-typescript/hello-cdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class HelloCDK extends cdk.Stack {
writeCapacity: 1
});

table.addPartitionKey('ID', dynamodb.KeyAttributeType.String);
table.addSortKey('Timestamp', dynamodb.KeyAttributeType.Number);
table.addPartitionKey({ name: 'ID', type: dynamodb.AttributeType.String });
table.addSortKey({ name: 'Timestamp', type: dynamodb.AttributeType.Number });
}
}

Expand Down
26 changes: 19 additions & 7 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ export interface TableProps {
writeAutoScaling?: AutoScalingProps;
}

export interface Attribute {
/**
* The name of an attribute.
*/
name: string;

/**
* The data type of an attribute.
*/
type: AttributeType;
}

/* tslint:disable:max-line-length */
export interface AutoScalingProps {
/**
Expand Down Expand Up @@ -150,13 +162,13 @@ export class Table extends Construct {
}
}

public addPartitionKey(name: string, type: KeyAttributeType): this {
this.addKey(name, type, HASH_KEY_TYPE);
public addPartitionKey(attribute: Attribute): this {
this.addKey(attribute.name, attribute.type, HASH_KEY_TYPE);
return this;
}

public addSortKey(name: string, type: KeyAttributeType): this {
this.addKey(name, type, RANGE_KEY_TYPE);
public addSortKey(attribute: Attribute): this {
this.addKey(attribute.name, attribute.type, RANGE_KEY_TYPE);
return this;
}

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

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

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

export enum KeyAttributeType {
export enum AttributeType {
Binary = 'B',
Number = 'N',
String = 'S',
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, Stack } from '@aws-cdk/cdk';
import { KeyAttributeType, StreamViewType, Table } from '../lib';
import { AttributeType, StreamViewType, Table } from '../lib';

const app = new App(process.argv);

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

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

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

0 comments on commit e6cc189

Please sign in to comment.