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

BREAKING: Introduce an interface for DynamoDB attribute #720

Merged
merged 1 commit into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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